Skip to content

Commit

Permalink
Fix static reflection issues with custom autoloaders
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 26, 2022
1 parent 50cbc45 commit 6962280
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"src/"
]
},
"files": ["src/dumpType.php","src/Testing/functions.php"]
"files": ["src/dumpType.php", "src/autoloadFunctions.php", "src/Testing/functions.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
20 changes: 20 additions & 0 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use function is_string;
use function mkdir;
use function register_shutdown_function;
use function spl_autoload_functions;
use function sprintf;
use function str_ends_with;
use function str_repeat;
Expand Down Expand Up @@ -289,6 +290,8 @@ public static function begin(
$createDir($tmpDir);
}

$autoloadFunctionsBefore = spl_autoload_functions();

try {
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, $paths, $composerAutoloaderProjectPaths, $analysedPathsFromConfig, $level ?? self::DEFAULT_LEVEL, $generateBaselineFile, $autoloadFile, $singleReflectionFile, $singleReflectionInsteadOfFile);
} catch (InvalidConfigurationException | AssertionException $e) {
Expand Down Expand Up @@ -371,6 +374,23 @@ public static function begin(
foreach ($container->getParameter('bootstrapFiles') as $bootstrapFileFromArray) {
self::executeBootstrapFile($bootstrapFileFromArray, $container, $errorOutput, $debugEnabled);
}
$autoloadFunctionsAfter = spl_autoload_functions();

$newAutoloadFunctions = [];
if ($autoloadFunctionsBefore !== false && $autoloadFunctionsAfter !== false) {
$newAutoloadFunctions = [];
foreach ($autoloadFunctionsAfter as $after) {
foreach ($autoloadFunctionsBefore as $before) {
if ($after === $before) {
continue 2;
}
}

$newAutoloadFunctions[] = $after;
}

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

if (PHP_VERSION_ID >= 80000) {
require_once __DIR__ . '/../../stubs/runtime/Enum/UnitEnum.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadFunctionsSourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository;
Expand Down Expand Up @@ -65,6 +66,8 @@ public function create(): SourceLocator
$locators[] = $this->optimizedSingleFileSourceLocatorRepository->getOrCreate($this->singleReflectionFile);
}

$locators[] = new AutoloadFunctionsSourceLocator();

$analysedDirectories = [];
$analysedFiles = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection\BetterReflection\SourceLocator;

use PHPStan\BetterReflection\Identifier\Identifier;
use PHPStan\BetterReflection\Identifier\IdentifierType;
use PHPStan\BetterReflection\Reflection\Reflection;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use function PHPStan\autoloadFunctions;

class AutoloadFunctionsSourceLocator implements SourceLocator
{

public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
{
if (!$identifier->isClass()) {
return null;
}

$autoloadFunctions = autoloadFunctions();
foreach ($autoloadFunctions as $autoloadFunction) {
$autoloadFunction($identifier->getName());
}

return null;
}

public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
{
return [];
}

}
11 changes: 11 additions & 0 deletions src/autoloadFunctions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);

namespace PHPStan;

/**
* @return array<int, callable(string): void>
*/
function autoloadFunctions(): array // phpcs:ignore Squiz.Functions.GlobalFunction.Found
{
return $GLOBALS['__phpstanAutoloadFunctions'] ?? [];
}

0 comments on commit 6962280

Please sign in to comment.