diff --git a/packages/NodeTypeResolver/src/ComplexNodeTypeResolver.php b/packages/NodeTypeResolver/src/ComplexNodeTypeResolver.php index 037303a7b751..e2a7b97a4a67 100644 --- a/packages/NodeTypeResolver/src/ComplexNodeTypeResolver.php +++ b/packages/NodeTypeResolver/src/ComplexNodeTypeResolver.php @@ -99,6 +99,6 @@ public function resolvePropertyTypeInfo(Property $property): ?VarTypeInfo $types = array_filter($types); - return new VarTypeInfo($types, $this->typeAnalyzer, $types, true); + return new VarTypeInfo($types, $this->typeAnalyzer, $types); } } diff --git a/packages/NodeTypeResolver/src/Php/AbstractTypeInfo.php b/packages/NodeTypeResolver/src/Php/AbstractTypeInfo.php index 3a4d430b8cd5..e94ced6bc77b 100644 --- a/packages/NodeTypeResolver/src/Php/AbstractTypeInfo.php +++ b/packages/NodeTypeResolver/src/Php/AbstractTypeInfo.php @@ -52,21 +52,17 @@ abstract class AbstractTypeInfo * @param string[] $types * @param string[] $fqnTypes */ - public function __construct( - array $types, - TypeAnalyzer $typeAnalyzer, - array $fqnTypes = [], - bool $allowTypedArrays = false - ) { + public function __construct(array $types, TypeAnalyzer $typeAnalyzer, array $fqnTypes = []) + { $this->typeAnalyzer = $typeAnalyzer; - $this->types = $this->analyzeAndNormalizeTypes($types, $allowTypedArrays); + $this->types = $this->analyzeAndNormalizeTypes($types); // fallback if ($fqnTypes === []) { $fqnTypes = $types; } - $this->fqnTypes = $this->analyzeAndNormalizeTypes($fqnTypes, $allowTypedArrays); + $this->fqnTypes = $this->analyzeAndNormalizeTypes($fqnTypes); } public function isNullable(): bool @@ -165,11 +161,35 @@ protected function normalizeName(string $name): string return ltrim($name, '$'); } + /** + * @param string[] $types + */ + protected function isArraySubtype(array $types): bool + { + if ($types === []) { + return false; + } + + foreach ($types as $type) { + if (in_array($type, ['array', 'iterable'], true)) { + continue; + } + + if (Strings::endsWith($type, '[]')) { + continue; + } + + return false; + } + + return true; + } + /** * @param string|string[] $types * @return string[] */ - private function analyzeAndNormalizeTypes($types, bool $allowTypedArrays = false): array + private function analyzeAndNormalizeTypes($types): array { $types = (array) $types; @@ -207,7 +227,7 @@ private function analyzeAndNormalizeTypes($types, bool $allowTypedArrays = false continue; } - $types[$i] = $this->typeAnalyzer->normalizeType($type, $allowTypedArrays); + $types[$i] = $this->typeAnalyzer->normalizeType($type); } // remove undesired types @@ -226,26 +246,6 @@ private function hasRemovedTypes(): bool return count($this->removedTypes) > 1; } - /** - * @param string[] $types - */ - private function isArraySubtype(array $types): bool - { - foreach ($types as $type) { - if (in_array($type, ['array', 'iterable'], true)) { - continue; - } - - if (Strings::endsWith($type, '[]')) { - continue; - } - - return false; - } - - return true; - } - private function normalizeNullable(string $type): string { if (Strings::startsWith($type, '?')) { diff --git a/packages/NodeTypeResolver/src/Php/VarTypeInfo.php b/packages/NodeTypeResolver/src/Php/VarTypeInfo.php index 8058cc7b07e8..00ee7d384b57 100644 --- a/packages/NodeTypeResolver/src/Php/VarTypeInfo.php +++ b/packages/NodeTypeResolver/src/Php/VarTypeInfo.php @@ -51,16 +51,6 @@ public function getFqnType(): ?string public function isIterable(): bool { - if ($this->types === []) { - return false; - } - - foreach ($this->types as $type) { - if ($type !== 'array') { - return false; - } - } - - return true; + return $this->isArraySubtype($this->types); } } diff --git a/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php b/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php index 4605611ab31b..3b44a27252e5 100644 --- a/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php +++ b/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php @@ -203,7 +203,7 @@ public function getReturnTypeInfo(Node $node): ?ReturnTypeInfo $fqnTypes = $phpDocInfo->getReturnTypes(); - return new ReturnTypeInfo($types, $this->typeAnalyzer, $fqnTypes, true); + return new ReturnTypeInfo($types, $this->typeAnalyzer, $fqnTypes); } /** diff --git a/packages/TypeDeclaration/src/TypeInferer/ReturnTypeInferer/ReturnedNodeFunctionLikeReturnTypeInferer.php b/packages/TypeDeclaration/src/TypeInferer/ReturnTypeInferer/ReturnedNodeFunctionLikeReturnTypeInferer.php index ec277733c1bd..bed68f66c80e 100644 --- a/packages/TypeDeclaration/src/TypeInferer/ReturnTypeInferer/ReturnedNodeFunctionLikeReturnTypeInferer.php +++ b/packages/TypeDeclaration/src/TypeInferer/ReturnTypeInferer/ReturnedNodeFunctionLikeReturnTypeInferer.php @@ -29,10 +29,7 @@ public function __construct(FunctionLikeManipulator $functionLikeManipulator) public function inferFunctionLike(FunctionLike $functionLike): array { $resolvedReturnTypeInfo = $this->functionLikeManipulator->resolveStaticReturnTypeInfo($functionLike); - if ($resolvedReturnTypeInfo === null) { - return []; - } - return $resolvedReturnTypeInfo->getDocTypes(); + return $resolvedReturnTypeInfo ? $resolvedReturnTypeInfo->getDocTypes() : []; } } diff --git a/src/Php/TypeAnalyzer.php b/src/Php/TypeAnalyzer.php index dc2d51e6bb6e..6546ef81f7a3 100644 --- a/src/Php/TypeAnalyzer.php +++ b/src/Php/TypeAnalyzer.php @@ -52,15 +52,8 @@ public function isPhpReservedType(string $type): bool return false; } - public function normalizeType(string $type, bool $allowTypedArrays = false): string + public function normalizeType(string $type): string { - // reduction needed for typehint - if (! $allowTypedArrays) { - if (Strings::endsWith($type, '[]')) { - return 'array'; - } - } - if (strtolower($type) === 'boolean') { return 'bool'; } diff --git a/src/PhpParser/Node/Manipulator/FunctionLikeManipulator.php b/src/PhpParser/Node/Manipulator/FunctionLikeManipulator.php index 27a35fa285b6..6ee2ab369587 100644 --- a/src/PhpParser/Node/Manipulator/FunctionLikeManipulator.php +++ b/src/PhpParser/Node/Manipulator/FunctionLikeManipulator.php @@ -102,7 +102,7 @@ public function resolveStaticReturnTypeInfo(FunctionLike $functionLike): ?Return if (! $functionLike->returnType->getAttribute( AbstractTypeDeclarationRector::HAS_NEW_INHERITED_TYPE ) && $types !== []) { - return new ReturnTypeInfo($types, $this->typeAnalyzer, $types, true); + return new ReturnTypeInfo($types, $this->typeAnalyzer, $types); } } @@ -117,12 +117,12 @@ public function resolveStaticReturnTypeInfo(FunctionLike $functionLike): ?Return } if ($this->isVoid) { - return new ReturnTypeInfo(['void'], $this->typeAnalyzer, ['void'], true); + return new ReturnTypeInfo(['void'], $this->typeAnalyzer, ['void']); } $types = array_filter($types); - return new ReturnTypeInfo($types, $this->typeAnalyzer, $types, true); + return new ReturnTypeInfo($types, $this->typeAnalyzer, $types); } /** diff --git a/src/Rector/ClassMethod/AddReturnTypeDeclarationRector.php b/src/Rector/ClassMethod/AddReturnTypeDeclarationRector.php index 802fb5dbac81..a936f69422a0 100644 --- a/src/Rector/ClassMethod/AddReturnTypeDeclarationRector.php +++ b/src/Rector/ClassMethod/AddReturnTypeDeclarationRector.php @@ -103,7 +103,7 @@ private function processClassMethodNodeWithTypehints(ClassMethod $classMethod, s if ($newType === '') { $classMethod->returnType = null; } else { - $returnTypeInfo = new ReturnTypeInfo([$newType], $this->typeAnalyzer); + $returnTypeInfo = new ReturnTypeInfo([$newType], $this->typeAnalyzer, [$newType]); $classMethod->returnType = $returnTypeInfo->getFqnTypeNode(); }