-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
StaticCallToMethodCall.php
73 lines (58 loc) · 1.75 KB
/
StaticCallToMethodCall.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace Rector\Transform\ValueObject;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;
final class StaticCallToMethodCall
{
public function __construct(
private readonly string $staticClass,
private readonly string $staticMethod,
private readonly string $classType,
private readonly string $methodName
) {
RectorAssert::className($staticClass);
// special char to match all method names
if ($staticMethod !== '*') {
RectorAssert::methodName($staticMethod);
}
RectorAssert::className($classType);
if ($methodName !== '*') {
RectorAssert::methodName($methodName);
}
}
public function getClassObjectType(): ObjectType
{
return new ObjectType($this->classType);
}
public function getClassType(): string
{
return $this->classType;
}
public function getMethodName(): string
{
return $this->methodName;
}
public function isStaticCallMatch(StaticCall $staticCall): bool
{
if (! $staticCall->class instanceof Name) {
return false;
}
$staticCallClassName = $staticCall->class->toString();
if ($staticCallClassName !== $this->staticClass) {
return false;
}
if (! $staticCall->name instanceof Identifier) {
return false;
}
// all methods
if ($this->staticMethod === '*') {
return true;
}
$staticCallMethodName = $staticCall->name->toString();
return $staticCallMethodName === $this->staticMethod;
}
}