diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/skip_callable_type_different_definition.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/skip_callable_type_different_definition.php.inc new file mode 100644 index 00000000000..f0d9289764d --- /dev/null +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/skip_callable_type_different_definition.php.inc @@ -0,0 +1,16 @@ +fallback = $fallback; + } +} + +?> diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/skip_callable_type_different_definition2.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/skip_callable_type_different_definition2.php.inc new file mode 100644 index 00000000000..b4a75d57bad --- /dev/null +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/skip_callable_type_different_definition2.php.inc @@ -0,0 +1,16 @@ +fallback = $fallback; + } +} + +?> diff --git a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php index dce30f65410..7ab663ea265 100644 --- a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php +++ b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php @@ -206,22 +206,25 @@ private function shouldSkipParam(Param $param): bool $type = $param->type; } + if ($this->isCallableTypeIdentifier($type)) { + return true; + } + if (! $type instanceof UnionType) { return false; } foreach ($type->types as $type) { - if (! $type instanceof Identifier) { - continue; - } - - if (! $this->isName($type, 'callable')) { - continue; + if ($this->isCallableTypeIdentifier($type)) { + return true; } - - return true; } return false; } + + private function isCallableTypeIdentifier(?Node $node): bool + { + return $node instanceof Identifier && $this->isName($node, 'callable'); + } } diff --git a/src/NodeAnalyzer/PropertyAnalyzer.php b/src/NodeAnalyzer/PropertyAnalyzer.php index edbc0f253d7..d31909f918a 100644 --- a/src/NodeAnalyzer/PropertyAnalyzer.php +++ b/src/NodeAnalyzer/PropertyAnalyzer.php @@ -11,6 +11,7 @@ use PHPStan\Type\TypeWithClassName; use PHPStan\Type\UnionType; use Rector\NodeTypeResolver\NodeTypeResolver; +use Rector\StaticTypeMapper\ValueObject\Type\NonExistingObjectType; final class PropertyAnalyzer { @@ -26,7 +27,7 @@ public function hasForbiddenType(Property $property): bool return true; } - if ($this->isCallableType($propertyType)) { + if ($this->isForbiddenType($propertyType)) { return true; } @@ -36,7 +37,7 @@ public function hasForbiddenType(Property $property): bool $types = $propertyType->getTypes(); foreach ($types as $type) { - if ($this->isCallableType($type)) { + if ($this->isForbiddenType($type)) { return true; } } @@ -44,6 +45,15 @@ public function hasForbiddenType(Property $property): bool return false; } + private function isForbiddenType(Type $type): bool + { + if ($type instanceof NonExistingObjectType) { + return true; + } + + return $this->isCallableType($type); + } + private function isCallableType(Type $type): bool { if ($type instanceof TypeWithClassName) {