Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,19 @@ jobs:
cd e2e/bug-14514
composer install
../../bin/phpstan analyze bug-14515.php
# Fails since the revert of the bootstrap autoloader changes:
# Fails until https://github.com/phpstan/phpstan/issues/14988 is fixed again:
# - script: |
# cd e2e/bug-14988
# composer install
# ../../bin/phpstan analyse
# - script: |
# cd e2e/bug-12972b
# composer install
# ../../bin/phpstan analyze
- script: |
cd e2e/bug-12972b
composer install
../../bin/phpstan analyze
- script: |
cd e2e/debug-class-loader
composer install
../../bin/phpstan analyze
- script: |
cd e2e/bug-12972c
composer install
Expand Down
2 changes: 2 additions & 0 deletions e2e/debug-class-loader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
/composer.lock
8 changes: 8 additions & 0 deletions e2e/debug-class-loader/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

// What a Symfony application's entry script does in dev: DebugClassLoader wraps every
// registered autoloader and throws a RuntimeException when the file it found does not
// declare the class it was asked for, or when the case does not match.
require_once __DIR__ . '/vendor/autoload.php';

Symfony\Component\ErrorHandler\DebugClassLoader::enable();
10 changes: 10 additions & 0 deletions e2e/debug-class-loader/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"require": {
"symfony/error-handler": "^7.0"
},
"autoload": {
"psr-4": {
"DebugClassLoaderE2e\\": "src/"
}
}
}
12 changes: 12 additions & 0 deletions e2e/debug-class-loader/phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
level: 6
bootstrapFiles:
- bootstrap.php
paths:
- test.php
ignoreErrors:
# The point of this project is that analysis completes and reports the unknown class
# instead of dying inside the autoloader.
-
message: '#^Parameter \$widget of method DebugClassLoaderE2e\\Consumer\:\:doFoo\(\) has invalid type DebugClassLoaderE2e\\Widget\.$#'
path: test.php
10 changes: 10 additions & 0 deletions e2e/debug-class-loader/src/Widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

namespace DebugClassLoaderE2e;

// The declared name does not match the file name, so Composer's PSR-4 map sends
// DebugClassLoaderE2e\Widget to this file and DebugClassLoader throws for it.
class Widgets
{

}
17 changes: 17 additions & 0 deletions e2e/debug-class-loader/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace DebugClassLoaderE2e;

class Consumer
{

// Nothing ever instantiates the mistyped name, so no autoloader is invoked for it at
// runtime. PHPStan resolves every name it sees, so it does ask - and the exception used
// to abort the analysis of this file with an internal error instead of reporting the
// unknown class. See https://github.com/phpstan/phpstan/issues/14976
/** @param Widget $widget */
public function doFoo($widget): void
{
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\BetterReflection\Reflection\Reflection;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use Throwable;
use function class_exists;
use function interface_exists;
use function PHPStan\autoloadFunctions;
Expand Down Expand Up @@ -37,7 +38,19 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):

$autoloadFunctions = autoloadFunctions();
foreach ($autoloadFunctions as $autoloadFunction) {
$autoloadFunction($className);
try {
$autoloadFunction($className);
} catch (Throwable) {
// This locator asks every bootstrap-registered autoloader for the class, which is not
// the order PHP uses: at runtime the class loader that resolves the class first is
// often another one, so an autoloader that throws for names outside its own scope is
// never invoked for them and its exception cannot happen. Letting it propagate here
// aborts the analysis of the file with an internal error instead, so it is swallowed
// and the remaining autoloaders and source locators get their turn. The class may
// still have been defined before the throw, so the locators below run either way.
// See https://github.com/phpstan/phpstan/issues/14976
}

$reflection = $this->autoloadSourceLocator->locateIdentifier($reflector, $identifier);
if ($reflection !== null) {
return $reflection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PHPStan\Type\ConstantTypeHelper;
use ReflectionClass;
use ReflectionFunction;
use Throwable;
use function array_key_exists;
use function array_keys;
use function class_exists;
Expand Down Expand Up @@ -343,7 +344,15 @@ static function () use ($className): ?array {
}

foreach ($functions as $preExistingAutoloader) {
$preExistingAutoloader($className);
try {
$preExistingAutoloader($className);
} catch (Throwable) {
// Asking every registered autoloader for the class is not the order PHP uses,
// so an autoloader that throws for names outside its own scope throws here for
// names it is never invoked for at runtime. Its exception would abort the
// analysis of the file with an internal error, so it is swallowed - the file the
// autoloader asked for before throwing is still recorded by the trap below.
}

/**
* This static variable is populated by the side-effect of the stream wrapper
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection\BetterReflection\SourceLocator;

use LogicException;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\BetterReflection\Identifier\Identifier;
use PHPStan\BetterReflection\Identifier\IdentifierType;
use PHPStan\BetterReflection\Reflector\DefaultReflector;
use PHPStan\BetterReflection\SourceLocator\Ast\Locator;
use PHPStan\BetterReflection\SourceLocator\SourceStubber\ReflectionSourceStubber;
use PHPStan\Testing\PHPStanTestCase;
use TestSingleFileSourceLocator\AFoo;
use function class_alias;
use function class_exists;

class AutoloadFunctionsSourceLocatorTest extends PHPStanTestCase
{

/**
* A bootstrap autoloader that throws for names outside its own scope must not abort the
* analysis: this locator asks every registered autoloader, while at runtime the class loader
* that resolves the class first means the throwing one is never invoked for that name.
*
* @see https://github.com/phpstan/phpstan/issues/14976
*/
public function testAThrowingAutoloaderDoesNotStopTheRemainingOnes(): void
{
require_once __DIR__ . '/data/a.php';
$this->assertFalse(class_exists('ThrowingAutoloaderAlias', false), 'precondition: the alias does not exist yet');

$GLOBALS['__phpstanAutoloadFunctions'] = [
static function (string $class): void {
throw new LogicException('this should not happen');
},
static function (string $class): void {
if ($class !== 'ThrowingAutoloaderAlias') {
return;
}

class_alias(AFoo::class, 'ThrowingAutoloaderAlias');
},
];

try {
$locator = $this->createLocator();
$reflection = $locator->locateIdentifier(
new DefaultReflector($locator),
new Identifier('ThrowingAutoloaderAlias', new IdentifierType(IdentifierType::IDENTIFIER_CLASS)),
);

$this->assertNotNull($reflection, 'the class defined by the second autoloader should be located');
$this->assertSame(AFoo::class, $reflection->getName());
} finally {
unset($GLOBALS['__phpstanAutoloadFunctions']);
}
}

/**
* Nothing resolves the name, so the locator declines - the point is that it declines instead of
* letting the autoloader's exception surface as an internal error.
*/
public function testAThrowingAutoloaderMakesTheLocatorDecline(): void
{
$GLOBALS['__phpstanAutoloadFunctions'] = [
static function (string $class): void {
throw new LogicException('this should not happen');
},
];

try {
$locator = $this->createLocator();
$reflection = $locator->locateIdentifier(
new DefaultReflector($locator),
new Identifier('NeverDefinedByAnyAutoloader', new IdentifierType(IdentifierType::IDENTIFIER_CLASS)),
);

$this->assertNull($reflection);
} finally {
unset($GLOBALS['__phpstanAutoloadFunctions']);
}
}

private function createLocator(): AutoloadFunctionsSourceLocator
{
$container = self::getContainer();

return new AutoloadFunctionsSourceLocator(
new AutoloadSourceLocator($container->getByType(FileNodesFetcher::class), false),
new ReflectionClassSourceLocator(
new Locator($container->getService('phpParserDecorator')),
new ReflectionSourceStubber(new Standard()),
),
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace PHPStan\Reflection\BetterReflection\SourceLocator;

use LogicException;
use PHPStan\BetterReflection\Identifier\Identifier;
use PHPStan\BetterReflection\Identifier\IdentifierType;
use PHPStan\BetterReflection\Reflection\ReflectionClass;
use PHPStan\BetterReflection\Reflector\DefaultReflector;
use PHPStan\Reflection\InitializerExprContext;
Expand All @@ -12,6 +15,8 @@
use TestSingleFileSourceLocator\InCondition;
use function array_merge;
use function class_alias;
use function spl_autoload_register;
use function spl_autoload_unregister;

function testFunctionForLocator(): void // phpcs:disable
{
Expand Down Expand Up @@ -79,6 +84,39 @@ class_alias(AFoo::class, 'A_Foo');
$this->assertSame(AFoo::class, $class->getName());
}

/**
* This locator asks every registered autoloader for the class, which is not the order PHP uses,
* so an autoloader that throws for names outside its own scope must not abort the analysis.
*
* @see https://github.com/phpstan/phpstan/issues/14976
*/
public function testThrowingAutoloader(): void
{
// Everything that needs autoloading has to be loaded before the throwing autoloader joins
// the queue - it is registered globally, so anything loaded lazily inside the try would
// throw as well. That includes building the container.
$locator = new AutoloadSourceLocator(self::getContainer()->getByType(FileNodesFetcher::class), true);

$autoloader = static function (string $class): void {
if ($class !== 'NeverDefinedByAnyAutoloader') {
return;
}

throw new LogicException('this should not happen');
};
spl_autoload_register($autoloader);

try {
$reflection = $locator->locateIdentifier(
new DefaultReflector($locator),
new Identifier('NeverDefinedByAnyAutoloader', new IdentifierType(IdentifierType::IDENTIFIER_CLASS)),
);
$this->assertNull($reflection);
} finally {
spl_autoload_unregister($autoloader);
}
}

public static function getAdditionalConfigFiles(): array
{
return array_merge(
Expand Down
Loading