Repository navigation
Fix global constant discovery after class name tokens - #6756
Conversation
|
CI note: the The PHP 7.4 call syntax and Turbo version-pin failures from my first push have been corrected in the current head. |
4e6c817 to
1abd49e
Compare
dd7bc45 to
083179b
Compare
|
//cc @SanderMuller |
|
the issue phpstan/phpstan#15482 talks about unexpected rule errors. we should have a test which asserts these unexpected rule errors no longer appear |
|
I ran the reporter's three files from phpstan/phpstan#15482 as a project. 2.3.x reports Two shapes still lose the constant on this branch: function foo(): string
{
return bar(class: 'x');
}
const BAR = 'bar';and if ($typeLevel <= 0) {
$typeLevel = 0;
$inType = false;
}With that change on top of this branch, all 8 shapes I tried find About a test for the rule errors: a What I checked:
|
|
Thanks for checking this. I added an E2E case using the reporter's three-file layout and covered the named class argument and spaced class constant variants in the scanner fixture. Both the PHP and native scanners now reset the type state when its depth reaches zero. The E2E analysis and targeted symbol-finder tests pass locally. The update is in 5988ead. |
SanderMuller
left a comment
There was a problem hiding this comment.
I checked the update on d2a30e4, with and without the Turbo extension.
- With the base scanner, the reporter's files from phpstan/phpstan#15482,
bar(class: 'x')andstdClass:: classall reportConstant BAR not found.This branch reports no errors for them. The PHP cleaner and a native build give the same result. SymbolFinderInFilesTestfails with the basePhpFileCleanerand with a native build that has the baseSymbolScan.h. It passes with the branch on both paths.- Over-reach: a class, interface, enum, function and constant declared after
Foo:: class,$o?->classandnew class {}are still found. I also ran the base and branch finders over every PHP file in the repository andvendor/(18369 files). Onlyclass-name-before-global-constant.phpande2e/bug-15482/src/z_functions.phpget different symbols. - The native corpora (
php-file-cleaner-corpus,symbol-finder-corpus) andsmoke.phpprint ALL OK with the branch build. make tests(22639 tests),make phpstan, phpcs on the changed files ande2e/bug-15482pass.- Performance: no slowdown. I timed one scan of the same 18369 files, 7 interleaved rounds. The PHP finder takes a median 3.26 s with the branch and 3.46 s with base. The native finder takes 2.70 s in both.
CI has not run on d2a30e4 yet. On 5988ead, Lint (7.4) failed on the new fixture, which d2a30e4 excludes. The calebdw/phpstan-laravel integration failed with five No error with identifier phpstanApi.trait is reported. That comes from 486821c in the base, which marks LateResolvableTypeTrait as @api and makes the trait rule respect it. calebdw/phpstan-laravel removed those five ignores on 2026-10-25. The branch finds the same symbols as base in all 232 files of that project's src.
|
(needs rebase) |
| if ($typeLevel <= 0) { | ||
| $typeLevel = 0; |
There was a problem hiding this comment.
is this just noise or a required change?
There was a problem hiding this comment.
It is required. I ran the PR's fixture with the PR's PhpFileCleaner and with the same file minus the clamp. Without it, AFTER_NAMED_CLASS_ARGUMENT and AFTER_SPACED_CLASS_CONSTANT are lost again. In symbolFinderNamedArgument(class: 'named argument') and \stdClass:: class, the byte before class is ( or a space, so the boundary check still enters a type. No { follows, so the function's closing } takes $typeLevel to -1, and with === 0 the cleaner never leaves the type.
d2a30e4 to
c654fb3
Compare
| - script: | | ||
| cd e2e/bug-15482 | ||
| ../../bin/phpstan analyse |
There was a problem hiding this comment.
on my macOS machine the added reproducer does not emit the errors of the origin reproducer, without the fix beeing applied
There was a problem hiding this comment.
for some reason I can reproduce the initial errors now, after I renamed the tests
013b685 to
29f4de3
Compare
|
thank you! |
When a global constant follows
stdClass::classinside a function, the directory symbol scan can mistake theclassname token for a declaration and lose the constant. The same can happen with identifiers beginning withclass. This change checks the keyword boundary before entering type scope in both the PHP cleaner and the Turbo scanner.The regression covers
::class,className(), and a real anonymous class. The native differential corpora also include the trigger. Thanks to @Victor-emil for identifying the cause and suggesting the boundary check in phpstan/phpstan#15482.Validation: the focused PHPUnit test passes (3 tests); the source-locator suite passes (78 tests, 159 assertions, 1 skip); and the issue's two-file example analyzes without errors using this branch. PHP syntax and
git diff --checkpass. I could not run the native differential scripts locally because the Turbo extension is not installed.Fixes phpstan/phpstan#15482