Skip to content

Commit 3899a36

Browse files
committed
refactor: Fix phpstan errors related to Autoloader
1 parent a9fa3d0 commit 3899a36

14 files changed

+43
-212
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,6 @@
77
'count' => 1,
88
'path' => __DIR__ . '/app/Config/Filters.php',
99
];
10-
$ignoreErrors[] = [
11-
// identifier: function.alreadyNarrowedType
12-
'message' => '#^Call to function method_exists\\(\\) with \'Composer\\\\\\\\InstalledVersions\' and \'getAllRawData\' will always evaluate to true\\.$#',
13-
'count' => 1,
14-
'path' => __DIR__ . '/system/Autoloader/Autoloader.php',
15-
];
16-
$ignoreErrors[] = [
17-
// identifier: missingType.iterableValue
18-
'message' => '#^Method CodeIgniter\\\\Autoloader\\\\Autoloader\\:\\:loadComposerNamespaces\\(\\) has parameter \\$composerPackages with no value type specified in iterable type array\\.$#',
19-
'count' => 1,
20-
'path' => __DIR__ . '/system/Autoloader/Autoloader.php',
21-
];
22-
$ignoreErrors[] = [
23-
// identifier: isset.property
24-
'message' => '#^Property Config\\\\Autoload\\:\\:\\$helpers \\(array\\<int, string\\>\\) in isset\\(\\) is not nullable\\.$#',
25-
'count' => 1,
26-
'path' => __DIR__ . '/system/Autoloader/Autoloader.php',
27-
];
28-
$ignoreErrors[] = [
29-
// identifier: ternary.condNotBoolean
30-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\|string given\\.$#',
31-
'count' => 1,
32-
'path' => __DIR__ . '/system/Autoloader/FileLocator.php',
33-
];
34-
$ignoreErrors[] = [
35-
// identifier: ternary.shortNotAllowed
36-
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
37-
'count' => 6,
38-
'path' => __DIR__ . '/system/Autoloader/FileLocator.php',
39-
];
40-
$ignoreErrors[] = [
41-
// identifier: missingType.iterableValue
42-
'message' => '#^Method CodeIgniter\\\\Autoloader\\\\FileLocatorCached\\:\\:search\\(\\) return type has no value type specified in iterable type array\\.$#',
43-
'count' => 1,
44-
'path' => __DIR__ . '/system/Autoloader/FileLocatorCached.php',
45-
];
46-
$ignoreErrors[] = [
47-
// identifier: missingType.iterableValue
48-
'message' => '#^Property CodeIgniter\\\\Autoloader\\\\FileLocatorCached\\:\\:\\$cache type has no value type specified in iterable type array\\.$#',
49-
'count' => 1,
50-
'path' => __DIR__ . '/system/Autoloader/FileLocatorCached.php',
51-
];
52-
$ignoreErrors[] = [
53-
// identifier: missingType.iterableValue
54-
'message' => '#^Method CodeIgniter\\\\Autoloader\\\\FileLocatorInterface\\:\\:search\\(\\) return type has no value type specified in iterable type array\\.$#',
55-
'count' => 1,
56-
'path' => __DIR__ . '/system/Autoloader/FileLocatorInterface.php',
57-
];
5810
$ignoreErrors[] = [
5911
// identifier: missingType.iterableValue
6012
'message' => '#^Method CodeIgniter\\\\BaseModel\\:\\:__call\\(\\) has parameter \\$params with no value type specified in iterable type array\\.$#',
@@ -1297,18 +1249,6 @@
12971249
'count' => 1,
12981250
'path' => __DIR__ . '/system/Config/Factories.php',
12991251
];
1300-
$ignoreErrors[] = [
1301-
// identifier: booleanNot.exprNotBoolean
1302-
'message' => '#^Only booleans are allowed in a negated boolean, array given\\.$#',
1303-
'count' => 1,
1304-
'path' => __DIR__ . '/system/Config/Factories.php',
1305-
];
1306-
$ignoreErrors[] = [
1307-
// identifier: booleanNot.exprNotBoolean
1308-
'message' => '#^Only booleans are allowed in a negated boolean, string\\|null given\\.$#',
1309-
'count' => 2,
1310-
'path' => __DIR__ . '/system/Config/Factories.php',
1311-
];
13121252
$ignoreErrors[] = [
13131253
// identifier: missingType.iterableValue
13141254
'message' => '#^Property CodeIgniter\\\\Config\\\\Factory\\:\\:\\$default type has no value type specified in iterable type array\\.$#',
@@ -9241,12 +9181,6 @@
92419181
'count' => 1,
92429182
'path' => __DIR__ . '/system/Test/CIUnitTestCase.php',
92439183
];
9244-
$ignoreErrors[] = [
9245-
// identifier: missingType.callable
9246-
'message' => '#^Method CodeIgniter\\\\Test\\\\CIUnitTestCase\\:\\:getPrivateMethodInvoker\\(\\) return type has no signature specified for Closure\\.$#',
9247-
'count' => 1,
9248-
'path' => __DIR__ . '/system/Test/CIUnitTestCase.php',
9249-
];
92509184
$ignoreErrors[] = [
92519185
// identifier: missingType.return
92529186
'message' => '#^Method CodeIgniter\\\\Test\\\\CIUnitTestCase\\:\\:mockCache\\(\\) has no return type specified\\.$#',
@@ -9277,12 +9211,6 @@
92779211
'count' => 1,
92789212
'path' => __DIR__ . '/system/Test/CIUnitTestCase.php',
92799213
];
9280-
$ignoreErrors[] = [
9281-
// identifier: missingType.return
9282-
'message' => '#^Method CodeIgniter\\\\Test\\\\CIUnitTestCase\\:\\:setPrivateProperty\\(\\) has no return type specified\\.$#',
9283-
'count' => 1,
9284-
'path' => __DIR__ . '/system/Test/CIUnitTestCase.php',
9285-
];
92869214
$ignoreErrors[] = [
92879215
// identifier: booleanNot.exprNotBoolean
92889216
'message' => '#^Only booleans are allowed in a negated boolean, CodeIgniter\\\\CodeIgniter given\\.$#',
@@ -11821,36 +11749,6 @@
1182111749
'count' => 1,
1182211750
'path' => __DIR__ . '/tests/system/AutoReview/FrameworkCodeTest.php',
1182311751
];
11824-
$ignoreErrors[] = [
11825-
// identifier: missingType.callable
11826-
'message' => '#^Method CodeIgniter\\\\Autoloader\\\\AutoloaderTest\\:\\:getPrivateMethodInvoker\\(\\) return type has no signature specified for Closure\\.$#',
11827-
'count' => 1,
11828-
'path' => __DIR__ . '/tests/system/Autoloader/AutoloaderTest.php',
11829-
];
11830-
$ignoreErrors[] = [
11831-
// identifier: missingType.return
11832-
'message' => '#^Method CodeIgniter\\\\Autoloader\\\\AutoloaderTest\\:\\:setPrivateProperty\\(\\) has no return type specified\\.$#',
11833-
'count' => 1,
11834-
'path' => __DIR__ . '/tests/system/Autoloader/AutoloaderTest.php',
11835-
];
11836-
$ignoreErrors[] = [
11837-
// identifier: ternary.shortNotAllowed
11838-
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
11839-
'count' => 2,
11840-
'path' => __DIR__ . '/tests/system/Autoloader/AutoloaderTest.php',
11841-
];
11842-
$ignoreErrors[] = [
11843-
// identifier: method.notFound
11844-
'message' => '#^Call to an undefined method CodeIgniter\\\\Autoloader\\\\FileLocatorInterface\\:\\:__destruct\\(\\)\\.$#',
11845-
'count' => 1,
11846-
'path' => __DIR__ . '/tests/system/Autoloader/FileLocatorCachedTest.php',
11847-
];
11848-
$ignoreErrors[] = [
11849-
// identifier: method.notFound
11850-
'message' => '#^Call to an undefined method CodeIgniter\\\\Autoloader\\\\FileLocatorInterface\\:\\:deleteCache\\(\\)\\.$#',
11851-
'count' => 1,
11852-
'path' => __DIR__ . '/tests/system/Autoloader/FileLocatorCachedTest.php',
11853-
];
1185411752
$ignoreErrors[] = [
1185511753
// identifier: codeigniter.superglobalAccessAssign
1185611754
'message' => '#^Assigning 3 directly on offset \'argc\' of \\$_SERVER is discouraged\\.$#',
@@ -13141,24 +13039,12 @@
1314113039
'count' => 1,
1314213040
'path' => __DIR__ . '/tests/system/Database/Builder/WhereTest.php',
1314313041
];
13144-
$ignoreErrors[] = [
13145-
// identifier: missingType.callable
13146-
'message' => '#^Method CodeIgniter\\\\Database\\\\ConfigTest\\:\\:getPrivateMethodInvoker\\(\\) return type has no signature specified for Closure\\.$#',
13147-
'count' => 1,
13148-
'path' => __DIR__ . '/tests/system/Database/ConfigTest.php',
13149-
];
1315013042
$ignoreErrors[] = [
1315113043
// identifier: missingType.iterableValue
1315213044
'message' => '#^Method CodeIgniter\\\\Database\\\\ConfigTest\\:\\:provideConvertDSN\\(\\) return type has no value type specified in iterable type iterable\\.$#',
1315313045
'count' => 1,
1315413046
'path' => __DIR__ . '/tests/system/Database/ConfigTest.php',
1315513047
];
13156-
$ignoreErrors[] = [
13157-
// identifier: missingType.return
13158-
'message' => '#^Method CodeIgniter\\\\Database\\\\ConfigTest\\:\\:setPrivateProperty\\(\\) has no return type specified\\.$#',
13159-
'count' => 1,
13160-
'path' => __DIR__ . '/tests/system/Database/ConfigTest.php',
13161-
];
1316213048
$ignoreErrors[] = [
1316313049
// identifier: missingType.iterableValue
1316413050
'message' => '#^Property CodeIgniter\\\\Database\\\\ConfigTest\\:\\:\\$dsnGroup type has no value type specified in iterable type array\\.$#',
@@ -13501,18 +13387,6 @@
1350113387
'count' => 1,
1350213388
'path' => __DIR__ . '/tests/system/Debug/ExceptionsTest.php',
1350313389
];
13504-
$ignoreErrors[] = [
13505-
// identifier: missingType.callable
13506-
'message' => '#^Method CodeIgniter\\\\Debug\\\\ExceptionsTest\\:\\:getPrivateMethodInvoker\\(\\) return type has no signature specified for Closure\\.$#',
13507-
'count' => 1,
13508-
'path' => __DIR__ . '/tests/system/Debug/ExceptionsTest.php',
13509-
];
13510-
$ignoreErrors[] = [
13511-
// identifier: missingType.return
13512-
'message' => '#^Method CodeIgniter\\\\Debug\\\\ExceptionsTest\\:\\:setPrivateProperty\\(\\) has no return type specified\\.$#',
13513-
'count' => 1,
13514-
'path' => __DIR__ . '/tests/system/Debug/ExceptionsTest.php',
13515-
];
1351613390
$ignoreErrors[] = [
1351713391
// identifier: argument.type
1351813392
'message' => '#^Parameter \\#2 \\$callable of method CodeIgniter\\\\Debug\\\\Timer\\:\\:record\\(\\) expects callable\\(\\)\\: mixed, \'strlen\' given\\.$#',
@@ -13651,12 +13525,6 @@
1365113525
'count' => 1,
1365213526
'path' => __DIR__ . '/tests/system/Entity/EntityTest.php',
1365313527
];
13654-
$ignoreErrors[] = [
13655-
// identifier: missingType.callable
13656-
'message' => '#^Method CodeIgniter\\\\Entity\\\\EntityTest\\:\\:getPrivateMethodInvoker\\(\\) return type has no signature specified for Closure\\.$#',
13657-
'count' => 1,
13658-
'path' => __DIR__ . '/tests/system/Entity/EntityTest.php',
13659-
];
1366013528
$ignoreErrors[] = [
1366113529
// identifier: missingType.return
1366213530
'message' => '#^Method CodeIgniter\\\\Entity\\\\EntityTest\\:\\:getSimpleSwappedEntity\\(\\) has no return type specified\\.$#',
@@ -13669,12 +13537,6 @@
1366913537
'count' => 1,
1367013538
'path' => __DIR__ . '/tests/system/Entity/EntityTest.php',
1367113539
];
13672-
$ignoreErrors[] = [
13673-
// identifier: missingType.return
13674-
'message' => '#^Method CodeIgniter\\\\Entity\\\\EntityTest\\:\\:setPrivateProperty\\(\\) has no return type specified\\.$#',
13675-
'count' => 1,
13676-
'path' => __DIR__ . '/tests/system/Entity/EntityTest.php',
13677-
];
1367813540
$ignoreErrors[] = [
1367913541
// identifier: codeigniter.configArgumentInstanceof
1368013542
'message' => '#^Argument \\#1 \\$name \\(\'Modules\'\\) passed to function config does not extend CodeIgniter\\\\\\\\Config\\\\\\\\BaseConfig\\.$#',
@@ -15979,18 +15841,6 @@
1597915841
'count' => 1,
1598015842
'path' => __DIR__ . '/tests/system/Models/InsertModelTest.php',
1598115843
];
15982-
$ignoreErrors[] = [
15983-
// identifier: missingType.callable
15984-
'message' => '#^Method CodeIgniter\\\\Models\\\\LiveModelTestCase\\:\\:getPrivateMethodInvoker\\(\\) return type has no signature specified for Closure\\.$#',
15985-
'count' => 1,
15986-
'path' => __DIR__ . '/tests/system/Models/LiveModelTestCase.php',
15987-
];
15988-
$ignoreErrors[] = [
15989-
// identifier: missingType.return
15990-
'message' => '#^Method CodeIgniter\\\\Models\\\\LiveModelTestCase\\:\\:setPrivateProperty\\(\\) has no return type specified\\.$#',
15991-
'count' => 1,
15992-
'path' => __DIR__ . '/tests/system/Models/LiveModelTestCase.php',
15993-
];
1599415844
$ignoreErrors[] = [
1599515845
// identifier: property.nonObject
1599615846
'message' => '#^Cannot access property \\$created_at on array\\.$#',
@@ -17041,18 +16891,6 @@
1704116891
'count' => 1,
1704216892
'path' => __DIR__ . '/tests/system/Session/Handlers/Database/AbstractHandlerTestCase.php',
1704316893
];
17044-
$ignoreErrors[] = [
17045-
// identifier: missingType.callable
17046-
'message' => '#^Method CodeIgniter\\\\Session\\\\Handlers\\\\Database\\\\AbstractHandlerTestCase\\:\\:getPrivateMethodInvoker\\(\\) return type has no signature specified for Closure\\.$#',
17047-
'count' => 1,
17048-
'path' => __DIR__ . '/tests/system/Session/Handlers/Database/AbstractHandlerTestCase.php',
17049-
];
17050-
$ignoreErrors[] = [
17051-
// identifier: missingType.return
17052-
'message' => '#^Method CodeIgniter\\\\Session\\\\Handlers\\\\Database\\\\AbstractHandlerTestCase\\:\\:setPrivateProperty\\(\\) has no return type specified\\.$#',
17053-
'count' => 1,
17054-
'path' => __DIR__ . '/tests/system/Session/Handlers/Database/AbstractHandlerTestCase.php',
17055-
];
1705616894
$ignoreErrors[] = [
1705716895
// identifier: missingType.return
1705816896
'message' => '#^Method CodeIgniter\\\\Session\\\\Handlers\\\\Database\\\\MySQLiHandlerTest\\:\\:getInstance\\(\\) has no return type specified\\.$#',
@@ -18325,18 +18163,6 @@
1832518163
'count' => 1,
1832618164
'path' => __DIR__ . '/tests/system/Validation/ValidationTest.php',
1832718165
];
18328-
$ignoreErrors[] = [
18329-
// identifier: assign.propertyType
18330-
'message' => '#^Property CodeIgniter\\\\View\\\\DecoratorsTest\\:\\:\\$loader \\(CodeIgniter\\\\Autoloader\\\\FileLocator\\) does not accept CodeIgniter\\\\Autoloader\\\\FileLocatorInterface\\.$#',
18331-
'count' => 1,
18332-
'path' => __DIR__ . '/tests/system/View/DecoratorsTest.php',
18333-
];
18334-
$ignoreErrors[] = [
18335-
// identifier: assign.propertyType
18336-
'message' => '#^Property CodeIgniter\\\\View\\\\ParserFilterTest\\:\\:\\$loader \\(CodeIgniter\\\\Autoloader\\\\FileLocator\\) does not accept CodeIgniter\\\\Autoloader\\\\FileLocatorInterface\\.$#',
18337-
'count' => 1,
18338-
'path' => __DIR__ . '/tests/system/View/ParserFilterTest.php',
18339-
];
1834018166
$ignoreErrors[] = [
1834118167
// identifier: missingType.return
1834218168
'message' => '#^Method CodeIgniter\\\\View\\\\ParserPluginTest\\:\\:setHints\\(\\) has no return type specified\\.$#',
@@ -18367,12 +18193,6 @@
1836718193
'count' => 3,
1836818194
'path' => __DIR__ . '/tests/system/View/ParserTest.php',
1836918195
];
18370-
$ignoreErrors[] = [
18371-
// identifier: assign.propertyType
18372-
'message' => '#^Property CodeIgniter\\\\View\\\\ParserTest\\:\\:\\$loader \\(CodeIgniter\\\\Autoloader\\\\FileLocator\\) does not accept CodeIgniter\\\\Autoloader\\\\FileLocatorInterface\\.$#',
18373-
'count' => 1,
18374-
'path' => __DIR__ . '/tests/system/View/ParserTest.php',
18375-
];
1837618196
$ignoreErrors[] = [
1837718197
// identifier: method.notFound
1837818198
'message' => '#^Call to an undefined method CodeIgniter\\\\View\\\\Table\\:\\:compileTemplate\\(\\)\\.$#',
@@ -18463,11 +18283,5 @@
1846318283
'count' => 1,
1846418284
'path' => __DIR__ . '/tests/system/View/TableTest.php',
1846518285
];
18466-
$ignoreErrors[] = [
18467-
// identifier: assign.propertyType
18468-
'message' => '#^Property CodeIgniter\\\\View\\\\ViewTest\\:\\:\\$loader \\(CodeIgniter\\\\Autoloader\\\\FileLocator\\) does not accept CodeIgniter\\\\Autoloader\\\\FileLocatorInterface\\.$#',
18469-
'count' => 1,
18470-
'path' => __DIR__ . '/tests/system/View/ViewTest.php',
18471-
];
1847218286

1847318287
return ['parameters' => ['ignoreErrors' => $ignoreErrors]];

system/Autoloader/Autoloader.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function initialize(Autoload $config, Modules $modules)
123123
$this->files = $config->files;
124124
}
125125

126-
if (isset($config->helpers)) {
126+
if ($config->helpers !== []) {
127127
$this->helpers = [...$this->helpers, ...$config->helpers];
128128
}
129129

@@ -366,6 +366,9 @@ public function sanitizeFilename(string $filename): string
366366
return $cleanFilename;
367367
}
368368

369+
/**
370+
* @param array{only?: list<string>, exclude?: list<string>} $composerPackages
371+
*/
369372
private function loadComposerNamespaces(ClassLoader $composer, array $composerPackages): void
370373
{
371374
$namespacePaths = $composer->getPrefixesPsr4();
@@ -379,6 +382,7 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa
379382
}
380383
}
381384

385+
// @phpstan-ignore-next-line
382386
if (! method_exists(InstalledVersions::class, 'getAllRawData')) {
383387
throw new RuntimeException(
384388
'Your Composer version is too old.'

system/Autoloader/FileLocator.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,11 @@ public function getClassname(string $file): string
145145

146146
if ((isset($tokens[$i - 2][1]) && ($tokens[$i - 2][1] === 'phpnamespace' || $tokens[$i - 2][1] === 'namespace')) || ($dlm && $tokens[$i - 1][0] === T_NS_SEPARATOR && $token[0] === T_STRING)) {
147147
if (! $dlm) {
148-
$namespace = 0;
148+
$namespace = '';
149149
}
150+
150151
if (isset($token[1])) {
151-
$namespace = $namespace ? $namespace . '\\' . $token[1] : $token[1];
152+
$namespace = $namespace !== '' ? $namespace . '\\' . $token[1] : $token[1];
152153
$dlm = true;
153154
}
154155
} elseif ($dlm && ($token[0] !== T_NS_SEPARATOR) && ($token[0] !== T_STRING)) {
@@ -195,7 +196,7 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =
195196
foreach ($this->getNamespaces() as $namespace) {
196197
if (isset($namespace['path']) && is_file($namespace['path'] . $path)) {
197198
$fullPath = $namespace['path'] . $path;
198-
$fullPath = realpath($fullPath) ?: $fullPath;
199+
$fullPath = realpath($fullPath) !== false ? realpath($fullPath) : $fullPath;
199200

200201
if ($prioritizeApp) {
201202
$foundPaths[] = $fullPath;
@@ -272,14 +273,14 @@ protected function getNamespaces()
272273
*/
273274
public function findQualifiedNameFromPath(string $path)
274275
{
275-
$path = realpath($path) ?: $path;
276+
$path = realpath($path) !== false ? realpath($path) : $path;
276277

277278
if (! is_file($path)) {
278279
return false;
279280
}
280281

281282
foreach ($this->getNamespaces() as $namespace) {
282-
$namespace['path'] = realpath($namespace['path']) ?: $namespace['path'];
283+
$namespace['path'] = realpath($namespace['path']) !== false ? realpath($namespace['path']) : $namespace['path'];
283284

284285
if ($namespace['path'] === '') {
285286
continue;
@@ -332,7 +333,7 @@ public function listFiles(string $path): array
332333

333334
foreach ($this->getNamespaces() as $namespace) {
334335
$fullPath = $namespace['path'] . $path;
335-
$fullPath = realpath($fullPath) ?: $fullPath;
336+
$fullPath = realpath($fullPath) !== false ? realpath($fullPath) : $fullPath;
336337

337338
if (! is_dir($fullPath)) {
338339
continue;
@@ -366,7 +367,7 @@ public function listNamespaceFiles(string $prefix, string $path): array
366367
// autoloader->getNamespace($prefix) returns an array of paths for that namespace
367368
foreach ($this->autoloader->getNamespace($prefix) as $namespacePath) {
368369
$fullPath = rtrim($namespacePath, '/') . '/' . $path;
369-
$fullPath = realpath($fullPath) ?: $fullPath;
370+
$fullPath = realpath($fullPath) !== false ? realpath($fullPath) : $fullPath;
370371

371372
if (! is_dir($fullPath)) {
372373
continue;
@@ -393,7 +394,7 @@ public function listNamespaceFiles(string $prefix, string $path): array
393394
protected function legacyLocate(string $file, ?string $folder = null)
394395
{
395396
$path = APPPATH . ($folder === null ? $file : $folder . '/' . $file);
396-
$path = realpath($path) ?: $path;
397+
$path = realpath($path) !== false ? realpath($path) : $path;
397398

398399
if (is_file($path)) {
399400
return $path;

0 commit comments

Comments
 (0)