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 |
||
10 | class FileDateMigrationCreator extends Creator implements MigrationCreator |
||
11 | { |
||
12 | /** |
||
13 | * Create a migration file. |
||
14 | * |
||
15 | * @param string $name |
||
16 | * @param string $create |
||
17 | * |
||
18 | * @return string |
||
19 | */ |
||
20 | public function create($name, $create = false) |
||
39 | |||
40 | /** |
||
41 | * Return StudlyCase class name. |
||
42 | * |
||
43 | * @param string $name |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | protected function getClassName($name) |
||
51 | |||
52 | /** |
||
53 | * Create necessary directories for migrations. |
||
54 | */ |
||
55 | View Code Duplication | protected function createDirectories() |
|
66 | |||
67 | /** |
||
68 | * Get stub with appropriate class name/table name. |
||
69 | * |
||
70 | * @param string $className |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | protected function getStub($className, $create) |
||
82 | |||
83 | /** |
||
84 | * Populate stub with class name and table name. |
||
85 | * |
||
86 | * @param string $stub |
||
87 | * @param string $className |
||
88 | * @param string $create |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | protected function populateStub($stub, $className, $create) |
||
100 | |||
101 | /** |
||
102 | * Get the full path to save file to. |
||
103 | * |
||
104 | * @param string $name |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | protected function getSavePath($name) |
||
114 | |||
115 | /** |
||
116 | * Build file name for migration. |
||
117 | * |
||
118 | * @param string $name |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | protected function buildFileName($name) |
||
126 | |||
127 | /** |
||
128 | * Get the date prefix for the migration. |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | protected function getDatePrefix() |
||
136 | } |
||
137 |
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.