Skip to content

Commit eab4542

Browse files
committed
Include custom autoloaders registered in composer files
1 parent 99acc02 commit eab4542

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

bin/phpstan

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ use Symfony\Component\Console\Helper\ProgressBar;
7272

7373
$autoloaderInWorkingDirectory = getcwd() . '/vendor/autoload.php';
7474
$composerAutoloaderProjectPaths = [];
75+
76+
/** @var array<callable>|false $autoloadFunctionsBefore */
77+
$autoloadFunctionsBefore = spl_autoload_functions();
78+
7579
if (is_file($autoloaderInWorkingDirectory)) {
7680
$composerAutoloaderProjectPaths[] = dirname($autoloaderInWorkingDirectory, 2);
7781

@@ -107,6 +111,32 @@ use Symfony\Component\Console\Helper\ProgressBar;
107111

108112
$autoloadProjectAutoloaderFile('/../../autoload.php');
109113

114+
/** @var array<callable>|false $autoloadFunctionsAfter */
115+
$autoloadFunctionsAfter = spl_autoload_functions();
116+
117+
if ($autoloadFunctionsBefore !== false && $autoloadFunctionsAfter !== false) {
118+
$newAutoloadFunctions = [];
119+
foreach ($autoloadFunctionsAfter as $after) {
120+
if (
121+
is_array($after)
122+
&& count($after) > 0
123+
&& is_object($after[0])
124+
&& get_class($after[0]) === \Composer\Autoload\ClassLoader::class
125+
) {
126+
continue;
127+
}
128+
foreach ($autoloadFunctionsBefore as $before) {
129+
if ($after === $before) {
130+
continue 2;
131+
}
132+
}
133+
134+
$newAutoloadFunctions[] = $after;
135+
}
136+
137+
$GLOBALS['__phpstanAutoloadFunctions'] = $newAutoloadFunctions;
138+
}
139+
110140
$devOrPharLoader->register(true);
111141

112142
$version = 'Version unknown';

src/Command/CommandHelper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,8 @@ public static function begin(
379379
/** @var array<callable>|false $autoloadFunctionsAfter */
380380
$autoloadFunctionsAfter = spl_autoload_functions();
381381

382-
$newAutoloadFunctions = [];
383382
if ($autoloadFunctionsBefore !== false && $autoloadFunctionsAfter !== false) {
384-
$newAutoloadFunctions = [];
383+
$newAutoloadFunctions = $GLOBALS['__phpstanAutoloadFunctions'] ?? [];
385384
foreach ($autoloadFunctionsAfter as $after) {
386385
foreach ($autoloadFunctionsBefore as $before) {
387386
if ($after === $before) {

0 commit comments

Comments
 (0)