Conditions | 10 |
Paths | 11 |
Total Lines | 43 |
Code Lines | 24 |
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 |
||
115 | public static function parseAnB($rule): array |
||
116 | { |
||
117 | if ($rule === 'even') { |
||
118 | return [2, 0]; |
||
119 | } |
||
120 | |||
121 | if ($rule === 'odd') { |
||
122 | return [2, 1]; |
||
123 | } |
||
124 | |||
125 | if ($rule === 'n') { |
||
126 | return [1, 0]; |
||
127 | } |
||
128 | |||
129 | if (is_numeric($rule)) { |
||
130 | return [0, (int)$rule]; |
||
131 | } |
||
132 | |||
133 | $regex = '/^\s*([+\-]?[0-9]*)n\s*([+\-]?)\s*([0-9]*)\s*$/'; |
||
134 | $matches = []; |
||
135 | $res = preg_match($regex, $rule, $matches); |
||
136 | |||
137 | // If it doesn't parse, return 0, 0. |
||
138 | if (!$res) { |
||
139 | return [0, 0]; |
||
140 | } |
||
141 | |||
142 | $aVal = $matches[1] ?? 1; |
||
143 | if ($aVal === '-') { |
||
144 | $aVal = -1; |
||
145 | } else { |
||
146 | $aVal = (int)$aVal; |
||
147 | } |
||
148 | |||
149 | $bVal = 0; |
||
150 | if (isset($matches[3])) { |
||
151 | $bVal = (int)$matches[3]; |
||
152 | if (isset($matches[2]) && $matches[2] === '-') { |
||
153 | $bVal *= -1; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | return [$aVal, $bVal]; |
||
158 | } |
||
161 |