Issues (5)

src/TwigOverrideNodeVisitor.php (3 issues)

1
<?php
2
3
namespace TwigOverride;
4
5
/**
6
 * Twig parse node visitor to dynamically rewrite include / embed / extends.
7
 */
8
class TwigOverrideNodeVisitor extends \Twig_BaseNodeVisitor {
9
10
  /**
11
   * {@inheritdoc}
12
   */
13 8
  protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) {
14 8
    return $node;
15
  }
16
17
  /**
18
   * {@inheritdoc}
19
   */
20 15
  protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) {
21
22
    // Replace:
23
    // {% embed ... %} with {% embed twig_override(...) %}.
24
    // and:
25
    // {% extends ... %} with {% extends twig_override(...) %}.
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26 15
    if ($node instanceof \Twig_Node_Module && $node->hasNode('parent')) {
27 5
      $line = $node->getTemplateLine();
28 5
      $with = new \Twig_Node_Expression_Constant(NULL, $line);
29 5
      $template_name = $node->getNode('parent');
30 5
      $only = new \Twig_Node_Expression_Constant(FALSE, $line);
31 5
      $_context = new \Twig_Node_Expression_Name('_context', $line);
32 5
      $arguments = new \Twig_Node([$template_name, $only, $with, $_context]);
33 5
      $node->setNode('parent', new \Twig_Node_Expression_Function(TwigOverrideExtension::TEMPLATE_OVERRIDE_FUNCTION, $arguments, $line));
34
    }
35
36
    // Replace
37
    // {% include 1 with 2 %}
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
38
    // with {% include twig_override(1) with twig_override_parameters(2) %}.
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
    // and
40
    // {% embed 1 with 2 %} with {% embed 1 with twig_override_parameters(2) %}.
41 14
    elseif ($node instanceof \Twig_Node_Include) {
42 12
      $line = $node->getTemplateLine();
43 12
      $with = $node->hasNode('variables') ? $node->getNode('variables') : new \Twig_Node_Expression_Constant(NULL, $line);
44 12
      $only = new \Twig_Node_Expression_Constant($node->hasAttribute('only') ? $node->getAttribute('only') : FALSE, $line);
45 12
      $_context = new \Twig_Node_Expression_Name('_context', $line);
46
47
      // The order of these checks is important since Twig_Node_Embed is a
48
      // subclass of Twig_Node_Include.
49 12
      if ($node instanceof \Twig_Node_Embed) {
50 6
        $template_name = new \Twig_Node_Expression_Constant($node->getAttribute('name'), $line);
51 6
        $arguments = new \Twig_Node([$template_name, $only, $with, $_context]);
52
      }
53
      else {
54 6
        $template_name = $node->getNode('expr');
55 6
        $arguments = new \Twig_Node([$template_name, $only, $with, $_context]);
56 6
        $node->setNode('expr', new \Twig_Node_Expression_Function(TwigOverrideExtension::TEMPLATE_OVERRIDE_FUNCTION, $arguments, $line));
57
      }
58
59 12
      $node->setNode('variables', new \Twig_Node_Expression_Function(TwigOverrideExtension::PARAMETER_OVERRIDE_FUNCTION, $arguments, $line));
60
    }
61
62 15
    return $node;
63
  }
64
65
  /**
66
   * {@inheritdoc}
67
   */
68 8
  public function getPriority() {
69
    // Just above the Optimizer, which is the normal last one.
70 8
    return 256;
71
  }
72
73
}
74