Skip to content

Commit 7da017c

Browse files
[TypedCollections] Skip with string key already on CompleteReturnDocblockFromToManyRector (#480)
* [TypedCollections] Skip with string key already on CompleteReturnDocblockFromToManyRector * [rector] Rector fixes --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 46e4f77 commit 7da017c

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\Class_\CompleteReturnDocblockFromToManyRector\Fixture\OneToMany;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Entity
9+
*/
10+
final class Trainer
11+
{
12+
/**
13+
* @ORM\OneToMany(targetEntity=Training::class, indexBy=someField)
14+
* @var \Doctrine\Common\Collections\Collection<string, Training>
15+
*/
16+
private $trainings;
17+
18+
/**
19+
* @return \Doctrine\Common\Collections\Collection<string, Training>
20+
*/
21+
public function getTrainings()
22+
{
23+
return $this->trainings;
24+
}
25+
}

rules/TypedCollections/DocBlockProcessor/UnionCollectionTagValueNodeNarrower.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ private function isCollectionIdentifierTypeNode(TypeNode $typeNode): bool
220220

221221
return in_array(
222222
$typeNode->name,
223-
[DoctrineClass::COLLECTION, DoctrineClass::ARRAY_COLLECTION, 'Collection', 'ArrayCollection']
223+
[DoctrineClass::COLLECTION, DoctrineClass::ARRAY_COLLECTION, 'Collection', 'ArrayCollection'],
224+
true
224225
);
225226
}
226227
}

rules/TypedCollections/Rector/Class_/CompleteReturnDocblockFromToManyRector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Stmt\Class_;
99
use PhpParser\Node\Stmt\Property;
10+
use PHPStan\Type\Generic\GenericObjectType;
1011
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1112
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
1213
use Rector\Doctrine\NodeAnalyzer\MethodUniqueReturnedPropertyResolver;
@@ -134,6 +135,12 @@ public function refactor(Node $node): ?Node
134135
$this->collectionTypeResolver->hasIndexBy($property),
135136
$property
136137
);
138+
139+
$returnType = $phpDocInfo->getReturnType();
140+
if ($returnType instanceof GenericObjectType) {
141+
continue;
142+
}
143+
137144
$this->phpDocTypeChanger->changeReturnType($classMethod, $phpDocInfo, $newVarType);
138145
$hasChanged = true;
139146
}

0 commit comments

Comments
 (0)