Skip to content

Commit 24d5c1b

Browse files
authored
[traverse] Remove decorateNodeAttrGroups(), use directly attribute (#8552)
1 parent e77302a commit 24d5c1b

5 files changed

Lines changed: 27 additions & 53 deletions

File tree

‎rules/Php81/Rector/Array_/ArrayToFirstClassCallableRector.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public function refactor(Node $node): StaticCall|MethodCall|null
9898
return null;
9999
}
100100

101+
if ($node->getAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE) === true) {
102+
return null;
103+
}
104+
101105
$scope = ScopeFetcher::fetch($node);
102106

103107
$arrayCallable = $this->arrayCallableMethodMatcher->match($node, $scope);

‎src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php‎

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PhpParser\Node\Expr;
1111
use PhpParser\Node\Expr\Array_;
1212
use PhpParser\Node\Expr\ArrayDimFetch;
13-
use PhpParser\Node\Expr\ArrowFunction;
1413
use PhpParser\Node\Expr\Assign;
1514
use PhpParser\Node\Expr\AssignOp;
1615
use PhpParser\Node\Expr\AssignRef;
@@ -21,7 +20,6 @@
2120
use PhpParser\Node\Expr\Cast;
2221
use PhpParser\Node\Expr\ClassConstFetch;
2322
use PhpParser\Node\Expr\Clone_;
24-
use PhpParser\Node\Expr\Closure;
2523
use PhpParser\Node\Expr\ConstFetch;
2624
use PhpParser\Node\Expr\Empty_;
2725
use PhpParser\Node\Expr\ErrorSuppress;
@@ -56,13 +54,9 @@
5654
use PhpParser\Node\Name;
5755
use PhpParser\Node\Name\FullyQualified;
5856
use PhpParser\Node\NullableType;
59-
use PhpParser\Node\Param;
6057
use PhpParser\Node\Stmt;
6158
use PhpParser\Node\Stmt\Catch_;
6259
use PhpParser\Node\Stmt\Class_;
63-
use PhpParser\Node\Stmt\ClassConst;
64-
use PhpParser\Node\Stmt\ClassLike;
65-
use PhpParser\Node\Stmt\ClassMethod;
6660
use PhpParser\Node\Stmt\Do_;
6761
use PhpParser\Node\Stmt\Echo_;
6862
use PhpParser\Node\Stmt\ElseIf_;
@@ -72,7 +66,6 @@
7266
use PhpParser\Node\Stmt\Finally_;
7367
use PhpParser\Node\Stmt\For_;
7468
use PhpParser\Node\Stmt\Foreach_;
75-
use PhpParser\Node\Stmt\Function_;
7669
use PhpParser\Node\Stmt\If_;
7770
use PhpParser\Node\Stmt\Interface_;
7871
use PhpParser\Node\Stmt\Property;
@@ -207,8 +200,6 @@ public function processNodes(
207200
return;
208201
}
209202

210-
$this->decorateNodeAttrGroups($node, $mutatingScope, $nodeCallback);
211-
212203
if ((
213204
$node instanceof Expression ||
214205
$node instanceof Return_ ||
@@ -466,8 +457,8 @@ private function nodeScopeResolverProcessNodes(
466457
try {
467458
$this->nodeScopeResolver->processNodes($stmts, $mutatingScope, $nodeCallback);
468459
} catch (ParserErrorsException|ParserException|ShouldNotHappenException|UndefinedVariableException) {
469-
// nothing we can do more precise here as error parsing from deep internal PHPStan service with service injection we cannot reset
470-
// in the middle of process
460+
// nothing we can do more precise here as error parsing from deep internal PHPStan
461+
// service with service injection we cannot reset in the middle of process
471462
// fallback to fill by found scope
472463
RectorNodeScopeResolver::processNodes($stmts, $mutatingScope);
473464
}
@@ -528,40 +519,6 @@ private function processArrayItem(ArrayItem $arrayItem, MutatingScope $mutatingS
528519
}
529520
}
530521

531-
/**
532-
* @param callable(Node $trait, MutatingScope $scope): void $nodeCallback
533-
*/
534-
private function decorateNodeAttrGroups(Node $node, MutatingScope $mutatingScope, callable $nodeCallback): void
535-
{
536-
// better to have AttrGroupsAwareInterface for all Node definition with attrGroups property
537-
// but because may conflict with StmtsAwareInterface patch, this needs to be here
538-
if (
539-
! $node instanceof Param &&
540-
! $node instanceof ArrowFunction &&
541-
! $node instanceof Closure &&
542-
! $node instanceof ClassConst &&
543-
! $node instanceof ClassLike &&
544-
! $node instanceof ClassMethod &&
545-
! $node instanceof EnumCase &&
546-
! $node instanceof Function_ &&
547-
! $node instanceof Property
548-
) {
549-
return;
550-
}
551-
552-
foreach ($node->attrGroups as $attrGroup) {
553-
foreach ($attrGroup->attrs as $attr) {
554-
foreach ($attr->args as $arg) {
555-
$this->nodeScopeResolverProcessNodes(
556-
[new Expression($arg->value)],
557-
$mutatingScope,
558-
$nodeCallback
559-
);
560-
}
561-
}
562-
}
563-
}
564-
565522
private function processSwitch(Switch_ $switch, MutatingScope $mutatingScope): void
566523
{
567524
$switch->cond->setAttribute(AttributeKey::SCOPE, $mutatingScope);
@@ -682,11 +639,6 @@ private function resolveClassOrInterfaceScope(
682639
$context = $this->privatesAccessor->getPrivateProperty($mutatingScope, 'context');
683640
$this->privatesAccessor->setPrivateProperty($context, 'classReflection', null);
684641

685-
try {
686-
return $mutatingScope->enterClass($classReflection);
687-
} catch (ShouldNotHappenException) {
688-
}
689-
690642
return $mutatingScope;
691643
}
692644

@@ -713,7 +665,6 @@ private function processTrait(Trait_ $trait, MutatingScope $mutatingScope, calla
713665
if (! $this->reflectionProvider->hasClass($traitName)) {
714666
$trait->setAttribute(AttributeKey::SCOPE, $mutatingScope);
715667
$this->nodeScopeResolverProcessNodes($trait->stmts, $mutatingScope, $nodeCallback);
716-
$this->decorateNodeAttrGroups($trait, $mutatingScope, $nodeCallback);
717668

718669
return;
719670
}
@@ -736,6 +687,5 @@ private function processTrait(Trait_ $trait, MutatingScope $mutatingScope, calla
736687

737688
$trait->setAttribute(AttributeKey::SCOPE, $traitScope);
738689
$this->nodeScopeResolverProcessNodes($trait->stmts, $traitScope, $nodeCallback);
739-
$this->decorateNodeAttrGroups($trait, $traitScope, $nodeCallback);
740690
}
741691
}

‎tests/Issues/Issue7374/Fixture/fixture.php.inc‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Rector\Tests\Issues\Issue7374\Fixture;
44

5+
use Rector\Tests\Issues\Issue7374\Source\BusibooksBusinesses;
6+
57
final class ClassWithTwoMethods
68
{
79
private function displayTree($name, $data, $other = null)
@@ -19,11 +21,15 @@ final class ClassWithTwoMethods
1921
));
2022
}
2123
}
24+
25+
?>
2226
-----
2327
<?php
2428

2529
namespace Rector\Tests\Issues\Issue7374\Fixture;
2630

31+
use Rector\Tests\Issues\Issue7374\Source\BusibooksBusinesses;
32+
2733
final class ClassWithTwoMethods
2834
{
2935
private function displayTree($data)
@@ -38,3 +44,5 @@ final class ClassWithTwoMethods
3844
$this->displayTree($equity);
3945
}
4046
}
47+
48+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\Issue7374\Source;
6+
7+
final class BusibooksBusinesses
8+
{
9+
}

‎tests/Issues/Issue7374/config/configured_rule.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
use Rector\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector;
88

99
return RectorConfig::configure()
10-
->withRules([RemoveUnusedPrivateMethodParameterRector::class, RenameVariableToMatchNewTypeRector::class]);
10+
->withRules([
11+
RemoveUnusedPrivateMethodParameterRector::class,
12+
RenameVariableToMatchNewTypeRector::class,
13+
]);

0 commit comments

Comments
 (0)