| Conditions | 15 |
| Paths | 80 |
| Total Lines | 75 |
| Code Lines | 43 |
| 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 |
||
| 79 | public function listAction(Request $request, Player $me, Team $team = null) |
||
| 80 | { |
||
| 81 | $query = Player::getQueryBuilder(); |
||
| 82 | |||
| 83 | // Load all countries into the cache so they are ready for later |
||
| 84 | Country::getQueryBuilder()->addToCache(); |
||
| 85 | |||
| 86 | if ($team) { |
||
| 87 | $query->where('team', '=', $team); |
||
| 88 | } else { |
||
| 89 | // Add all teams to the cache |
||
| 90 | Team::getQueryBuilder() |
||
| 91 | ->where('members', '>', 0) |
||
| 92 | ->addToCache() |
||
| 93 | ; |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($request->query->has('exceptMe')) { |
||
| 97 | $query->except($me); |
||
| 98 | } |
||
| 99 | |||
| 100 | $groupBy = $request->query->get('groupBy'); |
||
| 101 | $sortBy = $request->query->get('sortBy'); |
||
| 102 | $sortOrder = $request->query->get('sortOrder'); |
||
| 103 | |||
| 104 | $query |
||
| 105 | ->active() |
||
| 106 | ->withMatchActivity() |
||
| 107 | ->orderBy('username') |
||
| 108 | ; |
||
| 109 | |||
| 110 | if (!$request->query->get('showAll')) { |
||
| 111 | $query->having('activity', '>', 0); |
||
| 112 | } |
||
| 113 | |||
| 114 | if ($sortBy || $sortOrder) { |
||
| 115 | $sortBy = $sortBy ? 'activity' : 'callsign'; |
||
| 116 | $sortOrder = $sortOrder ? 'DESC' : 'ASC'; |
||
| 117 | |||
| 118 | $query->orderBy($sortBy, $sortOrder); |
||
| 119 | } |
||
| 120 | |||
| 121 | $players = $query->getModels($fast = true); |
||
| 122 | |||
| 123 | if ($groupBy) { |
||
| 124 | $grouped = []; |
||
| 125 | |||
| 126 | /** @var Player $player */ |
||
| 127 | foreach ($players as $player) { |
||
| 128 | $key = ''; |
||
| 129 | |||
| 130 | if ($groupBy == 'country') { |
||
| 131 | $key = $player->getCountry()->getName(); |
||
|
|
|||
| 132 | } elseif ($groupBy == 'team') { |
||
| 133 | $key = $player->getTeam()->getEscapedName(); |
||
| 134 | |||
| 135 | if ($key == '<em>None</em>') { |
||
| 136 | $key = ' '; |
||
| 137 | } |
||
| 138 | } elseif ($groupBy == 'activity') { |
||
| 139 | $key = ($player->getMatchActivity() > 0.0) ? 'Active' : 'Inactive'; |
||
| 140 | } |
||
| 141 | |||
| 142 | $grouped[$key][] = $player; |
||
| 143 | } |
||
| 144 | |||
| 145 | ksort($grouped); |
||
| 146 | $players = $grouped; |
||
| 147 | } |
||
| 148 | |||
| 149 | return array( |
||
| 150 | 'grouped' => ($groupBy !== null), |
||
| 151 | 'players' => $players, |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 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: