Skip to content

Commit 5717673

Browse files
authored
skip assign of New_ to CollectionParamTypeSetterToCollectionPropertyRector (#419)
* add failing fixture * skip assign of New_ to CollectionParamTypeSetterToCollectionPropertyRector
1 parent 0985d2e commit 5717673

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\CollectionParamTypeSetterToCollectionPropertyRector\Fixture;
6+
7+
use Doctrine\Common\Collections\ArrayCollection;
8+
use Doctrine\Common\Collections\Collection;
9+
10+
final class SkipConstructorSetter
11+
{
12+
/**
13+
* @var Collection<int, string>
14+
*/
15+
public $items;
16+
17+
public function __construct($item)
18+
{
19+
$this->items = new ArrayCollection([]);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector\Fixture;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
7+
use Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector\Source\SomeInstance;
8+
9+
final class SkipNonCollection
10+
{
11+
public Collection $items;
12+
13+
public function __construct($item)
14+
{
15+
$this->items = new ArrayCollection();
16+
if ($item instanceof SomeInstance) {
17+
echo 'check';
18+
}
19+
}
20+
}
21+
22+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector\Source;
4+
5+
class SomeInstance
6+
{
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Fixture;
4+
5+
use Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Source\ItemType;
6+
use Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Source\SomeClassWithConstructorCollection;
7+
use Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Source\SomeClassWithSetter;
8+
9+
final class NewInstance
10+
{
11+
public function run()
12+
{
13+
$someVariable = [new ItemType(), new ItemType()];
14+
15+
$someClassWithSetter = new SomeClassWithConstructorCollection($someVariable);
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Fixture;
24+
25+
use Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Source\ItemType;
26+
use Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Source\SomeClassWithConstructorCollection;
27+
use Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Source\SomeClassWithSetter;
28+
29+
final class NewInstance
30+
{
31+
public function run()
32+
{
33+
$someVariable = [new ItemType(), new ItemType()];
34+
35+
$someClassWithSetter = new SomeClassWithConstructorCollection(new \Doctrine\Common\Collections\ArrayCollection($someVariable));
36+
}
37+
}
38+
39+
?>

rules/TypedCollections/Rector/ClassMethod/CollectionParamTypeSetterToCollectionPropertyRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\Assign;
10+
use PhpParser\Node\Expr\New_;
1011
use PhpParser\Node\Expr\PropertyFetch;
1112
use PhpParser\Node\Name;
1213
use PhpParser\Node\Name\FullyQualified;
@@ -131,6 +132,10 @@ private function isAssignToPropertyFetchCollection(Assign $assign): bool
131132
return false;
132133
}
133134

135+
if ($assign->expr instanceof New_) {
136+
return false;
137+
}
138+
134139
$propertyFetchType = $this->getType($assign->var);
135140
if (! $propertyFetchType instanceof ObjectType) {
136141
return false;

0 commit comments

Comments
 (0)