Conditions | 10 |
Paths | 10 |
Total Lines | 25 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
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 |
||
86 | private function getValue($comparator, $value) |
||
87 | { |
||
88 | switch ($comparator) { |
||
89 | case self::TYPE_EQUAL: |
||
90 | return $value; |
||
91 | case self::TYPE_EMPTY: |
||
92 | return; |
||
93 | case self::TYPE_NOT_EMPTY: |
||
94 | return; |
||
95 | case self::TYPE_CONTAINS: |
||
96 | return '%' . $value . '%'; |
||
97 | case self::TYPE_NOT_CONTAINS: |
||
98 | return '%' . $value . '%'; |
||
99 | case self::TYPE_STARTS_WITH: |
||
100 | return $value . '%'; |
||
101 | case self::TYPE_ENDS_WITH: |
||
102 | return '%' . $value; |
||
103 | case self::TYPE_IN: |
||
104 | return array_map('trim', explode(',', $value)); |
||
105 | case self::TYPE_NOT_IN: |
||
106 | return array_map('trim', explode(',', $value)); |
||
107 | } |
||
108 | |||
109 | throw new \InvalidArgumentException(sprintf('Could not determine value for comparator "%s"', $comparator)); |
||
110 | } |
||
111 | } |
||
112 |
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: