Skip to content

Commit

Permalink
[DowngradePhp80] Fix downgrade PhpToken::tokenize() and call with ->id (
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Mar 19, 2024
1 parent 9a6a85b commit eadea25
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector\Fixture;

$code = '<?php echo 1;';
$tokens = \PhpToken::tokenize($code, TOKEN_PARSE);

while ($token = current($tokens)) {
next($tokens);
switch ($token->id) {

}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector\Fixture;

$code = '<?php echo 1;';
$tokens = token_get_all($code, TOKEN_PARSE);

while ($token = current($tokens)) {
next($tokens);
switch (is_array($token) ? $token[0] : $token) {

}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ private function refactorMethodCall(MethodCall $methodCall): ?Ternary

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

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

$isArrayFuncCall = new FuncCall(new Name('is_array'), [new Arg($propertyFetch->var)]);
$arrayDimFetch = new ArrayDimFetch($propertyFetch->var, new LNumber(1));
$arrayDimFetch = new ArrayDimFetch(
$propertyFetch->var,
$propertyFetchName === 'id' ? new LNumber(0) : new LNumber(1)
);

return new Ternary($isArrayFuncCall, $arrayDimFetch, $propertyFetch->var);
}
Expand Down

0 comments on commit eadea25

Please sign in to comment.