Conditions | 10 |
Paths | 64 |
Total Lines | 51 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
31 | public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
||
32 | { |
||
33 | if (!$node instanceof \Twig_Node_Expression_Array) { |
||
34 | throw new \RuntimeException( |
||
35 | sprintf('$node must be an instanceof of \Expression_Array, but got "%s".', get_class($node)) |
||
36 | ); |
||
37 | } |
||
38 | |||
39 | |||
40 | $pairs = $this->getKeyValuePairs($node); |
||
41 | |||
42 | if ($isList = $this->isList($pairs)) { |
||
43 | $compiler->raw('['); |
||
44 | } elseif ($hasDynamicKeys = $this->hasDynamicKeys($pairs)) { |
||
45 | $compiler->raw('twig.createObj('); |
||
46 | } else { |
||
47 | $compiler->raw('{'); |
||
48 | } |
||
49 | |||
50 | $first = true; |
||
51 | foreach ($pairs as $pair) { |
||
52 | if (!$first) { |
||
53 | $compiler->raw(', '); |
||
54 | } |
||
55 | $first = false; |
||
56 | |||
57 | if ($isList) { |
||
58 | $compiler->subcompile($pair['value']); |
||
59 | } elseif ($hasDynamicKeys) { |
||
|
|||
60 | $compiler |
||
61 | ->subcompile($pair['key']) |
||
62 | ->raw(', ') |
||
63 | ->subcompile($pair['value']) |
||
64 | ; |
||
65 | } else { |
||
66 | $compiler |
||
67 | ->subcompile($pair['key']) |
||
68 | ->raw(': ') |
||
69 | ->subcompile($pair['value']) |
||
70 | ; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | if ($isList) { |
||
75 | $compiler->raw(']'); |
||
76 | } elseif ($hasDynamicKeys) { |
||
77 | $compiler->raw(')'); |
||
78 | } else { |
||
79 | $compiler->raw('}'); |
||
80 | } |
||
81 | } |
||
82 | |||
123 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: