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 |
||
9 | class ProfileReader implements FormattersDefinition |
||
10 | { |
||
11 | const |
||
12 | TEMPLATE_SUFFIX_INDEX = 'suffix', |
||
13 | MASTER_FILENAME_INDEX = 'master', |
||
14 | CONFIGURATION_DIRECTORY_INDEX = 'confDir', |
||
15 | SOURCE_PATH_INDEX = 'sourcePath', |
||
16 | TARGET_PATH_INDEX = 'targetPath', |
||
17 | DEFAULT_FORMATTER_INDEX = 'defaultFormatter', |
||
18 | FORMATTERS_INDEX = 'formatters', |
||
19 | FILE_EXTENSION_FORMATTERS_INDEX = 'fileExtensionFormatters', |
||
20 | GENERATOR_INDEX = 'generator'; |
||
21 | |||
22 | private |
||
23 | $attributes; |
||
24 | |||
25 | public function __construct(Filesystem $fs) |
||
41 | |||
42 | private function read(Filesystem $fs) |
||
51 | |||
52 | private function processProfileContent($content) |
||
78 | |||
79 | public function hasTemplatesSuffix() |
||
83 | |||
84 | public function getTemplatesSuffix() |
||
88 | |||
89 | public function hasMasterFilename() |
||
93 | |||
94 | public function getMasterFilename() |
||
98 | |||
99 | public function hasConfigurationDirectory() |
||
103 | |||
104 | public function getConfigurationDirectory() |
||
108 | |||
109 | public function hasSourcePath() |
||
113 | |||
114 | public function getSourcePath() |
||
118 | |||
119 | public function hasTargetPath() |
||
123 | |||
124 | public function getTargetPath() |
||
128 | |||
129 | public function getDefaultFormatterName() |
||
133 | |||
134 | public function getFormatters() |
||
138 | |||
139 | public function getFileExtensionFormatters() |
||
143 | |||
144 | public function getGeneratorOptions() |
||
148 | |||
149 | private function has($attributeName) |
||
153 | |||
154 | private function hasString($attributeName) |
||
158 | |||
159 | View Code Duplication | private function get($attributeName) |
|
170 | |||
171 | View Code Duplication | private function getString($attributeName) |
|
182 | |||
183 | private function ensureParameterFormatIsValid($parameter, $value) |
||
204 | } |
||
205 |
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.