Conditions | 9 |
Paths | 32 |
Total Lines | 51 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
83 | private function parseParameters(array $parameterConfiguration = []): array |
||
84 | { |
||
85 | // parse expressions |
||
86 | $expressions = []; |
||
87 | while (!$this->parser->getStream()->test(\Twig_Token::BLOCK_END_TYPE)) { |
||
88 | $expressions[] = $this->parser->getExpressionParser()->parseExpression(); |
||
89 | } |
||
90 | |||
91 | // end of expressions |
||
92 | $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); |
||
93 | |||
94 | // map expressions to parameters |
||
95 | $parameters = []; |
||
96 | foreach ($parameterConfiguration as $parameterName => $parameterOptions) { |
||
97 | // try mapping expression |
||
98 | $expression = reset($expressions); |
||
99 | if ($expression !== false) { |
||
100 | switch ($parameterOptions['type']) { |
||
101 | case self::PARAMETER_TYPE_ARRAY: |
||
102 | // check if expression is valid array |
||
103 | $valid = $expression instanceof \Twig_Node_Expression_Array; |
||
104 | break; |
||
105 | case self::PARAMETER_TYPE_VALUE: |
||
106 | // check if expression is valid value |
||
107 | $valid = !($expression instanceof \Twig_Node_Expression_Array); |
||
108 | break; |
||
109 | default: |
||
110 | throw new \InvalidArgumentException('Invalid parameter type'); |
||
111 | break; |
||
|
|||
112 | } |
||
113 | |||
114 | if ($valid) { |
||
115 | // set expression as parameter and remove it from expressions list |
||
116 | $parameters[$parameterName] = array_shift($expressions); |
||
117 | continue; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | // set default as parameter otherwise or throw exception if default is false |
||
122 | if ($parameterOptions['default'] === false) { |
||
123 | throw new \Twig_Error_Syntax('A required parameter is missing'); |
||
124 | } |
||
125 | $parameters[$parameterName] = $parameterOptions['default']; |
||
126 | } |
||
127 | |||
128 | if (count($expressions) > 0) { |
||
129 | throw new \Twig_Error_Syntax('Too many parameters'); |
||
130 | } |
||
131 | |||
132 | return $parameters; |
||
133 | } |
||
134 | |||
153 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.