Skip to content

refac: replace AbstractNodeVisitor with NodeVisitorInterface #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions Twig/Visitor/DefaultApplyingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Twig\Node\Expression\Ternary\ConditionalTernary;
use Twig\Node\Node;
use Twig\Node\Nodes;
use Twig\NodeVisitor\AbstractNodeVisitor;
use Twig\NodeVisitor\NodeVisitorInterface;
use Twig\TwigFilter;

/**
Expand All @@ -32,19 +32,16 @@
*
* @author Johannes M. Schmitt <[email protected]>
*/
final class DefaultApplyingNodeVisitor extends AbstractNodeVisitor
final class DefaultApplyingNodeVisitor implements NodeVisitorInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

{
/**
* @var bool
*/
private $enabled = true;
private bool $enabled = true;

public function setEnabled(bool $bool): void
{
$this->enabled = $bool;
}

public function doEnterNode(Node $node, Environment $env): Node
public function enterNode(Node $node, Environment $env): Node
{
if (!$this->enabled) {
return $node;
Expand Down Expand Up @@ -141,7 +138,7 @@ public function doEnterNode(Node $node, Environment $env): Node
return $node;
}

public function doLeaveNode(Node $node, Environment $env): Node
public function leaveNode(Node $node, Environment $env): Node
{
return $node;
}
Expand Down
11 changes: 4 additions & 7 deletions Twig/Visitor/NormalizingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Twig\Node\Expression\Binary\ConcatBinary;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Node;
use Twig\NodeVisitor\AbstractNodeVisitor;
use Twig\NodeVisitor\NodeVisitorInterface;

/**
* Performs equivalence transformations on the AST to ensure that
Expand All @@ -25,17 +25,14 @@
*
* @author Johannes M. Schmitt <[email protected]>
*/
final class NormalizingNodeVisitor extends AbstractNodeVisitor
final class NormalizingNodeVisitor implements NodeVisitorInterface
{
protected function doEnterNode(Node $node, Environment $env): Node
public function enterNode(Node $node, Environment $env): Node
{
return $node;
}

/**
* @return ConstantExpression|Node
*/
protected function doLeaveNode(Node $node, Environment $env): Node
public function leaveNode(Node $node, Environment $env): ConstantExpression|Node
{
if ($node instanceof ConcatBinary
&& ($left = $node->getNode('left')) instanceof ConstantExpression
Expand Down
13 changes: 5 additions & 8 deletions Twig/Visitor/RemovingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,23 @@
use Twig\Environment;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Node;
use Twig\NodeVisitor\AbstractNodeVisitor;
use Twig\NodeVisitor\NodeVisitorInterface;

/**
* Removes translation metadata filters from the AST.
*
* @author Johannes M. Schmitt <[email protected]>
*/
final class RemovingNodeVisitor extends AbstractNodeVisitor
final class RemovingNodeVisitor implements NodeVisitorInterface
{
/**
* @var bool
*/
private $enabled = true;
private bool $enabled = true;

public function setEnabled(bool $bool): void
{
$this->enabled = $bool;
}

protected function doEnterNode(Node $node, Environment $env): Node
public function enterNode(Node $node, Environment $env): Node
{
if ($this->enabled && $node instanceof FilterExpression) {
$name = $node->getNode('filter')->getAttribute('value');
Expand All @@ -46,7 +43,7 @@ protected function doEnterNode(Node $node, Environment $env): Node
return $node;
}

protected function doLeaveNode(Node $node, Environment $env): Node
public function leaveNode(Node $node, Environment $env): Node
{
return $node;
}
Expand Down