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 |
||
| 25 | class EditCounterController extends XtoolsController |
||
| 26 | { |
||
| 27 | |||
| 28 | /** @var User The user being queried. */ |
||
| 29 | protected $user; |
||
| 30 | |||
| 31 | /** @var Project The project being queried. */ |
||
| 32 | protected $project; |
||
| 33 | |||
| 34 | /** @var EditCounter The edit-counter, that does all the work. */ |
||
| 35 | protected $editCounter; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get the tool's shortname. |
||
| 39 | * @return string |
||
| 40 | * @codeCoverageIgnore |
||
| 41 | */ |
||
| 42 | public function getToolShortname() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Every action in this controller (other than 'index') calls this first. |
||
| 49 | * If a response is returned, the calling action is expected to return it. |
||
| 50 | * @param Request $request |
||
| 51 | * @param string $key API key, as given in the reuqest. Omit this for actions |
||
| 52 | * that are public (only /api/ec actions should pass this in). |
||
| 53 | * @return null|RedirectResponse |
||
| 54 | */ |
||
| 55 | protected function setUpEditCounter(Request $request, $key = null) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The initial GET request that displays the search form. |
||
| 85 | * |
||
| 86 | * @Route("/ec", name="ec") |
||
| 87 | * @Route("/ec", name="EditCounter") |
||
| 88 | * @Route("/ec/", name="EditCounterSlash") |
||
| 89 | * @Route("/ec/index.php", name="EditCounterIndexPhp") |
||
| 90 | * @Route("/ec/{project}", name="EditCounterProject") |
||
| 91 | * |
||
| 92 | * @param Request $request |
||
| 93 | * @return RedirectResponse|Response |
||
| 94 | */ |
||
| 95 | public function indexAction(Request $request) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Display all results. |
||
| 117 | * @Route("/ec/{project}/{username}", name="EditCounterResult") |
||
| 118 | * @param Request $request |
||
| 119 | * @param string $project |
||
| 120 | * @param string $username |
||
| 121 | * @return Response |
||
| 122 | * @codeCoverageIgnore |
||
| 123 | */ |
||
| 124 | public function resultAction(Request $request, $project, $username) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Display the general statistics section. |
||
| 154 | * @Route("/ec-generalstats/{project}/{username}", name="EditCounterGeneralStats") |
||
| 155 | * @param Request $request |
||
| 156 | * @return Response |
||
| 157 | * @codeCoverageIgnore |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function generalStatsAction(Request $request) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Display the namespace totals section. |
||
| 179 | * @Route("/ec-namespacetotals/{project}/{username}", name="EditCounterNamespaceTotals") |
||
| 180 | * @param Request $request |
||
| 181 | * @return Response |
||
| 182 | * @codeCoverageIgnore |
||
| 183 | */ |
||
| 184 | View Code Duplication | public function namespaceTotalsAction(Request $request) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Display the timecard section. |
||
| 204 | * @Route("/ec-timecard/{project}/{username}", name="EditCounterTimecard") |
||
| 205 | * @param Request $request |
||
| 206 | * @return Response |
||
| 207 | * @codeCoverageIgnore |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function timecardAction(Request $request) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Display the year counts section. |
||
| 233 | * @Route("/ec-yearcounts/{project}/{username}", name="EditCounterYearCounts") |
||
| 234 | * @param Request $request |
||
| 235 | * @return Response |
||
| 236 | * @codeCoverageIgnore |
||
| 237 | */ |
||
| 238 | View Code Duplication | public function yearcountsAction(Request $request) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Display the month counts section. |
||
| 258 | * @Route("/ec-monthcounts/{project}/{username}", name="EditCounterMonthCounts") |
||
| 259 | * @param Request $request |
||
| 260 | * @return Response |
||
| 261 | * @codeCoverageIgnore |
||
| 262 | */ |
||
| 263 | View Code Duplication | public function monthcountsAction(Request $request) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Display the latest global edits section. |
||
| 287 | * @Route("/ec-latestglobal/{project}/{username}", name="EditCounterLatestGlobal") |
||
| 288 | * @param Request $request The HTTP request. |
||
| 289 | * @return Response |
||
| 290 | * @codeCoverageIgnore |
||
| 291 | */ |
||
| 292 | View Code Duplication | public function latestglobalAction(Request $request) |
|
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Below are internal API endpoints for the Edit Counter. |
||
| 314 | * All only respond with JSON and only to requests passing in the value |
||
| 315 | * of the 'secret' parameter. This should not be used in JavaScript or clientside |
||
| 316 | * applications, rather only used internally. |
||
| 317 | */ |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get (most) of the general statistics as JSON. |
||
| 321 | * @Route("/api/ec/pairdata/{project}/{username}/{key}", name="EditCounterApiPairData") |
||
| 322 | * @param Request $request |
||
| 323 | * @param string $key API key. |
||
| 324 | * @return JsonResponse |
||
| 325 | * @codeCoverageIgnore |
||
| 326 | */ |
||
| 327 | View Code Duplication | public function pairDataApiAction(Request $request, $key) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Get various log counts for the user as JSON. |
||
| 342 | * @Route("/api/ec/logcounts/{project}/{username}/{key}", name="EditCounterApiLogCounts") |
||
| 343 | * @param Request $request |
||
| 344 | * @param string $key API key. |
||
| 345 | * @return JsonResponse |
||
| 346 | * @codeCoverageIgnore |
||
| 347 | */ |
||
| 348 | View Code Duplication | public function logCountsApiAction(Request $request, $key) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Get edit sizes for the user as JSON. |
||
| 363 | * @Route("/api/ec/editsizes/{project}/{username}/{key}", name="EditCounterApiEditSizes") |
||
| 364 | * @param Request $request |
||
| 365 | * @param string $key API key. |
||
| 366 | * @return JsonResponse |
||
| 367 | * @codeCoverageIgnore |
||
| 368 | */ |
||
| 369 | View Code Duplication | public function editSizesApiAction(Request $request, $key) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Get the namespace totals for the user as JSON. |
||
| 384 | * @Route("/api/ec/namespacetotals/{project}/{username}/{key}", name="EditCounterApiNamespaceTotals") |
||
| 385 | * @param Request $request |
||
| 386 | * @param string $key API key. |
||
| 387 | * @return Response |
||
| 388 | * @codeCoverageIgnore |
||
| 389 | */ |
||
| 390 | View Code Duplication | public function namespaceTotalsApiAction(Request $request, $key) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Display or fetch the month counts for the user. |
||
| 405 | * @Route("/api/ec/monthcounts/{project}/{username}/{key}", name="EditCounterApiMonthCounts") |
||
| 406 | * @param Request $request |
||
| 407 | * @param string $key API key. |
||
| 408 | * @return Response |
||
| 409 | * @codeCoverageIgnore |
||
| 410 | */ |
||
| 411 | View Code Duplication | public function monthcountsApiAction(Request $request, $key) |
|
| 423 | } |
||
| 424 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: