| Conditions | 10 |
| Paths | 256 |
| Total Lines | 34 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 121 | public function getParameters() |
||
| 122 | { |
||
| 123 | $parameters = [ |
||
| 124 | 'ACTIVITE' => $this->activity, |
||
| 125 | 'DATEQ' => $this->date instanceof \DateTime ? $this->date->format('dmYHis') : null, |
||
| 126 | ]; |
||
| 127 | |||
| 128 | if ($this->showCountry) { |
||
| 129 | $parameters['PAYS'] = ''; |
||
| 130 | } |
||
| 131 | if ($this->showSha1) { |
||
| 132 | $parameters['SHA-1'] = ''; |
||
| 133 | } |
||
| 134 | if ($this->showCardType) { |
||
| 135 | $parameters['TYPECARTE'] = ''; |
||
| 136 | } |
||
| 137 | |||
| 138 | if (method_exists($this, 'getTransactionNumber')) { |
||
| 139 | $parameters['NUMTRANS'] = $this->getTransactionNumber(); |
||
|
|
|||
| 140 | } |
||
| 141 | if (method_exists($this, 'getCallNumber')) { |
||
| 142 | $parameters['NUMAPPEL'] = $this->getCallNumber(); |
||
| 143 | } |
||
| 144 | if (method_exists($this, 'getAuthorization') && null !== $this->getAuthorization()) { |
||
| 145 | $parameters['AUTORISATION'] = $this->getAuthorization(); |
||
| 146 | } |
||
| 147 | |||
| 148 | // Direct Plus requests special case. |
||
| 149 | if (null !== $this->getSubscriberRef()) { |
||
| 150 | $parameters['REFABONNE'] = $this->getSubscriberRef(); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $parameters; |
||
| 154 | } |
||
| 155 | } |
||
| 156 |
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: