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 |
||
7 | View Code Duplication | class ValidatorGenerator extends Generator |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * Get stub name. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $stub = 'validator/validator'; |
||
15 | |||
16 | /** |
||
17 | * Get root namespace. |
||
18 | * |
||
19 | * @return string |
||
20 | */ |
||
21 | public function getRootNamespace() |
||
25 | |||
26 | /** |
||
27 | * Get generator path config node. |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | public function getPathConfigNode() |
||
35 | |||
36 | /** |
||
37 | * Get destination path for generated file. |
||
38 | * |
||
39 | * @return string |
||
40 | */ |
||
41 | public function getPath() |
||
45 | |||
46 | /** |
||
47 | * Get base path of destination file. |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | public function getBasePath() |
||
55 | |||
56 | /** |
||
57 | * Get array replacements. |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | public function getReplacements() |
||
67 | |||
68 | /** |
||
69 | * Get the rules. |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function getRules() |
||
86 | |||
87 | /** |
||
88 | * Get schema parser. |
||
89 | * |
||
90 | * @return RulesParser |
||
91 | */ |
||
92 | public function getSchemaParser() |
||
96 | } |
||
97 |
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.