Skip to content

Commit

Permalink
[CodeQuality] Handle crash on BinaryOp/Mul and variable variable: $$n…
Browse files Browse the repository at this point in the history
…ame on IntvalToTypeCastRector (#3608)

* [CodeQuality] Handle crash on BinaryOp/Mul on IntvalToTypeCastRector

* Fixed

* [ci-review] Rector Rectify

* Revert [ci-review] Rector Rectify

This reverts commit a18c051.

* Revert Fixed

This reverts commit 93f6219.

* Handle with re-printed node

* clean up

* Final touch: comment

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 12, 2023
1 parent 54a340c commit 3a9bebf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
Expand Down Expand Up @@ -113,7 +114,8 @@ public function processNodes(
$node instanceof Return_ ||
$node instanceof Assign ||
$node instanceof EnumCase ||
$node instanceof AssignOp
$node instanceof AssignOp ||
$node instanceof Cast
) && $node->expr instanceof Expr) {
$node->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
Expand Down Expand Up @@ -162,7 +164,7 @@ public function processNodes(
$node->name->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

if ($node instanceof Assign) {
if ($node instanceof Assign || $node instanceof AssignOp) {
// decorate value as well
$node->var->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector\Fixture;

class InMulBinaryOp
{
public function run()
{
$$name = GetTime(time()-86400*intval($FILTER[$bdays]));
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector\Fixture;

class InMulBinaryOp
{
public function run()
{
$$name = GetTime(time()-86400*(int) $FILTER[$bdays]);
}
}

?>
6 changes: 5 additions & 1 deletion src/NodeAnalyzer/ScopeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ public function resolveScope(
}

/**
* Node and parent Node doesn't has Scope, and parent Node Start token pos is < 0,
* Node and parent Node doesn't has Scope, and node and/or parent Node Start token pos is < 0,
* it means the node and parent node just re-printed, the Scope need to be resolved from file
*/
if ($parentNode->getStartTokenPos() < 0) {
return $this->scopeFactory->createFromFile($filePath);
}

if ($node->getStartTokenPos() < 0) {
return $this->scopeFactory->createFromFile($filePath);
}

return null;
}
}

0 comments on commit 3a9bebf

Please sign in to comment.