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 EditCounterController 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 EditCounterController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class EditCounterController extends Controller |
||
| 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 | */ |
||
| 41 | public function getToolShortname() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Every action in this controller (other than 'index') calls this first. |
||
| 48 | * If a response is returned, the calling action is expected to return it. |
||
| 49 | * @param string|bool $project The project name. |
||
| 50 | * @param string|bool $username The username. |
||
| 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($project = false, $username = false, $key = null) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The initial GET request that displays the search form. |
||
| 82 | * |
||
| 83 | * @Route("/ec", name="ec") |
||
| 84 | * @Route("/ec", name="EditCounter") |
||
| 85 | * @Route("/ec/", name="EditCounterSlash") |
||
| 86 | * @Route("/ec/index.php", name="EditCounterIndexPhp") |
||
| 87 | * @Route("/ec/{project}", name="EditCounterProject") |
||
| 88 | * |
||
| 89 | * @param Request $request |
||
| 90 | * @param string|null $project |
||
| 91 | * @return RedirectResponse|Response |
||
| 92 | */ |
||
| 93 | public function indexAction(Request $request, $project = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Display all results. |
||
| 121 | * @Route("/ec/{project}/{username}", name="EditCounterResult") |
||
| 122 | * @param Request $request |
||
| 123 | * @param string $project |
||
| 124 | * @param string $username |
||
| 125 | * @return Response |
||
| 126 | */ |
||
| 127 | public function resultAction(Request $request, $project, $username) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Display the general statistics section. |
||
| 157 | * @Route("/ec-generalstats/{project}/{username}", name="EditCounterGeneralStats") |
||
| 158 | * @param string $project |
||
| 159 | * @param string $username |
||
| 160 | * @return Response |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function generalStatsAction($project, $username) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Display the namespace totals section. |
||
| 182 | * @Route("/ec-namespacetotals/{project}/{username}", name="EditCounterNamespaceTotals") |
||
| 183 | * @param Request $request |
||
| 184 | * @param string $project |
||
| 185 | * @param string $username |
||
| 186 | * @return Response |
||
| 187 | */ |
||
| 188 | View Code Duplication | public function namespaceTotalsAction(Request $request, $project, $username) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Display the timecard section. |
||
| 208 | * @Route("/ec-timecard/{project}/{username}", name="EditCounterTimecard") |
||
| 209 | * @param string $project |
||
| 210 | * @param string $username |
||
| 211 | * @return Response |
||
| 212 | */ |
||
| 213 | View Code Duplication | public function timecardAction($project, $username) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Display the year counts section. |
||
| 237 | * @Route("/ec-yearcounts/{project}/{username}", name="EditCounterYearCounts") |
||
| 238 | * @param string $project |
||
| 239 | * @param string $username |
||
| 240 | * @return Response |
||
| 241 | */ |
||
| 242 | View Code Duplication | public function yearcountsAction($project, $username) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Display the month counts section. |
||
| 262 | * @Route("/ec-monthcounts/{project}/{username}", name="EditCounterMonthCounts") |
||
| 263 | * @param Request $request |
||
| 264 | * @param string $project |
||
| 265 | * @param string $username |
||
| 266 | * @return Response |
||
| 267 | */ |
||
| 268 | View Code Duplication | public function monthcountsAction(Request $request, $project, $username) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Display the latest global edits section. |
||
| 292 | * @Route("/ec-latestglobal/{project}/{username}", name="EditCounterLatestGlobal") |
||
| 293 | * @param Request $request The HTTP request. |
||
| 294 | * @param string $project The project name. |
||
| 295 | * @param string $username The username. |
||
| 296 | * @return Response |
||
| 297 | */ |
||
| 298 | View Code Duplication | public function latestglobalAction(Request $request, $project, $username) |
|
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Below are internal API endpoints for the Edit Counter. |
||
| 320 | * All only respond with JSON and only to requests passing in the value |
||
| 321 | * of the 'secret' parameter. This should not be used in JavaScript or clientside |
||
| 322 | * applications, rather only used internally. |
||
| 323 | */ |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get (most) of the general statistics as JSON. |
||
| 327 | * @Route("/api/ec/pairdata/{project}/{username}/{key}", name="EditCounterApiPairData") |
||
| 328 | * @param Request $request |
||
| 329 | * @param string $project |
||
| 330 | * @param string $username |
||
| 331 | * @param string $key API key. |
||
| 332 | * @return JsonResponse |
||
| 333 | */ |
||
| 334 | View Code Duplication | public function pairDataApiAction(Request $request, $project, $username, $key) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Get various log counts for the user as JSON. |
||
| 349 | * @Route("/api/ec/logcounts/{project}/{username}/{key}", name="EditCounterApiLogCounts") |
||
| 350 | * @param Request $request |
||
| 351 | * @param string $project |
||
| 352 | * @param string $username |
||
| 353 | * @param string $key API key. |
||
| 354 | * @return JsonResponse |
||
| 355 | */ |
||
| 356 | View Code Duplication | public function logCountsApiAction(Request $request, $project, $username, $key) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Get edit sizes for the user as JSON. |
||
| 371 | * @Route("/api/ec/editsizes/{project}/{username}/{key}", name="EditCounterApiEditSizes") |
||
| 372 | * @param Request $request |
||
| 373 | * @param string $project |
||
| 374 | * @param string $username |
||
| 375 | * @param string $key API key. |
||
| 376 | * @return JsonResponse |
||
| 377 | */ |
||
| 378 | View Code Duplication | public function editSizesApiAction(Request $request, $project, $username, $key) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Get the namespace totals for the user as JSON. |
||
| 393 | * @Route("/api/ec/namespacetotals/{project}/{username}/{key}", name="EditCounterApiNamespaceTotals") |
||
| 394 | * @param Request $request |
||
| 395 | * @param string $project |
||
| 396 | * @param string $username |
||
| 397 | * @param string $key API key. |
||
| 398 | * @return Response |
||
| 399 | */ |
||
| 400 | View Code Duplication | public function namespaceTotalsApiAction(Request $request, $project, $username, $key) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Display or fetch the month counts for the user. |
||
| 415 | * @Route("/api/ec/monthcounts/{project}/{username}/{key}", name="EditCounterApiMonthCounts") |
||
| 416 | * @param Request $request |
||
| 417 | * @param string $project |
||
| 418 | * @param string $username |
||
| 419 | * @param string $key API key. |
||
| 420 | * @return Response |
||
| 421 | */ |
||
| 422 | View Code Duplication | public function monthcountsApiAction(Request $request, $project, $username, $key) |
|
| 434 | } |
||
| 435 |
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: