2021-05-04 16:59:15 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-05-04 16:59:15 +02:00
|
|
|
namespace Rector\CodingStyle\NodeFactory;
|
|
|
|
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Expr\ArrayItem;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
final class ArrayCallableToMethodCallFactory
|
|
|
|
{
|
2021-05-10 22:10:16 +00:00
|
|
|
public function create(Array_ $array) : ?MethodCall
|
2021-05-04 16:59:15 +02:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($array->items) !== 2) {
|
2021-05-04 16:59:15 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$firstItem = $array->items[0];
|
|
|
|
$secondItem = $array->items[1];
|
2021-05-10 22:10:16 +00:00
|
|
|
if (!$firstItem instanceof ArrayItem) {
|
2021-05-04 16:59:15 +02:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-10 22:10:16 +00:00
|
|
|
if (!$secondItem instanceof ArrayItem) {
|
2021-05-04 16:59:15 +02:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-10 22:10:16 +00:00
|
|
|
if (!$secondItem->value instanceof String_) {
|
2021-05-04 16:59:15 +02:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-10 22:10:16 +00:00
|
|
|
if (!$firstItem->value instanceof PropertyFetch && !$firstItem->value instanceof Variable) {
|
2021-05-04 16:59:15 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$string = $secondItem->value;
|
|
|
|
$methodName = $string->value;
|
2021-05-10 22:10:16 +00:00
|
|
|
return new MethodCall($firstItem->value, $methodName);
|
2021-05-04 16:59:15 +02:00
|
|
|
}
|
|
|
|
}
|