2019-04-04 12:08:55 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\Array_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2019-04-17 19:17:29 +00:00
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Expr\ClosureUse;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
2019-04-30 22:49:59 +00:00
|
|
|
use PhpParser\Node\Param;
|
2019-04-17 19:17:29 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2019-04-13 09:20:27 +00:00
|
|
|
use Rector\NodeContainer\ParsedNodesByType;
|
2019-04-04 12:08:55 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\RectorDefinition\CodeSample;
|
|
|
|
use Rector\RectorDefinition\RectorDefinition;
|
|
|
|
|
|
|
|
/**
|
2019-04-06 17:09:29 +00:00
|
|
|
* @see 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-04-04 12:08:55 +00:00
|
|
|
*/
|
|
|
|
final class CallableThisArrayToAnonymousFunctionRector extends AbstractRector
|
|
|
|
{
|
2019-04-06 07:23:53 +00:00
|
|
|
/**
|
2019-04-13 09:20:27 +00:00
|
|
|
* @var ParsedNodesByType
|
2019-04-06 07:23:53 +00:00
|
|
|
*/
|
2019-04-13 09:20:27 +00:00
|
|
|
private $parsedNodesByType;
|
2019-04-06 07:23:53 +00:00
|
|
|
|
2019-04-13 09:20:27 +00:00
|
|
|
public function __construct(ParsedNodesByType $parsedNodesByType)
|
2019-04-06 07:23:53 +00:00
|
|
|
{
|
2019-04-13 09:20:27 +00:00
|
|
|
$this->parsedNodesByType = $parsedNodesByType;
|
2019-04-06 07:23:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 12:08:55 +00:00
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Convert [$this, "method"] to proper anonymous function', [
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$values = [1, 5, 3];
|
|
|
|
usort($values, [$this, 'compareSize']);
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function compareSize($first, $second)
|
|
|
|
{
|
|
|
|
return $first <=> $second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2019-04-17 19:17:29 +00:00
|
|
|
return [Array_::class];
|
2019-04-04 12:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-30 22:49:59 +00:00
|
|
|
* @param Array_ $node
|
2019-04-04 12:08:55 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (count($node->items) !== 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-05-07 13:37:50 +00:00
|
|
|
// is callable?
|
|
|
|
// can be totally empty, e.g [, $value]
|
|
|
|
if ($node->items[0] === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-06 07:23:53 +00:00
|
|
|
$objectVariable = $node->items[0]->value;
|
2019-04-04 12:08:55 +00:00
|
|
|
|
2019-04-06 07:23:53 +00:00
|
|
|
$classMethod = $this->matchCallableMethod($objectVariable, $node->items[1]->value);
|
2019-04-04 12:08:55 +00:00
|
|
|
if ($classMethod === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-17 19:17:29 +00:00
|
|
|
$anonymousFunction = new Closure();
|
2019-04-04 12:08:55 +00:00
|
|
|
$anonymousFunction->params = $classMethod->params;
|
|
|
|
|
2019-04-17 19:17:29 +00:00
|
|
|
$innerMethodCall = new MethodCall($objectVariable, $classMethod->name);
|
2019-04-04 12:08:55 +00:00
|
|
|
$innerMethodCall->args = $this->convertParamsToArgs($classMethod->params);
|
|
|
|
|
2019-04-25 12:14:14 +00:00
|
|
|
if ($classMethod->returnType !== null) {
|
2019-04-04 12:08:55 +00:00
|
|
|
$anonymousFunction->returnType = $classMethod->returnType;
|
|
|
|
}
|
|
|
|
|
2019-04-17 19:17:29 +00:00
|
|
|
$anonymousFunction->stmts[] = new Return_($innerMethodCall);
|
2019-04-04 12:08:55 +00:00
|
|
|
|
2019-04-17 19:17:29 +00:00
|
|
|
if ($objectVariable instanceof Variable) {
|
2019-04-06 07:23:53 +00:00
|
|
|
if (! $this->isName($objectVariable, 'this')) {
|
2019-04-17 19:17:29 +00:00
|
|
|
$anonymousFunction->uses[] = new ClosureUse($objectVariable);
|
2019-04-06 07:23:53 +00:00
|
|
|
}
|
2019-04-04 12:08:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 07:23:53 +00:00
|
|
|
return $anonymousFunction;
|
2019-04-04 12:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-30 22:49:59 +00:00
|
|
|
* @param Param[] $params
|
|
|
|
* @return Arg[]
|
2019-04-04 12:08:55 +00:00
|
|
|
*/
|
|
|
|
private function convertParamsToArgs(array $params): array
|
|
|
|
{
|
|
|
|
$args = [];
|
|
|
|
foreach ($params as $key => $param) {
|
2019-04-17 19:17:29 +00:00
|
|
|
$args[$key] = new Arg($param->var);
|
2019-04-04 12:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $args;
|
|
|
|
}
|
2019-04-06 07:23:53 +00:00
|
|
|
|
2019-04-17 19:17:29 +00:00
|
|
|
private function matchCallableMethod(Expr $objectExpr, Expr $methodExpr): ?ClassMethod
|
2019-04-06 07:23:53 +00:00
|
|
|
{
|
|
|
|
$methodName = $this->getValue($methodExpr);
|
|
|
|
|
|
|
|
foreach ($this->getTypes($objectExpr) as $type) {
|
2019-04-13 09:20:27 +00:00
|
|
|
$class = $this->parsedNodesByType->findClass($type);
|
2019-04-06 07:23:53 +00:00
|
|
|
if ($class === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$classMethod = $class->getMethod($methodName);
|
|
|
|
if ($classMethod === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isName($objectExpr, 'this')) {
|
|
|
|
return $classMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($classMethod->isPublic()) {
|
|
|
|
return $classMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2019-04-04 12:08:55 +00:00
|
|
|
}
|