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 | abstract class AbstractCompiler |
||
11 | { |
||
12 | protected $cachePrefix ; |
||
13 | protected $stubFilename; |
||
14 | |||
15 | protected $stub; |
||
16 | protected $scaffolderConfig ; |
||
17 | protected $modelName ; |
||
18 | protected $modelData ; |
||
19 | protected $stubsDirectory ; |
||
20 | |||
21 | protected $eagerTable ; |
||
22 | |||
23 | const CACHE_EXT = '.scf'; |
||
24 | |||
25 | public function __construct($scaffolderConfig, $modelData = null) |
||
33 | |||
34 | /** |
||
35 | * Compiles . |
||
36 | * |
||
37 | * @param null $extra |
||
38 | * |
||
39 | * @return string |
||
40 | */ |
||
41 | public function compile($extra = null) |
||
57 | |||
58 | /** |
||
59 | * Replace and store the Stub. |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | abstract protected function replaceAndStore(); |
||
64 | |||
65 | /** |
||
66 | * Store the compiled stub. |
||
67 | * |
||
68 | * @param string $eagerTable |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | protected function setEagerTable($eagerTable){ |
||
75 | |||
76 | /** |
||
77 | * Store the compiled stub. |
||
78 | * |
||
79 | * @param FileToCompile $fileToCompile |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | protected function store(FileToCompile $fileToCompile) |
||
100 | |||
101 | /** |
||
102 | * Get output filename |
||
103 | * |
||
104 | * |
||
105 | * @return $this |
||
106 | */ |
||
107 | abstract protected function getOutputFilename(); |
||
108 | |||
109 | /** |
||
110 | * Replace the primary key. |
||
111 | * |
||
112 | * @param $this->modelData |
||
113 | */ |
||
114 | protected function replacePrimaryKey() |
||
123 | |||
124 | View Code Duplication | protected function getPrimaryKeyField(){ |
|
146 | |||
147 | /** |
||
148 | * Replace the class name. |
||
149 | * |
||
150 | * |
||
151 | * @return $this |
||
152 | */ |
||
153 | protected function replaceClassName() |
||
160 | |||
161 | /** |
||
162 | * Replace the table name. |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | protected function replaceTableName() |
||
172 | |||
173 | /** |
||
174 | * Replace the namespace. |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | protected function replaceNamespace() |
||
184 | |||
185 | /** |
||
186 | * Replace the foreign strings by field. |
||
187 | * |
||
188 | * @param stdClass $field |
||
189 | * @param string $originalStubPart |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | protected function replaceForeingStrings($field, $originalStubPart){ |
||
206 | |||
207 | /** |
||
208 | * Replace the fields strings by field. |
||
209 | * |
||
210 | * @param stdClass $field |
||
211 | * @param string $originalStubPart |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | protected function replaceFieldStrings($field, $originalStubPart){ |
||
225 | |||
226 | /** |
||
227 | * Replace the prefix. |
||
228 | * |
||
229 | * @return $this |
||
230 | */ |
||
231 | protected function replaceRoutePrefix() |
||
237 | |||
238 | /** |
||
239 | * get anoter model data |
||
240 | * |
||
241 | * @param string $tableName |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | protected $modelDataArray = []; |
||
246 | protected function getModelData($tableName){ |
||
274 | } |
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.