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 |
||
| 45 | class PluginsPluginHandler extends XoopsPersistableObjectHandler |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @param null|Connection $db database |
||
| 49 | */ |
||
| 50 | public function __construct(Connection $db = null) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $listener |
||
| 57 | * @param string $caller |
||
| 58 | * @return bool|PluginsPlugin |
||
| 59 | */ |
||
| 60 | public function getLC($listener, $caller) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param string $listener |
||
| 76 | * @return array Array of PluginsPlugin |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function getByListener($listener) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $caller |
||
| 89 | * @return array Array of PluginsPlugin |
||
| 90 | */ |
||
| 91 | View Code Duplication | public function getByCaller($caller) |
|
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * @return array Array of PluginsPlugin |
||
| 103 | */ |
||
| 104 | public function getThemAll() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return array Array of Listeners |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function getListeners() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @return array Array of Callers |
||
| 131 | */ |
||
| 132 | View Code Duplication | public function getCallers() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Gets the module name but checks if it is active or not |
||
| 148 | * There is a preload that calls this method before deleting deactivated module entries |
||
| 149 | * |
||
| 150 | * @param string $dirname |
||
| 151 | * @return mixed |
||
| 152 | */ |
||
| 153 | public function getModuleName(string $dirname) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $listener |
||
| 164 | * @param string $caller |
||
| 165 | * @param int $status |
||
| 166 | * @param int $order |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function addNew($listener, $caller, $status = 1, $order = 0) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Updates the order value after a post request |
||
| 182 | * |
||
| 183 | * @param int $id |
||
| 184 | * @param int $order |
||
| 185 | */ |
||
| 186 | public function updateOrder(int $id, int $order) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Updates the status value after a post request |
||
| 193 | * |
||
| 194 | * @param int $id |
||
| 195 | * @param int $status |
||
| 196 | */ |
||
| 197 | public function updateStatus(int $id, int $status) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get Listeners By Caller |
||
| 204 | * Check if the module is active in case it was deactivated |
||
| 205 | * |
||
| 206 | * @param string $caller |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | public function getActiveListenersByCaller(string $caller) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Deletes all entries by name |
||
| 230 | * Useful when a module is deleted |
||
| 231 | * |
||
| 232 | * @param string $name |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | public function deleteLC(string $name) |
||
| 242 | } |
||
| 243 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: