Qualify global function and constant references - #276
Draft
JasonTheAdams wants to merge 1 commit into
Draft
Conversation
Inside a namespace, PHP resolves an unqualified function or constant name by checking the current namespace before falling back to the global one. That fallback is a runtime lookup on every call and prevents opcache from substituting its optimized handlers for common built-ins. Normalize src/ and tests/ so no global function or constant is reached via the fallback: names referenced once in a file get a leading backslash, names referenced twice or more get a `use function` / `use const` import. Enforce the fallback half via SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly and document the convention in AGENTS.md. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## trunk #276 +/- ##
=========================================
Coverage 86.49% 86.49%
Complexity 1327 1327
=========================================
Files 68 68
Lines 4295 4295
=========================================
Hits 3715 3715
Misses 580 580
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Why
Performance. Inside a namespace, PHP resolves an unqualified function or constant name by looking in the current namespace first and only then falling back to the global one. That fallback is a runtime lookup on every single call, and it also stops opcache from substituting its optimized handlers for common built-ins like
sprintf(),count(), andis_array(). Qualifying the reference lets it resolve at compile time instead.What
Normalizes
src/andtests/so no global function or constant is reached via the fallback. Per file:\gettype($value)use function/use constimport, call sites left bareApplied with a
token_get_all()-based transform rather than regex, so method calls,new/instanceof, type declarations,catchblocks, and class constants are correctly skipped. Only functions PHP reports as internal were touched.Notes
cli.phpandsrc/polyfills.phpare in the global namespace and are untouched.SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnlytophpcs.xml.distto prevent regressions. It fails on any fallback reference and accepts both approved forms. It does not enforce the once-vs-multiple split — no stock sniff has a usage-count threshold — so that part is documented inAGENTS.mdas a convention for reviewers.ProviderModelsMetadataTest.phpwas wrapped after the added\pushed it past 120 chars.Testing
composer phpcsclean across 207 files;phpcbfreports no violations, so the new rule is stable rather than merely satisfiedcomposer phpstanno errorscomposer test:unit1164 tests, 4260 assertions passing🤖 Generated with Claude Code