mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-11 03:01:12 +01:00
82b689d713
add trigger warning about deprecated resolve() and getStaticType() methods in NodeTypeResolver + update all packages (#976)
59 lines
1.9 KiB
PHP
59 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Defluent\NodeAnalyzer;
|
|
|
|
use PhpParser\Node\Arg;
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
use PhpParser\Node\Expr\New_;
|
|
use PHPStan\Type\MixedType;
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
|
final class NewFluentChainMethodCallNodeAnalyzer
|
|
{
|
|
/**
|
|
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
|
*/
|
|
private $nodeTypeResolver;
|
|
public function __construct(\Rector\NodeTypeResolver\NodeTypeResolver $nodeTypeResolver)
|
|
{
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
|
}
|
|
public function isNewMethodCallReturningSelf(\PhpParser\Node\Expr\MethodCall $methodCall) : bool
|
|
{
|
|
$newStaticType = $this->nodeTypeResolver->getType($methodCall->var);
|
|
$methodCallStaticType = $this->nodeTypeResolver->getType($methodCall);
|
|
return $methodCallStaticType->equals($newStaticType);
|
|
}
|
|
/**
|
|
* Method call with "new X", that returns "X"?
|
|
* e.g.
|
|
*
|
|
* $this->setItem(new Item) // → returns "Item"
|
|
*/
|
|
public function matchNewInFluentSetterMethodCall(\PhpParser\Node\Expr\MethodCall $methodCall) : ?\PhpParser\Node\Expr\New_
|
|
{
|
|
if (\count($methodCall->args) !== 1) {
|
|
return null;
|
|
}
|
|
if (!isset($methodCall->args[0])) {
|
|
return null;
|
|
}
|
|
if (!$methodCall->args[0] instanceof \PhpParser\Node\Arg) {
|
|
return null;
|
|
}
|
|
$onlyArgValue = $methodCall->args[0]->value;
|
|
if (!$onlyArgValue instanceof \PhpParser\Node\Expr\New_) {
|
|
return null;
|
|
}
|
|
$newType = $this->nodeTypeResolver->getType($onlyArgValue);
|
|
if ($newType instanceof \PHPStan\Type\MixedType) {
|
|
return null;
|
|
}
|
|
$parentMethodCallReturnType = $this->nodeTypeResolver->getType($methodCall);
|
|
if (!$newType->equals($parentMethodCallReturnType)) {
|
|
return null;
|
|
}
|
|
return $onlyArgValue;
|
|
}
|
|
}
|