File tree Expand file tree Collapse file tree 6 files changed +52
-5
lines changed
rules-tests/Php81/Rector/Array_/FirstClassCallableRector/Fixture
rules/Php81/Rector/Array_
PHPStan/Scope/NodeVisitor Expand file tree Collapse file tree 6 files changed +52
-5
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,10 @@ final class SkipOnClassConst
66{
77 private const CLASS_CONST = [SkipOnClassConst::class, 'size ' ];
88
9+ private const CLASS_CONST_NESTED = [
10+ 'hey ' => [SkipOnClassConst::class, 'size ' ]
11+ ];
12+
913 public function size ()
1014 {
1115 }
Original file line number Diff line number Diff line change @@ -6,6 +6,10 @@ final class SkipOnProperty
66{
77 private $ prop = [SkipOnProperty::class, 'size ' ];
88
9+ private $ nestedProperty = [
10+ 'some_callback ' => [SkipOnProperty::class, 'size ' ]
11+ ];
12+
913 public function size ()
1014 {
1115 }
Original file line number Diff line number Diff line change 3131use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
3232
3333/**
34+ * @see RFC https://wiki.php.net/rfc/first_class_callable_syntax
3435 * @see \Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\FirstClassCallableRectorTest
3536 */
3637final class FirstClassCallableRector extends AbstractRector implements MinPhpVersionInterface
@@ -45,7 +46,6 @@ public function __construct(
4546
4647 public function getRuleDefinition (): RuleDefinition
4748 {
48- // see RFC https://wiki.php.net/rfc/first_class_callable_syntax
4949 return new RuleDefinition ('Upgrade array callable to first class callable ' , [
5050 new CodeSample (
5151 <<<'CODE_SAMPLE'
@@ -114,7 +114,7 @@ public function refactor(Node $node): StaticCall|MethodCall|null|int
114114 return null ;
115115 }
116116
117- if ($ node ->getAttribute (AttributeKey::IS_DEFAULT_CLASS_CONST_VALUE )) {
117+ if ($ node ->getAttribute (AttributeKey::IS_CLASS_CONST_VALUE )) {
118118 return null ;
119119 }
120120
Original file line number Diff line number Diff line change @@ -289,5 +289,5 @@ final class AttributeKey
289289
290290 public const IS_DEFAULT_PROPERTY_VALUE = 'is_default_property_value ' ;
291291
292- public const IS_DEFAULT_CLASS_CONST_VALUE = 'is_default_class_const_value ' ;
292+ public const IS_CLASS_CONST_VALUE = 'is_default_class_const_value ' ;
293293}
Original file line number Diff line number Diff line change 1111use PhpParser \NodeVisitorAbstract ;
1212use Rector \Contract \PhpParser \DecoratingNodeVisitorInterface ;
1313use Rector \NodeTypeResolver \Node \AttributeKey ;
14+ use Rector \PhpParser \NodeTraverser \SimpleTraverser ;
1415
1516final class PropertyOrClassConstDefaultNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
1617{
@@ -23,13 +24,13 @@ public function enterNode(Node $node): ?Node
2324 continue ;
2425 }
2526
26- $ default-> setAttribute ( AttributeKey::IS_DEFAULT_PROPERTY_VALUE , true );
27+ SimpleTraverser:: decorateWithTrueAttribute ( $ default, AttributeKey::IS_DEFAULT_PROPERTY_VALUE );
2728 }
2829 }
2930
3031 if ($ node instanceof ClassConst) {
3132 foreach ($ node ->consts as $ const ) {
32- $ const ->value -> setAttribute ( AttributeKey::IS_DEFAULT_CLASS_CONST_VALUE , true );
33+ SimpleTraverser:: decorateWithTrueAttribute ( $ const ->value , AttributeKey::IS_CLASS_CONST_VALUE );
3334 }
3435 }
3536
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Rector \PhpParser \NodeTraverser ;
6+
7+ use PhpParser \Node ;
8+ use PhpParser \NodeTraverser ;
9+ use PhpParser \NodeVisitorAbstract ;
10+ use Rector \NodeTypeResolver \Node \AttributeKey ;
11+
12+ final class SimpleTraverser
13+ {
14+ /**
15+ * @param Node[]|Node $nodesOrNode
16+ * @param AttributeKey::* $attributeKey
17+ */
18+ public static function decorateWithTrueAttribute (array |Node $ nodesOrNode , string $ attributeKey ): void
19+ {
20+ $ callableNodeVisitor = new class ($ attributeKey ) extends NodeVisitorAbstract {
21+ public function __construct (
22+ private readonly string $ attributeKey
23+ ) {
24+ }
25+
26+ public function enterNode (Node $ node )
27+ {
28+ $ node ->setAttribute ($ this ->attributeKey , true );
29+ return null ;
30+ }
31+ };
32+
33+ $ nodeTraverser = new NodeTraverser ($ callableNodeVisitor );
34+
35+ $ nodes = $ nodesOrNode instanceof Node ? [$ nodesOrNode ] : $ nodesOrNode ;
36+ $ nodeTraverser ->traverse ($ nodes );
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments