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 |
||
11 | final class Generator |
||
12 | { |
||
13 | /** @var Layer[] */ |
||
14 | private $layers; |
||
15 | /** @var Primitive[] */ |
||
16 | private $primitives; |
||
17 | /** @var array of placeholders which can be replaced in stub files */ |
||
18 | private $placeholders = []; |
||
19 | |||
20 | /** |
||
21 | * Generator constructor. |
||
22 | * |
||
23 | * @param Layer[] $layers |
||
24 | * @param Primitive[] $primitives |
||
25 | */ |
||
26 | 2 | public function __construct(array $layers, array $primitives) |
|
33 | |||
34 | |||
35 | 2 | private function validate() |
|
43 | |||
44 | /** |
||
45 | * Generate command will generate files from stubs and put them to target folders |
||
46 | * |
||
47 | * |
||
48 | * @param string $layer_name f.e. app or infrastructure |
||
49 | * @param string $primitive_name f.e. event |
||
50 | * @param FQCN $qcn f.e. Some/Namespaced/Name |
||
51 | * |
||
52 | * @return array of generated files |
||
53 | */ |
||
54 | 2 | public function generate($layer_name, $primitive_name, FQCN $qcn): array |
|
64 | |||
65 | /** |
||
66 | * generateDry - prepare map of new final files to source stubs |
||
67 | * |
||
68 | * @param string $layer_name f.e. app or infrastructure |
||
69 | * @param string $primitive_name f.e. event |
||
70 | * @param FQCN $qcn f.e. Some/Namespaced/Name |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 2 | public function generateDry($layer_name, $primitive_name, FQCN $qcn): array |
|
97 | |||
98 | |||
99 | /** |
||
100 | * generateStubs for given primitive in given layer |
||
101 | * |
||
102 | * |
||
103 | * @param Primitive $primitive |
||
104 | * @param Layer $layer |
||
105 | * @param FQCN $qcn |
||
106 | * |
||
107 | * @return array [final_path => stub_file] |
||
108 | */ |
||
109 | 2 | private function generateStubs($primitive, $layer, $qcn): array |
|
151 | |||
152 | /** |
||
153 | * writeStubs to final files |
||
154 | * |
||
155 | * |
||
156 | * @param string $target_path |
||
157 | * @param string $source_file |
||
158 | * |
||
159 | * @return void |
||
160 | */ |
||
161 | 2 | private function writeStubs(string $target_path, string $source_file): void |
|
175 | |||
176 | /** |
||
177 | * getPrimitiveByName |
||
178 | * |
||
179 | * |
||
180 | * @param string $name |
||
181 | * |
||
182 | * @throws \Exception |
||
183 | * |
||
184 | * @return Primitive |
||
185 | */ |
||
186 | 2 | private function getPrimitiveByName(string $name): Primitive |
|
196 | |||
197 | /** |
||
198 | * updatePlaceholders |
||
199 | * |
||
200 | * |
||
201 | * @param array $placeholders |
||
202 | * |
||
203 | * @return void |
||
204 | */ |
||
205 | 2 | private function updatePlaceholders(array $placeholders): void |
|
211 | |||
212 | /** |
||
213 | * replacePlaceholders |
||
214 | * |
||
215 | * |
||
216 | * @param $text |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | 2 | private function replacePlaceholdersInText($text): string |
|
226 | |||
227 | |||
228 | 2 | public function getLayerByName(string $name): Layer |
|
238 | } |
||
239 |
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.