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 |
||
| 5 | trait Lists |
||
| 6 | { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * |
||
| 10 | * @param \hemio\html\Li $child |
||
| 11 | * @return \hemio\html\Li |
||
| 12 | */ |
||
| 13 | public function addChild(\hemio\html\Li $child) |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Adds <<li>> tag containing a given entry |
||
| 21 | * |
||
| 22 | * @param \hemio\html\Interface_\HtmlCode $withEntry The contained entry |
||
| 23 | * @return \hemio\html\Li |
||
| 24 | */ |
||
| 25 | public function addEntryWith(\hemio\html\Interface_\HtmlCode $withEntry) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * |
||
| 34 | * @param \hemio\html\Interface_\HtmlCode $header |
||
| 35 | * @return \hemio\html\Ul |
||
| 36 | */ |
||
| 37 | View Code Duplication | public function addSubUl(\hemio\html\Interface_\HtmlCode $header) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * |
||
| 48 | * @param \hemio\html\Interface_\HtmlCode $header |
||
| 49 | * @return \hemio\html\Ol |
||
| 50 | */ |
||
| 51 | View Code Duplication | public function addSubOl(\hemio\html\Interface_\HtmlCode $header) |
|
| 59 | |||
| 60 | public function isValidChild(\hemio\html\Interface_\HtmlCode $child) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Used by addChild, therefore has to be defined here |
||
| 67 | */ |
||
| 68 | abstract public function addChildInternal(\hemio\html\Interface_\HtmlCode $child); |
||
| 69 | } |
||
| 70 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.