Conditions | 10 |
Paths | 256 |
Total Lines | 34 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 1 |
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 |
||
145 | public function getParameters() |
||
146 | { |
||
147 | $parameters = [ |
||
148 | 'ACTIVITE' => $this->activity, |
||
149 | 'DATEQ' => $this->date instanceof \DateTime ? $this->date->format('dmYHis') : null, |
||
150 | ]; |
||
151 | |||
152 | if ($this->showCountry) { |
||
153 | $parameters['PAYS'] = ''; |
||
154 | } |
||
155 | if ($this->showSha1) { |
||
156 | $parameters['SHA-1'] = ''; |
||
157 | } |
||
158 | if ($this->showCardType) { |
||
159 | $parameters['TYPECARTE'] = ''; |
||
160 | } |
||
161 | |||
162 | if (method_exists($this, 'getTransactionNumber')) { |
||
163 | $parameters['NUMTRANS'] = $this->getTransactionNumber(); |
||
|
|||
164 | } |
||
165 | if (method_exists($this, 'getCallNumber')) { |
||
166 | $parameters['NUMAPPEL'] = $this->getCallNumber(); |
||
167 | } |
||
168 | if (method_exists($this, 'hasAuthorization') && $this->hasAuthorization()) { |
||
169 | $parameters['AUTORISATION'] = $this->getAuthorization(); |
||
170 | } |
||
171 | |||
172 | // Direct Plus requests special case. |
||
173 | if ($this->hasSubscriberRef()) { |
||
174 | $parameters['REFABONNE'] = $this->getSubscriberRef(); |
||
175 | } |
||
176 | |||
177 | return $parameters; |
||
178 | } |
||
179 | } |
||
180 |
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 sub-classes 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 parent class: