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 | class IndexModuleCompiler extends AbstractCompiler |
||
12 | { |
||
13 | protected $stubFilename = 'IndexModule.js' ; |
||
14 | |||
15 | protected $stubResourceFilename = 'IndexModuleModel.js' ; |
||
16 | protected $stubResource ; |
||
17 | |||
18 | |||
19 | View Code Duplication | public function __construct($scaffolderConfig, $modelData = null) |
|
26 | |||
27 | /** |
||
28 | * Replace and store the Stub. |
||
29 | * |
||
30 | * @return string |
||
31 | */ |
||
32 | public function replaceAndStore(){} |
||
33 | |||
34 | /** |
||
35 | * Compiles a resource. |
||
36 | * |
||
37 | * @param $hash |
||
38 | * @param null $extra |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | public function compile($extra = null) {} |
||
43 | |||
44 | /** |
||
45 | * Compiles a group of routes. |
||
46 | * |
||
47 | * @param $hash |
||
48 | * @param null $extra |
||
49 | * |
||
50 | * @return mixed |
||
51 | */ |
||
52 | public function compileGroup($compiledIndexes) |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Get output filename |
||
64 | * |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | protected function getOutputFilename() |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Replace the resource. |
||
78 | * |
||
79 | * @param $this->modelName |
||
80 | * |
||
81 | * @return string routeStub |
||
82 | */ |
||
83 | public function replaceResource($modelData) |
||
91 | |||
92 | /** |
||
93 | * Replace compiled routes. |
||
94 | * |
||
95 | * @param $compiledRoutes |
||
96 | * |
||
97 | * @return $this |
||
98 | */ |
||
99 | private function replaceIndexes($compiledIndexes) |
||
105 | |||
106 | |||
107 | } |
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.