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 |
||
14 | class LineGenerator extends AbstractContentGenerator |
||
15 | { |
||
16 | /** @var array|string[] */ |
||
17 | private $content = []; |
||
18 | |||
19 | /** @var string */ |
||
20 | private $separator = ' '; |
||
21 | |||
22 | /** |
||
23 | * @param string|array|GeneratorInterface[]|AbstractContentGenerator[] $content |
||
24 | * @return $this |
||
25 | * @throws InvalidArgumentException |
||
26 | */ |
||
27 | public function add($content) |
||
48 | |||
49 | /** |
||
50 | * @param $separator |
||
51 | */ |
||
52 | public function setSeparator($separator) |
||
56 | |||
57 | public function clear() |
||
61 | |||
62 | /** |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function hasContent() |
||
69 | |||
70 | /** |
||
71 | * @throws InvalidArgumentException|RuntimeException |
||
72 | * @return string |
||
73 | */ |
||
74 | public function generate() |
||
78 | |||
79 | /** |
||
80 | * @return int |
||
81 | */ |
||
82 | public function count() |
||
86 | } |
||
87 |
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.