| Conditions | 11 |
| Paths | 512 |
| Total Lines | 36 |
| 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 |
||
| 152 | public function getParameters() |
||
| 153 | { |
||
| 154 | $parameters = [ |
||
| 155 | 'DATEQ' => $this->date instanceof \DateTime ? $this->date->format('dmYHis') : null, |
||
| 156 | ]; |
||
| 157 | |||
| 158 | if ($this->activity) { |
||
| 159 | $parameters['ACTIVITE'] = $this->activity; |
||
| 160 | } |
||
| 161 | if ($this->showCountry) { |
||
| 162 | $parameters['PAYS'] = ''; |
||
| 163 | } |
||
| 164 | if ($this->showSha1) { |
||
| 165 | $parameters['SHA-1'] = ''; |
||
| 166 | } |
||
| 167 | if ($this->showCardType) { |
||
| 168 | $parameters['TYPECARTE'] = ''; |
||
| 169 | } |
||
| 170 | |||
| 171 | if (method_exists($this, 'getTransactionNumber')) { |
||
| 172 | $parameters['NUMTRANS'] = $this->getTransactionNumber(); |
||
|
|
|||
| 173 | } |
||
| 174 | if (method_exists($this, 'getCallNumber')) { |
||
| 175 | $parameters['NUMAPPEL'] = $this->getCallNumber(); |
||
| 176 | } |
||
| 177 | if (method_exists($this, 'hasAuthorization') && $this->hasAuthorization()) { |
||
| 178 | $parameters['AUTORISATION'] = $this->getAuthorization(); |
||
| 179 | } |
||
| 180 | |||
| 181 | // Direct Plus requests special case. |
||
| 182 | if ($this->hasSubscriberRef()) { |
||
| 183 | $parameters['REFABONNE'] = $this->getSubscriberRef(); |
||
| 184 | } |
||
| 185 | |||
| 186 | return $parameters; |
||
| 187 | } |
||
| 188 | } |
||
| 189 |
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: