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 ClassLoader 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 ClassLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class ClassLoader |
||
| 44 | { |
||
| 45 | // PSR-4 |
||
| 46 | private $prefixLengthsPsr4 = array(); |
||
| 47 | private $prefixDirsPsr4 = array(); |
||
| 48 | private $fallbackDirsPsr4 = array(); |
||
| 49 | |||
| 50 | // PSR-0 |
||
| 51 | private $prefixesPsr0 = array(); |
||
| 52 | private $fallbackDirsPsr0 = array(); |
||
| 53 | |||
| 54 | private $useIncludePath = false; |
||
| 55 | private $classMap = array(); |
||
| 56 | |||
| 57 | private $classMapAuthoritative = false; |
||
| 58 | |||
| 59 | public function getPrefixes() |
||
| 67 | |||
| 68 | public function getPrefixesPsr4() |
||
| 72 | |||
| 73 | public function getFallbackDirs() |
||
| 77 | |||
| 78 | public function getFallbackDirsPsr4() |
||
| 82 | |||
| 83 | public function getClassMap() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param array $classMap Class to filename map |
||
| 90 | */ |
||
| 91 | public function addClassMap(array $classMap) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Registers a set of PSR-0 directories for a given prefix, either |
||
| 102 | * appending or prepending to the ones previously set for this prefix. |
||
| 103 | * |
||
| 104 | * @param string $prefix The prefix |
||
| 105 | * @param array|string $paths The PSR-0 root directories |
||
| 106 | * @param bool $prepend Whether to prepend the directories |
||
| 107 | */ |
||
| 108 | public function add($prefix, $paths, $prepend = false) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Registers a set of PSR-4 directories for a given namespace, either |
||
| 147 | * appending or prepending to the ones previously set for this namespace. |
||
| 148 | * |
||
| 149 | * @param string $prefix The prefix/namespace, with trailing '\\' |
||
| 150 | * @param array|string $paths The PSR-4 base directories |
||
| 151 | * @param bool $prepend Whether to prepend the directories |
||
| 152 | * |
||
| 153 | * @throws \InvalidArgumentException |
||
| 154 | */ |
||
| 155 | public function addPsr4($prefix, $paths, $prepend = false) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Registers a set of PSR-0 directories for a given prefix, |
||
| 195 | * replacing any others previously set for this prefix. |
||
| 196 | * |
||
| 197 | * @param string $prefix The prefix |
||
| 198 | * @param array|string $paths The PSR-0 base directories |
||
| 199 | */ |
||
| 200 | public function set($prefix, $paths) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Registers a set of PSR-4 directories for a given namespace, |
||
| 211 | * replacing any others previously set for this namespace. |
||
| 212 | * |
||
| 213 | * @param string $prefix The prefix/namespace, with trailing '\\' |
||
| 214 | * @param array|string $paths The PSR-4 base directories |
||
| 215 | * |
||
| 216 | * @throws \InvalidArgumentException |
||
| 217 | */ |
||
| 218 | public function setPsr4($prefix, $paths) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Turns on searching the include path for class files. |
||
| 234 | * |
||
| 235 | * @param bool $useIncludePath |
||
| 236 | */ |
||
| 237 | public function setUseIncludePath($useIncludePath) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Can be used to check if the autoloader uses the include path to check |
||
| 244 | * for classes. |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | public function getUseIncludePath() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Turns off searching the prefix and fallback directories for classes |
||
| 255 | * that have not been registered with the class map. |
||
| 256 | * |
||
| 257 | * @param bool $classMapAuthoritative |
||
| 258 | */ |
||
| 259 | public function setClassMapAuthoritative($classMapAuthoritative) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Should class lookup fail if not found in the current class map? |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function isClassMapAuthoritative() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Registers this instance as an autoloader. |
||
| 276 | * |
||
| 277 | * @param bool $prepend Whether to prepend the autoloader or not |
||
| 278 | */ |
||
| 279 | public function register($prepend = false) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Unregisters this instance as an autoloader. |
||
| 286 | */ |
||
| 287 | public function unregister() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Loads the given class or interface. |
||
| 294 | * |
||
| 295 | * @param string $class The name of the class |
||
| 296 | * @return bool|null True if loaded, null otherwise |
||
| 297 | */ |
||
| 298 | public function loadClass($class) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Finds the path to the file where the class is defined. |
||
| 309 | * |
||
| 310 | * @param string $class The name of the class |
||
| 311 | * |
||
| 312 | * @return string|false The path if found, false otherwise |
||
| 313 | */ |
||
| 314 | public function findFile($class) |
||
| 343 | |||
| 344 | private function findFileWithExtension($class, $ext) |
||
| 403 | } |
||
| 404 | |||
| 414 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.