Repository navigation
Expand file tree
/
Copy pathDowngradeDereferenceableOperationRector.php
More file actions
110 lines (92 loc) · 2.94 KB
/
Copy pathDowngradeDereferenceableOperationRector.php
File metadata and controls
110 lines (92 loc) · 2.94 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
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp80\Rector\ArrayDimFetch;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Scalar\InterpolatedString;
use PhpParser\Node\Scalar\MagicConst;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://wiki.php.net/rfc/variable_syntax_tweaks
*
* @see \Rector\Tests\DowngradePhp80\Rector\ArrayDimFetch\DowngradeDereferenceableOperationRector\DowngradeDereferenceableOperationRectorTest
*/
final class DowngradeDereferenceableOperationRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add parentheses around non-dereferenceable expressions.', [
new CodeSample(
<<<'CODE_SAMPLE'
function getFirstChar(string $str, string $suffix = '')
{
return "$str$suffix"[0];
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
function getFirstChar(string $str, string $suffix = '')
{
return ("$str$suffix")[0];
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ArrayDimFetch::class];
}
/**
* @param ArrayDimFetch $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}
$node->var->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
return $node;
}
private function shouldSkip(ArrayDimFetch $arrayDimFetch): bool
{
if (! $arrayDimFetch->dim instanceof Expr) {
return true;
}
if ($arrayDimFetch->var instanceof InterpolatedString) {
return $this->hasParentheses($arrayDimFetch);
}
if ($arrayDimFetch->var instanceof MagicConst) {
return $this->hasParentheses($arrayDimFetch);
}
return true;
}
private function hasParentheses(ArrayDimFetch $arrayDimFetch): bool
{
$wrappedInParentheses = $arrayDimFetch->var->getAttribute(AttributeKey::WRAPPED_IN_PARENTHESES);
if ($wrappedInParentheses === true) {
return true;
}
assert($arrayDimFetch->dim instanceof Expr); // already checked in shouldSkip()
$oldTokens = $this->getFile()->getOldTokens();
$varEndTokenPos = $arrayDimFetch->var->getEndTokenPos();
$dimStartTokenPos = $arrayDimFetch->dim->getStartTokenPos();
for ($i = $varEndTokenPos + 1; $i < $dimStartTokenPos; ++$i) {
if (! isset($oldTokens[$i])) {
continue;
}
if ((string) $oldTokens[$i] !== ')') {
continue;
}
return true;
}
return false;
}
}