From 3e2e1dc746ebc892e9731fe12bc503ad2f66484e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 23 Jul 2023 16:33:18 +0700 Subject: [PATCH 01/12] [PHPStanStaticTypeMapper] FQCN for Closure type mapper --- .../var-closure-annotation-missing-null-prefer-prepend.php.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/var-closure-annotation-missing-null-prefer-prepend.php.inc b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/var-closure-annotation-missing-null-prefer-prepend.php.inc index a557e92ac0f..b82fcd03eb5 100644 --- a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/var-closure-annotation-missing-null-prefer-prepend.php.inc +++ b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/var-closure-annotation-missing-null-prefer-prepend.php.inc @@ -20,7 +20,7 @@ use Closure; final class VarClosureAnnotationMissingNullPreferPrepend { - /** @var null|Closure(string, int, Closure(float): int): string */ + /** @var null|\Closure(string, int, \Closure(float): int): string */ private ?Closure $tooltipGenerator = null; } From 21c5bfa54e038357cd0d0b4975f2cb1f846176ad Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 23 Jul 2023 16:33:23 +0700 Subject: [PATCH 02/12] [PHPStanStaticTypeMapper] FQCN for Closure type mapper --- ...losure_var_annotation_missing_null.php.inc | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc new file mode 100644 index 00000000000..1872c9a4d1a --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc @@ -0,0 +1,23 @@ + +----- + From c55081800ee382f0de048b901662e5ff0ff6c397 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 23 Jul 2023 16:51:57 +0700 Subject: [PATCH 03/12] Fix --- .../TypeMapper/ClosureTypeMapper.php | 22 ++++++++++++++++++- ...losure_var_annotation_missing_null.php.inc | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php b/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php index f0e75e5233f..8cf454eac79 100644 --- a/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php +++ b/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php @@ -6,9 +6,13 @@ use PhpParser\Node; use PhpParser\Node\Name\FullyQualified; +use PHPStan\PhpDocParser\Ast\Node as AstNode; +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\Type\ClosureType; use PHPStan\Type\Type; +use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode; +use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser; use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; @@ -30,7 +34,23 @@ public function getNodeClass(): string */ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode { - return $type->toPhpDocNode(); + $typeNode = $type->toPhpDocNode(); + + $phpDocNodeTraverser = new PhpDocNodeTraverser(); + $phpDocNodeTraverser->traverseWithCallable($typeNode, '', function (AstNode $astNode) { + if (! $astNode instanceof IdentifierTypeNode) { + return null; + } + + if ($astNode->name !== 'Closure') { + return null; + } + + $astNode = new FullyQualifiedIdentifierTypeNode('Closure'); + return $astNode; + }); + + return $typeNode; } /** diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc index 1872c9a4d1a..5afc3abb66e 100644 --- a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc +++ b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc @@ -16,7 +16,7 @@ namespace Rector\Tests\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNul final class FqcnVarClosureVarAnnotationMissingNull { - /** @var null|\Closure(string, int, \Closure(float):int):string */ + /** @var null|\Closure(string, int, \Closure(float): int): string */ private ?Closure $tooltipGenerator = null; } From 7a4ed3f633ea1349a4c5c9a2777e7b538169eaf4 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sun, 23 Jul 2023 09:53:48 +0000 Subject: [PATCH 04/12] [ci-review] Rector Rectify --- .../PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php b/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php index 8cf454eac79..c8cd5b76827 100644 --- a/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php +++ b/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php @@ -37,15 +37,13 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode $typeNode = $type->toPhpDocNode(); $phpDocNodeTraverser = new PhpDocNodeTraverser(); - $phpDocNodeTraverser->traverseWithCallable($typeNode, '', function (AstNode $astNode) { + $phpDocNodeTraverser->traverseWithCallable($typeNode, '', static function (AstNode $astNode) : ?FullyQualifiedIdentifierTypeNode { if (! $astNode instanceof IdentifierTypeNode) { return null; } - if ($astNode->name !== 'Closure') { return null; } - $astNode = new FullyQualifiedIdentifierTypeNode('Closure'); return $astNode; }); From e96e85f6160cf5347b753b79be7268686fc5aa20 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sun, 23 Jul 2023 09:55:13 +0000 Subject: [PATCH 05/12] [ci-review] Rector Rectify --- .../PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php b/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php index c8cd5b76827..6365f5d45ff 100644 --- a/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php +++ b/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php @@ -41,9 +41,11 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode if (! $astNode instanceof IdentifierTypeNode) { return null; } + if ($astNode->name !== 'Closure') { return null; } + $astNode = new FullyQualifiedIdentifierTypeNode('Closure'); return $astNode; }); From 07be40c1b93ba323be516af39bd6dda3243539e9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 23 Jul 2023 16:57:05 +0700 Subject: [PATCH 06/12] remove replace --- .../PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php b/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php index 6365f5d45ff..682b5d1a356 100644 --- a/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php +++ b/packages/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php @@ -46,8 +46,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode return null; } - $astNode = new FullyQualifiedIdentifierTypeNode('Closure'); - return $astNode; + return new FullyQualifiedIdentifierTypeNode('Closure'); }); return $typeNode; From d7403391cf66f8fb29a4eeabef799793ae222659 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 24 Jul 2023 06:07:02 +0700 Subject: [PATCH 07/12] add auto import test --- .../AutoImportTest.php | 28 +++++++++++++++++++ ...losure_var_annotation_missing_null.php.inc | 2 +- ...losure_var_annotation_missing_null.php.inc | 25 +++++++++++++++++ .../config/configured_rule_auto_import.php | 13 +++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100755 rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/AutoImportTest.php create mode 100644 rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/FixtureAutoImport/fqcn_var_closure_var_annotation_missing_null.php.inc create mode 100755 rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/config/configured_rule_auto_import.php diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/AutoImportTest.php b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/AutoImportTest.php new file mode 100755 index 00000000000..b0ab1c4a065 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/AutoImportTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/FixtureAutoImport'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule_auto_import.php'; + } +} diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc index 5afc3abb66e..98bb3da511f 100644 --- a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc +++ b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc @@ -5,7 +5,7 @@ namespace Rector\Tests\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNul final class FqcnVarClosureVarAnnotationMissingNull { /** @var \Closure(string, int, \Closure(float):int):string */ - private ?Closure $tooltipGenerator = null; + private ?\Closure $tooltipGenerator = null; } ?> diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/FixtureAutoImport/fqcn_var_closure_var_annotation_missing_null.php.inc b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/FixtureAutoImport/fqcn_var_closure_var_annotation_missing_null.php.inc new file mode 100644 index 00000000000..53d1162885e --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/FixtureAutoImport/fqcn_var_closure_var_annotation_missing_null.php.inc @@ -0,0 +1,25 @@ + +----- + diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/config/configured_rule_auto_import.php b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/config/configured_rule_auto_import.php new file mode 100755 index 00000000000..a10cba3c2bb --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/config/configured_rule_auto_import.php @@ -0,0 +1,13 @@ +importNames(); + $rectorConfig->rule(VarAnnotationIncorrectNullableRector::class); +}; From 1829f6c37098fc9f70a3953d9fb28cbd73a56edb Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 24 Jul 2023 06:18:19 +0700 Subject: [PATCH 08/12] revert auto import test --- .../AutoImportTest.php | 28 ------------------- ...losure_var_annotation_missing_null.php.inc | 2 +- ...losure_var_annotation_missing_null.php.inc | 25 ----------------- .../config/configured_rule_auto_import.php | 13 --------- 4 files changed, 1 insertion(+), 67 deletions(-) delete mode 100755 rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/AutoImportTest.php delete mode 100644 rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/FixtureAutoImport/fqcn_var_closure_var_annotation_missing_null.php.inc delete mode 100755 rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/config/configured_rule_auto_import.php diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/AutoImportTest.php b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/AutoImportTest.php deleted file mode 100755 index b0ab1c4a065..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/AutoImportTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/FixtureAutoImport'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule_auto_import.php'; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc index 98bb3da511f..58a9c418e3c 100644 --- a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc +++ b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/fqcn_var_closure_var_annotation_missing_null.php.inc @@ -17,7 +17,7 @@ namespace Rector\Tests\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNul final class FqcnVarClosureVarAnnotationMissingNull { /** @var null|\Closure(string, int, \Closure(float): int): string */ - private ?Closure $tooltipGenerator = null; + private ?\Closure $tooltipGenerator = null; } ?> diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/FixtureAutoImport/fqcn_var_closure_var_annotation_missing_null.php.inc b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/FixtureAutoImport/fqcn_var_closure_var_annotation_missing_null.php.inc deleted file mode 100644 index 53d1162885e..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/FixtureAutoImport/fqcn_var_closure_var_annotation_missing_null.php.inc +++ /dev/null @@ -1,25 +0,0 @@ - ------ - diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/config/configured_rule_auto_import.php b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/config/configured_rule_auto_import.php deleted file mode 100755 index a10cba3c2bb..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/config/configured_rule_auto_import.php +++ /dev/null @@ -1,13 +0,0 @@ -importNames(); - $rectorConfig->rule(VarAnnotationIncorrectNullableRector::class); -}; From a331bf891fda1ccb716ca1b49ecb45056ec3afe3 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 24 Jul 2023 06:22:49 +0700 Subject: [PATCH 09/12] skip already nullable closure var --- .../Fixture/skip_already_nullable_closure_var.php.inc | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/skip_already_nullable_closure_var.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/skip_already_nullable_closure_var.php.inc b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/skip_already_nullable_closure_var.php.inc new file mode 100644 index 00000000000..ea60aa93fe8 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/skip_already_nullable_closure_var.php.inc @@ -0,0 +1,11 @@ + Date: Sun, 23 Jul 2023 23:22:57 +0000 Subject: [PATCH 10/12] [ci-review] Rector Rectify --- ...pedPropertyFromStrictConstructorReadonlyClassRector.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorReadonlyClassRector.php b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorReadonlyClassRector.php index 380495057b1..8cb632d569a 100644 --- a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorReadonlyClassRector.php +++ b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorReadonlyClassRector.php @@ -194,11 +194,6 @@ private function shouldSkipProperty( $isReadOnlyByPhpdoc = $propertyReflection->isReadOnlyByPhpDoc(); } } - - if (! $isReadOnlyByPhpdoc) { - return true; - } - - return false; + return ! $isReadOnlyByPhpdoc; } } From cf36d6cadedd600a6fde98652659734a351e7816 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 24 Jul 2023 06:24:18 +0700 Subject: [PATCH 11/12] rename fxture --- .../Fixture/skip_already_nullable_closure_var.php.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/skip_already_nullable_closure_var.php.inc b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/skip_already_nullable_closure_var.php.inc index ea60aa93fe8..8666eb63860 100644 --- a/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/skip_already_nullable_closure_var.php.inc +++ b/rules-tests/TypeDeclaration/Rector/Property/VarAnnotationIncorrectNullableRector/Fixture/skip_already_nullable_closure_var.php.inc @@ -4,7 +4,7 @@ namespace Rector\Tests\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNul use Closure; -final class FqcnVarClosureVarAnnotationMissingNull +final class SkipAlreadyNullableClosureVar { /** @var null|Closure(string, int, Closure(float):int):string */ private ?Closure $tooltipGenerator = null; From febd3906ae310b8f83cdbeb9bc6b080d095ab57f Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sun, 23 Jul 2023 23:25:05 +0000 Subject: [PATCH 12/12] [ci-review] Rector Rectify --- .../TypedPropertyFromStrictConstructorReadonlyClassRector.php | 1 + 1 file changed, 1 insertion(+) diff --git a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorReadonlyClassRector.php b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorReadonlyClassRector.php index 8cb632d569a..556f06189a2 100644 --- a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorReadonlyClassRector.php +++ b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorReadonlyClassRector.php @@ -194,6 +194,7 @@ private function shouldSkipProperty( $isReadOnlyByPhpdoc = $propertyReflection->isReadOnlyByPhpDoc(); } } + return ! $isReadOnlyByPhpdoc; } }