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 | View Code Duplication | trait FluidAliasesTrait |
|
|
|||
6 | { |
||
7 | |||
8 | // Alias of ->length(). |
||
9 | public function size() |
||
13 | |||
14 | // Alias of ->query(). |
||
15 | public function __invoke(...$query) |
||
19 | |||
20 | // Alias of ->addChild(). |
||
21 | public function add($child, ...$optionals) |
||
25 | |||
26 | // Alias of ->prependSibling(). |
||
27 | public function prepend($sibling, ...$optionals) |
||
31 | |||
32 | // Alias of ->appendSibling(). |
||
33 | public function append($sibling, ...$optionals) |
||
37 | |||
38 | // Alias of ->setAttribute(). |
||
39 | public function attr(...$arguments) |
||
43 | |||
44 | // Alias of ->setText(). |
||
45 | public function text($text) |
||
49 | |||
50 | // Alias of ->setCdata(). |
||
51 | public function cdata($text) |
||
55 | |||
56 | // Alias of ->setComment(). |
||
57 | public function comment($text) |
||
61 | |||
62 | } |
||
63 |
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.