Skip to content

Commit

Permalink
Include custom autoloaders registered in composer files
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 30, 2022
1 parent 99acc02 commit eab4542
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
30 changes: 30 additions & 0 deletions bin/phpstan
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ use Symfony\Component\Console\Helper\ProgressBar;

$autoloaderInWorkingDirectory = getcwd() . '/vendor/autoload.php';
$composerAutoloaderProjectPaths = [];

/** @var array<callable>|false $autoloadFunctionsBefore */
$autoloadFunctionsBefore = spl_autoload_functions();

if (is_file($autoloaderInWorkingDirectory)) {
$composerAutoloaderProjectPaths[] = dirname($autoloaderInWorkingDirectory, 2);

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

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

/** @var array<callable>|false $autoloadFunctionsAfter */
$autoloadFunctionsAfter = spl_autoload_functions();

if ($autoloadFunctionsBefore !== false && $autoloadFunctionsAfter !== false) {
$newAutoloadFunctions = [];
foreach ($autoloadFunctionsAfter as $after) {
if (
is_array($after)
&& count($after) > 0
&& is_object($after[0])
&& get_class($after[0]) === \Composer\Autoload\ClassLoader::class
) {
continue;
}
foreach ($autoloadFunctionsBefore as $before) {
if ($after === $before) {
continue 2;
}
}

$newAutoloadFunctions[] = $after;
}

$GLOBALS['__phpstanAutoloadFunctions'] = $newAutoloadFunctions;
}

$devOrPharLoader->register(true);

$version = 'Version unknown';
Expand Down
3 changes: 1 addition & 2 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,8 @@ public static function begin(
/** @var array<callable>|false $autoloadFunctionsAfter */
$autoloadFunctionsAfter = spl_autoload_functions();

$newAutoloadFunctions = [];
if ($autoloadFunctionsBefore !== false && $autoloadFunctionsAfter !== false) {
$newAutoloadFunctions = [];
$newAutoloadFunctions = $GLOBALS['__phpstanAutoloadFunctions'] ?? [];
foreach ($autoloadFunctionsAfter as $after) {
foreach ($autoloadFunctionsBefore as $before) {
if ($after === $before) {
Expand Down

0 comments on commit eab4542

Please sign in to comment.