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 |
||
19 | class ConfigurationLoader |
||
20 | { |
||
21 | /** |
||
22 | * Create the configuration loader based the current shell environment variables. |
||
23 | * |
||
24 | * @param string $configFilename |
||
25 | * @param Version $version |
||
26 | * @return ConfigurationLoader |
||
27 | * @codeCoverageIgnore |
||
28 | */ |
||
29 | public static function fromEnv($configFilename, Version $version) |
||
47 | |||
48 | protected $sourceFiles = array(); |
||
49 | protected $plugins = array(); |
||
50 | protected $configFilename; |
||
51 | protected $configLocator; |
||
52 | protected $loader; |
||
53 | |||
54 | |||
55 | |||
56 | /** |
||
57 | * Construct the loader. |
||
58 | * |
||
59 | * @param string $configFilename |
||
60 | * @param \Symfony\Component\Config\FileLocatorInterface $configLocator |
||
61 | * @param FileLoader $loader |
||
62 | */ |
||
63 | 5 | public function __construct($configFilename, FileLocatorInterface $configLocator, FileLoader $loader) |
|
69 | |||
70 | /** |
||
71 | * Add a plugin on-the-fly |
||
72 | * |
||
73 | * @param string $name |
||
74 | * @return void |
||
75 | */ |
||
76 | public function addPlugin($name) |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Processes the configuration contents |
||
84 | * |
||
85 | * @return array |
||
86 | * |
||
87 | * @throws \UnexpectedValueException |
||
88 | */ |
||
89 | 4 | public function processConfiguration() |
|
122 | |||
123 | |||
124 | /** |
||
125 | * Load the specified plugin instance. |
||
126 | * |
||
127 | * @param string $name |
||
128 | * @param string $file |
||
129 | * @return void |
||
130 | * |
||
131 | * @throws \UnexpectedValueException |
||
132 | */ |
||
133 | 2 | protected function loadPlugin($name, $file) |
|
143 | |||
144 | |||
145 | /** |
||
146 | * Returns all plugins registered while loading. |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | 1 | public function getPlugins() |
|
154 | |||
155 | /** |
||
156 | * @return array |
||
157 | */ |
||
158 | public function getSourceFiles() |
||
162 | } |
||
163 |
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.