2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2019-04-04 12:08:55 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Array_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2019-04-17 19:17:29 +00:00
|
|
|
use PhpParser\Node\Expr\Array_;
|
2021-02-28 07:47:48 +00:00
|
|
|
use PHPStan\Reflection\Php\PhpMethodReflection;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-06-29 00:31:58 +00:00
|
|
|
use Rector\Core\Reflection\ReflectionResolver;
|
|
|
|
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
|
|
|
|
use Rector\NodeCollector\ValueObject\ArrayCallable;
|
2019-11-07 20:37:39 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-06-10 10:46:24 +00:00
|
|
|
use Rector\Php72\NodeFactory\AnonymousFunctionFactory;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-04-04 12:08:55 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://www.php.net/manual/en/language.types.callable.php#117260
|
2019-04-04 12:08:55 +00:00
|
|
|
* @see https://3v4l.org/MsMbQ
|
2019-04-06 07:23:53 +00:00
|
|
|
* @see https://3v4l.org/KM1Ji
|
2019-09-04 12:10:29 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector\CallableThisArrayToAnonymousFunctionRectorTest
|
2019-04-04 12:08:55 +00:00
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
final class CallableThisArrayToAnonymousFunctionRector extends \Rector\Core\Rector\AbstractRector
|
2019-04-04 12:08:55 +00:00
|
|
|
{
|
2021-01-16 21:45:18 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-06-10 10:46:24 +00:00
|
|
|
* @var \Rector\Php72\NodeFactory\AnonymousFunctionFactory
|
2021-01-16 21:45:18 +00:00
|
|
|
*/
|
|
|
|
private $anonymousFunctionFactory;
|
2021-06-29 00:31:58 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-06-29 00:31:58 +00:00
|
|
|
* @var \Rector\Core\Reflection\ReflectionResolver
|
|
|
|
*/
|
|
|
|
private $reflectionResolver;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-06-29 00:31:58 +00:00
|
|
|
* @var \Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher
|
|
|
|
*/
|
|
|
|
private $arrayCallableMethodMatcher;
|
|
|
|
public function __construct(\Rector\Php72\NodeFactory\AnonymousFunctionFactory $anonymousFunctionFactory, \Rector\Core\Reflection\ReflectionResolver $reflectionResolver, \Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher $arrayCallableMethodMatcher)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-01-16 21:45:18 +00:00
|
|
|
$this->anonymousFunctionFactory = $anonymousFunctionFactory;
|
2021-06-29 00:31:58 +00:00
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
|
|
|
$this->arrayCallableMethodMatcher = $arrayCallableMethodMatcher;
|
2021-01-16 21:45:18 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
2019-04-04 12:08:55 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Convert [$this, "method"] to proper anonymous function', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
2019-04-04 12:08:55 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$values = [1, 5, 3];
|
|
|
|
usort($values, [$this, 'compareSize']);
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function compareSize($first, $second)
|
|
|
|
{
|
|
|
|
return $first <=> $second;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-04-04 12:08:55 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$values = [1, 5, 3];
|
|
|
|
usort($values, function ($first, $second) {
|
|
|
|
return $this->compareSize($first, $second);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function compareSize($first, $second)
|
|
|
|
{
|
|
|
|
return $first <=> $second;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-04-04 12:08:55 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-04-04 12:08:55 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-04-04 12:08:55 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return [\PhpParser\Node\Expr\Array_::class];
|
2019-04-04 12:08:55 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 09:57:54 +00:00
|
|
|
* @param \PhpParser\Node $node
|
2019-04-04 12:08:55 +00:00
|
|
|
*/
|
2021-12-10 09:57:54 +00:00
|
|
|
public function refactor($node) : ?\PhpParser\Node
|
2019-04-04 12:08:55 +00:00
|
|
|
{
|
2021-06-29 00:31:58 +00:00
|
|
|
$arrayCallable = $this->arrayCallableMethodMatcher->match($node);
|
|
|
|
if (!$arrayCallable instanceof \Rector\NodeCollector\ValueObject\ArrayCallable) {
|
2019-04-04 12:08:55 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-06-29 00:31:58 +00:00
|
|
|
$scope = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
|
|
|
|
$phpMethodReflection = $this->reflectionResolver->resolveMethodReflection($arrayCallable->getClass(), $arrayCallable->getMethod(), $scope);
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$phpMethodReflection instanceof \PHPStan\Reflection\Php\PhpMethodReflection) {
|
2019-04-04 12:08:55 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-06-29 00:31:58 +00:00
|
|
|
return $this->anonymousFunctionFactory->createFromPhpMethodReflection($phpMethodReflection, $arrayCallable->getCallerExpr());
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2019-04-04 12:08:55 +00:00
|
|
|
}
|