Repository navigation
Expand file tree
/
Copy pathBinaryOpBetweenNumberAndStringRector.php
More file actions
157 lines (135 loc) · 4 KB
/
Copy pathBinaryOpBetweenNumberAndStringRector.php
File metadata and controls
157 lines (135 loc) · 4 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
157
<?php
declare(strict_types=1);
namespace Rector\Php71\Rector\BinaryOp;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\Float_;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\MagicConst\Line;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\Constant\ConstantStringType;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\ValueObject\PhpVersion;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
/**
* @see \Rector\Tests\Php71\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector\BinaryOpBetweenNumberAndStringRectorTest
*/
final class BinaryOpBetweenNumberAndStringRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ExprAnalyzer $exprAnalyzer
) {
}
public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_71;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change binary operation between some number + string to PHP 7.1 compatible version',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$value = 5 + '';
$value = 5.0 + 'hi';
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$value = 5 + 0;
$value = 5.0 + 0.0;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [BinaryOp::class];
}
/**
* @param BinaryOp $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof Concat) {
return null;
}
if ($node instanceof Coalesce) {
return null;
}
// array element number type only comes from phpdoc array shape, never a runtime guarantee
if ($node->left instanceof ArrayDimFetch || $node->right instanceof ArrayDimFetch) {
return null;
}
if ($this->exprAnalyzer->isNonTypedFromParam($node->left)) {
return null;
}
if ($this->exprAnalyzer->isNonTypedFromParam($node->right)) {
return null;
}
if ($this->isStringOrStaticNonNumericString($node->left) && $this->nodeTypeResolver->isNumberType(
$node->right
)) {
$node->left = $this->nodeTypeResolver->getNativeType($node->right)
->isInteger()
->yes()
? new Int_(0)
: new Float_(0);
return $node;
}
if ($this->isStringOrStaticNonNumericString($node->right) && $this->nodeTypeResolver->isNumberType(
$node->left
)) {
$node->right = $this->nodeTypeResolver->getNativeType($node->left)
->isInteger()
->yes()
? new Int_(0)
: new Float_(0);
return $node;
}
return null;
}
private function isStringOrStaticNonNumericString(Expr $expr): bool
{
// replace only scalar values, not variables/constants/etc.
if (! $expr instanceof Scalar && ! $expr instanceof Variable) {
return false;
}
if ($expr instanceof Line) {
return false;
}
if ($expr instanceof String_) {
return ! is_numeric($expr->value);
}
$exprStaticType = $this->getType($expr);
if ($exprStaticType instanceof ConstantStringType) {
return ! is_numeric($exprStaticType->getValue());
}
return false;
}
}