mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-21 01:41:00 +01:00
remove duplicated code
This commit is contained in:
parent
7d2ffa5337
commit
b748309775
@ -2,20 +2,14 @@
|
||||
|
||||
namespace Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\NullableType;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\TypeDeclaration\Contract\TypeInferer\PropertyTypeInfererInterface;
|
||||
use Rector\TypeDeclaration\TypeDeclarationToStringConverter;
|
||||
use Rector\TypeDeclaration\TypeInferer\AbstractTypeInferer;
|
||||
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer\ReturnedNodesReturnTypeInferer;
|
||||
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer\ReturnTagReturnTypeInferer;
|
||||
@ -32,12 +26,19 @@ final class GetterPropertyTypeInferer extends AbstractTypeInferer implements Pro
|
||||
*/
|
||||
private $returnTagReturnTypeInferer;
|
||||
|
||||
/**
|
||||
* @var TypeDeclarationToStringConverter
|
||||
*/
|
||||
private $typeDeclarationToStringConverter;
|
||||
|
||||
public function __construct(
|
||||
ReturnedNodesReturnTypeInferer $returnedNodesReturnTypeInferer,
|
||||
ReturnTagReturnTypeInferer $returnTagReturnTypeInferer
|
||||
ReturnTagReturnTypeInferer $returnTagReturnTypeInferer,
|
||||
TypeDeclarationToStringConverter $typeDeclarationToStringConverter
|
||||
) {
|
||||
$this->returnedNodesReturnTypeInferer = $returnedNodesReturnTypeInferer;
|
||||
$this->returnTagReturnTypeInferer = $returnTagReturnTypeInferer;
|
||||
$this->typeDeclarationToStringConverter = $typeDeclarationToStringConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,28 +94,14 @@ final class GetterPropertyTypeInferer extends AbstractTypeInferer implements Pro
|
||||
return $this->nameResolver->isName($return->expr, $propertyName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Intentionally local method ↓
|
||||
* @todo possible move to ReturnTypeInferer, but allow to disable/enable in case of override returnType (99 %)
|
||||
*
|
||||
* @param ClassMethod|Function_|Closure $functionLike
|
||||
* @return string[]
|
||||
*/
|
||||
private function resolveFunctionLikeReturnTypeDeclaration(FunctionLike $functionLike): array
|
||||
{
|
||||
if ($functionLike->returnType === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->resolveReturnTypeToString($functionLike->returnType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function inferClassMethodReturnTypes(ClassMethod $classMethod): array
|
||||
{
|
||||
$returnTypeDeclarationTypes = $this->resolveFunctionLikeReturnTypeDeclaration($classMethod);
|
||||
$returnTypeDeclarationTypes = $this->typeDeclarationToStringConverter->resolveFunctionLikeReturnTypeToString(
|
||||
$classMethod
|
||||
);
|
||||
if ($returnTypeDeclarationTypes) {
|
||||
return $returnTypeDeclarationTypes;
|
||||
}
|
||||
@ -126,25 +113,4 @@ final class GetterPropertyTypeInferer extends AbstractTypeInferer implements Pro
|
||||
|
||||
return $this->returnTagReturnTypeInferer->inferFunctionLike($classMethod);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Identifier|Name|NullableType $node
|
||||
* @return string[]
|
||||
*/
|
||||
private function resolveReturnTypeToString(Node $node): array
|
||||
{
|
||||
$types = [];
|
||||
|
||||
$type = $node instanceof NullableType ? $node->type : $node;
|
||||
$result = $this->nameResolver->getName($type);
|
||||
if ($result !== null) {
|
||||
$types[] = $result;
|
||||
}
|
||||
|
||||
if ($node instanceof NullableType) {
|
||||
$types[] = 'null';
|
||||
}
|
||||
|
||||
return $types;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user