From a6e9e9cc15e68f9476d2682720701d36f1f09757 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Jul 2022 05:25:46 +0700 Subject: [PATCH 1/7] [CodeQuality] Handle crash attribute used on trait on CallableThisArrayToAnonymousFunctionRector --- .../Fixture/skip_attribute_on_trait.php.inc | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/Array_/CallableThisArrayToAnonymousFunctionRector/Fixture/skip_attribute_on_trait.php.inc diff --git a/rules-tests/CodeQuality/Rector/Array_/CallableThisArrayToAnonymousFunctionRector/Fixture/skip_attribute_on_trait.php.inc b/rules-tests/CodeQuality/Rector/Array_/CallableThisArrayToAnonymousFunctionRector/Fixture/skip_attribute_on_trait.php.inc new file mode 100644 index 00000000000..d1ef8c29e6d --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Array_/CallableThisArrayToAnonymousFunctionRector/Fixture/skip_attribute_on_trait.php.inc @@ -0,0 +1,10 @@ + Date: Mon, 18 Jul 2022 06:52:05 +0700 Subject: [PATCH 2/7] Fixed :tada: --- .../Scope/PHPStanNodeScopeResolver.php | 76 ++++++++++++------- 1 file changed, 48 insertions(+), 28 deletions(-) diff --git a/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index bb23a48be0f..edb88f7b9af 100644 --- a/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -13,6 +13,7 @@ use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; +use PhpParser\Node\Param; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Enum_; @@ -110,7 +111,7 @@ public function processNodes( $isScopeRefreshing, $smartFileInfo ): void { - if ($node instanceof Expression) { + if (($node instanceof Expression || $node instanceof Return_) && $node->expr instanceof Expr) { $node->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope); } @@ -140,30 +141,11 @@ public function processNodes( } if ($node instanceof Switch_) { - // decorate value as well - foreach ($node->cases as $case) { - $case->setAttribute(AttributeKey::SCOPE, $mutatingScope); - } + $this->processSwitch($node, $mutatingScope); } if ($node instanceof TryCatch) { - foreach ($node->catches as $catch) { - $varName = $catch->var instanceof Variable - ? $this->nodeNameResolver->getName($catch->var) - : null; - $type = TypeCombinator::union( - ...array_map( - static fn (Name $class): ObjectType => new ObjectType((string) $class), - $catch->types - ) - ); - $catchMutatingScope = $mutatingScope->enterCatchType($type, $varName); - $this->processNodes($catch->stmts, $smartFileInfo, $catchMutatingScope); - } - - if ($node->finally instanceof Finally_) { - $node->finally->setAttribute(AttributeKey::SCOPE, $mutatingScope); - } + $this->processTryCatch($node, $smartFileInfo, $mutatingScope); } if ($node instanceof Assign) { @@ -172,11 +154,6 @@ public function processNodes( $node->var->setAttribute(AttributeKey::SCOPE, $mutatingScope); } - // decorate value as well - if ($node instanceof Return_ && $node->expr instanceof Expr) { - $node->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope); - } - if ($node instanceof Trait_) { $traitName = $this->resolveClassName($node); @@ -207,8 +184,9 @@ public function processNodes( ); $node->setAttribute(AttributeKey::SCOPE, $traitScope); - $this->nodeScopeResolver->processNodes($node->stmts, $traitScope, $nodeCallback); + $this->decorateTraitAttrGroups($node, $traitScope); + return; } @@ -232,6 +210,48 @@ public function processNodes( return $this->processNodesWithDependentFiles($smartFileInfo, $stmts, $scope, $nodeCallback); } + private function decorateTraitAttrGroups(Trait_ $trait, MutatingScope $mutatingScope): void + { + foreach ($trait->attrGroups as $attrGroup) { + foreach ($attrGroup->attrs as $attr) { + foreach ($attr->args as $arg) { + $arg->value->setAttribute(AttributeKey::SCOPE, $mutatingScope); + } + } + } + } + + private function processSwitch(Switch_ $switch, MutatingScope $mutatingScope): void + { + // decorate value as well + foreach ($switch->cases as $case) { + $case->setAttribute(AttributeKey::SCOPE, $mutatingScope); + } + } + + private function processTryCatch( + TryCatch $tryCatch, + SmartFileInfo $smartFileInfo, + MutatingScope $mutatingScope + ): void { + foreach ($tryCatch->catches as $catch) { + $varName = $catch->var instanceof Variable + ? $this->nodeNameResolver->getName($catch->var) + : null; + + $type = TypeCombinator::union( + ...array_map(static fn (Name $class): ObjectType => new ObjectType((string) $class), $catch->types) + ); + + $catchMutatingScope = $mutatingScope->enterCatchType($type, $varName); + $this->processNodes($catch->stmts, $smartFileInfo, $catchMutatingScope); + } + + if ($tryCatch->finally instanceof Finally_) { + $tryCatch->finally->setAttribute(AttributeKey::SCOPE, $mutatingScope); + } + } + private function processUnreachableStatementNode( UnreachableStatementNode $unreachableStatementNode, SmartFileInfo $smartFileInfo, From 067d459def8f9e7acd8cbd7f3edf6ba52a65e868 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Jul 2022 06:53:33 +0700 Subject: [PATCH 3/7] eol --- .../Fixture/skip_attribute_on_trait.php.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules-tests/CodeQuality/Rector/Array_/CallableThisArrayToAnonymousFunctionRector/Fixture/skip_attribute_on_trait.php.inc b/rules-tests/CodeQuality/Rector/Array_/CallableThisArrayToAnonymousFunctionRector/Fixture/skip_attribute_on_trait.php.inc index d1ef8c29e6d..5a88b5d15b1 100644 --- a/rules-tests/CodeQuality/Rector/Array_/CallableThisArrayToAnonymousFunctionRector/Fixture/skip_attribute_on_trait.php.inc +++ b/rules-tests/CodeQuality/Rector/Array_/CallableThisArrayToAnonymousFunctionRector/Fixture/skip_attribute_on_trait.php.inc @@ -7,4 +7,4 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\UniqueConstraint(columns: ['public_id', 'tenant_id'])] trait SkipAttributeOnTrait { -} \ No newline at end of file +} From 531152115bebb953673dabd4ebc06c3352a49579 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Jul 2022 06:55:44 +0700 Subject: [PATCH 4/7] phpstan --- phpstan.neon | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 7c15d844005..c648a4f0430 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -672,3 +672,5 @@ parameters: - message: '#Call to static method Webmozart\\Assert\\Assert\:\:allString\(\) with array will always evaluate to true#' path: rules/Transform/ValueObject/ParentClassToTraits.php + + - '#Relative file path ".+" is not allowed, use absolute one with __DIR__#' From 7792e30b93fc2e0863552510b32da8fec7a3637f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Jul 2022 06:57:28 +0700 Subject: [PATCH 5/7] clean up --- .../NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index edb88f7b9af..36bcb7d8b5b 100644 --- a/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -13,7 +13,6 @@ use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; -use PhpParser\Node\Param; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Enum_; From 4ab7897a231b224d42dde84644a33d4baa6a3da0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Jul 2022 10:00:50 +0700 Subject: [PATCH 6/7] final touch: define list of files for phpstan notice absolute path ignore --- phpstan.neon | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index c648a4f0430..c94b154c5e5 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -673,4 +673,10 @@ parameters: message: '#Call to static method Webmozart\\Assert\\Assert\:\:allString\(\) with array will always evaluate to true#' path: rules/Transform/ValueObject/ParentClassToTraits.php - - '#Relative file path ".+" is not allowed, use absolute one with __DIR__#' + - + message: '#Relative file path ".+" is not allowed, use absolute one with __DIR__#' + paths: + - bin/rector.php + - src/Bootstrap/RectorConfigsResolver.php + - src/ValueObject/StaticNonPhpFileSuffixes.php + - tests/FileSystem/FilesFinder/FilesFinderTest.php \ No newline at end of file From c0f20a0430ed2dcb698cd348a19b1103c1d3d892 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Jul 2022 10:02:29 +0700 Subject: [PATCH 7/7] final touch: eol --- phpstan.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index c94b154c5e5..913069db8b1 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -679,4 +679,4 @@ parameters: - bin/rector.php - src/Bootstrap/RectorConfigsResolver.php - src/ValueObject/StaticNonPhpFileSuffixes.php - - tests/FileSystem/FilesFinder/FilesFinderTest.php \ No newline at end of file + - tests/FileSystem/FilesFinder/FilesFinderTest.php