Total Complexity | 82 |
Total Lines | 322 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DebugClassLoader 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 DebugClassLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class DebugClassLoader |
||
26 | { |
||
27 | private $classLoader; |
||
28 | private $isFinder; |
||
29 | private $wasFinder; |
||
30 | private static $caseCheck; |
||
31 | private static $deprecated = array(); |
||
32 | private static $php7Reserved = array('int', 'float', 'bool', 'string', 'true', 'false', 'null'); |
||
33 | private static $darwinCache = array('/' => array('/', array())); |
||
34 | |||
35 | /** |
||
36 | * Constructor. |
||
37 | * |
||
38 | * @param callable|object $classLoader Passing an object is @deprecated since version 2.5 and support for it will be removed in 3.0 |
||
39 | */ |
||
40 | public function __construct($classLoader) |
||
41 | { |
||
42 | $this->wasFinder = is_object($classLoader) && method_exists($classLoader, 'findFile'); |
||
43 | |||
44 | if ($this->wasFinder) { |
||
45 | @trigger_error('The '.__METHOD__.' method will no longer support receiving an object into its $classLoader argument in 3.0.', E_USER_DEPRECATED); |
||
46 | $this->classLoader = array($classLoader, 'loadClass'); |
||
47 | $this->isFinder = true; |
||
48 | } else { |
||
49 | $this->classLoader = $classLoader; |
||
50 | $this->isFinder = is_array($classLoader) && method_exists($classLoader[0], 'findFile'); |
||
51 | } |
||
52 | |||
53 | if (!isset(self::$caseCheck)) { |
||
54 | $file = file_exists(__FILE__) ? __FILE__ : rtrim(realpath('.'), DIRECTORY_SEPARATOR); |
||
55 | $i = strrpos($file, DIRECTORY_SEPARATOR); |
||
56 | $dir = substr($file, 0, 1 + $i); |
||
57 | $file = substr($file, 1 + $i); |
||
58 | $test = strtoupper($file) === $file ? strtolower($file) : strtoupper($file); |
||
59 | $test = realpath($dir.$test); |
||
60 | |||
61 | if (false === $test || false === $i) { |
||
62 | // filesystem is case sensitive |
||
63 | self::$caseCheck = 0; |
||
64 | } elseif (substr($test, -strlen($file)) === $file) { |
||
65 | // filesystem is case insensitive and realpath() normalizes the case of characters |
||
66 | self::$caseCheck = 1; |
||
67 | } elseif (false !== stripos(PHP_OS, 'darwin')) { |
||
68 | // on MacOSX, HFS+ is case insensitive but realpath() doesn't normalize the case of characters |
||
69 | self::$caseCheck = 2; |
||
70 | } else { |
||
71 | // filesystem case checks failed, fallback to disabling them |
||
72 | self::$caseCheck = 0; |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Gets the wrapped class loader. |
||
79 | * |
||
80 | * @return callable|object A class loader. Since version 2.5, returning an object is @deprecated and support for it will be removed in 3.0 |
||
81 | */ |
||
82 | public function getClassLoader() |
||
83 | { |
||
84 | return $this->wasFinder ? $this->classLoader[0] : $this->classLoader; |
||
|
|||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Wraps all autoloaders. |
||
89 | */ |
||
90 | public static function enable() |
||
91 | { |
||
92 | // Ensures we don't hit https://bugs.php.net/42098 |
||
93 | class_exists('Symfony\Component\Debug\ErrorHandler'); |
||
94 | class_exists('Psr\Log\LogLevel'); |
||
95 | |||
96 | if (!is_array($functions = spl_autoload_functions())) { |
||
97 | return; |
||
98 | } |
||
99 | |||
100 | foreach ($functions as $function) { |
||
101 | spl_autoload_unregister($function); |
||
102 | } |
||
103 | |||
104 | foreach ($functions as $function) { |
||
105 | if (!is_array($function) || !$function[0] instanceof self) { |
||
106 | $function = array(new static($function), 'loadClass'); |
||
107 | } |
||
108 | |||
109 | spl_autoload_register($function); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Disables the wrapping. |
||
115 | */ |
||
116 | public static function disable() |
||
117 | { |
||
118 | if (!is_array($functions = spl_autoload_functions())) { |
||
119 | return; |
||
120 | } |
||
121 | |||
122 | foreach ($functions as $function) { |
||
123 | spl_autoload_unregister($function); |
||
124 | } |
||
125 | |||
126 | foreach ($functions as $function) { |
||
127 | if (is_array($function) && $function[0] instanceof self) { |
||
128 | $function = $function[0]->getClassLoader(); |
||
129 | } |
||
130 | |||
131 | spl_autoload_register($function); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Finds a file by class name. |
||
137 | * |
||
138 | * @param string $class A class name to resolve to file |
||
139 | * |
||
140 | * @return string|null |
||
141 | * |
||
142 | * @deprecated since version 2.5, to be removed in 3.0. |
||
143 | */ |
||
144 | public function findFile($class) |
||
150 | } |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Loads the given class or interface. |
||
155 | * |
||
156 | * @param string $class The name of the class |
||
157 | * |
||
158 | * @return bool|null True, if loaded |
||
159 | * |
||
160 | * @throws \RuntimeException |
||
161 | */ |
||
162 | public function loadClass($class) |
||
347 | } |
||
348 | } |
||
350 |