Conditions | 14 |
Paths | 248 |
Total Lines | 88 |
Code Lines | 69 |
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 |
||
205 | public function render(CommercialShipping $commercialShipping) { |
||
206 | switch ($commercialShipping->typeOfTransaction) { |
||
207 | case Transaction::TYP_RESOURCE: $class = 'resources'; break; |
||
208 | case Transaction::TYP_COMMANDER: $class = 'commander'; break; |
||
209 | case Transaction::TYP_SHIP: |
||
210 | $class = 'ship'; |
||
211 | break; |
||
212 | default: break; |
||
213 | } |
||
214 | |||
215 | echo '<div class="transaction ' . $class . '">'; |
||
216 | if ($commercialShipping->statement != CommercialShipping::ST_MOVING_BACK) { |
||
217 | echo '<div class="product">'; |
||
218 | if ($commercialShipping->statement == CommercialShipping::ST_WAITING) { |
||
219 | echo '<a href="' . Format::actionBuilder('canceltransaction', $this->sessionWrapper->get('token'), ['rtransaction' => $commercialShipping->rTransaction]) . '" class="hb lt right-link" title="supprimer cette offre coûtera ' . Format::number(floor($commercialShipping->price * Transaction::PERCENTAGE_TO_CANCEL / 100)) . ' crédits">×</a>'; |
||
220 | } |
||
221 | if ($commercialShipping->typeOfTransaction == Transaction::TYP_RESOURCE) { |
||
222 | echo '<img src="' . MEDIA . 'market/resources-pack-' . Transaction::getResourcesIcon($commercialShipping->quantity) . '.png" alt="" class="picto" />'; |
||
223 | echo '<div class="offer">'; |
||
224 | if ($commercialShipping->resourceTransported == NULL) { |
||
225 | # transaction |
||
226 | echo Format::numberFormat($commercialShipping->quantity) . ' <img src="' . MEDIA . 'resources/resource.png" alt="" class="icon-color" />'; |
||
227 | } else { |
||
228 | # resources sending |
||
229 | echo Format::numberFormat($commercialShipping->resourceTransported) . ' <img src="' . MEDIA . 'resources/resource.png" alt="" class="icon-color" />'; |
||
230 | } |
||
231 | echo '</div>'; |
||
232 | } elseif ($commercialShipping->typeOfTransaction == Transaction::TYP_COMMANDER) { |
||
233 | echo '<img src="' . MEDIA . 'commander/small/' . $commercialShipping->commanderAvatar . '.png" alt="" class="picto" />'; |
||
234 | echo '<div class="offer">'; |
||
235 | echo '<strong>' . CommanderResources::getInfo($commercialShipping->commanderLevel, 'grade') . ' ' . $commercialShipping->commanderName . '</strong>'; |
||
236 | echo '<em>' . Format::numberFormat($commercialShipping->commanderExperience) . ' xp | ' . $commercialShipping->commanderVictory . ' victoire' . Format::addPlural($commercialShipping->commanderVictory) . '</em>'; |
||
237 | echo '</div>'; |
||
238 | } elseif ($commercialShipping->typeOfTransaction == Transaction::TYP_SHIP) { |
||
239 | echo '<img src="' . MEDIA . 'ship/picto/ship' . $commercialShipping->identifier . '.png" alt="" class="picto" />'; |
||
240 | echo '<div class="offer">'; |
||
241 | echo '<strong>' . $commercialShipping->quantity . ' ' . ShipResource::getInfo($commercialShipping->identifier, 'codeName') . Format::plural($commercialShipping->quantity) . '</strong>'; |
||
242 | echo '<em>' . ShipResource::getInfo($commercialShipping->identifier, 'name') . ' / ' . ShipResource::getInfo($commercialShipping->identifier, 'pev') . ' pev</em>'; |
||
243 | echo '</div>'; |
||
244 | } |
||
245 | |||
246 | if ($commercialShipping->resourceTransported === NULL) { |
||
247 | # transaction |
||
248 | echo '<div class="for">'; |
||
249 | echo '<span>pour</span>'; |
||
250 | echo '</div>'; |
||
251 | echo '<div class="price">'; |
||
252 | echo Format::numberFormat($commercialShipping->price) . ' <img src="' . MEDIA . 'resources/credit.png" alt="" class="icon-color" />'; |
||
253 | echo '</div>'; |
||
254 | } elseif ($commercialShipping->resourceTransported == 0) { |
||
255 | # ships sending |
||
256 | echo '<div class="for"><span></span></div>'; |
||
257 | echo '<div class="price">'; |
||
258 | echo 'envoi de vaisseaux'; |
||
259 | echo '</div>'; |
||
260 | } else { |
||
261 | # resources sending |
||
262 | echo '<div class="for"><span></span></div>'; |
||
263 | echo '<div class="price">'; |
||
264 | echo 'envoi de ressources'; |
||
265 | echo '</div>'; |
||
266 | } |
||
267 | echo '</div>'; |
||
268 | } |
||
269 | |||
270 | $totalTime = Utils::interval($commercialShipping->dDeparture, $commercialShipping->dArrival, 's'); |
||
271 | $currentTime = Utils::interval(Utils::now(), $commercialShipping->dDeparture, 's'); |
||
272 | |||
273 | echo ($commercialShipping->statement != CommercialShipping::ST_WAITING) |
||
274 | ?'<div class="shipping progress" data-progress-total-time="' . $totalTime . '" data-progress-current-time="' . ($totalTime - $currentTime) . '" data-progress-output="lite">' |
||
275 | : '<div class="shipping">'; |
||
276 | echo '<span class="progress-container">'; |
||
277 | echo '<span style="width: ' . Format::percent($currentTime, $totalTime) . '%;" class="progress-bar"></span>'; |
||
278 | echo '</span>'; |
||
279 | |||
280 | echo '<div class="ships">'; |
||
281 | echo $commercialShipping->shipQuantity; |
||
282 | echo '<img src="' . MEDIA . 'resources/transport.png" alt="" class="icon-color" />'; |
||
283 | echo '</div>'; |
||
284 | |||
285 | if ($commercialShipping->statement == CommercialShipping::ST_WAITING) { |
||
286 | echo '<div class="time">à quai</div>'; |
||
287 | } else { |
||
288 | echo '<div class="time progress-text"></div>'; |
||
289 | } |
||
290 | echo '</div>'; |
||
291 | echo '</div>'; |
||
292 | } |
||
293 | } |
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: