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:
Complex classes like Module often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Module, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | */ |
||
| 12 | class Module { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Storage of cur requested module |
||
| 16 | * |
||
| 17 | * @var Module |
||
| 18 | */ |
||
| 19 | public static $cur = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Module name |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | public $name = ''; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Module config |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | public $config = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Module info |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | public $info = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Requested module params |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | public $params = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Module directory path |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | public $path = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Module app |
||
| 58 | * |
||
| 59 | * @var App |
||
| 60 | */ |
||
| 61 | public $app = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Parse cur module |
||
| 65 | * |
||
| 66 | * @param App $app |
||
| 67 | */ |
||
| 68 | public function __construct($app) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get all posible directorys for module files |
||
| 85 | * |
||
| 86 | * @param string $moduleName |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public static function getModulePaths($moduleName) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Return directory where places module file |
||
| 102 | * |
||
| 103 | * @param string $moduleName |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public static function getModulePath($moduleName) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Check module for installed |
||
| 118 | * |
||
| 119 | * @param string $moduleName |
||
| 120 | * @param \Inji\App $app |
||
| 121 | * @return boolean |
||
| 122 | */ |
||
| 123 | public static function installed($moduleName, $app) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get installed modules for app |
||
| 132 | * |
||
| 133 | * @param \Inji\App $app |
||
| 134 | * @param App $primary |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public static function getInstalled($app, $primary = false) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Find module controllers |
||
| 150 | * |
||
| 151 | * @param string $moduleName |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | public static function getModuleControllers($moduleName) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Find module by request |
||
| 177 | * |
||
| 178 | * @param \Inji\App $app |
||
| 179 | * @param array|null $params |
||
| 180 | * @return \Inji\Module |
||
| 181 | */ |
||
| 182 | public static function resolveModule($app, $params = null) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get posible path for controller |
||
| 205 | * |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public function getPossibleControllers() { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Find controller by request |
||
| 221 | * |
||
| 222 | * @return \Inji\Controller |
||
| 223 | */ |
||
| 224 | public function findController() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Return module info |
||
| 247 | * |
||
| 248 | * @param string $moduleName |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | public static function getInfo($moduleName = '') { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Return snippets by name |
||
| 268 | * |
||
| 269 | * @param string $snippetsPath |
||
| 270 | * @param boolean $extensions |
||
| 271 | * @param string $dir |
||
| 272 | * @param string $moduleName |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | public function getSnippets($snippetsPath, $extensions = true, $dir = '/snippets', $moduleName = '') { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return module objects |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function getObjects($filterNamespace = '') { |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Return module models |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | View Code Duplication | public static function getModels($moduleName, $filterNamespace = '') { |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Return extensions for type |
||
| 368 | * |
||
| 369 | * @param string $extensionType |
||
| 370 | * @param string $request |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | public function getExtensions($extensionType, $request) { |
||
| 382 | |||
| 383 | public function checkDbMigration() { |
||
| 394 | |||
| 395 | public function sitemap() { |
||
| 398 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: