diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/Class_/ClassMethodArrayDocblockParamFromLocalCallsRector/Fixture/objects_overrule_empty_array.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/Class_/ClassMethodArrayDocblockParamFromLocalCallsRector/Fixture/objects_overrule_empty_array.php.inc new file mode 100644 index 00000000000..bc0fdeeb75e --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/Class_/ClassMethodArrayDocblockParamFromLocalCallsRector/Fixture/objects_overrule_empty_array.php.inc @@ -0,0 +1,80 @@ +run($this->getItemsWithArray()); + $this->run($this->getItems()); + + $this->run($emptyArray); + } + + public function run(array $result) + { + } + + /** + * @return SomeReturnedObject[]|array + */ + private function getItems() + { + return []; + } + + /** + * @return SomeReturnedObject[] + */ + private function getItemsWithArray() + { + return []; + } +} + +?> +----- +run($this->getItemsWithArray()); + $this->run($this->getItems()); + + $this->run($emptyArray); + } + + /** + * @param \Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject[] $result + */ + public function run(array $result) + { + } + + /** + * @return SomeReturnedObject[]|array + */ + private function getItems() + { + return []; + } + + /** + * @return SomeReturnedObject[] + */ + private function getItemsWithArray() + { + return []; + } +} + +?> diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/Class_/ClassMethodArrayDocblockParamFromLocalCallsRector/Source/SomeReturnedObject.php b/rules-tests/TypeDeclarationDocblocks/Rector/Class_/ClassMethodArrayDocblockParamFromLocalCallsRector/Source/SomeReturnedObject.php new file mode 100644 index 00000000000..0dc1ef5c6e3 --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/Class_/ClassMethodArrayDocblockParamFromLocalCallsRector/Source/SomeReturnedObject.php @@ -0,0 +1,7 @@ +unionToSingleType($staticTypesByArgumentPosition); + return $this->unionToSingleType($staticTypesByArgumentPosition, true); } private function resolveStrictArgValueType(Arg $arg): Type @@ -113,13 +114,19 @@ private function correctSelfType(Type $argValueType): Type * @param array $staticTypesByArgumentPosition * @return array */ - private function unionToSingleType(array $staticTypesByArgumentPosition): array + private function unionToSingleType(array $staticTypesByArgumentPosition, bool $removeMixedArray = false): array { $staticTypeByArgumentPosition = []; + foreach ($staticTypesByArgumentPosition as $position => $staticTypes) { - $unionedType = $this->typeFactory->createMixedPassedOrUnionType($staticTypes); + if ($removeMixedArray) { + $staticTypes = array_filter( + $staticTypes, + fn (Type $type): bool => ! $this->isArrayMixedMixedType($type) + ); + } - // narrow parents to most child type + $unionedType = $this->typeFactory->createMixedPassedOrUnionType($staticTypes); $staticTypeByArgumentPosition[$position] = $this->narrowParentObjectTreeToSingleObjectChildType( $unionedType @@ -212,4 +219,17 @@ private function isEmptyArray(Expr $expr): bool return $expr->items === []; } + + private function isArrayMixedMixedType(Type $type): bool + { + if (! $type instanceof ArrayType) { + return false; + } + + if (! $type->getItemType() instanceof MixedType) { + return false; + } + + return $type->getKeyType() instanceof MixedType; + } } diff --git a/src/NodeTypeResolver/NodeTypeCorrector/AccessoryNonEmptyArrayTypeCorrector.php b/src/NodeTypeResolver/NodeTypeCorrector/AccessoryNonEmptyArrayTypeCorrector.php index d3fcd962cc8..f8489bfd867 100644 --- a/src/NodeTypeResolver/NodeTypeCorrector/AccessoryNonEmptyArrayTypeCorrector.php +++ b/src/NodeTypeResolver/NodeTypeCorrector/AccessoryNonEmptyArrayTypeCorrector.php @@ -30,7 +30,9 @@ public function correct(Type $mainType): Type if ($type instanceof ArrayType && $type->getIterableValueType() instanceof IntersectionType - && $type->getIterableValueType()->isString()->yes()) { + && $type->getIterableValueType() + ->isString() + ->yes()) { return new ArrayType(new MixedType(), new StringType()); } } diff --git a/src/PostRector/Collector/UseNodesToAddCollector.php b/src/PostRector/Collector/UseNodesToAddCollector.php index 867cd822996..d9d792a756b 100644 --- a/src/PostRector/Collector/UseNodesToAddCollector.php +++ b/src/PostRector/Collector/UseNodesToAddCollector.php @@ -83,10 +83,8 @@ public function getUseImportTypesByNode(File $file): array return $objectTypes; } - public function hasImport( - File $file, - FullyQualifiedObjectType $fullyQualifiedObjectType - ): bool { + public function hasImport(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType): bool + { $useImports = $this->getUseImportTypesByNode($file); foreach ($useImports as $useImport) {