Repository navigation
Expand file tree
/
Copy pathVersionCompareFuncCallToConstantRector.php
More file actions
156 lines (135 loc) · 4.05 KB
/
Copy pathVersionCompareFuncCallToConstantRector.php
File metadata and controls
156 lines (135 loc) · 4.05 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
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Rector\Util\PhpVersionFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector\VersionCompareFuncCallToConstantRectorTest
*/
final class VersionCompareFuncCallToConstantRector extends AbstractRector
{
/**
* @var array<string, class-string<BinaryOp>>
*/
private const array OPERATOR_TO_COMPARISON = [
'=' => Identical::class,
'==' => Identical::class,
'eq' => Identical::class,
'!=' => NotIdentical::class,
'<>' => NotIdentical::class,
'ne' => NotIdentical::class,
'>' => Greater::class,
'gt' => Greater::class,
'<' => Smaller::class,
'lt' => Smaller::class,
'>=' => GreaterOrEqual::class,
'ge' => GreaterOrEqual::class,
'<=' => SmallerOrEqual::class,
'le' => SmallerOrEqual::class,
];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes use of call to version compare function to use of PHP version constant',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
version_compare(PHP_VERSION, '5.3.0', '<');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
PHP_VERSION_ID < 50300;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'version_compare')) {
return null;
}
if ($node->isFirstClassCallable()) {
return null;
}
if (count($node->getArgs()) !== 3) {
return null;
}
$args = $node->getArgs();
if (! $this->isPhpVersionConstant($args[0]->value) && ! $this->isPhpVersionConstant($args[1]->value)) {
return null;
}
$left = $this->getNewNodeForArg($args[0]->value);
$right = $this->getNewNodeForArg($args[1]->value);
if (! $left instanceof Expr) {
return null;
}
if (! $right instanceof Expr) {
return null;
}
/** @var String_ $operator */
$operator = $args[2]->value;
$comparisonClass = self::OPERATOR_TO_COMPARISON[$operator->value];
return new $comparisonClass($left, $right);
}
private function isPhpVersionConstant(Expr $expr): bool
{
if (! $expr instanceof ConstFetch) {
return false;
}
return $expr->name->toString() === 'PHP_VERSION';
}
private function getNewNodeForArg(Expr $expr): ConstFetch | Int_ | null
{
if ($this->isPhpVersionConstant($expr)) {
return new ConstFetch(new Name('PHP_VERSION_ID'));
}
return $this->getVersionNumberFormVersionString($expr);
}
private function getVersionNumberFormVersionString(Expr $expr): ?Int_
{
if (! $expr instanceof String_) {
return null;
}
$value = PhpVersionFactory::createIntVersion($expr->value);
return new Int_($value);
}
}