Conditions | 8 |
Paths | 18 |
Total Lines | 52 |
Code Lines | 32 |
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 |
||
12 | public function showAction(Player $player, Player $me, Request $request) |
||
13 | { |
||
14 | $formView = null; |
||
15 | |||
16 | if ($me->hasPermission(Permission::VIEW_VISITOR_LOG) && !$this->isDemoMode()) { |
||
17 | $this->creator = new FormCreator($player); |
||
18 | $form = $this->creator->create()->handleRequest($request); |
||
19 | |||
20 | if ($form->isValid()) { |
||
21 | $form = $this->handleAdminNotesForm($form, $player, $me); |
||
22 | } |
||
23 | |||
24 | $formView = $form->createView(); |
||
25 | } |
||
26 | |||
27 | $periods = []; |
||
28 | $season = Season::getCurrentSeasonRange(); |
||
29 | $periodLength = round($season->getEndOfRange()->diffInDays($season->getStartOfRange()) / 30); |
||
30 | $seasonStart = $season->getStartOfRange(); |
||
31 | |||
32 | for ($i = 0; $i < $periodLength; $i++) { |
||
33 | $periods[] = $seasonStart->firstOfMonth()->copy(); |
||
34 | $periods[] = $seasonStart->day(15)->copy(); |
||
35 | $periods[] = $seasonStart->lastOfMonth()->copy(); |
||
36 | |||
37 | $seasonStart->addMonth(); |
||
38 | } |
||
39 | |||
40 | $currentPeriod = new ArrayIterator($periods); |
||
41 | $playerEloSeason = $player->getEloSeasonHistory(); |
||
42 | $seasonSummary = []; |
||
43 | |||
44 | foreach ($playerEloSeason as $elo) { |
||
45 | if ($elo['month'] > $currentPeriod->current()->month || $elo['day'] > $currentPeriod->current()->day) { |
||
46 | $currentPeriod->next(); |
||
47 | } |
||
48 | |||
49 | $seasonSummary[$currentPeriod->current()->format('M d')] = $elo['elo']; |
||
50 | } |
||
51 | |||
52 | $bans = Ban::getQueryBuilder() |
||
53 | ->where('player', '=', $player->getId()) |
||
54 | ->getModels($fast = true) |
||
55 | ; |
||
56 | |||
57 | return array( |
||
58 | 'bans' => $bans, |
||
59 | 'player' => $player, |
||
60 | 'seasonSummary' => $seasonSummary, |
||
61 | 'adminNotesForm' => $formView, |
||
62 | ); |
||
63 | } |
||
64 | |||
176 |
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: