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