Skip to content

Resolve constants in attributes of class members in the class namespace - #260

Merged
lisachenko merged 1 commit into
4.xfrom
feature/pensive-hypatia-bx7t23
Nov 11, 2026
Merged

lisachenko merged 1 commit into
4.xfrom
feature/pensive-hypatia-bx7t23

Conversation

@lisachenko

@lisachenko lisachenko commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Fixes #259

Problem

ReflectionMethod::getAttributes() threw for unqualified constants in attribute arguments (true, false, null, PHP_INT_MAX, namespaced consts). NodeExpressionResolver::resolveExprConstFetch() looked the constant up in the namespace of the context, and a method's namespace is always '' (same as native reflection). So ReflectionFileNamespace threw Namespace was not found in the file …, which is what breaks weaving in goaop/framework. When the method was reflected on its own, its file name was empty too, so you got ValueError: Path must not be empty instead.

Fix

NodeExpressionResolver now takes the namespace and file from the reflector that owns the code, through two private helpers, getContextScope() and getContextFileName():

  • methods, properties, class constants → the declaring class (for methods imported from a trait, that's the trait, so names resolve in the trait's namespace);
  • parameters → the declaring function, or the declaring class when the function is a method;
  • classes, functions, file namespaces → themselves.

Side effects of the change:

  • __NAMESPACE__, __FILE__ and __DIR__ now resolve in these contexts. Before, a method gave '' for __NAMESPACE__, and properties, parameters and class constants threw.
  • The namespace lookup is skipped for true/false/null, now matched case-insensitively (so TRUE too), and for code with no file name.

Tests

New test tests/ConstantsInMemberAttributesTest.php with stub tests/Stub/FileWithConstantsInMemberAttributes.php. It covers true/false/null/TRUE/\true, a global constant, a namespaced constant, self::class, __NAMESPACE__, constants inside an array, and trait methods from another namespace. Methods are checked both through their class and on their own; properties, class constants, method parameters, functions and function parameters are covered too. A parity test compares every argument with native reflection once the stub is loaded.

  • Without the fix: 16 errors and 4 failures in the new test. With it: everything passes.
  • On 4.x (PHPUnit 11): full suite of 13793 tests passes.
  • phpstan analyse src: no errors.
  • Run on PHP 8.5.12 with opcache.jit=disable, to avoid the tracing-JIT bug in that PHP version. PHP 8.4 is covered by CI.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FxhC1GTbduntXjyF8E9Feb

@lisachenko
lisachenko changed the base branch from master to 4.x November 11, 2026 21:43
@lisachenko

Copy link
Copy Markdown
Member Author

rebase to 4.x please

…mespace

ReflectionMethod::getAttributes() failed for unqualified constants (true,
false, null, PHP_INT_MAX, namespaced constants): NodeExpressionResolver
looked them up in the namespace of the method itself, which is always ''
(like the native reflection), so ReflectionFileNamespace threw "Namespace
 was not found" (or a ValueError for an empty path when the method was
reflected on its own and had no file name).

The resolver now takes the namespace and the file from the reflector that
owns the code: the declaring class for methods, properties and class
constants (the trait for imported trait methods), the declaring function
or class for parameters. This also makes __NAMESPACE__, __FILE__ and
__DIR__ work in these contexts, and skips the namespace lookup for
true/false/null (case-insensitively) and for code without a file.

Fixes #259

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FxhC1GTbduntXjyF8E9Feb
@lisachenko
lisachenko force-pushed the feature/pensive-hypatia-bx7t23 branch from 0796315 to f6b20ce Compare November 11, 2026 21:50
@lisachenko
lisachenko marked this pull request as ready for review November 11, 2026 21:51
@lisachenko
lisachenko merged commit 07f58d3 into 4.x Nov 11, 2026
7 checks passed
@lisachenko
lisachenko deleted the feature/pensive-hypatia-bx7t23 branch November 11, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

ReflectionMethod::getAttributes() fails for true/false/null and global constants in attribute arguments

2 participants