diff --git a/rules-tests/TypedCollections/Rector/MethodCall/SetArrayToNewCollectionRector/Fixture/include_local_setter.php.inc b/rules-tests/TypedCollections/Rector/MethodCall/SetArrayToNewCollectionRector/Fixture/include_local_setter.php.inc new file mode 100644 index 00000000..9ad2dd69 --- /dev/null +++ b/rules-tests/TypedCollections/Rector/MethodCall/SetArrayToNewCollectionRector/Fixture/include_local_setter.php.inc @@ -0,0 +1,49 @@ +setItems($someVariable); + } + + public function setItems(Collection $collection) + { + } +} + +?> +----- +setItems(new \Doctrine\Common\Collections\ArrayCollection($someVariable)); + } + + public function setItems(Collection $collection) + { + } +} + +?> diff --git a/rules/TypedCollections/NodeAnalyzer/CollectionParamCallDetector.php b/rules/TypedCollections/NodeAnalyzer/CollectionParamCallDetector.php index a030c7cc..07c82cbf 100644 --- a/rules/TypedCollections/NodeAnalyzer/CollectionParamCallDetector.php +++ b/rules/TypedCollections/NodeAnalyzer/CollectionParamCallDetector.php @@ -11,6 +11,7 @@ use PHPStan\Reflection\Php\PhpParameterReflection; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\ObjectType; +use PHPStan\Type\ThisType; use PHPStan\Type\TypeCombinator; use Rector\Doctrine\Enum\DoctrineClass; use Rector\NodeNameResolver\NodeNameResolver; @@ -42,6 +43,12 @@ public function detect(MethodCall|StaticCall|New_ $callLike, int $position): boo } $callerType = TypeCombinator::removeNull($callerType); + + // to support same-class calls as well + if ($callerType instanceof ThisType) { + $callerType = $callerType->getStaticObjectType(); + } + if (! $callerType instanceof ObjectType) { return false; }