| Total Complexity | 64 |
| Total Lines | 391 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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.
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 | /** @var \Closure(string):void */ |
||
| 46 | private static $includeFile; |
||
| 47 | |||
| 48 | /** @var ?string */ |
||
| 49 | private $vendorDir; |
||
| 50 | |||
| 51 | // PSR-4 |
||
| 52 | /** |
||
| 53 | * @var array[] |
||
| 54 | * @psalm-var array<string, array<string, int>> |
||
| 55 | */ |
||
| 56 | private $prefixLengthsPsr4 = array(); |
||
| 57 | /** |
||
| 58 | * @var array[] |
||
| 59 | * @psalm-var array<string, array<int, string>> |
||
| 60 | */ |
||
| 61 | private $prefixDirsPsr4 = array(); |
||
| 62 | /** |
||
| 63 | * @var array[] |
||
| 64 | * @psalm-var array<string, string> |
||
| 65 | */ |
||
| 66 | private $fallbackDirsPsr4 = array(); |
||
| 67 | |||
| 68 | // PSR-0 |
||
| 69 | /** |
||
| 70 | * @var array[] |
||
| 71 | * @psalm-var array<string, array<string, string[]>> |
||
| 72 | */ |
||
| 73 | private $prefixesPsr0 = array(); |
||
| 74 | /** |
||
| 75 | * @var array[] |
||
| 76 | * @psalm-var array<string, string> |
||
| 77 | */ |
||
| 78 | private $fallbackDirsPsr0 = array(); |
||
| 79 | |||
| 80 | /** @var bool */ |
||
| 81 | private $useIncludePath = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string[] |
||
| 85 | * @psalm-var array<string, string> |
||
| 86 | */ |
||
| 87 | private $classMap = array(); |
||
| 88 | |||
| 89 | /** @var bool */ |
||
| 90 | private $classMapAuthoritative = false; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var bool[] |
||
| 94 | * @psalm-var array<string, bool> |
||
|
|
|||
| 95 | */ |
||
| 96 | private $missingClasses = array(); |
||
| 97 | |||
| 98 | /** @var ?string */ |
||
| 99 | private $apcuPrefix; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var self[] |
||
| 103 | */ |
||
| 104 | private static $registeredLoaders = array(); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param ?string $vendorDir |
||
| 108 | */ |
||
| 109 | public function __construct($vendorDir = null) |
||
| 110 | { |
||
| 111 | $this->vendorDir = $vendorDir; |
||
| 112 | self::initializeIncludeClosure(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string[] |
||
| 117 | */ |
||
| 118 | public function getPrefixes() |
||
| 119 | { |
||
| 120 | if (!empty($this->prefixesPsr0)) { |
||
| 121 | return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
||
| 122 | } |
||
| 123 | |||
| 124 | return array(); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return array[] |
||
| 129 | * @psalm-return array<string, array<int, string>> |
||
| 130 | */ |
||
| 131 | public function getPrefixesPsr4() |
||
| 132 | { |
||
| 133 | return $this->prefixDirsPsr4; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return array[] |
||
| 138 | * @psalm-return array<string, string> |
||
| 139 | */ |
||
| 140 | public function getFallbackDirs() |
||
| 141 | { |
||
| 142 | return $this->fallbackDirsPsr0; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return array[] |
||
| 147 | * @psalm-return array<string, string> |
||
| 148 | */ |
||
| 149 | public function getFallbackDirsPsr4() |
||
| 150 | { |
||
| 151 | return $this->fallbackDirsPsr4; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return string[] Array of classname => path |
||
| 156 | * @psalm-return array<string, string> |
||
| 157 | */ |
||
| 158 | public function getClassMap() |
||
| 159 | { |
||
| 160 | return $this->classMap; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string[] $classMap Class to filename map |
||
| 165 | * @psalm-param array<string, string> $classMap |
||
| 166 | * |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | public function addClassMap(array $classMap) |
||
| 170 | { |
||
| 171 | if ($this->classMap) { |
||
| 172 | $this->classMap = array_merge($this->classMap, $classMap); |
||
| 173 | } else { |
||
| 174 | $this->classMap = $classMap; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Registers a set of PSR-0 directories for a given prefix, either |
||
| 180 | * appending or prepending to the ones previously set for this prefix. |
||
| 181 | * |
||
| 182 | * @param string $prefix The prefix |
||
| 183 | * @param string[]|string $paths The PSR-0 root directories |
||
| 184 | * @param bool $prepend Whether to prepend the directories |
||
| 185 | * |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | public function add($prefix, $paths, $prepend = false) |
||
| 189 | { |
||
| 190 | if (!$prefix) { |
||
| 191 | if ($prepend) { |
||
| 192 | $this->fallbackDirsPsr0 = array_merge( |
||
| 193 | (array) $paths, |
||
| 194 | $this->fallbackDirsPsr0 |
||
| 195 | ); |
||
| 196 | } else { |
||
| 197 | $this->fallbackDirsPsr0 = array_merge( |
||
| 198 | $this->fallbackDirsPsr0, |
||
| 199 | (array) $paths |
||
| 200 | ); |
||
| 201 | } |
||
| 202 | |||
| 203 | return; |
||
| 204 | } |
||
| 205 | |||
| 206 | $first = $prefix[0]; |
||
| 207 | if (!isset($this->prefixesPsr0[$first][$prefix])) { |
||
| 208 | $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
||
| 209 | |||
| 210 | return; |
||
| 211 | } |
||
| 212 | if ($prepend) { |
||
| 213 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
||
| 214 | (array) $paths, |
||
| 215 | $this->prefixesPsr0[$first][$prefix] |
||
| 216 | ); |
||
| 217 | } else { |
||
| 218 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
||
| 219 | $this->prefixesPsr0[$first][$prefix], |
||
| 220 | (array) $paths |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Registers a set of PSR-4 directories for a given namespace, either |
||
| 227 | * appending or prepending to the ones previously set for this namespace. |
||
| 228 | * |
||
| 229 | * @param string $prefix The prefix/namespace, with trailing '\\' |
||
| 230 | * @param string[]|string $paths The PSR-4 base directories |
||
| 231 | * @param bool $prepend Whether to prepend the directories |
||
| 232 | * |
||
| 233 | * @throws \InvalidArgumentException |
||
| 234 | * |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | public function addPsr4($prefix, $paths, $prepend = false) |
||
| 238 | { |
||
| 239 | if (!$prefix) { |
||
| 240 | // Register directories for the root namespace. |
||
| 241 | if ($prepend) { |
||
| 242 | $this->fallbackDirsPsr4 = array_merge( |
||
| 243 | (array) $paths, |
||
| 244 | $this->fallbackDirsPsr4 |
||
| 245 | ); |
||
| 246 | } else { |
||
| 247 | $this->fallbackDirsPsr4 = array_merge( |
||
| 248 | $this->fallbackDirsPsr4, |
||
| 249 | (array) $paths |
||
| 250 | ); |
||
| 251 | } |
||
| 252 | } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
||
| 253 | // Register directories for a new namespace. |
||
| 254 | $length = strlen($prefix); |
||
| 255 | if ('\\' !== $prefix[$length - 1]) { |
||
| 256 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
||
| 257 | } |
||
| 258 | $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
||
| 259 | $this->prefixDirsPsr4[$prefix] = (array) $paths; |
||
| 260 | } elseif ($prepend) { |
||
| 261 | // Prepend directories for an already registered namespace. |
||
| 262 | $this->prefixDirsPsr4[$prefix] = array_merge( |
||
| 263 | (array) $paths, |
||
| 264 | $this->prefixDirsPsr4[$prefix] |
||
| 265 | ); |
||
| 266 | } else { |
||
| 267 | // Append directories for an already registered namespace. |
||
| 268 | $this->prefixDirsPsr4[$prefix] = array_merge( |
||
| 269 | $this->prefixDirsPsr4[$prefix], |
||
| 270 | (array) $paths |
||
| 271 | ); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Registers a set of PSR-0 directories for a given prefix, |
||
| 277 | * replacing any others previously set for this prefix. |
||
| 278 | * |
||
| 279 | * @param string $prefix The prefix |
||
| 280 | * @param string[]|string $paths The PSR-0 base directories |
||
| 281 | * |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | public function set($prefix, $paths) |
||
| 285 | { |
||
| 286 | if (!$prefix) { |
||
| 287 | $this->fallbackDirsPsr0 = (array) $paths; |
||
| 288 | } else { |
||
| 289 | $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Registers a set of PSR-4 directories for a given namespace, |
||
| 295 | * replacing any others previously set for this namespace. |
||
| 296 | * |
||
| 297 | * @param string $prefix The prefix/namespace, with trailing '\\' |
||
| 298 | * @param string[]|string $paths The PSR-4 base directories |
||
| 299 | * |
||
| 300 | * @throws \InvalidArgumentException |
||
| 303 | */ |
||
| 304 | public function setPsr4($prefix, $paths) |
||
| 305 | { |
||
| 306 | if (!$prefix) { |
||
| 307 | $this->fallbackDirsPsr4 = (array) $paths; |
||
| 308 | } else { |
||
| 309 | $length = strlen($prefix); |
||
| 310 | if ('\\' !== $prefix[$length - 1]) { |
||
| 311 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
||
| 312 | } |
||
| 313 | $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
||
| 314 | $this->prefixDirsPsr4[$prefix] = (array) $paths; |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Turns on searching the include path for class files. |
||
| 325 | public function setUseIncludePath($useIncludePath) |
||
| 326 | { |
||
| 327 | $this->useIncludePath = $useIncludePath; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Can be used to check if the autoloader uses the include path to check |
||
| 332 | * for classes. |
||
| 333 | * |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public function getUseIncludePath() |
||
| 337 | { |
||
| 338 | return $this->useIncludePath; |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Turns off searching the prefix and fallback directories for classes |
||
| 343 | * that have not been registered with the class map. |
||
| 344 | * |
||
| 345 | * @param bool $classMapAuthoritative |
||
| 346 | * |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | public function setClassMapAuthoritative($classMapAuthoritative) |
||
| 350 | { |
||
| 351 | $this->classMapAuthoritative = $classMapAuthoritative; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Should class lookup fail if not found in the current class map? |
||
| 356 | * |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | public function isClassMapAuthoritative() |
||
| 360 | { |
||
| 361 | return $this->classMapAuthoritative; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
||
| 366 | * |
||
| 367 | * @param string|null $apcuPrefix |
||
| 368 | * |
||
| 369 | * @return void |
||
| 370 | */ |
||
| 434 | return true; |
||
| 435 | } |
||
| 436 | |||
| 437 | return null; |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Finds the path to the file where the class is defined. |
||
| 582 |
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.