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 |
||
13 | class ScriptHandler |
||
14 | { |
||
15 | const CONFIG_MAIN_KEY = 'css-compiler'; |
||
16 | const OPTION_KEY_INPUT = 'input'; |
||
17 | const OPTION_KEY_OUTPUT = 'output'; |
||
18 | const OPTION_KEY_FORMATTER = 'format'; |
||
19 | const DEFAULT_OPTION_FORMATTER = 'compact'; |
||
20 | protected static $mandatoryOptions = [ |
||
21 | self::OPTION_KEY_INPUT, |
||
22 | self::OPTION_KEY_OUTPUT |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * @param Event $event |
||
27 | * |
||
28 | * @throws \InvalidArgumentException |
||
29 | */ |
||
30 | public static function generateCSS(Event $event) |
||
49 | |||
50 | /** |
||
51 | * @param array $config |
||
52 | * |
||
53 | * @return bool |
||
54 | * @throws \InvalidArgumentException |
||
55 | */ |
||
56 | protected static function validateConfiguration(array $config) |
||
68 | |||
69 | /** |
||
70 | * @param array $config |
||
71 | * |
||
72 | * @return bool |
||
73 | * @throws \InvalidArgumentException |
||
74 | */ |
||
75 | protected static function validateOptions(array $config) |
||
87 | |||
88 | /** |
||
89 | * @param array $config |
||
90 | * |
||
91 | * @return bool |
||
92 | * @throws \InvalidArgumentException |
||
93 | */ |
||
94 | protected static function validateMandatoryOptions(array $config) |
||
113 | |||
114 | /** |
||
115 | * @param array $option |
||
116 | * |
||
117 | * @return bool |
||
118 | */ |
||
119 | View Code Duplication | protected static function validateIsArray($option) |
|
127 | |||
128 | /** |
||
129 | * @param string $option |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | View Code Duplication | protected static function validateIsString($option) |
|
141 | } |
||
142 |
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.