rector/rules/Defluent/NodeAnalyzer/SameClassMethodCallAnalyzer.php
2021-06-27 23:35:53 +00:00

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;
}
}