Repository navigation
Expand file tree
/
Copy pathConditionResolver.php
More file actions
145 lines (115 loc) · 4.62 KB
/
Copy pathConditionResolver.php
File metadata and controls
145 lines (115 loc) · 4.62 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace Rector\DeadCode;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\FuncCall;
use Rector\DeadCode\Contract\ConditionInterface;
use Rector\DeadCode\ValueObject\BinaryToVersionCompareCondition;
use Rector\DeadCode\ValueObject\VersionCompareCondition;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Php\PhpVersionProvider;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Util\PhpVersionFactory;
final readonly class ConditionResolver
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private PhpVersionProvider $phpVersionProvider,
private ValueResolver $valueResolver,
) {
}
public function resolveFromExpr(Expr $expr): ?ConditionInterface
{
if ($this->isVersionCompareFuncCall($expr)) {
/** @var FuncCall $expr */
return $this->resolveVersionCompareConditionForFuncCall($expr);
}
if (! $expr instanceof Identical && ! $expr instanceof Equal && ! $expr instanceof NotIdentical && ! $expr instanceof NotEqual) {
return null;
}
$binaryClass = $expr::class;
if ($this->isVersionCompareFuncCall($expr->left)) {
/** @var FuncCall $funcCall */
$funcCall = $expr->left;
return $this->resolveFuncCall($funcCall, $expr->right, $binaryClass);
}
if ($this->isVersionCompareFuncCall($expr->right)) {
/** @var FuncCall $funcCall */
$funcCall = $expr->right;
$versionCompareCondition = $this->resolveVersionCompareConditionForFuncCall($funcCall);
if (! $versionCompareCondition instanceof VersionCompareCondition) {
return null;
}
$expectedValue = $this->valueResolver->getValue($expr->left);
return new BinaryToVersionCompareCondition($versionCompareCondition, $binaryClass, $expectedValue);
}
return null;
}
private function isVersionCompareFuncCall(Expr $expr): bool
{
if (! $expr instanceof FuncCall) {
return false;
}
return $this->nodeNameResolver->isName($expr, 'version_compare');
}
private function resolveVersionCompareConditionForFuncCall(FuncCall $funcCall): ?VersionCompareCondition
{
$firstVersion = $this->resolveArgumentValue($funcCall, 0);
if ($firstVersion === null) {
return null;
}
$secondVersion = $this->resolveArgumentValue($funcCall, 1);
if ($secondVersion === null) {
return null;
}
// includes compare sign as 3rd argument
$versionCompareSign = null;
if (isset($funcCall->args[2]) && $funcCall->args[2] instanceof Arg) {
$versionCompareSign = $this->valueResolver->getValue($funcCall->args[2]->value);
}
return new VersionCompareCondition($firstVersion, $secondVersion, $versionCompareSign);
}
private function resolveFuncCall(
FuncCall $funcCall,
Expr $expr,
string $binaryClass
): ?BinaryToVersionCompareCondition {
$versionCompareCondition = $this->resolveVersionCompareConditionForFuncCall($funcCall);
if (! $versionCompareCondition instanceof VersionCompareCondition) {
return null;
}
$expectedValue = $this->valueResolver->getValue($expr);
return new BinaryToVersionCompareCondition($versionCompareCondition, $binaryClass, $expectedValue);
}
private function resolveArgumentValue(FuncCall $funcCall, int $argumentPosition): ?int
{
if (! isset($funcCall->args[$argumentPosition])) {
return null;
}
if (! $funcCall->args[$argumentPosition] instanceof Arg) {
return null;
}
$firstArgValue = $funcCall->args[$argumentPosition]->value;
/** @var mixed|null $version */
$version = $this->valueResolver->getValue($firstArgValue);
if (in_array($version, ['PHP_VERSION', 'PHP_VERSION_ID'], true)) {
return $this->phpVersionProvider->provide();
}
// version_compare() juggles the version to string first, e.g. 7.3 to "7.3"
if (is_float($version)) {
$version = (string) $version;
}
if (is_string($version)) {
return PhpVersionFactory::createIntVersion($version);
}
if (! is_int($version)) {
return null;
}
return $version;
}
}