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 |
||
15 | class BlockGenerator extends AbstractContentGenerator implements LineGeneratorDependentInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var boolean |
||
19 | */ |
||
20 | private $addIndented; |
||
21 | |||
22 | /** |
||
23 | * @var array|BlockGenerator[]|LineGenerator[] |
||
24 | */ |
||
25 | private $content = []; |
||
26 | |||
27 | /** |
||
28 | * @var LineGenerator |
||
29 | */ |
||
30 | private $lineGenerator; |
||
31 | |||
32 | /** |
||
33 | * @param LineGenerator $lineGenerator |
||
34 | * @param Indention $indention |
||
35 | * @param null|string|array|GeneratorInterface $content |
||
36 | */ |
||
37 | public function __construct(LineGenerator $lineGenerator, Indention $indention, $content = null) |
||
43 | |||
44 | /** |
||
45 | * @param LineGenerator $generator |
||
46 | * @return $this |
||
47 | */ |
||
48 | public function setLineGenerator(LineGenerator $generator) |
||
54 | |||
55 | /** |
||
56 | * @param Indention $indention |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function setIndention(Indention $indention) |
||
66 | |||
67 | /** |
||
68 | * @param array|GeneratorInterface|string $content |
||
69 | * @return $this |
||
70 | * @throws InvalidArgumentException |
||
71 | * @todo rename to addContent, easy up this method by working on todo in generate method |
||
72 | */ |
||
73 | public function add($content) |
||
93 | |||
94 | /** |
||
95 | * @return $this |
||
96 | */ |
||
97 | public function clear() |
||
103 | |||
104 | /** |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function hasContent() |
||
111 | |||
112 | /** |
||
113 | * @throws InvalidArgumentException|RuntimeException |
||
114 | * @return string |
||
115 | * @todo implement support for string|array beside AbstractContentGenerator in $this->content |
||
116 | */ |
||
117 | public function generate() |
||
140 | |||
141 | /** |
||
142 | * @return int |
||
143 | */ |
||
144 | public function count() |
||
148 | |||
149 | /** |
||
150 | * @todo find better name |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function startIndention() |
||
160 | |||
161 | /** |
||
162 | * @todo find better name |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function stopIndention() |
||
174 | |||
175 | /** |
||
176 | * @param null|string $content |
||
177 | * @return LineGenerator |
||
178 | */ |
||
179 | final protected function getLineGenerator($content = null) |
||
189 | } |
||
190 |
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.