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 |
||
| 3 | class Plugins { |
||
|
|
|||
| 4 | private $globalPlugins; |
||
| 5 | |||
| 6 | public function __construct($globalPlugins=array()) { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Returns the plugin configurations found from plugin folders inside the plugins folder |
||
| 12 | * @return array |
||
| 13 | */ |
||
| 14 | private function getPlugins() |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Returns the plugin configurations found from plugin folders |
||
| 29 | * inside the plugins folder filtered by filetype. |
||
| 30 | * @param string $type filetype e.g. 'css', 'js' or 'template' |
||
| 31 | * @return array |
||
| 32 | */ |
||
| 33 | private function filterPlugins($type) { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Returns the plugin configurations found from plugin folders |
||
| 51 | * inside the plugins folder filtered by plugin name (the folder name). |
||
| 52 | * @param string $type filetype e.g. 'css', 'js' or 'template' |
||
| 53 | * @param array $names the plugin name strings (foldernames) in an array |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | private function filterPluginsByName($type, $names) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns an array of javascript filepaths |
||
| 68 | * @param array $names the plugin name strings (foldernames) in an array |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function getPluginsJS($names=null) { |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Returns an array of css filepaths |
||
| 81 | * @param array $names the plugin name strings (foldernames) in an array |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function getPluginsCSS($names=null) { |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Returns an array of template filepaths |
||
| 94 | * @param array $names the plugin name strings (foldernames) in an array |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function getPluginsTemplates($names=null) { |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Returns an array of template files contents as strings |
||
| 107 | * @param array $names the plugin name strings (foldernames) in an array |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | public function getTemplates($names=null) { |
||
| 124 | } |
||
| 125 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.