2020-07-24 13:14:32 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-08-31 02:26:25 +02:00
|
|
|
namespace Rector\Transform\ValueObject;
|
2020-07-24 13:14:32 +02:00
|
|
|
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use PhpParser\Node\Name;
|
2021-02-28 08:47:48 +01:00
|
|
|
use PHPStan\Type\ObjectType;
|
2020-07-24 13:14:32 +02:00
|
|
|
final class StaticCallToMethodCall
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $staticClass;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $staticMethod;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $classType;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $methodName;
|
|
|
|
public function __construct(string $staticClass, string $staticMethod, string $classType, string $methodName)
|
|
|
|
{
|
|
|
|
$this->staticClass = $staticClass;
|
|
|
|
$this->staticMethod = $staticMethod;
|
|
|
|
$this->classType = $classType;
|
|
|
|
$this->methodName = $methodName;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getClassObjectType() : \PHPStan\Type\ObjectType
|
2021-02-28 08:47:48 +01:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PHPStan\Type\ObjectType($this->classType);
|
2021-02-28 08:47:48 +01:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getClassType() : string
|
2020-07-24 13:14:32 +02:00
|
|
|
{
|
|
|
|
return $this->classType;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getMethodName() : string
|
2020-07-24 13:14:32 +02:00
|
|
|
{
|
|
|
|
return $this->methodName;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function isStaticCallMatch(\PhpParser\Node\Expr\StaticCall $staticCall) : bool
|
2020-07-24 13:14:32 +02:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$staticCall->class instanceof \PhpParser\Node\Name) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-07-24 13:14:32 +02:00
|
|
|
}
|
|
|
|
$staticCallClassName = $staticCall->class->toString();
|
|
|
|
if ($staticCallClassName !== $this->staticClass) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-07-24 13:14:32 +02:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$staticCall->name instanceof \PhpParser\Node\Identifier) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-07-24 13:14:32 +02:00
|
|
|
}
|
2020-08-31 10:48:14 +02:00
|
|
|
// all methods
|
|
|
|
if ($this->staticMethod === '*') {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-08-31 10:48:14 +02:00
|
|
|
}
|
2020-07-24 13:14:32 +02:00
|
|
|
$staticCallMethodName = $staticCall->name->toString();
|
|
|
|
return $staticCallMethodName === $this->staticMethod;
|
|
|
|
}
|
|
|
|
}
|