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 |
||
9 | class FileGenerator |
||
10 | { |
||
11 | /** |
||
12 | * Output directory filesystem. |
||
13 | * |
||
14 | * @var \League\Flysystem\FilesystemInterface |
||
15 | */ |
||
16 | protected $outbox; |
||
17 | |||
18 | /** |
||
19 | * Stub directory filessytem. |
||
20 | * |
||
21 | * @var \League\Flysystem\FilesystemInterface |
||
22 | */ |
||
23 | protected $stubbox; |
||
24 | |||
25 | /** |
||
26 | * Context of generate. |
||
27 | * |
||
28 | * @var \stdClass |
||
29 | */ |
||
30 | protected $context; |
||
31 | |||
32 | /** |
||
33 | * Create file generator. |
||
34 | * |
||
35 | * @param string $outbox_root_path |
||
36 | * @param string $stubbox_root_path |
||
37 | * |
||
38 | * @return static |
||
39 | */ |
||
40 | 32 | public static function make($outbox_root_path, $stubbox_root_path) |
|
59 | |||
60 | /** |
||
61 | * the Constructor. |
||
62 | * |
||
63 | * @param \Legue\Flysystem\FilesystemInterface $outbox |
||
64 | * @param \Legue\Flysystem\FilesystemInterface $stubbox |
||
65 | * @param \stdClass $context |
||
66 | */ |
||
67 | 33 | public function __construct(FilesystemInterface $outbox, FilesystemInterface $stubbox, stdClass $context) |
|
73 | |||
74 | /** |
||
75 | * Get directory walker. |
||
76 | * |
||
77 | * @param string $path |
||
78 | * @param callable $callable |
||
79 | * |
||
80 | * @return static |
||
81 | */ |
||
82 | 10 | public function directory($path, callable $callable = null) |
|
99 | |||
100 | /** |
||
101 | * Generate sources. |
||
102 | * |
||
103 | * @param string $path |
||
104 | */ |
||
105 | 2 | View Code Duplication | public function sourceDirectory($path) |
116 | |||
117 | /** |
||
118 | * Generate sources from templates. |
||
119 | * |
||
120 | * @param string $path |
||
121 | * @param array $arguments |
||
122 | */ |
||
123 | 2 | View Code Duplication | public function templateDirectory($path, array $arguments = []) |
134 | |||
135 | /** |
||
136 | * Generate blank directory with '.gitkeep'. |
||
137 | * |
||
138 | * @param string $path |
||
139 | * @param string $file |
||
140 | */ |
||
141 | 2 | public function keepDirectory($path, $file = '.gitkeep') |
|
145 | |||
146 | /** |
||
147 | * Set file path. |
||
148 | * |
||
149 | * @param string $path |
||
150 | * @return $this |
||
151 | */ |
||
152 | 27 | public function file($path) |
|
158 | |||
159 | /** |
||
160 | * Check file existing. |
||
161 | * |
||
162 | * @param string $path |
||
163 | * @return bool |
||
164 | */ |
||
165 | 2 | public function exists($path) |
|
169 | |||
170 | /** |
||
171 | * Generate blank file. |
||
172 | */ |
||
173 | 7 | public function blank() |
|
177 | |||
178 | /** |
||
179 | * Generate text file. |
||
180 | * |
||
181 | * @param string $content |
||
182 | * @param array $arguments |
||
183 | */ |
||
184 | 7 | public function text($content, array $arguments = []) |
|
188 | |||
189 | /** |
||
190 | * Generate json file. |
||
191 | * |
||
192 | * @param array $data |
||
193 | */ |
||
194 | 1 | public function json(array $data) |
|
198 | |||
199 | /** |
||
200 | * Generate source file from stub. |
||
201 | * |
||
202 | * @param string $stub_path |
||
203 | */ |
||
204 | 5 | public function source($stub_path) |
|
208 | |||
209 | /** |
||
210 | * Generate source file from template. |
||
211 | * |
||
212 | * @param string $stub_path |
||
213 | * @param array $arguments |
||
214 | */ |
||
215 | 5 | public function template($stub_path, array $arguments = []) |
|
219 | |||
220 | /** |
||
221 | * Generate '.gitkeep' file. |
||
222 | * |
||
223 | * @param string $path |
||
224 | */ |
||
225 | 4 | public function gitKeepFile($path = '.gitkeep') |
|
229 | |||
230 | /** |
||
231 | * Generate PHP blank file. |
||
232 | * |
||
233 | * @param string $path |
||
234 | */ |
||
235 | 1 | public function phpBlankFile($path) |
|
239 | |||
240 | /** |
||
241 | * Generate PHP config file. |
||
242 | * |
||
243 | * @param string $path |
||
244 | * @param string $namespace |
||
245 | */ |
||
246 | 2 | public function phpConfigFile($path, array $config = [], $namespace = null) |
|
250 | |||
251 | /** |
||
252 | * Generate PHP source file. |
||
253 | * |
||
254 | * @param string $path |
||
255 | * @param string $source |
||
256 | * @param string $namespace |
||
257 | */ |
||
258 | 2 | public function phpSourceFile($path, $source, $namespace = '') |
|
266 | |||
267 | /** |
||
268 | * Generate source file, same name. |
||
269 | * |
||
270 | * @param string $path |
||
271 | */ |
||
272 | 1 | public function sourceFile($path) |
|
276 | |||
277 | /** |
||
278 | * Generate template file, same name. |
||
279 | * |
||
280 | * @param string $path |
||
281 | * @param array $arguments |
||
282 | */ |
||
283 | 1 | public function templateFile($path, array $arguments = []) |
|
287 | |||
288 | /** |
||
289 | * Create relative path in box. |
||
290 | * |
||
291 | * @param $path |
||
292 | * @return string |
||
293 | */ |
||
294 | 31 | protected function makePath($path) |
|
298 | |||
299 | /** |
||
300 | * Get all files in directory |
||
301 | * |
||
302 | * @param FilesystemInterface $filesystem |
||
303 | * @param string $path |
||
304 | * @return array |
||
305 | */ |
||
306 | 4 | protected function allFiles(FilesystemInterface $filesystem, $path) |
|
318 | |||
319 | /** |
||
320 | * Generate template |
||
321 | * |
||
322 | * @param string $content |
||
323 | * @param array $arguments |
||
324 | * @return mixed |
||
325 | */ |
||
326 | 5 | protected function generate($content, array $arguments) |
|
337 | |||
338 | /** |
||
339 | * Read stub file |
||
340 | * |
||
341 | * @param string $content |
||
342 | * @param array $arguments |
||
343 | * @return mixed |
||
344 | */ |
||
345 | 10 | protected function read($stub_path) |
|
355 | } |
||
356 |
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.