Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Fixture;

use Rector\Tests\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector\Source\stdClass;
use Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Source\SomeExternalObject;

return static function (stdClass $container): void {
$container->services()
->factory([SomeExternalObject::class, 'sleepStatic']);
};

?>
-----
<?php

namespace Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Fixture;

use Rector\Tests\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector\Source\stdClass;
use Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Source\SomeExternalObject;

return static function (stdClass $container): void {
$container->services()
->factory(SomeExternalObject::sleepStatic(...));
};

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Fixture;

use Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Source\SomeExternalObject;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $container): void {
$container->services()
->factory([SomeExternalObject::class, 'sleepStatic']);
};
17 changes: 14 additions & 3 deletions rules/Php81/Rector/Array_/FirstClassCallableRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
Expand All @@ -24,6 +25,7 @@
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\Symfony\NodeAnalyzer\SymfonyPhpClosureDetector;
use Rector\ValueObject\PhpVersion;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -37,7 +39,8 @@ final class FirstClassCallableRector extends AbstractRector implements MinPhpVer
public function __construct(
private readonly ArrayCallableMethodMatcher $arrayCallableMethodMatcher,
private readonly ReflectionProvider $reflectionProvider,
private readonly ReflectionResolver $reflectionResolver
private readonly ReflectionResolver $reflectionResolver,
private readonly SymfonyPhpClosureDetector $symfonyPhpClosureDetector
) {
}

Expand Down Expand Up @@ -83,15 +86,23 @@ public function name()
*/
public function getNodeTypes(): array
{
return [Property::class, ClassConst::class, Array_::class];
return [Property::class, ClassConst::class, Array_::class, Closure::class];
}

/**
* @param Property|ClassConst|Array_ $node
* @param Property|ClassConst|Array_|Closure $node
* @return StaticCall|MethodCall|null|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN
*/
public function refactor(Node $node): int|null|StaticCall|MethodCall
{
if ($node instanceof Closure) {
if ($this->symfonyPhpClosureDetector->detect($node)) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

return null;
}

if ($node instanceof Property || $node instanceof ClassConst) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
Expand Down