mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-11 11:11:12 +01:00
54 lines
1.9 KiB
PHP
54 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Defluent\NodeAnalyzer;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
use PHPStan\Reflection\MethodReflection;
|
|
use Rector\Core\Reflection\ReflectionResolver;
|
|
use Rector\Defluent\Contract\ValueObject\FirstCallFactoryAwareInterface;
|
|
final class SameClassMethodCallAnalyzer
|
|
{
|
|
/**
|
|
* @var \Rector\Core\Reflection\ReflectionResolver
|
|
*/
|
|
private $reflectionResolver;
|
|
public function __construct(\Rector\Core\Reflection\ReflectionResolver $reflectionResolver)
|
|
{
|
|
$this->reflectionResolver = $reflectionResolver;
|
|
}
|
|
/**
|
|
* @param MethodCall[] $chainMethodCalls
|
|
*/
|
|
public function haveSingleClass(array $chainMethodCalls) : bool
|
|
{
|
|
// are method calls located in the same class?
|
|
$classOfClassMethod = [];
|
|
foreach ($chainMethodCalls as $chainMethodCall) {
|
|
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromMethodCall($chainMethodCall);
|
|
if ($methodReflection instanceof \PHPStan\Reflection\MethodReflection) {
|
|
$declaringClass = $methodReflection->getDeclaringClass();
|
|
$classOfClassMethod[] = $declaringClass->getName();
|
|
} else {
|
|
$classOfClassMethod[] = null;
|
|
}
|
|
}
|
|
$uniqueClasses = \array_unique($classOfClassMethod);
|
|
return \count($uniqueClasses) < 2;
|
|
}
|
|
/**
|
|
* @param string[] $calleeUniqueTypes
|
|
*/
|
|
public function isCorrectTypeCount(array $calleeUniqueTypes, \Rector\Defluent\Contract\ValueObject\FirstCallFactoryAwareInterface $firstCallFactoryAware) : bool
|
|
{
|
|
if ($calleeUniqueTypes === []) {
|
|
return \false;
|
|
}
|
|
// in case of factory method, 2 methods are allowed
|
|
if ($firstCallFactoryAware->isFirstCallFactory()) {
|
|
return \count($calleeUniqueTypes) === 2;
|
|
}
|
|
return \count($calleeUniqueTypes) === 1;
|
|
}
|
|
}
|