diff --git a/rules-tests/TypedCollections/Rector/Class_/CompleteReturnDocblockFromToManyRector/Fixture/OneToMany/skip_with_string_key.php.inc b/rules-tests/TypedCollections/Rector/Class_/CompleteReturnDocblockFromToManyRector/Fixture/OneToMany/skip_with_string_key.php.inc new file mode 100644 index 00000000..739ba100 --- /dev/null +++ b/rules-tests/TypedCollections/Rector/Class_/CompleteReturnDocblockFromToManyRector/Fixture/OneToMany/skip_with_string_key.php.inc @@ -0,0 +1,25 @@ + + */ + private $trainings; + + /** + * @return \Doctrine\Common\Collections\Collection + */ + public function getTrainings() + { + return $this->trainings; + } + } \ No newline at end of file diff --git a/rules/TypedCollections/DocBlockProcessor/UnionCollectionTagValueNodeNarrower.php b/rules/TypedCollections/DocBlockProcessor/UnionCollectionTagValueNodeNarrower.php index f1329157..bc5c8287 100644 --- a/rules/TypedCollections/DocBlockProcessor/UnionCollectionTagValueNodeNarrower.php +++ b/rules/TypedCollections/DocBlockProcessor/UnionCollectionTagValueNodeNarrower.php @@ -220,7 +220,8 @@ private function isCollectionIdentifierTypeNode(TypeNode $typeNode): bool return in_array( $typeNode->name, - [DoctrineClass::COLLECTION, DoctrineClass::ARRAY_COLLECTION, 'Collection', 'ArrayCollection'] + [DoctrineClass::COLLECTION, DoctrineClass::ARRAY_COLLECTION, 'Collection', 'ArrayCollection'], + true ); } } diff --git a/rules/TypedCollections/Rector/Class_/CompleteReturnDocblockFromToManyRector.php b/rules/TypedCollections/Rector/Class_/CompleteReturnDocblockFromToManyRector.php index f7056927..7c4edf2c 100644 --- a/rules/TypedCollections/Rector/Class_/CompleteReturnDocblockFromToManyRector.php +++ b/rules/TypedCollections/Rector/Class_/CompleteReturnDocblockFromToManyRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Property; +use PHPStan\Type\Generic\GenericObjectType; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; use Rector\Doctrine\NodeAnalyzer\MethodUniqueReturnedPropertyResolver; @@ -134,6 +135,12 @@ public function refactor(Node $node): ?Node $this->collectionTypeResolver->hasIndexBy($property), $property ); + + $returnType = $phpDocInfo->getReturnType(); + if ($returnType instanceof GenericObjectType) { + continue; + } + $this->phpDocTypeChanger->changeReturnType($classMethod, $phpDocInfo, $newVarType); $hasChanged = true; }