-
-
Notifications
You must be signed in to change notification settings - Fork 687
/
VersionCompareFuncCallToConstantRector.php
118 lines (116 loc) · 3.85 KB
/
VersionCompareFuncCallToConstantRector.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
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
<?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\LNumber;
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 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';
}
/**
* @return \PhpParser\Node\Expr\ConstFetch|\PhpParser\Node\Scalar\LNumber|null
*/
private function getNewNodeForArg(Expr $expr)
{
if ($this->isPhpVersionConstant($expr)) {
return new ConstFetch(new Name('PHP_VERSION_ID'));
}
return $this->getVersionNumberFormVersionString($expr);
}
private function getVersionNumberFormVersionString(Expr $expr) : ?LNumber
{
if (!$expr instanceof String_) {
return null;
}
$value = PhpVersionFactory::createIntVersion($expr->value);
return new LNumber($value);
}
}