Skip to content

Commit 63abe8e

Browse files
committed
Updated Rector to commit 3f60b07605cbbea6be4a6e5936b202310c092668
rectorphp/rector-src@3f60b07 [cleanup] Make FirstClassCallableRector use attributes over node traverser hacks (#7696)
1 parent 2730798 commit 63abe8e

File tree

8 files changed

+92
-11
lines changed

8 files changed

+92
-11
lines changed

rules/Php81/Rector/Array_/FirstClassCallableRector.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
use PhpParser\Node\Expr\PropertyFetch;
1212
use PhpParser\Node\Expr\StaticCall;
1313
use PhpParser\Node\Expr\Variable;
14-
use PhpParser\Node\Stmt\ClassConst;
15-
use PhpParser\Node\Stmt\Property;
1614
use PhpParser\Node\VariadicPlaceholder;
1715
use PhpParser\NodeVisitor;
1816
use PHPStan\Analyser\Scope;
1917
use PHPStan\Reflection\ClassReflection;
2018
use PHPStan\Reflection\ReflectionProvider;
2119
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
2220
use Rector\NodeCollector\ValueObject\ArrayCallable;
21+
use Rector\NodeTypeResolver\Node\AttributeKey;
2322
use Rector\PHPStan\ScopeFetcher;
2423
use Rector\Rector\AbstractRector;
2524
use Rector\Reflection\ReflectionResolver;
@@ -30,6 +29,7 @@
3029
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
3130
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
3231
/**
32+
* @see RFC https://wiki.php.net/rfc/first_class_callable_syntax
3333
* @see \Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\FirstClassCallableRectorTest
3434
*/
3535
final class FirstClassCallableRector extends AbstractRector implements MinPhpVersionInterface
@@ -59,7 +59,6 @@ public function __construct(ArrayCallableMethodMatcher $arrayCallableMethodMatch
5959
}
6060
public function getRuleDefinition(): RuleDefinition
6161
{
62-
// see RFC https://wiki.php.net/rfc/first_class_callable_syntax
6362
return new RuleDefinition('Upgrade array callable to first class callable', [new CodeSample(<<<'CODE_SAMPLE'
6463
final class SomeClass
6564
{
@@ -93,10 +92,10 @@ public function name()
9392
*/
9493
public function getNodeTypes(): array
9594
{
96-
return [Property::class, ClassConst::class, Array_::class, Closure::class];
95+
return [Array_::class, Closure::class];
9796
}
9897
/**
99-
* @param Property|ClassConst|Array_|Closure $node
98+
* @param Array_|Closure $node
10099
* @return StaticCall|MethodCall|null|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN
101100
*/
102101
public function refactor(Node $node)
@@ -107,9 +106,6 @@ public function refactor(Node $node)
107106
}
108107
return null;
109108
}
110-
if ($node instanceof Property || $node instanceof ClassConst) {
111-
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
112-
}
113109
$scope = ScopeFetcher::fetch($node);
114110
$arrayCallable = $this->arrayCallableMethodMatcher->match($node, $scope);
115111
if (!$arrayCallable instanceof ArrayCallable) {
@@ -119,6 +115,12 @@ public function refactor(Node $node)
119115
if (!$callerExpr instanceof Variable && !$callerExpr instanceof PropertyFetch && !$callerExpr instanceof ClassConstFetch) {
120116
return null;
121117
}
118+
if ($node->getAttribute(AttributeKey::IS_CLASS_CONST_VALUE)) {
119+
return null;
120+
}
121+
if ($node->getAttribute(AttributeKey::IS_DEFAULT_PROPERTY_VALUE)) {
122+
return null;
123+
}
122124
$args = [new VariadicPlaceholder()];
123125
if ($callerExpr instanceof ClassConstFetch) {
124126
$type = $this->getType($callerExpr->class);

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '3ec5fda3c43798afd41b326e7dfc7e6aab6ba597';
22+
public const PACKAGE_VERSION = '3f60b07605cbbea6be4a6e5936b202310c092668';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2025-12-01 22:54:29';
27+
public const RELEASE_DATE = '2025-12-02 00:32:30';
2828
/**
2929
* @var int
3030
*/

src/DependencyInjection/LazyContainerFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\ContextNodeVisitor;
102102
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\GlobalVariableNodeVisitor;
103103
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\NameNodeVisitor;
104+
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\PropertyOrClassConstDefaultNodeVisitor;
104105
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\StaticVariableNodeVisitor;
105106
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
106107
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
@@ -203,7 +204,7 @@ final class LazyContainerFactory
203204
/**
204205
* @var array<class-string<DecoratingNodeVisitorInterface>>
205206
*/
206-
private const DECORATING_NODE_VISITOR_CLASSES = [ArgNodeVisitor::class, AssignedToNodeVisitor::class, ByRefReturnNodeVisitor::class, ByRefVariableNodeVisitor::class, ContextNodeVisitor::class, GlobalVariableNodeVisitor::class, NameNodeVisitor::class, StaticVariableNodeVisitor::class];
207+
private const DECORATING_NODE_VISITOR_CLASSES = [ArgNodeVisitor::class, AssignedToNodeVisitor::class, ByRefReturnNodeVisitor::class, ByRefVariableNodeVisitor::class, ContextNodeVisitor::class, GlobalVariableNodeVisitor::class, NameNodeVisitor::class, StaticVariableNodeVisitor::class, PropertyOrClassConstDefaultNodeVisitor::class];
207208
/**
208209
* @var array<class-string<PhpDocTypeMapperInterface>>
209210
*/

src/NodeTypeResolver/Node/AttributeKey.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,6 @@ final class AttributeKey
235235
* @var string
236236
*/
237237
public const HAS_MERGED_COMMENTS = 'has_merged_comments';
238+
public const IS_DEFAULT_PROPERTY_VALUE = 'is_default_property_value';
239+
public const IS_CLASS_CONST_VALUE = 'is_default_class_const_value';
238240
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor;
5+
6+
use PhpParser\Node;
7+
use PhpParser\Node\Expr;
8+
use PhpParser\Node\Stmt\ClassConst;
9+
use PhpParser\Node\Stmt\Property;
10+
use PhpParser\NodeVisitorAbstract;
11+
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
12+
use Rector\NodeTypeResolver\Node\AttributeKey;
13+
use Rector\PhpParser\NodeTraverser\SimpleTraverser;
14+
final class PropertyOrClassConstDefaultNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
15+
{
16+
public function enterNode(Node $node): ?Node
17+
{
18+
if ($node instanceof Property) {
19+
foreach ($node->props as $propertyItem) {
20+
$default = $propertyItem->default;
21+
if (!$default instanceof Expr) {
22+
continue;
23+
}
24+
SimpleTraverser::decorateWithTrueAttribute($default, AttributeKey::IS_DEFAULT_PROPERTY_VALUE);
25+
}
26+
}
27+
if ($node instanceof ClassConst) {
28+
foreach ($node->consts as $const) {
29+
SimpleTraverser::decorateWithTrueAttribute($const->value, AttributeKey::IS_CLASS_CONST_VALUE);
30+
}
31+
}
32+
return null;
33+
}
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\PhpParser\NodeTraverser;
5+
6+
use PhpParser\Node;
7+
use PhpParser\NodeTraverser;
8+
use PhpParser\NodeVisitorAbstract;
9+
use Rector\NodeTypeResolver\Node\AttributeKey;
10+
final class SimpleTraverser
11+
{
12+
/**
13+
* @param Node[]|Node $nodesOrNode
14+
* @param AttributeKey::* $attributeKey
15+
*/
16+
public static function decorateWithTrueAttribute($nodesOrNode, string $attributeKey): void
17+
{
18+
$callableNodeVisitor = new class($attributeKey) extends NodeVisitorAbstract
19+
{
20+
/**
21+
* @readonly
22+
*/
23+
private string $attributeKey;
24+
public function __construct(string $attributeKey)
25+
{
26+
$this->attributeKey = $attributeKey;
27+
}
28+
public function enterNode(Node $node)
29+
{
30+
$node->setAttribute($this->attributeKey, \true);
31+
return null;
32+
}
33+
};
34+
$nodeTraverser = new NodeTraverser($callableNodeVisitor);
35+
$nodes = $nodesOrNode instanceof Node ? [$nodesOrNode] : $nodesOrNode;
36+
$nodeTraverser->traverse($nodes);
37+
}
38+
}

vendor/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,7 @@
18371837
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\ContextNodeVisitor' => $baseDir . '/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php',
18381838
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\GlobalVariableNodeVisitor' => $baseDir . '/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php',
18391839
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\NameNodeVisitor' => $baseDir . '/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/NameNodeVisitor.php',
1840+
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\PropertyOrClassConstDefaultNodeVisitor' => $baseDir . '/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/PropertyOrClassConstDefaultNodeVisitor.php',
18401841
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\StaticVariableNodeVisitor' => $baseDir . '/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php',
18411842
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\PHPStanNodeScopeResolver' => $baseDir . '/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php',
18421843
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\RectorNodeScopeResolver' => $baseDir . '/src/NodeTypeResolver/PHPStan/Scope/RectorNodeScopeResolver.php',
@@ -2275,6 +2276,7 @@
22752276
'Rector\\PhpParser\\NodeTraverser\\AbstractImmutableNodeTraverser' => $baseDir . '/src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php',
22762277
'Rector\\PhpParser\\NodeTraverser\\FileWithoutNamespaceNodeTraverser' => $baseDir . '/src/PhpParser/NodeTraverser/FileWithoutNamespaceNodeTraverser.php',
22772278
'Rector\\PhpParser\\NodeTraverser\\RectorNodeTraverser' => $baseDir . '/src/PhpParser/NodeTraverser/RectorNodeTraverser.php',
2279+
'Rector\\PhpParser\\NodeTraverser\\SimpleTraverser' => $baseDir . '/src/PhpParser/NodeTraverser/SimpleTraverser.php',
22782280
'Rector\\PhpParser\\Node\\AssignAndBinaryMap' => $baseDir . '/src/PhpParser/Node/AssignAndBinaryMap.php',
22792281
'Rector\\PhpParser\\Node\\BetterNodeFinder' => $baseDir . '/src/PhpParser/Node/BetterNodeFinder.php',
22802282
'Rector\\PhpParser\\Node\\CustomNode\\FileWithoutNamespace' => $baseDir . '/src/PhpParser/Node/CustomNode/FileWithoutNamespace.php',

vendor/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,6 +2085,7 @@ class ComposerStaticInita05020a1ba1c38c2596fcbea78eb7e22
20852085
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\ContextNodeVisitor' => __DIR__ . '/../..' . '/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php',
20862086
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\GlobalVariableNodeVisitor' => __DIR__ . '/../..' . '/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php',
20872087
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\NameNodeVisitor' => __DIR__ . '/../..' . '/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/NameNodeVisitor.php',
2088+
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\PropertyOrClassConstDefaultNodeVisitor' => __DIR__ . '/../..' . '/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/PropertyOrClassConstDefaultNodeVisitor.php',
20882089
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\StaticVariableNodeVisitor' => __DIR__ . '/../..' . '/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php',
20892090
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\PHPStanNodeScopeResolver' => __DIR__ . '/../..' . '/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php',
20902091
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\RectorNodeScopeResolver' => __DIR__ . '/../..' . '/src/NodeTypeResolver/PHPStan/Scope/RectorNodeScopeResolver.php',
@@ -2523,6 +2524,7 @@ class ComposerStaticInita05020a1ba1c38c2596fcbea78eb7e22
25232524
'Rector\\PhpParser\\NodeTraverser\\AbstractImmutableNodeTraverser' => __DIR__ . '/../..' . '/src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php',
25242525
'Rector\\PhpParser\\NodeTraverser\\FileWithoutNamespaceNodeTraverser' => __DIR__ . '/../..' . '/src/PhpParser/NodeTraverser/FileWithoutNamespaceNodeTraverser.php',
25252526
'Rector\\PhpParser\\NodeTraverser\\RectorNodeTraverser' => __DIR__ . '/../..' . '/src/PhpParser/NodeTraverser/RectorNodeTraverser.php',
2527+
'Rector\\PhpParser\\NodeTraverser\\SimpleTraverser' => __DIR__ . '/../..' . '/src/PhpParser/NodeTraverser/SimpleTraverser.php',
25262528
'Rector\\PhpParser\\Node\\AssignAndBinaryMap' => __DIR__ . '/../..' . '/src/PhpParser/Node/AssignAndBinaryMap.php',
25272529
'Rector\\PhpParser\\Node\\BetterNodeFinder' => __DIR__ . '/../..' . '/src/PhpParser/Node/BetterNodeFinder.php',
25282530
'Rector\\PhpParser\\Node\\CustomNode\\FileWithoutNamespace' => __DIR__ . '/../..' . '/src/PhpParser/Node/CustomNode/FileWithoutNamespace.php',

0 commit comments

Comments
 (0)