Conditions | 15 |
Paths | 164 |
Total Lines | 31 |
Lines | 20 |
Ratio | 64.52 % |
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 |
||
41 | protected function initFilters($arrayData) |
||
42 | { |
||
43 | $keys = array(); |
||
44 | |||
45 | foreach ($arrayData as $key => $row) { |
||
46 | View Code Duplication | if ($value = $this->getParamAdapter()->getValueOfFilter('name')) { |
|
47 | if (strpos($row['name'], $value) === false && !isset($keys[$key])) { |
||
48 | $keys[] = $key; |
||
49 | } |
||
50 | } |
||
51 | View Code Duplication | if ($value = $this->getParamAdapter()->getValueOfFilter('surname')) { |
|
52 | if (strpos($row['surname'], $value) === false && !isset($keys[$key])) { |
||
53 | $keys[] = $key; |
||
54 | } |
||
55 | } |
||
56 | View Code Duplication | if ($value = $this->getParamAdapter()->getValueOfFilter('street')) { |
|
57 | if (strpos($row['street'], $value) === false && !isset($keys[$key])) { |
||
58 | $keys[] = $key; |
||
59 | } |
||
60 | } |
||
61 | View Code Duplication | if ($value = $this->getParamAdapter()->getValueOfFilter('city')) { |
|
62 | if (strpos($row['city'], $value) === false && !isset($keys[$key])) { |
||
63 | $keys[] = $key; |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | |||
68 | foreach ($keys as $key) { |
||
69 | unset($arrayData[$key]); |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: