Conditions | 10 |
Paths | 16 |
Total Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Tests | 12 |
CRAP Score | 10 |
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 defined('SYSPATH') OR die('No direct script access.'); |
||
21 | 16 | public function validate(Jam_Validated $model, $attribute, $value) |
|
22 | { |
||
23 | 16 | // Since PHP 7.2 method count() parameter must be an array or an object that implements Countable |
|
24 | 16 | // https://www.php.net/manual/en/function.count.php#124263 |
|
25 | $count = count((array) $value); |
||
26 | 16 | $params = (array) $this; |
|
|
|||
27 | |||
28 | 2 | if ($this->minimum !== NULL AND ! ($count >= $this->minimum)) |
|
29 | { |
||
30 | $model->errors()->add($attribute, 'count_minimum', array(':minimum' => $this->minimum)); |
||
31 | 16 | } |
|
32 | |||
33 | 2 | if ($this->maximum !== NULL AND ! ($count <= $this->maximum)) |
|
34 | { |
||
35 | $model->errors()->add($attribute, 'count_maximum', array(':maximum' => $this->maximum)); |
||
36 | 16 | } |
|
37 | |||
38 | 4 | if ($this->within !== NULL AND ! ($count >= $this->within[0] AND $count <= $this->within[1])) |
|
39 | { |
||
40 | $model->errors()->add($attribute, 'count_within', array(':minimum' => $this->within[0], ':maximum' => $this->within[1])); |
||
41 | 16 | } |
|
42 | |||
43 | 3 | if ($this->is !== NULL AND ! ($count == $this->is)) |
|
44 | { |
||
45 | $model->errors()->add($attribute, 'count_is', array(':is' => $this->is)); |
||
46 | } |
||
47 | |||
48 | 16 | ||
49 | |||
50 | } |
||
51 | } |
||
52 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.