Repository navigation
Resolve parameter defaults lazily and load classes through the autoloader - #263
Merged
Merged
Conversation
…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
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.
Fixes #261
Problem
Creating a
ReflectionParameterwhose default is anewexpression (Collaborator $h = new Collaborator('x')) evaluated the default in the constructor.NodeExpressionResolver::loadClassDefinition()then loaded the class withinclude_onceof 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:nulldefault), the same way PHP decides it at compile time.isDefaultValueConstant()andgetDefaultValueConstantName()only evaluate when the default is a constant or class-constant fetch. For any other default they returnfalse/nullstraight away.getDefaultValue(), not while the parameter is being constructed.No more
include_onceinNodeExpressionResolver.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.mdnow states both rules.Tests
ConstantExpressionClosuresTest::testNewInInitializerDoesNotTriggerAutoloadingasserted the old behaviour (load without the autoloader). It is replaced bytestNewInInitializerIsResolvedLazilyThroughAutoloader:getParameters(),getType()andisDefaultValueConstant()load nothing;getDefaultValue()loads the class through an autoloader (a test autoloader stands in for Composer, which doesn't know the stub files).getParameters().testNewExpressionForUnknownClassThrowsDescriptiveExceptionchecks the new error message.phpstan analyse src: no errors. Run on PHP 8.5.12 withopcache.jit=disable.Generated by Claude Code