42
42
*/
43
43
class ClassLoader
44
44
{
45
+ /** @var ?string */
45
46
private $ vendorDir ;
46
47
47
48
// PSR-4
49
+ /**
50
+ * @var array[]
51
+ * @psalm-var array<string, array<string, int>>
52
+ */
48
53
private $ prefixLengthsPsr4 = array ();
54
+ /**
55
+ * @var array[]
56
+ * @psalm-var array<string, array<int, string>>
57
+ */
49
58
private $ prefixDirsPsr4 = array ();
59
+ /**
60
+ * @var array[]
61
+ * @psalm-var array<string, string>
62
+ */
50
63
private $ fallbackDirsPsr4 = array ();
51
64
52
65
// PSR-0
66
+ /**
67
+ * @var array[]
68
+ * @psalm-var array<string, array<string, string[]>>
69
+ */
53
70
private $ prefixesPsr0 = array ();
71
+ /**
72
+ * @var array[]
73
+ * @psalm-var array<string, string>
74
+ */
54
75
private $ fallbackDirsPsr0 = array ();
55
76
77
+ /** @var bool */
56
78
private $ useIncludePath = false ;
79
+
80
+ /**
81
+ * @var string[]
82
+ * @psalm-var array<string, string>
83
+ */
57
84
private $ classMap = array ();
85
+
86
+ /** @var bool */
58
87
private $ classMapAuthoritative = false ;
88
+
89
+ /**
90
+ * @var bool[]
91
+ * @psalm-var array<string, bool>
92
+ */
59
93
private $ missingClasses = array ();
94
+
95
+ /** @var ?string */
60
96
private $ apcuPrefix ;
61
97
98
+ /**
99
+ * @var self[]
100
+ */
62
101
private static $ registeredLoaders = array ();
63
102
103
+ /**
104
+ * @param ?string $vendorDir
105
+ */
64
106
public function __construct ($ vendorDir = null )
65
107
{
66
108
$ this ->vendorDir = $ vendorDir ;
67
109
}
68
110
111
+ /**
112
+ * @return string[]
113
+ */
69
114
public function getPrefixes ()
70
115
{
71
116
if (!empty ($ this ->prefixesPsr0 )) {
@@ -75,28 +120,47 @@ public function getPrefixes()
75
120
return array ();
76
121
}
77
122
123
+ /**
124
+ * @return array[]
125
+ * @psalm-return array<string, array<int, string>>
126
+ */
78
127
public function getPrefixesPsr4 ()
79
128
{
80
129
return $ this ->prefixDirsPsr4 ;
81
130
}
82
131
132
+ /**
133
+ * @return array[]
134
+ * @psalm-return array<string, string>
135
+ */
83
136
public function getFallbackDirs ()
84
137
{
85
138
return $ this ->fallbackDirsPsr0 ;
86
139
}
87
140
141
+ /**
142
+ * @return array[]
143
+ * @psalm-return array<string, string>
144
+ */
88
145
public function getFallbackDirsPsr4 ()
89
146
{
90
147
return $ this ->fallbackDirsPsr4 ;
91
148
}
92
149
150
+ /**
151
+ * @return string[] Array of classname => path
152
+ * @psalm-return array<string, string>
153
+ */
93
154
public function getClassMap ()
94
155
{
95
156
return $ this ->classMap ;
96
157
}
97
158
98
159
/**
99
- * @param array $classMap Class to filename map
160
+ * @param string[] $classMap Class to filename map
161
+ * @psalm-param array<string, string> $classMap
162
+ *
163
+ * @return void
100
164
*/
101
165
public function addClassMap (array $ classMap )
102
166
{
@@ -111,9 +175,11 @@ public function addClassMap(array $classMap)
111
175
* Registers a set of PSR-0 directories for a given prefix, either
112
176
* appending or prepending to the ones previously set for this prefix.
113
177
*
114
- * @param string $prefix The prefix
115
- * @param array|string $paths The PSR-0 root directories
116
- * @param bool $prepend Whether to prepend the directories
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
117
183
*/
118
184
public function add ($ prefix , $ paths , $ prepend = false )
119
185
{
@@ -156,11 +222,13 @@ public function add($prefix, $paths, $prepend = false)
156
222
* Registers a set of PSR-4 directories for a given namespace, either
157
223
* appending or prepending to the ones previously set for this namespace.
158
224
*
159
- * @param string $prefix The prefix/namespace, with trailing '\\'
160
- * @param array |string $paths The PSR-4 base directories
161
- * @param bool $prepend Whether to prepend the directories
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
162
228
*
163
229
* @throws \InvalidArgumentException
230
+ *
231
+ * @return void
164
232
*/
165
233
public function addPsr4 ($ prefix , $ paths , $ prepend = false )
166
234
{
@@ -204,8 +272,10 @@ public function addPsr4($prefix, $paths, $prepend = false)
204
272
* Registers a set of PSR-0 directories for a given prefix,
205
273
* replacing any others previously set for this prefix.
206
274
*
207
- * @param string $prefix The prefix
208
- * @param array|string $paths The PSR-0 base directories
275
+ * @param string $prefix The prefix
276
+ * @param string[]|string $paths The PSR-0 base directories
277
+ *
278
+ * @return void
209
279
*/
210
280
public function set ($ prefix , $ paths )
211
281
{
@@ -220,10 +290,12 @@ public function set($prefix, $paths)
220
290
* Registers a set of PSR-4 directories for a given namespace,
221
291
* replacing any others previously set for this namespace.
222
292
*
223
- * @param string $prefix The prefix/namespace, with trailing '\\'
224
- * @param array |string $paths The PSR-4 base directories
293
+ * @param string $prefix The prefix/namespace, with trailing '\\'
294
+ * @param string[] |string $paths The PSR-4 base directories
225
295
*
226
296
* @throws \InvalidArgumentException
297
+ *
298
+ * @return void
227
299
*/
228
300
public function setPsr4 ($ prefix , $ paths )
229
301
{
@@ -243,6 +315,8 @@ public function setPsr4($prefix, $paths)
243
315
* Turns on searching the include path for class files.
244
316
*
245
317
* @param bool $useIncludePath
318
+ *
319
+ * @return void
246
320
*/
247
321
public function setUseIncludePath ($ useIncludePath )
248
322
{
@@ -265,6 +339,8 @@ public function getUseIncludePath()
265
339
* that have not been registered with the class map.
266
340
*
267
341
* @param bool $classMapAuthoritative
342
+ *
343
+ * @return void
268
344
*/
269
345
public function setClassMapAuthoritative ($ classMapAuthoritative )
270
346
{
@@ -285,6 +361,8 @@ public function isClassMapAuthoritative()
285
361
* APCu prefix to use to cache found/not-found classes, if the extension is enabled.
286
362
*
287
363
* @param string|null $apcuPrefix
364
+ *
365
+ * @return void
288
366
*/
289
367
public function setApcuPrefix ($ apcuPrefix )
290
368
{
@@ -305,14 +383,18 @@ public function getApcuPrefix()
305
383
* Registers this instance as an autoloader.
306
384
*
307
385
* @param bool $prepend Whether to prepend the autoloader or not
386
+ *
387
+ * @return void
308
388
*/
309
389
public function register ($ prepend = false )
310
390
{
311
391
spl_autoload_register (array ($ this , 'loadClass ' ), true , $ prepend );
312
392
313
393
if (null === $ this ->vendorDir ) {
314
- //no-op
315
- } elseif ($ prepend ) {
394
+ return ;
395
+ }
396
+
397
+ if ($ prepend ) {
316
398
self ::$ registeredLoaders = array ($ this ->vendorDir => $ this ) + self ::$ registeredLoaders ;
317
399
} else {
318
400
unset(self ::$ registeredLoaders [$ this ->vendorDir ]);
@@ -322,6 +404,8 @@ public function register($prepend = false)
322
404
323
405
/**
324
406
* Unregisters this instance as an autoloader.
407
+ *
408
+ * @return void
325
409
*/
326
410
public function unregister ()
327
411
{
@@ -336,7 +420,7 @@ public function unregister()
336
420
* Loads the given class or interface.
337
421
*
338
422
* @param string $class The name of the class
339
- * @return bool |null True if loaded, null otherwise
423
+ * @return true |null True if loaded, null otherwise
340
424
*/
341
425
public function loadClass ($ class )
342
426
{
@@ -345,6 +429,8 @@ public function loadClass($class)
345
429
346
430
return true ;
347
431
}
432
+
433
+ return null ;
348
434
}
349
435
350
436
/**
@@ -399,6 +485,11 @@ public static function getRegisteredLoaders()
399
485
return self ::$ registeredLoaders ;
400
486
}
401
487
488
+ /**
489
+ * @param string $class
490
+ * @param string $ext
491
+ * @return string|false
492
+ */
402
493
private function findFileWithExtension ($ class , $ ext )
403
494
{
404
495
// PSR-4 lookup
@@ -470,6 +561,10 @@ private function findFileWithExtension($class, $ext)
470
561
* Scope isolated include.
471
562
*
472
563
* Prevents access to $this/self from included files.
564
+ *
565
+ * @param string $file
566
+ * @return void
567
+ * @private
473
568
*/
474
569
function includeFile ($ file )
475
570
{
0 commit comments