-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bug where blade ->get() functions would ruin traverse parser, and…
… make tests for it
- Loading branch information
Showing
5 changed files
with
491 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Bottelet\TranslationChecker\Node; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
class ChainedGetNodeRemover extends NodeVisitorAbstract | ||
{ | ||
public function enterNode(Node $node): Node | ||
{ | ||
if ($node instanceof Expression) { | ||
$this->processExpression($node->expr); | ||
} elseif ($node instanceof Node\Expr\FuncCall) { | ||
foreach ($node->getArgs() as $arg) { | ||
/** @var Node\Expr $value */ | ||
$value = $this->processNode($arg->value); | ||
$arg->value = $value; | ||
} | ||
} elseif ($node instanceof MethodCall) { | ||
return $this->processMethodCall($node); | ||
} elseif ($node instanceof Closure) { | ||
$this->processClosure($node); | ||
} | ||
return $node; | ||
} | ||
|
||
private function processExpression(Node $expr): void | ||
{ | ||
if ($expr instanceof Node\Expr\Assign) { | ||
/** @var Node\Expr $value */ | ||
$value = $this->processNode($expr->expr); | ||
$expr->expr = $value; | ||
} | ||
} | ||
|
||
private function processNode(Node $node): Node|Node\Expr | ||
{ | ||
if ($node instanceof MethodCall) { | ||
return $this->processMethodCall($node); | ||
} | ||
|
||
if ($node instanceof Closure) { | ||
$this->processClosure($node); | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
private function processMethodCall(MethodCall $node): Node | ||
{ | ||
if ($node->name instanceof Node\Identifier && $node->name->name === 'get') { | ||
return $node->var; | ||
} | ||
|
||
/** @var Node\Expr $value */ | ||
$value = $this->processNode($node->var); | ||
$node->var = $value; | ||
foreach ($node->getArgs() as $arg) { | ||
/** @var Node\Expr $value */ | ||
$value = $this->processNode($arg->value); | ||
$arg->value =$value; | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
private function processClosure(Closure $closure): void | ||
{ | ||
foreach ($closure->stmts as $stmt) { | ||
if ($stmt instanceof Node) { | ||
$this->processNode($stmt); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.