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:
1 | <?php |
||
24 | class EditCounterController extends Controller |
||
25 | { |
||
26 | |||
27 | /** @var User The user being queried. */ |
||
28 | protected $user; |
||
29 | |||
30 | /** @var Project The project being queried. */ |
||
31 | protected $project; |
||
32 | |||
33 | /** @var EditCounter The edit-counter, that does all the work. */ |
||
34 | protected $editCounter; |
||
35 | |||
36 | /** |
||
37 | * Get the tool's shortname. |
||
38 | * @return string |
||
39 | */ |
||
40 | public function getToolShortname() |
||
44 | |||
45 | /** |
||
46 | * Every action in this controller (other than 'index') calls this first. |
||
47 | * @param string|bool $project The project name. |
||
48 | * @param string|bool $username The username. |
||
49 | */ |
||
50 | protected function setUpEditCounter($project = false, $username = false) |
||
66 | |||
67 | /** |
||
68 | * The initial GET request that displays the search form. |
||
69 | * |
||
70 | * @Route("/ec", name="ec") |
||
71 | * @Route("/ec", name="EditCounter") |
||
72 | * @Route("/ec/", name="EditCounterSlash") |
||
73 | * @Route("/ec/index.php", name="EditCounterIndexPhp") |
||
74 | * @Route("/ec/{project}", name="EditCounterProject") |
||
75 | * |
||
76 | * @param Request $request |
||
77 | * @param string|null $project |
||
78 | * @return RedirectResponse|Response |
||
79 | */ |
||
80 | public function indexAction(Request $request, $project = null) |
||
105 | |||
106 | /** |
||
107 | * Display all results. |
||
108 | * @Route("/ec/{project}/{username}", name="EditCounterResult") |
||
109 | * @param Request $request |
||
110 | * @param string $project |
||
111 | * @param string $username |
||
112 | * @return Response |
||
113 | */ |
||
114 | public function resultAction(Request $request, $project, $username) |
||
129 | |||
130 | /** |
||
131 | * Display the general statistics section. |
||
132 | * @Route("/ec-generalstats/{project}/{username}", name="EditCounterGeneralStats") |
||
133 | * @param string $project |
||
134 | * @param string $username |
||
135 | * @return Response |
||
136 | */ |
||
137 | View Code Duplication | public function generalStatsAction($project, $username) |
|
151 | |||
152 | /** |
||
153 | * Display the namespace totals section. |
||
154 | * @Route("/ec-namespacetotals/{project}/{username}", name="EditCounterNamespaceTotals") |
||
155 | * @param string $project |
||
156 | * @param string $username |
||
157 | * @return Response |
||
158 | */ |
||
159 | View Code Duplication | public function namespaceTotalsAction($project, $username) |
|
173 | |||
174 | /** |
||
175 | * Display the timecard section. |
||
176 | * @Route("/ec-timecard/{project}/{username}", name="EditCounterTimecard") |
||
177 | * @param string $project |
||
178 | * @param string $username |
||
179 | * @return Response |
||
180 | */ |
||
181 | View Code Duplication | public function timecardAction($project, $username) |
|
199 | |||
200 | /** |
||
201 | * Display the year counts section. |
||
202 | * @Route("/ec-yearcounts/{project}/{username}", name="EditCounterYearCounts") |
||
203 | * @param string $project |
||
204 | * @param string $username |
||
205 | * @return Response |
||
206 | */ |
||
207 | View Code Duplication | public function yearcountsAction($project, $username) |
|
224 | |||
225 | /** |
||
226 | * Display the month counts section. |
||
227 | * @Route("/ec-monthcounts/{project}/{username}", name="EditCounterMonthCounts") |
||
228 | * @param string $project |
||
229 | * @param string $username |
||
230 | * @return Response |
||
231 | */ |
||
232 | View Code Duplication | public function monthcountsAction($project, $username) |
|
250 | |||
251 | /** |
||
252 | * Display the latest global edits section. |
||
253 | * @Route("/ec-latestglobal/{project}/{username}", name="EditCounterLatestGlobal") |
||
254 | * @param string $project |
||
255 | * @param string $username |
||
256 | * @return Response |
||
257 | */ |
||
258 | View Code Duplication | public function latestglobalAction($project, $username) |
|
271 | } |
||
272 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.