1 | <?php |
||
32 | final class DefaultApplyingNodeVisitor extends AbstractNodeVisitor |
||
33 | { |
||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $enabled = true; |
||
38 | |||
39 | /** |
||
40 | * @param $bool |
||
41 | */ |
||
42 | public function setEnabled($bool) |
||
46 | |||
47 | /** |
||
48 | * @return Node |
||
49 | */ |
||
50 | 1 | public function doEnterNode(Node $node, Environment $env) |
|
51 | { |
||
52 | 1 | if (!$this->enabled) { |
|
53 | return $node; |
||
54 | } |
||
55 | |||
56 | 1 | if (!($node instanceof FilterExpression && 'desc' === $node->getNode('filter')->getAttribute('value'))) { |
|
57 | 1 | return $node; |
|
58 | } |
||
59 | |||
60 | 1 | $transNode = $node->getNode('node'); |
|
61 | 1 | while ($transNode instanceof FilterExpression |
|
62 | 1 | && 'trans' !== $transNode->getNode('filter')->getAttribute('value') |
|
63 | 1 | && 'transchoice' !== $transNode->getNode('filter')->getAttribute('value')) { |
|
64 | $transNode = $transNode->getNode('node'); |
||
65 | } |
||
66 | |||
67 | 1 | if (!$transNode instanceof FilterExpression) { |
|
68 | throw new \RuntimeException(\sprintf('The "desc" filter must be applied after a "trans", or "transchoice" filter.')); |
||
69 | } |
||
70 | |||
71 | 1 | $wrappingNode = $node->getNode('node'); |
|
72 | 1 | $testNode = clone $wrappingNode; |
|
73 | 1 | $defaultNode = $node->getNode('arguments')->getNode(0); |
|
74 | |||
75 | // if the |transchoice filter is used, delegate the call to the TranslationExtension |
||
76 | // so that we can catch a possible exception when the default translation has not yet |
||
77 | // been extracted |
||
78 | 1 | if ('transchoice' === $transNode->getNode('filter')->getAttribute('value')) { |
|
79 | $transchoiceArguments = new ArrayExpression([], $transNode->getTemplateLine()); |
||
80 | $transchoiceArguments->addElement($wrappingNode->getNode('node')); |
||
|
|||
81 | $transchoiceArguments->addElement($defaultNode); |
||
82 | foreach ($wrappingNode->getNode('arguments') as $arg) { |
||
83 | $transchoiceArguments->addElement($arg); |
||
84 | } |
||
85 | |||
86 | $transchoiceNode = new Transchoice($transchoiceArguments, $transNode->getTemplateLine()); |
||
87 | $node->setNode('node', $transchoiceNode); |
||
88 | |||
89 | return $node; |
||
90 | } |
||
91 | |||
92 | // if the |trans filter has replacements parameters |
||
93 | // (e.g. |trans({'%foo%': 'bar'})) |
||
94 | 1 | if ($wrappingNode->getNode('arguments')->hasNode(0)) { |
|
95 | 1 | $lineno = $wrappingNode->getTemplateLine(); |
|
96 | |||
97 | // remove the replacements from the test node |
||
98 | 1 | $testNode->setNode('arguments', clone $testNode->getNode('arguments')); |
|
99 | 1 | $testNode->getNode('arguments')->setNode(0, new ArrayExpression([], $lineno)); |
|
100 | |||
101 | // wrap the default node in a |replace filter |
||
102 | 1 | $defaultNode = new FilterExpression( |
|
103 | 1 | clone $node->getNode('arguments')->getNode(0), |
|
104 | 1 | new ConstantExpression('replace', $lineno), |
|
105 | 1 | new Node([ |
|
106 | 1 | clone $wrappingNode->getNode('arguments')->getNode(0), |
|
107 | ]), |
||
108 | $lineno |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | 1 | $condition = new ConditionalExpression( |
|
113 | 1 | new EqualBinary($testNode, $transNode->getNode('node'), $wrappingNode->getTemplateLine()), |
|
114 | $defaultNode, |
||
115 | 1 | clone $wrappingNode, |
|
116 | 1 | $wrappingNode->getTemplateLine() |
|
117 | ); |
||
118 | 1 | $node->setNode('node', $condition); |
|
119 | |||
120 | 1 | return $node; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return Node |
||
125 | */ |
||
126 | 1 | public function doLeaveNode(Node $node, Environment $env) |
|
130 | |||
131 | /** |
||
132 | * @return int |
||
133 | */ |
||
134 | 1 | public function getPriority() |
|
138 | } |
||
139 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.