Skip to content
Merged
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
4 changes: 4 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ jobs:
cd e2e/bug-14514
composer install
../../bin/phpstan analyze bug-14515.php
- script: |
cd e2e/bug-14988
composer install
../../bin/phpstan analyse
- script: |
cd e2e/bug-14724
composer install
Expand Down
1 change: 1 addition & 0 deletions e2e/bug-14988/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
10 changes: 10 additions & 0 deletions e2e/bug-14988/classes/gadget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Redeclare\Builder;

class gadget
{

public int $size = 3;

}
7 changes: 7 additions & 0 deletions e2e/bug-14988/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"autoload": {
"files": [
"pkg/bootstrap.php"
]
}
}
18 changes: 18 additions & 0 deletions e2e/bug-14988/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions e2e/bug-14988/custom-autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace Redeclare;

use function is_file;
use function spl_autoload_register;
use function strlen;
use function strncmp;
use function substr;

/**
* Stands in for the real-world autoloader that reproduces phpstan/phpstan#14988: PHP_CodeSniffer's
* own autoloader, loaded via bootstrapFiles for a package that ships no Composer autoload metadata.
* It shares the three properties that make it fatal:
*
* 1. It is registered as a string callable ("Class::method"), which spl_autoload_functions()
* normalises to ['Class', 'method'] - an array whose first element is a string, not a
* ClassLoader object - so it survives bin/phpstan's Composer ClassLoader exclusion.
* 2. It is catch-all: it resolves names outside its own namespace.
* 3. It uses a plain include (not include_once), so a file already loaded is executed again.
*/
final class Autoloader
{

public static function load(string $class): void
{
if (strncmp($class, 'Redeclare\\Builder\\', 18) !== 0) {
return;
}

$name = substr($class, strlen('Redeclare\\Builder\\'));
foreach (['/classes/', '/pkg/'] as $dir) {
$file = __DIR__ . $dir . $name . '.php';
if (is_file($file)) {
include $file;
return;
}
}
}

}

spl_autoload_register('Redeclare\\Autoloader::load', true, true);
12 changes: 12 additions & 0 deletions e2e/bug-14988/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
level: 0
paths:
- src
bootstrapFiles:
- custom-autoloader.php
ignoreErrors:
# The probe deliberately references a name that is a function, not a class. The point of
# this fixture is that analysis completes instead of fatally re-including the function file.
-
message: '#^Class Redeclare\\Builder\\thing not found\.$#'
path: src/probe.php
9 changes: 9 additions & 0 deletions e2e/bug-14988/pkg/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

if (!function_exists('Redeclare\Builder\thing')) {
require_once __DIR__ . '/thing.php';
}

if (!function_exists('Redeclare\Builder\gadget')) {
require_once __DIR__ . '/gadget.php';
}
8 changes: 8 additions & 0 deletions e2e/bug-14988/pkg/gadget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Redeclare\Builder;

function gadget(): int
{
return 1;
}
8 changes: 8 additions & 0 deletions e2e/bug-14988/pkg/thing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Redeclare\Builder;

function thing(): string
{
return 'thing';
}
24 changes: 24 additions & 0 deletions e2e/bug-14988/src/probe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace App;

use Redeclare\Builder\gadget;

class Probe
{

// Probing a name that is only a function forces a hasClass() lookup, which used to re-run the
// custom autoloader and fatally re-include the already-loaded function file.
public function isThing(object $o): bool
{
return $o instanceof \Redeclare\Builder\thing;
}

// A class and a function may share a name in PHP. Even though the function is already loaded,
// the class defined in a separate file must still resolve (via the later source locators).
public function makeGadget(): int
{
return (new gadget())->size;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use function class_exists;
use function function_exists;
use function interface_exists;
use function PHPStan\autoloadFunctions;
use function trait_exists;
Expand All @@ -35,6 +36,18 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
return null;
}

// If the name is already a defined function, this locator must not run the bootstrap
// autoloaders for it: a catch-all autoloader (e.g. PHP_CodeSniffer's, which falls back to
// Composer's findFile()) would resolve the name to the function's own file and plain-include
// it a second time - it was loaded once already, e.g. by a package that ships one function
// per PSR-4 path and requires it from its bootstrap - fatally redeclaring the function.
// Returning null only declines this locator; a class and a function may share a name in PHP,
// and a class that genuinely exists under this name in another file is still located by the
// later source locators in the chain. See https://github.com/phpstan/phpstan/issues/14988
Comment thread
staabm marked this conversation as resolved.
if (function_exists($className)) {
return null;
}

$autoloadFunctions = autoloadFunctions();
foreach ($autoloadFunctions as $autoloadFunction) {
$autoloadFunction($className);
Expand Down
Loading