From db5688c285d6c02fedc1f9e9b4693e60a08e70ef Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 16 Sep 2024 08:29:09 +0700 Subject: [PATCH] [TypeDeclaration] Skip nullable callable on TypedPropertyFromAssignsRector (#6308) * [TypeDeclaration] Skip nullable callable on TypedPropertyFromAssignsRector * [TypeDeclaration] Skip nullable callable on TypedPropertyFromAssignsRector * Fix --- .../Fixture/union_callable_return.php.inc | 35 +++++++++++++++++++ .../Fixture/skip_nullable_callable.php.inc | 18 ++++++++++ .../TypeMapper/UnionTypeMapper.php | 10 ++++-- 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_callable_return.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector/Fixture/skip_nullable_callable.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_callable_return.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_callable_return.php.inc new file mode 100644 index 00000000000..561f3026450 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_callable_return.php.inc @@ -0,0 +1,35 @@ + +----- + diff --git a/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector/Fixture/skip_nullable_callable.php.inc b/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector/Fixture/skip_nullable_callable.php.inc new file mode 100644 index 00000000000..3afbf8f41dc --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector/Fixture/skip_nullable_callable.php.inc @@ -0,0 +1,18 @@ +prop = $prop; + } + + public function reset(): void + { + $this->prop = null; + } +} diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php index a2284997d53..6bc54256275 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php @@ -13,6 +13,7 @@ use PhpParser\Node\NullableType; use PhpParser\Node\UnionType as PhpParserUnionType; use PHPStan\PhpDocParser\Ast\Type\TypeNode; +use PHPStan\Type\CallableType; use PHPStan\Type\Type; use PHPStan\Type\UnionType; use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode; @@ -64,7 +65,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode */ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node { - $phpParserUnionType = $this->matchPhpParserUnionType($type); + $phpParserUnionType = $this->matchPhpParserUnionType($type, $typeKind); if ($phpParserUnionType instanceof PhpParserUnionType) { return $this->resolveUnionTypeNode($phpParserUnionType); } @@ -171,7 +172,7 @@ private function hasObjectAndStaticType(PhpParserUnionType $phpParserUnionType): /** * @return Name|FullyQualified|ComplexType|Identifier|null */ - private function matchPhpParserUnionType(UnionType $unionType): ?Node + private function matchPhpParserUnionType(UnionType $unionType, string $typeKind): ?Node { $phpParserUnionedTypes = []; @@ -183,6 +184,11 @@ private function matchPhpParserUnionType(UnionType $unionType): ?Node return null; } + // special callable type only not allowed on property + if ($typeKind === TypeKind::PROPERTY && $unionedType instanceof CallableType) { + return null; + } + $phpParserUnionedTypes[] = $phpParserNode; }