| Conditions | 19 |
| Paths | 40 |
| Total Lines | 85 |
| Code Lines | 49 |
| 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 |
||
| 47 | public function listAction(Request $request, Player $me, Team $team = null) |
||
| 48 | { |
||
| 49 | $query = $this->getQueryBuilder(); |
||
| 50 | |||
| 51 | // Load all countries into the cache so they are ready for later |
||
| 52 | Country::getQueryBuilder()->addToCache(); |
||
| 53 | |||
| 54 | if ($team) { |
||
| 55 | $query->where('team')->is($team); |
||
| 56 | } else { |
||
| 57 | // Add all teams to the cache |
||
| 58 | $this->getQueryBuilder('Team') |
||
| 59 | ->where('members')->greaterThan(0) |
||
| 60 | ->addToCache(); |
||
| 61 | } |
||
| 62 | |||
| 63 | if ($request->query->has('exceptMe')) { |
||
| 64 | $query->except($me); |
||
| 65 | } |
||
| 66 | |||
| 67 | $players = $query |
||
| 68 | ->sortBy('name') |
||
| 69 | ->getModels($fast = true) |
||
| 70 | ; |
||
| 71 | |||
| 72 | $groupBy = $request->query->get('groupBy'); |
||
| 73 | $sortBy = $request->query->get('sortBy'); |
||
| 74 | $sortOrder = $request->query->get('sortOrder'); |
||
| 75 | |||
| 76 | if ($groupBy) { |
||
| 77 | $grouped = []; |
||
| 78 | |||
| 79 | /** @var Player $player */ |
||
| 80 | foreach ($players as $player) { |
||
| 81 | $key = ''; |
||
| 82 | |||
| 83 | if ($groupBy == 'country') { |
||
| 84 | $key = $player->getCountry()->getName(); |
||
| 85 | } elseif ($groupBy == 'team') { |
||
| 86 | $key = $player->getTeam()->getEscapedName(); |
||
| 87 | |||
| 88 | if ($key == '<em>None</em>') { |
||
| 89 | $key = ' '; |
||
| 90 | } |
||
| 91 | } elseif ($groupBy == 'activity') { |
||
| 92 | $key = ($player->getMatchActivity() > 0.0) ? 'Active' : 'Inactive'; |
||
| 93 | } |
||
| 94 | |||
| 95 | $grouped[$key][] = $player; |
||
| 96 | } |
||
| 97 | |||
| 98 | ksort($grouped); |
||
| 99 | $players = $grouped; |
||
| 100 | } |
||
| 101 | |||
| 102 | if ($sortBy || $sortOrder) { |
||
| 103 | $sortBy = $sortBy ? $sortBy : 'callsign'; |
||
| 104 | $sortOrder = $sortOrder ? $sortOrder : 'ASC'; |
||
| 105 | |||
| 106 | foreach ($players as &$playerList) { |
||
| 107 | if ($sortBy == 'callsign') { |
||
| 108 | usort($playerList, function($a, $b) use ($sortOrder) { |
||
| 109 | if ($sortOrder == 'DESC') { |
||
| 110 | return strcmp($b->getUsername(), $a->getUsername()); |
||
| 111 | } |
||
| 112 | |||
| 113 | return strcmp($a->getUsername(), $b->getUsername()); |
||
| 114 | }); |
||
| 115 | } elseif ($sortBy == 'activity') { |
||
| 116 | usort($playerList, function($a, $b) use ($sortOrder) { |
||
| 117 | if ($sortOrder == 'DESC') { |
||
| 118 | return ($b->getMatchActivity() > $a->getMatchActivity()); |
||
| 119 | } |
||
| 120 | |||
| 121 | return ($a->getMatchActivity() > $b->getMatchActivity()); |
||
| 122 | }); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | return array( |
||
| 128 | 'grouped' => ($groupBy != null), |
||
| 129 | 'players' => $players, |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | |||
| 154 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.