Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MatchController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MatchController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class MatchController extends CRUDController |
||
9 | { |
||
10 | /** |
||
11 | * Whether the last edited match has had its ELO changed, requiring an ELO |
||
12 | * recalculation |
||
13 | * |
||
14 | * This is useful so that a confirmation form is shown, asking the user if |
||
15 | * they want to recalculate ELOs |
||
16 | * |
||
17 | * @var bool |
||
18 | */ |
||
19 | public $recalculateNeeded = false; |
||
20 | |||
21 | public function listAction(Request $request, Player $me, Team $team = null, $type = null) |
||
22 | { |
||
23 | $qb = $this->getQueryBuilder(); |
||
24 | |||
25 | $currentPage = $request->query->get('page', 1); |
||
26 | |||
27 | $query = $qb->sortBy('time')->reverse() |
||
|
|||
28 | ->with($team, $type) |
||
29 | ->limit(50)->fromPage($currentPage); |
||
30 | |||
31 | $matches = $query->getModels($fast = true); |
||
32 | |||
33 | foreach ($matches as $match) { |
||
34 | // Don't show wrong labels for matches |
||
35 | $match->getOriginalTimestamp()->setTimezone($me->getTimezone()); |
||
36 | } |
||
37 | |||
38 | return array( |
||
39 | "matches" => $matches, |
||
40 | "team" => $team, |
||
41 | "currentPage" => $currentPage, |
||
42 | "totalPages" => $qb->countPages() |
||
43 | ); |
||
44 | } |
||
45 | |||
46 | 1 | public function showAction(Match $match) |
|
50 | |||
51 | 1 | View Code Duplication | public function createAction(Player $me) |
67 | |||
68 | public function deleteAction(Player $me, Match $match) |
||
83 | |||
84 | public function editAction(Player $me, Match $match) |
||
85 | { |
||
86 | // TODO: Generating this response is unnecessary |
||
87 | $response = $this->edit($match, $me, "match"); |
||
88 | |||
89 | View Code Duplication | if ($this->recalculateNeeded && $match->isOfficial()) { |
|
90 | // Redirect to a confirmation form if we are assigning a new leader |
||
91 | $url = Service::getGenerator()->generate('match_recalculate', array( |
||
92 | 'match' => $match->getId(), |
||
93 | )); |
||
94 | |||
95 | return new RedirectResponse($url); |
||
96 | } |
||
97 | |||
98 | return $response; |
||
99 | } |
||
100 | |||
101 | public function recalculateAction(Player $me, $match) |
||
134 | |||
135 | /** |
||
136 | * Recalculates match history for all teams and matches |
||
137 | * |
||
138 | * Recalculation is done as follows: |
||
139 | * 1. A match is chosen as a starting point - it's stored old team ELOs are |
||
140 | * considered correct |
||
141 | * 2. Team ELOs are reset to their values at the starting point |
||
142 | * 3. Each match that occurred since the first specified match has its ELO |
||
143 | * recalculated based on the current team values, and the new match data |
||
144 | * and team ELOs are stored in the database |
||
145 | * |
||
146 | * @param Match $match The first match |
||
147 | */ |
||
148 | private function recalculate(Match $match) |
||
206 | |||
207 | /** |
||
208 | * Echo a string and flush the buffers |
||
209 | * |
||
210 | * Useful for streamed AJAX responses |
||
211 | * |
||
212 | * @param string $string The string to echo |
||
213 | */ |
||
214 | private function log($string) |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | 1 | protected function getMessages($type, $name = '') |
|
239 | |||
240 | 1 | protected function validate($form) |
|
241 | { |
||
242 | // Make sure that two different teams participated in a match, i.e. a team |
||
243 | // didn't match against itself |
||
244 | 1 | $firstTeam = $form->get('first_team')->get('team')->getData(); |
|
245 | 1 | $secondTeam = $form->get('second_team')->get('team')->getData(); |
|
246 | |||
247 | 1 | if (!$firstTeam || !$secondTeam) { |
|
248 | return; |
||
249 | } |
||
250 | |||
251 | 1 | if ($firstTeam->isSameAs($secondTeam)) { |
|
252 | 1 | $message = "You can't report a match where a team played against itself!"; |
|
253 | 1 | $form->addError(new FormError($message)); |
|
254 | } |
||
255 | |||
256 | 1 | foreach (array('first_team', 'second_team') as $team) { |
|
257 | 1 | $input = $form->get($team)->get('team'); |
|
258 | |||
259 | 1 | if ($form->get('type')->getData() == Match::OFFICIAL) { |
|
260 | 1 | if ($input->getData() instanceof ColorTeam) { |
|
261 | $message = "Please enter a valid team for an official match."; |
||
262 | 1 | $input->addError(new FormError($message)); |
|
263 | } |
||
264 | } else { |
||
265 | 1 | if (!$input->getData() instanceof ColorTeam) { |
|
266 | $message = "Please enter a team color for fun and special matches."; |
||
267 | 1 | $input->addError(new FormError($message)); |
|
268 | } |
||
269 | } |
||
270 | } |
||
271 | 1 | } |
|
272 | |||
273 | protected function validateEdit($form, $match) |
||
283 | } |
||
284 |
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: