Skip to content

Commit eadea25

Browse files
authored
[DowngradePhp80] Fix downgrade PhpToken::tokenize() and call with ->id (#216)
1 parent 9a6a85b commit eadea25

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector\Fixture;
4+
5+
$code = '<?php echo 1;';
6+
$tokens = \PhpToken::tokenize($code, TOKEN_PARSE);
7+
8+
while ($token = current($tokens)) {
9+
next($tokens);
10+
switch ($token->id) {
11+
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector\Fixture;
20+
21+
$code = '<?php echo 1;';
22+
$tokens = token_get_all($code, TOKEN_PARSE);
23+
24+
while ($token = current($tokens)) {
25+
next($tokens);
26+
switch (is_array($token) ? $token[0] : $token) {
27+
28+
}
29+
}
30+
31+
?>

rules/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ private function refactorMethodCall(MethodCall $methodCall): ?Ternary
111111

112112
private function refactorPropertyFetch(PropertyFetch $propertyFetch): ?Ternary
113113
{
114-
if (! $this->isName($propertyFetch->name, 'text')) {
114+
$propertyFetchName = $this->getName($propertyFetch->name);
115+
if (! in_array($propertyFetchName, ['id', 'text'], true)) {
115116
return null;
116117
}
117118

@@ -120,7 +121,10 @@ private function refactorPropertyFetch(PropertyFetch $propertyFetch): ?Ternary
120121
}
121122

122123
$isArrayFuncCall = new FuncCall(new Name('is_array'), [new Arg($propertyFetch->var)]);
123-
$arrayDimFetch = new ArrayDimFetch($propertyFetch->var, new LNumber(1));
124+
$arrayDimFetch = new ArrayDimFetch(
125+
$propertyFetch->var,
126+
$propertyFetchName === 'id' ? new LNumber(0) : new LNumber(1)
127+
);
124128

125129
return new Ternary($isArrayFuncCall, $arrayDimFetch, $propertyFetch->var);
126130
}

0 commit comments

Comments
 (0)