Conditions | 16 |
Paths | 19 |
Total Lines | 97 |
Code Lines | 58 |
Lines | 43 |
Ratio | 44.33 % |
Tests | 40 |
CRAP Score | 16 |
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 |
||
180 | 68 | public function authorize( |
|
181 | $class, |
||
182 | string $method = null, |
||
183 | array $attr = [] |
||
184 | ) : bool { |
||
185 | |||
186 | 68 | $auth = $this->extractAuth($class, $method); |
|
187 | |||
188 | // Class / method has no rules - allow all |
||
189 | 67 | if (empty($auth)) { |
|
190 | 2 | return true; |
|
191 | } |
||
192 | |||
193 | // Store mode, remove from auth array |
||
194 | 65 | $mode = $auth['mode'] ?? $this->defaultMode; |
|
195 | 65 | $e = new AuthException($mode); |
|
196 | 65 | unset($auth['mode']); |
|
197 | |||
198 | // Handle bans, remove them from auth |
||
199 | 65 | $this->handleBans($auth, $attr); |
|
200 | |||
201 | switch ($mode) { |
||
202 | 63 | case self::MODE_AND: |
|
203 | // All values in all arrays must match |
||
204 | 17 | foreach ($auth as $set => $values) { |
|
205 | 17 | if (!isset($attr[$set])) { |
|
206 | 15 | $e->addReason(new Reason( |
|
207 | $set, |
||
208 | 15 | [], |
|
209 | $values |
||
210 | )); |
||
211 | } else { |
||
212 | 5 | $attr[$set] = (array)$attr[$set]; |
|
213 | 5 | sort($values); |
|
214 | 5 | sort($attr[$set]); |
|
215 | 5 | if ($values != $attr[$set]) { |
|
216 | 1 | $e->addReason(new Reason( |
|
217 | $set, |
||
218 | 17 | $attr[$set], |
|
219 | $values |
||
220 | )); |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | 17 | if ($e->hasReasons()) { |
|
226 | 15 | throw $e; |
|
227 | } |
||
228 | 2 | return true; |
|
229 | 46 | View Code Duplication | case self::MODE_NONE: |
230 | // There must be no overlap between any of the array values |
||
231 | |||
232 | 13 | foreach ($auth as $set => $values) { |
|
233 | 13 | if (isset($attr[$set]) && count( |
|
234 | array_intersect( |
||
235 | 13 | (array)$attr[$set], |
|
236 | $values |
||
237 | ) |
||
238 | ) |
||
239 | ) { |
||
240 | 4 | $e->addReason(new Reason( |
|
241 | $set, |
||
242 | 13 | (array)($attr[$set] ?? []), |
|
243 | $values |
||
244 | )); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | 13 | if ($e->hasReasons()) { |
|
249 | 4 | throw $e; |
|
250 | } |
||
251 | 9 | return true; |
|
252 | 33 | View Code Duplication | case self::MODE_OR: |
253 | // At least one match must be present |
||
254 | 32 | foreach ($auth as $set => $values) { |
|
255 | 32 | if (isset($attr[$set]) && count( |
|
256 | array_intersect( |
||
257 | 32 | (array)$attr[$set], |
|
258 | $values |
||
259 | ) |
||
260 | ) |
||
261 | ) { |
||
262 | 19 | return true; |
|
263 | } |
||
264 | 22 | $e->addReason(new Reason( |
|
265 | $set, |
||
266 | 22 | (array)($attr[$set] ?? []), |
|
267 | $values |
||
268 | )); |
||
269 | } |
||
270 | |||
271 | 13 | throw $e; |
|
272 | default: |
||
273 | 1 | throw new \InvalidArgumentException('Durrrr'); |
|
274 | } |
||
275 | |||
276 | } |
||
277 | |||
301 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.