Complex classes like Loader 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 Loader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class Loader |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected array $classes = []; |
||
|
|
|||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected array $debug = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected array $extensions = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected array $files = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | protected bool $isRegistered = false; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected array $namespaces = []; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Loader constructor. |
||
| 79 | */ |
||
| 80 | 13 | public function __construct() |
|
| 81 | { |
||
| 82 | 13 | $this->extensions = ['php']; |
|
| 83 | 13 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Adds a class to the internal collection for the mapping |
||
| 87 | * |
||
| 88 | * @param string $name |
||
| 89 | * @param string $file |
||
| 90 | * |
||
| 91 | * @return Loader |
||
| 92 | */ |
||
| 93 | 2 | public function addClass(string $name, string $file): Loader |
|
| 94 | { |
||
| 95 | 2 | $this->classes[$name] = $file; |
|
| 96 | |||
| 97 | 2 | return $this; |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Adds an extension for the loaded files |
||
| 102 | * |
||
| 103 | * @param string $extension |
||
| 104 | * |
||
| 105 | * @return Loader |
||
| 106 | */ |
||
| 107 | 2 | public function addExtension(string $extension): Loader |
|
| 108 | { |
||
| 109 | 2 | $extensions = $this->extensions; |
|
| 110 | 2 | $extensions[] = $extension; |
|
| 111 | |||
| 112 | 2 | $this->extensions = array_unique($extensions); |
|
| 113 | |||
| 114 | 2 | return $this; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Adds a file to be added to the loader |
||
| 119 | * |
||
| 120 | * @param string $file |
||
| 121 | * |
||
| 122 | * @return Loader |
||
| 123 | */ |
||
| 124 | 2 | public function addFile(string $file): Loader |
|
| 125 | { |
||
| 126 | 2 | $this->files[sha1($file)] = $file; |
|
| 127 | |||
| 128 | 2 | return $this; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $namespace |
||
| 133 | * @param mixed $directories |
||
| 134 | * @param bool $prepend |
||
| 135 | * |
||
| 136 | * @return Loader |
||
| 137 | * @throws Exception |
||
| 138 | */ |
||
| 139 | 6 | public function addNamespace( |
|
| 140 | string $namespace, |
||
| 141 | $directories, |
||
| 142 | bool $prepend = false |
||
| 143 | ): Loader { |
||
| 144 | 6 | $ns = '\\'; |
|
| 145 | 6 | $ds = DIRECTORY_SEPARATOR; |
|
| 146 | 6 | $namespace = trim($namespace, $ns) . $ns; |
|
| 147 | 6 | $directories = $this->checkDirectories($directories); |
|
| 148 | |||
| 149 | // initialize the namespace prefix array if needed |
||
| 150 | 5 | if (!isset($this->namespaces[$namespace])) { |
|
| 151 | 5 | $this->namespaces[$namespace] = []; |
|
| 152 | } |
||
| 153 | |||
| 154 | 5 | $directories = $this->processDirectories($directories, $ds); |
|
| 155 | 5 | $source = ($prepend) ? $directories : $this->namespaces[$namespace]; |
|
| 156 | 5 | $target = ($prepend) ? $this->namespaces[$namespace] : $directories; |
|
| 157 | |||
| 158 | 5 | $this->namespaces[$namespace] = array_unique( |
|
| 159 | 5 | array_merge($source, $target) |
|
| 160 | ); |
||
| 161 | |||
| 162 | 5 | return $this; |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Loads the class based on the class name |
||
| 167 | * |
||
| 168 | * @param string $name |
||
| 169 | * |
||
| 170 | * @return bool|mixed|string |
||
| 171 | */ |
||
| 172 | 6 | public function autoload(string $name) |
|
| 173 | { |
||
| 174 | /** |
||
| 175 | * Debug information |
||
| 176 | */ |
||
| 177 | 6 | $this->debug = ['Loading: ' . $name]; |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Classes |
||
| 181 | */ |
||
| 182 | 6 | if (isset($this->classes[$name])) { |
|
| 183 | 1 | $file = $this->classes[$name]; |
|
| 184 | 1 | $exists = $this->requireFile($file); |
|
| 185 | 1 | if ($exists) { |
|
| 186 | 1 | $this->debug[] = 'Class: load: ' . $file; |
|
| 187 | 1 | return $file; |
|
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | 6 | $this->debug[] = 'Class: 404 : ' . $name; |
|
| 192 | |||
| 193 | 6 | return $this->processLoadNameSpaces($name); |
|
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns the registered classes array |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | 1 | public function getClasses(): array |
|
| 202 | { |
||
| 203 | 1 | return $this->classes; |
|
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Returns an array with debugging information after the last autoload |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | 6 | public function getDebug() |
|
| 212 | { |
||
| 213 | 6 | return $this->debug; |
|
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the registered extensions array |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | 1 | public function getExtensions(): array |
|
| 222 | { |
||
| 223 | 1 | return $this->extensions; |
|
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns the registered files array |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | 1 | public function getFiles(): array |
|
| 232 | { |
||
| 233 | 1 | return array_values($this->files); |
|
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns the registered namespaces array |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | 1 | public function getNamespaces(): array |
|
| 242 | { |
||
| 243 | 1 | return $this->namespaces; |
|
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Checks if a file exists and then adds the file by doing virtual require |
||
| 248 | */ |
||
| 249 | 2 | public function loadFiles(): void |
|
| 250 | { |
||
| 251 | 2 | foreach ($this->files as $file) { |
|
| 252 | /** |
||
| 253 | * Check if the file specified even exists |
||
| 254 | */ |
||
| 255 | 1 | $this->requireFile($file); |
|
| 256 | } |
||
| 257 | 2 | } |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Sets the classes for the loader. Overwrites existing entries |
||
| 261 | * |
||
| 262 | * @param array $classes |
||
| 263 | * |
||
| 264 | * @return Loader |
||
| 265 | */ |
||
| 266 | 1 | public function setClasses(array $classes): Loader |
|
| 267 | { |
||
| 268 | 1 | $this->classes = []; |
|
| 269 | 1 | foreach ($classes as $name => $file) { |
|
| 270 | 1 | $this->addClass($name, $file); |
|
| 271 | } |
||
| 272 | |||
| 273 | 1 | return $this; |
|
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Sets the extensions for the loader. Overwrites existing entries |
||
| 278 | * |
||
| 279 | * @param array $extensions |
||
| 280 | * |
||
| 281 | * @return Loader |
||
| 282 | */ |
||
| 283 | 2 | public function setExtensions(array $extensions): Loader |
|
| 284 | { |
||
| 285 | 2 | $this->extensions = ['php']; |
|
| 286 | 2 | foreach ($extensions as $extension) { |
|
| 287 | 2 | $this->addExtension($extension); |
|
| 288 | } |
||
| 289 | |||
| 290 | 2 | return $this; |
|
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Sets all the files that need to be loaded. Overwrites existing files |
||
| 295 | * |
||
| 296 | * @param array $files |
||
| 297 | * |
||
| 298 | * @return Loader |
||
| 299 | */ |
||
| 300 | 1 | public function setFiles(array $files): Loader |
|
| 301 | { |
||
| 302 | 1 | $this->files = []; |
|
| 303 | 1 | foreach ($files as $file) { |
|
| 304 | 1 | $this->addFile($file); |
|
| 305 | } |
||
| 306 | |||
| 307 | 1 | return $this; |
|
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Sets the namespaces for the loader. Overwrites existing entries |
||
| 312 | * |
||
| 313 | * @param array $namespaces |
||
| 314 | * |
||
| 315 | * @return Loader |
||
| 316 | * @throws Exception |
||
| 317 | */ |
||
| 318 | 3 | public function setNamespaces(array $namespaces): Loader |
|
| 319 | { |
||
| 320 | 3 | $this->namespaces = []; |
|
| 321 | 3 | foreach ($namespaces as $namespace => $directories) { |
|
| 322 | 3 | $this->addNamespace($namespace, $directories); |
|
| 323 | } |
||
| 324 | |||
| 325 | 3 | return $this; |
|
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Registers this autoloader with SPL. |
||
| 330 | * |
||
| 331 | * @param bool $prepend True to prepend to the autoload stack. |
||
| 332 | */ |
||
| 333 | 1 | public function register($prepend = false): void |
|
| 334 | { |
||
| 335 | 1 | if (!$this->isRegistered) { |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Include all files that are registered |
||
| 339 | */ |
||
| 340 | 1 | $this->loadFiles(); |
|
| 341 | |||
| 342 | 1 | spl_autoload_register( |
|
| 343 | 1 | [$this, 'autoload'], |
|
| 344 | 1 | true, |
|
| 345 | 1 | (bool) $prepend |
|
| 346 | ); |
||
| 347 | |||
| 348 | 1 | $this->isRegistered = true; |
|
| 349 | } |
||
| 350 | 1 | } |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Unregisters this autoloader from SPL. |
||
| 354 | */ |
||
| 355 | 1 | public function unregister(): void |
|
| 356 | { |
||
| 357 | 1 | if ($this->isRegistered) { |
|
| 358 | 1 | spl_autoload_unregister([$this, 'autoload']); |
|
| 359 | 1 | $this->isRegistered = false; |
|
| 360 | } |
||
| 361 | 1 | } |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Search for the file corresponding to the namespaced class and load it |
||
| 365 | * |
||
| 366 | * @param string $namespace |
||
| 367 | * @param string $class |
||
| 368 | * |
||
| 369 | * @return bool|string |
||
| 370 | */ |
||
| 371 | 5 | protected function loadFile(string $namespace, string $class) |
|
| 372 | { |
||
| 373 | 5 | if (!isset($this->namespaces[$namespace])) { |
|
| 374 | 3 | $this->debug[] = 'Load: No folders registered: ' . $namespace; |
|
| 375 | |||
| 376 | 3 | return false; |
|
| 377 | } |
||
| 378 | |||
| 379 | 4 | return $this->processFileNameSpaces($namespace, $class); |
|
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * If the file exists, require it and return true; false otherwise |
||
| 384 | * |
||
| 385 | * @param string $file The file to require |
||
| 386 | * |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | 6 | protected function requireFile(string $file): bool |
|
| 390 | { |
||
| 391 | 6 | if (file_exists($file)) { |
|
| 392 | 5 | require $file; |
|
| 393 | |||
| 394 | 5 | return true; |
|
| 395 | } |
||
| 396 | |||
| 397 | 4 | return false; |
|
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param mixed $directories |
||
| 402 | * |
||
| 403 | * @return array |
||
| 404 | * @throws Exception |
||
| 405 | */ |
||
| 406 | 6 | private function checkDirectories($directories): array |
|
| 407 | { |
||
| 408 | 6 | if (!is_string($directories) && !is_array($directories)) { |
|
| 409 | 1 | throw new Exception( |
|
| 410 | 1 | 'The directories parameter is not a string or array' |
|
| 411 | ); |
||
| 412 | } |
||
| 413 | |||
| 414 | 5 | if (is_string($directories)) { |
|
| 415 | 5 | $directories = [$directories]; |
|
| 416 | } |
||
| 417 | |||
| 418 | 5 | return $directories; |
|
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param array $directories |
||
| 423 | * @param string $ds |
||
| 424 | * |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | 5 | private function processDirectories(array $directories, string $ds): array |
|
| 428 | { |
||
| 429 | 5 | foreach ($directories as $key => $directory) { |
|
| 430 | 5 | $directories[$key] = rtrim($directory, $ds) . $ds; |
|
| 431 | } |
||
| 432 | |||
| 433 | 5 | return $directories; |
|
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $namespace |
||
| 438 | * @param string $class |
||
| 439 | * |
||
| 440 | * @return bool|string |
||
| 441 | */ |
||
| 442 | 4 | private function processFileNameSpaces(string $namespace, string $class) |
|
| 443 | { |
||
| 444 | 4 | $ns = '\\'; |
|
| 445 | 4 | $ds = DIRECTORY_SEPARATOR; |
|
| 446 | |||
| 447 | 4 | foreach ($this->namespaces[$namespace] as $directory) { |
|
| 448 | 4 | foreach ($this->extensions as $extension) { |
|
| 449 | 4 | $file = $directory . str_replace($ns, $ds, $class) . '.' . $extension; |
|
| 450 | |||
| 451 | 4 | if ($this->requireFile($file)) { |
|
| 452 | 3 | return $file; |
|
| 453 | } |
||
| 454 | |||
| 455 | 3 | $this->debug[] = "Load: 404 : " . $namespace . " - " . $file; |
|
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | 1 | return false; |
|
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param string $name |
||
| 464 | * |
||
| 465 | * @return bool|string |
||
| 466 | */ |
||
| 467 | 6 | private function processLoadNameSpaces(string $name) |
|
| 468 | { |
||
| 469 | 6 | $ns = '\\'; |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Namespaces |
||
| 473 | * |
||
| 474 | * Matching in reverse the namespace names in relation to class names |
||
| 475 | */ |
||
| 476 | 6 | $namespace = $name; |
|
| 477 | 6 | while (false !== $pos = strrpos($namespace, $ns)) { |
|
| 478 | // retain the trailing namespace separator in the prefix |
||
| 479 | 5 | $namespace = substr($name, 0, $pos + 1); |
|
| 480 | 5 | $remainder = substr($name, $pos + 1); |
|
| 481 | |||
| 482 | 5 | $file = $this->loadFile($namespace, $remainder); |
|
| 483 | 5 | if (false !== $file) { |
|
| 484 | 3 | $this->debug[] = "Namespace: " . $namespace . " - " . $file; |
|
| 485 | |||
| 486 | 3 | return $file; |
|
| 487 | } |
||
| 488 | |||
| 489 | 3 | $namespace = rtrim($namespace, $ns); |
|
| 490 | } |
||
| 491 | |||
| 492 | // 404 |
||
| 493 | 3 | $this->debug[] = "Namespace: 404 : " . $name; |
|
| 494 | |||
| 495 | 3 | return false; |
|
| 496 | } |
||
| 497 | } |
||
| 498 |