Completed
Push — master ( cb2703...d9d040 )
by Tobias
12:13
created

DefaultApplyingNodeVisitor::doEnterNode()   C

Complexity

Conditions 11
Paths 7

Size

Total Lines 70
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 13.5164

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 29
cts 40
cp 0.725
rs 5.7333
c 0
b 0
f 0
cc 11
eloc 40
nc 7
nop 2
crap 13.5164

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Twig\Visitor;
13
14
use Translation\Bundle\Twig\Node\Transchoice;
15
16
/**
17
 * Applies the value of the "desc" filter if the "trans" filter has no
18
 * translations.
19
 *
20
 * This is only active in your development environment.
21
 *
22
 * @author Johannes M. Schmitt <[email protected]>
23
 */
24
final class DefaultApplyingNodeVisitor extends \Twig_BaseNodeVisitor
25
{
26
    /**
27
     * @var bool
28
     */
29
    private $enabled = true;
30
31
    /**
32
     * @param $bool
33
     */
34
    public function setEnabled($bool)
35
    {
36
        $this->enabled = (bool) $bool;
37
    }
38
39
    /**
40
     * @param \Twig_Node        $node
41
     * @param \Twig_Environment $env
42
     *
43
     * @return \Twig_Node
44
     */
45 6
    public function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
46
    {
47 6
        if (!$this->enabled) {
48
            return $node;
49
        }
50
51 6
        if ($node instanceof \Twig_Node_Expression_Filter && 'desc' === $node->getNode('filter')->getAttribute('value')) {
52 1
            $transNode = $node->getNode('node');
53 1
            while ($transNode instanceof \Twig_Node_Expression_Filter
54 1
                       && 'trans' !== $transNode->getNode('filter')->getAttribute('value')
55 1
                       && 'transchoice' !== $transNode->getNode('filter')->getAttribute('value')) {
56
                $transNode = $transNode->getNode('node');
57
            }
58
59 1
            if (!$transNode instanceof \Twig_Node_Expression_Filter) {
60
                throw new \RuntimeException(sprintf('The "desc" filter must be applied after a "trans", or "transchoice" filter.'));
61
            }
62
63 1
            $wrappingNode = $node->getNode('node');
64 1
            $testNode = clone $wrappingNode;
65 1
            $defaultNode = $node->getNode('arguments')->getNode(0);
66
67
            // if the |transchoice filter is used, delegate the call to the TranslationExtension
68
            // so that we can catch a possible exception when the default translation has not yet
69
            // been extracted
70 1
            if ('transchoice' === $transNode->getNode('filter')->getAttribute('value')) {
71
                $transchoiceArguments = new \Twig_Node_Expression_Array([], $transNode->getTemplateLine());
72
                $transchoiceArguments->addElement($wrappingNode->getNode('node'));
0 ignored issues
show
Compatibility introduced by
$wrappingNode->getNode('node') of type object<Twig_Node> is not a sub-type of object<Twig_Node_Expression>. It seems like you assume a child class of the class Twig_Node to be always present.

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.

Loading history...
73
                $transchoiceArguments->addElement($defaultNode);
0 ignored issues
show
Compatibility introduced by
$defaultNode of type object<Twig_Node> is not a sub-type of object<Twig_Node_Expression>. It seems like you assume a child class of the class Twig_Node to be always present.

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.

Loading history...
74
                foreach ($wrappingNode->getNode('arguments') as $arg) {
75
                    $transchoiceArguments->addElement($arg);
76
                }
77
78
                $transchoiceNode = new Transchoice($transchoiceArguments, $transNode->getTemplateLine());
79
                $node->setNode('node', $transchoiceNode);
80
81
                return $node;
82
            }
83
84
            // if the |trans filter has replacements parameters
85
            // (e.g. |trans({'%foo%': 'bar'}))
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
86 1
            if ($wrappingNode->getNode('arguments')->hasNode(0)) {
87 1
                $lineno = $wrappingNode->getTemplateLine();
88
89
                // remove the replacements from the test node
90 1
                $testNode->setNode('arguments', clone $testNode->getNode('arguments'));
91 1
                $testNode->getNode('arguments')->setNode(0, new \Twig_Node_Expression_Array([], $lineno));
92
93
                // wrap the default node in a |replace filter
94 1
                $defaultNode = new \Twig_Node_Expression_Filter(
95 1
                    clone $node->getNode('arguments')->getNode(0),
96 1
                    new \Twig_Node_Expression_Constant('replace', $lineno),
97 1
                    new \Twig_Node([
98 1
                        clone $wrappingNode->getNode('arguments')->getNode(0),
99
                    ]),
100 1
                    $lineno
101
                );
102
            }
103
104 1
            $condition = new \Twig_Node_Expression_Conditional(
105 1
                new \Twig_Node_Expression_Binary_Equal($testNode, $transNode->getNode('node'), $wrappingNode->getTemplateLine()),
106 1
                $defaultNode,
0 ignored issues
show
Compatibility introduced by
$defaultNode of type object<Twig_Node> is not a sub-type of object<Twig_Node_Expression>. It seems like you assume a child class of the class Twig_Node to be always present.

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.

Loading history...
107 1
                clone $wrappingNode,
0 ignored issues
show
Compatibility introduced by
clone $wrappingNode of type object<Twig_Node> is not a sub-type of object<Twig_Node_Expression>. It seems like you assume a child class of the class Twig_Node to be always present.

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.

Loading history...
108 1
                $wrappingNode->getTemplateLine()
109
            );
110 1
            $node->setNode('node', $condition);
111
        }
112
113 6
        return $node;
114
    }
115
116
    /**
117
     * @param \Twig_Node        $node
118
     * @param \Twig_Environment $env
119
     *
120
     * @return \Twig_Node
121
     */
122 6
    public function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
123
    {
124 6
        return $node;
125
    }
126
127
    /**
128
     * @return int
129
     */
130 6
    public function getPriority()
131
    {
132 6
        return -2;
133
    }
134
}
135