Skip to content

Commit 0985d2e

Browse files
authored
fix reprint of type in CollectionSetterParamNativeTypeRector (#418)
* add failing fixture * fix reprint of type in CollectionSetterParamNativeTypeRector
1 parent a9cb6f0 commit 0985d2e

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

rules/TypedCollections/Rector/ClassMethod/CollectionParamTypeSetterToCollectionPropertyRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public function refactor(Node $node): ?ClassMethod
108108
// remove default param, as no longer needed
109109
if ($firstParam->default instanceof Expr) {
110110
$firstParam->default = null;
111+
111112
return $node;
112113
}
113114

rules/TypedCollections/Rector/ClassMethod/CollectionSetterParamNativeTypeRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1717
use Rector\Doctrine\Enum\DoctrineClass;
1818
use Rector\Doctrine\TypedCollections\DocBlockAnalyzer\CollectionTagValueNodeAnalyzer;
19+
use Rector\NodeTypeResolver\Node\AttributeKey;
1920
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
2021
use Rector\Rector\AbstractRector;
2122
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -120,6 +121,9 @@ public function refactor(Node $node): ?ClassMethod
120121
$hasChanged = true;
121122
$param->type = new FullyQualified(DoctrineClass::COLLECTION);
122123

124+
// fix reprint position of type
125+
$param->setAttribute(AttributeKey::ORIGINAL_NODE, null);
126+
123127
// make nullable only 1st param, as others might require a null
124128
if ($param->default instanceof Expr) {
125129
if ($isInTests === false) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\Set\TypedCollections\Fixture;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\ODM\MongoDB\DocumentManager;
7+
8+
final class ParamTypePosition
9+
{
10+
private array $items;
11+
12+
/**
13+
* @param mixed[]|ArrayCollection $collection
14+
*/
15+
public function __construct(
16+
private readonly DocumentManager $documentManager,
17+
private $collection
18+
) {
19+
foreach ($this->collection as $item) {
20+
$this->items[] = $item;
21+
}
22+
}
23+
}
24+
25+
?>
26+
-----
27+
<?php
28+
29+
namespace Rector\Doctrine\Tests\Set\TypedCollections\Fixture;
30+
31+
use Doctrine\Common\Collections\Collection;
32+
use Doctrine\Common\Collections\ArrayCollection;
33+
use Doctrine\ODM\MongoDB\DocumentManager;
34+
35+
final class ParamTypePosition
36+
{
37+
private array $items;
38+
39+
/**
40+
* @param Collection<int, mixed> $collection
41+
*/
42+
public function __construct(
43+
private readonly DocumentManager $documentManager,
44+
private Collection $collection
45+
) {
46+
foreach ($this->collection as $item) {
47+
$this->items[] = $item;
48+
}
49+
}
50+
}
51+
52+
?>

0 commit comments

Comments
 (0)