-
Notifications
You must be signed in to change notification settings - Fork 585
Do not autoload a class whose name is an already defined function #6185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /vendor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "autoload": { | ||
| "files": [ | ||
| "pkg/bootstrap.php" | ||
| ] | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.