Conditions | 13 |
Paths | 10 |
Total Lines | 40 |
Code Lines | 21 |
Lines | 6 |
Ratio | 15 % |
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 declare(strict_types=1); |
||
123 | protected function buildDefaultValue(string $definition) |
||
124 | { |
||
125 | if (is_numeric($definition)) { |
||
126 | if (false !== strpos($definition, '.')) { |
||
127 | return (float)$definition; |
||
128 | } |
||
129 | |||
130 | return (int)$definition; |
||
131 | } |
||
132 | |||
133 | switch (strtolower($definition)) { |
||
134 | case 'null': |
||
|
|||
135 | return null; |
||
136 | case 'true': |
||
137 | return true; |
||
138 | case 'false': |
||
139 | return false; |
||
140 | } |
||
141 | |||
142 | // after this point all expected definition values MUST be at least 2 chars length |
||
143 | |||
144 | if (strlen($definition) < 2) { |
||
145 | // Less likely we will ever get here because it should fail at a parsing step, but just in case |
||
146 | throw new ParameterSpecBuilderException("Unknown default value format '{$definition}'"); |
||
147 | } |
||
148 | |||
149 | if ('[' == $definition[0] && ']' == $definition[-1]) { |
||
150 | return []; |
||
151 | } |
||
152 | |||
153 | View Code Duplication | if ('"' == $definition[0] && '"' == $definition[-1]) { |
|
154 | return trim($definition, '"'); |
||
155 | } |
||
156 | View Code Duplication | if ("'" == $definition[0] && "'" == $definition[-1]) { |
|
157 | return trim($definition, "'"); |
||
158 | } |
||
159 | |||
160 | // Less likely we will ever get here because it should fail at a parsing step, but just in case |
||
161 | throw new ParameterSpecBuilderException("Unknown default value format '{$definition}'"); |
||
162 | } |
||
163 | } |
||
164 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.