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 |
||
8 | class SeederCreator extends Creator |
||
9 | { |
||
10 | /** |
||
11 | * Create a seeder file with the given name. |
||
12 | * |
||
13 | * @param string $name |
||
14 | * |
||
15 | * @throws WriteError |
||
16 | * |
||
17 | * @return string |
||
18 | */ |
||
19 | public function create($name) |
||
20 | { |
||
21 | $path = $this->config->getSeedDirectory($name.'.php'); |
||
22 | |||
23 | if (!file_exists($path)) { |
||
24 | $this->createDirectories(); |
||
25 | |||
26 | $this->writeFile($path, $this->getStub($name)); |
||
27 | |||
28 | $this->output->writeInfo("Created seeder {$name}."); |
||
29 | |||
30 | return $path; |
||
31 | } |
||
32 | |||
33 | throw WriteError::commandExists($name); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Create necessary directory structure. |
||
38 | */ |
||
39 | View Code Duplication | protected function createDirectories() |
|
|
|||
40 | { |
||
41 | $seedDir = $this->config->getSeedDirectory(); |
||
42 | |||
43 | $created = $this->makeDirectoryStructure([ |
||
44 | 'database' => $this->config->getDatabaseDirectory(), |
||
45 | 'seeds' => $seedDir, |
||
46 | ]); |
||
47 | |||
48 | foreach ($created as $key => $value) { |
||
49 | $this->output->writeInfo("Created {$key} directory."); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Get the stub and insert the given class name. |
||
55 | * |
||
56 | * @param string $name |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | public function getStub($name) |
||
66 | } |
||
67 |
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.