Skip to content

Commit 725d914

Browse files
committed
[fix] Skip symfony config closures on FirstClassCallableRector as they do not support first class callables
1 parent ade4c81 commit 725d914

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Fixture;
4+
5+
use Rector\Tests\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector\Source\stdClass;
6+
use Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Source\SomeExternalObject;
7+
8+
return static function (stdClass $container): void {
9+
$container->services()
10+
->factory([SomeExternalObject::class, 'sleepStatic']);
11+
};
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Fixture;
18+
19+
use Rector\Tests\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector\Source\stdClass;
20+
use Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Source\SomeExternalObject;
21+
22+
return static function (stdClass $container): void {
23+
$container->services()
24+
->factory(SomeExternalObject::sleepStatic(...));
25+
};
26+
27+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Fixture;
4+
5+
use Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Source\SomeExternalObject;
6+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
7+
8+
return static function (ContainerConfigurator $container): void {
9+
$container->services()
10+
->factory([SomeExternalObject::class, 'sleepStatic']);
11+
};

rules/Php81/Rector/Array_/FirstClassCallableRector.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\Array_;
99
use PhpParser\Node\Expr\ClassConstFetch;
10+
use PhpParser\Node\Expr\Closure;
1011
use PhpParser\Node\Expr\MethodCall;
1112
use PhpParser\Node\Expr\PropertyFetch;
1213
use PhpParser\Node\Expr\StaticCall;
@@ -24,6 +25,7 @@
2425
use Rector\Rector\AbstractRector;
2526
use Rector\Reflection\ReflectionResolver;
2627
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
28+
use Rector\Symfony\NodeAnalyzer\SymfonyPhpClosureDetector;
2729
use Rector\ValueObject\PhpVersion;
2830
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
2931
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -37,7 +39,8 @@ final class FirstClassCallableRector extends AbstractRector implements MinPhpVer
3739
public function __construct(
3840
private readonly ArrayCallableMethodMatcher $arrayCallableMethodMatcher,
3941
private readonly ReflectionProvider $reflectionProvider,
40-
private readonly ReflectionResolver $reflectionResolver
42+
private readonly ReflectionResolver $reflectionResolver,
43+
private readonly SymfonyPhpClosureDetector $symfonyPhpClosureDetector
4144
) {
4245
}
4346

@@ -83,15 +86,23 @@ public function name()
8386
*/
8487
public function getNodeTypes(): array
8588
{
86-
return [Property::class, ClassConst::class, Array_::class];
89+
return [Property::class, ClassConst::class, Array_::class, Closure::class];
8790
}
8891

8992
/**
90-
* @param Property|ClassConst|Array_ $node
93+
* @param Property|ClassConst|Array_|Closure $node
9194
* @return StaticCall|MethodCall|null|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN
9295
*/
9396
public function refactor(Node $node): int|null|StaticCall|MethodCall
9497
{
98+
if ($node instanceof Closure) {
99+
if ($this->symfonyPhpClosureDetector->detect($node)) {
100+
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
101+
}
102+
103+
return null;
104+
}
105+
95106
if ($node instanceof Property || $node instanceof ClassConst) {
96107
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
97108
}

0 commit comments

Comments
 (0)