Skip to content

Qualify global function and constant references - #276

Draft
JasonTheAdams wants to merge 1 commit into
trunkfrom
perf/qualify-global-function-references
Draft

Qualify global function and constant references#276
JasonTheAdams wants to merge 1 commit into
trunkfrom
perf/qualify-global-function-references

Conversation

@JasonTheAdams

Copy link
Copy Markdown
Member

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(), and is_array(). Qualifying the reference lets it resolve at compile time instead.

What

Normalizes src/ and tests/ so no global function or constant is reached via the fallback. Per file:

  • Referenced once → leading backslash, e.g. \gettype($value)
  • Referenced twice or moreuse function / use const import, call sites left bare

Applied with a token_get_all()-based transform rather than regex, so method calls, new/instanceof, type declarations, catch blocks, and class constants are correctly skipped. Only functions PHP reports as internal were touched.

Notes

  • Classes needed no changes. PHP has no global fallback for class names, so a global class must already be imported or fully qualified for the code to run at all. This is a functions-and-constants change only.
  • cli.php and src/polyfills.php are in the global namespace and are untouched.
  • Adds SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly to phpcs.xml.dist to 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 in AGENTS.md as a convention for reviewers.
  • One line in ProviderModelsMetadataTest.php was wrapped after the added \ pushed it past 120 chars.

Testing

  • composer phpcs clean across 207 files; phpcbf reports no violations, so the new rule is stable rather than merely satisfied
  • composer phpstan no errors
  • composer test:unit 1164 tests, 4260 assertions passing
  • Integration tests not run (billable live API calls)

🤖 Generated with Claude Code

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

codecov Bot commented Aug 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.16832% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.49%. Comparing base (a31b0ec) to head (2405c8b).

Files with missing lines Patch % Lines
src/Common/AbstractDataTransferObject.php 0.00% 3 Missing ⚠️
src/Providers/Http/DTO/Response.php 0.00% 3 Missing ⚠️
src/Providers/Models/Enums/OptionEnum.php 0.00% 3 Missing ⚠️
src/Providers/ProviderRegistry.php 62.50% 3 Missing ⚠️
...ers/ApiBasedImplementation/AbstractApiProvider.php 0.00% 1 Missing ⚠️
src/Providers/Http/DTO/RequestOptions.php 0.00% 1 Missing ⚠️
src/Providers/Http/Exception/NetworkException.php 0.00% 1 Missing ⚠️
src/Providers/Http/HttpTransporter.php 75.00% 1 Missing ⚠️
src/Providers/Http/Util/ResponseUtil.php 0.00% 1 Missing ⚠️
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           
Flag Coverage Δ
unit 86.49% <83.16%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jeffpaul jeffpaul added this to the 1.5.0 milestone Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants