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 namespace BuildR\ClassLoader\Modules\PSR4; |
||
19 | class PSR4ClassLoaderModule extends AbstractClassLoaderModule { |
||
20 | |||
21 | /** |
||
22 | * @type int |
||
23 | */ |
||
24 | protected $priority = 20; |
||
25 | |||
26 | /** |
||
27 | * @type array |
||
28 | */ |
||
29 | protected $registeredNamespaces = []; |
||
30 | |||
31 | /** |
||
32 | * @inheritDoc |
||
33 | * @codeCoverageIgnore |
||
34 | */ |
||
35 | public static function getName() { |
||
38 | |||
39 | /** |
||
40 | * @inheritDoc |
||
41 | * @codeCoverageIgnore |
||
42 | */ |
||
43 | public function getPriority() { |
||
46 | |||
47 | /** |
||
48 | * @inheritDoc |
||
49 | * @codeCoverageIgnore |
||
50 | */ |
||
51 | public function onRegistered() { |
||
54 | |||
55 | /** |
||
56 | * @inheritDoc |
||
57 | */ |
||
58 | 6 | public function load($className) { |
|
84 | |||
85 | /** |
||
86 | * Register a new PSR-4 compatible namespace to the module |
||
87 | * |
||
88 | * @param string $namespace The namespace name |
||
89 | * @param string $basePath Tha namespace base path |
||
90 | * |
||
91 | * @throws \BuildR\ClassLoader\Modules\PSR4\PSR4ModuleException |
||
92 | */ |
||
93 | 26 | public function registerNamespace($namespace, $basePath) { |
|
103 | |||
104 | /** |
||
105 | * Remove a registered namespace from the module |
||
106 | * |
||
107 | * @param string $namespace The namespace name |
||
108 | */ |
||
109 | 2 | View Code Duplication | public function unRegisterNamespace($namespace) { |
116 | |||
117 | /** |
||
118 | * Determines that the given namespace nem is registered in this module |
||
119 | * |
||
120 | * @param string $namespace The namespace name |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | 26 | View Code Duplication | public function namespaceIsRegistered($namespace) { |
133 | |||
134 | } |
||
135 |
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.