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 |
||
24 | class Loader implements ILoader |
||
25 | { |
||
26 | /** |
||
27 | * Stores the plugin directories. |
||
28 | * |
||
29 | * @see addDirectory |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $paths = array(); |
||
33 | |||
34 | /** |
||
35 | * Stores the plugins names/paths relationships |
||
36 | * don't edit this on your own, use addDirectory. |
||
37 | * |
||
38 | * @see addDirectory |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $classPath = array(); |
||
42 | |||
43 | /** |
||
44 | * Path where class paths cache files are written. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $cacheDir; |
||
49 | |||
50 | /** |
||
51 | * Path where builtin plugins are stored. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $corePluginDir; |
||
56 | |||
57 | /** |
||
58 | * Loader constructor. |
||
59 | * |
||
60 | * @param $cacheDir |
||
61 | */ |
||
62 | public function __construct($cacheDir) |
||
85 | |||
86 | /** |
||
87 | * Rebuilds class paths, scans the given directory recursively and saves all paths in the given file. |
||
88 | * |
||
89 | * @param string $path the plugin path to scan |
||
90 | * @param string|boolean $cacheFile the file where to store the plugin paths cache, it will be overwritten |
||
91 | * |
||
92 | * @throws Exception |
||
93 | */ |
||
94 | protected function rebuildClassPathCache($path, $cacheFile) |
||
121 | |||
122 | /** |
||
123 | * Loads a plugin file. |
||
124 | * |
||
125 | * @param string $class the plugin name, without the `Plugin` prefix |
||
126 | * @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it |
||
127 | * has just been added, defaults to true |
||
128 | * |
||
129 | * @throws Exception |
||
130 | */ |
||
131 | public function loadPlugin($class, $forceRehash = true) |
||
160 | |||
161 | /** |
||
162 | * Adds a plugin directory, the plugins found in the new plugin directory |
||
163 | * will take precedence over the other directories (including the default |
||
164 | * dwoo plugin directory), you can use this for example to override plugins |
||
165 | * in a specific directory for a specific application while keeping all your |
||
166 | * usual plugins in the same place for all applications. |
||
167 | * TOCOM don't forget that php functions overrides are not rehashed so you |
||
168 | * need to clear the classpath caches by hand when adding those. |
||
169 | * |
||
170 | * @param string $pluginDirectory the plugin path to scan |
||
171 | * |
||
172 | * @throws Exception |
||
173 | */ |
||
174 | public function addDirectory($pluginDirectory) |
||
190 | } |
||
191 |
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.