Skip to content

Resolve parameter defaults lazily and load classes through the autoloader - #263

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

Copy link
Copy Markdown
Member

Fixes #261

Problem

Creating a ReflectionParameter whose default is a new expression (Collaborator $h = new Collaborator('x')) evaluated the default in the constructor. NodeExpressionResolver::loadClassDefinition() then loaded the class with include_once of the located file. So reflecting a signature declared a class, without the autoloader seeing it. In goaop/framework that class was declared unwoven while another class was being woven, and its advices silently stopped applying.

Fix

Lazy defaults in ReflectionParameter. The default value is evaluated once, on first access: getDefaultValue(), getDefaultValueConstantName(), isDefaultValueConstant(), getDefaultValueExpression() or __toString(). Nothing else evaluates it:

  • The type's implicit nullability is decided on the AST (a literal null default), the same way PHP decides it at compile time.
  • isDefaultValueConstant() and getDefaultValueConstantName() only evaluate when the default is a constant or class-constant fetch. For any other default they return false/null straight away.
  • An unresolvable default now throws on getDefaultValue(), not while the parameter is being constructed.

No more include_once in NodeExpressionResolver. loadClassDefinition() now loads the class through the registered autoloaders (class_exists() / interface_exists() / trait_exists()), so a class loader hooked into Composer sees every load. Library code includes no source files any more.

CLAUDE.md now states both rules.

Tests

  • ConstantExpressionClosuresTest::testNewInInitializerDoesNotTriggerAutoloading asserted the old behaviour (load without the autoloader). It is replaced by testNewInInitializerIsResolvedLazilyThroughAutoloader:
    • getParameters(), getType() and isDefaultValueConstant() load nothing;
    • getDefaultValue() loads the class through an autoloader (a test autoloader stands in for Composer, which doesn't know the stub files).
  • On the old code the new test fails: the dependency class is already declared after getParameters().
  • testNewExpressionForUnknownClassThrowsDescriptiveException checks the new error message.
  • Full suite: 13793 tests pass. phpstan analyse src: no errors. Run on PHP 8.5.12 with opcache.jit=disable.

Generated by Claude Code

…utoloader

Constructing a ReflectionParameter whose default is a `new` expression
evaluated the default eagerly, and NodeExpressionResolver loaded the class
with include_once of the located file. Reflecting a signature thus declared
a class, bypassing the autoloader: in goaop/framework such a class could
not be woven any more in that process.

- ReflectionParameter resolves its default value on the first access
  (getDefaultValue(), getDefaultValueConstantName(), isDefaultValueConstant(),
  getDefaultValueExpression(), __toString()). The implicit nullability of
  the type is decided on the AST (a literal `null` default), like PHP does at
  compile time, and only constant fetches are evaluated to answer
  isDefaultValueConstant().
- NodeExpressionResolver::loadClassDefinition() loads the class through the
  autoloaders instead of including its file.

Fixes #261

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FxhC1GTbduntXjyF8E9Feb
@lisachenko
lisachenko marked this pull request as ready for review November 11, 2026 22:11
@lisachenko
lisachenko merged commit 0d7c586 into 4.x Nov 11, 2026
7 checks passed
@lisachenko
lisachenko deleted the feature/pensive-hypatia-bx7t23 branch November 11, 2026 22:12
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.

ReflectionParameter loads the class of a new default value with include_once while it is constructed

2 participants