mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-15 21:38:24 +01:00
62cca3bfeb
[CodingStyle] Fix ArrayCallableToMethodCallFactory for non-object types (#548)
52 lines
1.7 KiB
PHP
52 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
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_;
|
|
use PHPStan\Type\TypeWithClassName;
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
|
final class ArrayCallableToMethodCallFactory
|
|
{
|
|
/**
|
|
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
|
*/
|
|
private $nodeTypeResolver;
|
|
public function __construct(\Rector\NodeTypeResolver\NodeTypeResolver $nodeTypeResolver)
|
|
{
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
|
}
|
|
public function create(\PhpParser\Node\Expr\Array_ $array) : ?\PhpParser\Node\Expr\MethodCall
|
|
{
|
|
if (\count($array->items) !== 2) {
|
|
return null;
|
|
}
|
|
$firstItem = $array->items[0];
|
|
$secondItem = $array->items[1];
|
|
if (!$firstItem instanceof \PhpParser\Node\Expr\ArrayItem) {
|
|
return null;
|
|
}
|
|
if (!$secondItem instanceof \PhpParser\Node\Expr\ArrayItem) {
|
|
return null;
|
|
}
|
|
if (!$secondItem->value instanceof \PhpParser\Node\Scalar\String_) {
|
|
return null;
|
|
}
|
|
if (!$firstItem->value instanceof \PhpParser\Node\Expr\PropertyFetch && !$firstItem->value instanceof \PhpParser\Node\Expr\Variable) {
|
|
return null;
|
|
}
|
|
$firstItemType = $this->nodeTypeResolver->resolve($firstItem->value);
|
|
if (!$firstItemType instanceof \PHPStan\Type\TypeWithClassName) {
|
|
return null;
|
|
}
|
|
$string = $secondItem->value;
|
|
$methodName = $string->value;
|
|
return new \PhpParser\Node\Expr\MethodCall($firstItem->value, $methodName);
|
|
}
|
|
}
|