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 |
||
| 13 | class PagesRepository extends Repository |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Get metadata about a single page from the API. |
||
| 18 | * @param Project $project The project to which the page belongs. |
||
| 19 | * @param string $pageTitle Page title. |
||
| 20 | * @return string[] Array with some of the following keys: pageid, title, missing, displaytitle, |
||
| 21 | * url. |
||
| 22 | */ |
||
| 23 | public function getPageInfo(Project $project, $pageTitle) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Get metadata about a set of pages from the API. |
||
| 31 | * @param Project $project The project to which the pages belong. |
||
| 32 | * @param string[] $pageTitles Array of page titles. |
||
| 33 | * @return string[] Array keyed by the page names, each element with some of the |
||
| 34 | * following keys: pageid, title, missing, displaytitle, url. |
||
| 35 | */ |
||
| 36 | public function getPagesInfo(Project $project, $pageTitles) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get revisions of a single page. |
||
| 64 | * @param Page $page The page. |
||
| 65 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 66 | * @return string[] Each member with keys: id, timestamp, length- |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function getRevisions(Page $page, User $user = null) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Get a count of the number of revisions of a single page |
||
| 97 | * @param Page $page The page. |
||
| 98 | * @param User|null $user Specify to only count revisions by the given user. |
||
| 99 | * @return int |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function getNumRevisions(Page $page, User $user = null) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Get assessment data for the given pages |
||
| 120 | * @param Project $project The project to which the pages belong. |
||
| 121 | * @param int[] $pageIds Page IDs |
||
| 122 | * @return string[] Assessment data as retrieved from the database. |
||
| 123 | */ |
||
| 124 | public function getAssessments(Project $project, $pageIds) |
||
| 140 | } |
||
| 141 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.