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 |
||
12 | View Code Duplication | class XMLReader extends OriginalXMLReader |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * This is a blend of XMLReader's read & next, in that it accepts a node |
||
16 | * name to search for, but it doesn't assume the node if on the same level |
||
17 | * in DOM hierarchy as where you're currently at: it won't skip children & |
||
18 | * it will cross parents (unless. |
||
19 | * |
||
20 | * @param string $name Name of the node we're looking for. |
||
21 | * @param string $parent Name of parent node(s) to stay inside of. |
||
22 | * |
||
23 | * @return bool |
||
24 | */ |
||
25 | public function readNext($name, $parent = null) |
||
41 | } |
||
42 |
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.