Skip to content

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

Closed

Description

@lisachenko

Summary

Creating a Go\ParserReflection\ReflectionParameter whose default value is a new expression (Collaborator $helper = new Collaborator('x')) resolves the default eagerly, and NodeExpressionResolver::resolveExprNew() loads the class with include_once of its source file (loadClassDefinition()).

Two effects:

  • Reflecting a signature has a side effect: it declares a class, and it does so although nobody asked for the default value.
  • The include_once bypasses the autoloader. In goaop/framework the class is therefore declared from its original source while another class is being woven, and can not be woven any more in that process: its advices (for example an initialization(...) advice) silently do not apply.

Affected versions

Reproduced with 5.2.0 on PHP 8.5.12, through goaop/framework 4.x (0450cdf).

Reproduction (through goaop/framework, debug mode, empty cache)

namespace App\Lim;

class Collaborator
{
    public function __construct(public string $label = 'none') {}
}

class VA
{
    public function run(Collaborator $h = new Collaborator('param-default')): string
    {
        return $h->label;
    }
}

Aspects: a #[Before('execution(public App\Lim\VA->run(*))')] advice, and #[Before('initialization(App\Lim\Collaborator)')].

$o = new App\Lim\VA();
$o->run();
var_dump(is_subclass_of(App\Lim\Collaborator::class, Go\Aop\Proxy::class));

Actual: false. Collaborator is loaded unwoven, during the weaving of VA:

#0 src/Resolver/NodeExpressionResolver.php(1021): include_once()
#1 src/Resolver/NodeExpressionResolver.php(405): NodeExpressionResolver->loadClassDefinition()
#2 src/Resolver/NodeExpressionResolver.php(189): NodeExpressionResolver->resolveExprNew()
#3 src/Resolver/NodeExpressionResolver.php(166): NodeExpressionResolver->resolve()
#4 src/ReflectionParameter.php(112): NodeExpressionResolver->process()
#5 src/Traits/ReflectionFunctionLikeTrait.php(216): ReflectionParameter->__construct()
#6 goaop/framework src/Proxy/Part/FunctionCallArgumentListGenerator.php(53): ReflectionMethod->getParameters()
#7 goaop/framework src/Proxy/ClassProxyGenerator.php(423)

Expected: true, as when VA::run() has no such default, or as in production mode after cache:warmup:aop (verified: there Collaborator is woven, because weaving happened in another process).

The same new in a static variable initializer, in an attribute argument or as the default of a promoted constructor parameter of a method that is not intercepted does not trigger it; only the parameters of an intercepted method are reflected by the proxy generator.

Possible fix

  • Resolve the default value lazily, on getDefaultValue(), instead of in the constructor of ReflectionParameter; the framework only needs the parameter names, types and the default's AST there.
  • When a new expression has to be evaluated, load the class through the autoloader (class_exists($name)), not with include_once of the located file, so that a class loader hooked into Composer still sees the load.

Related

Found while verifying the entries of docs/php85-limitations.md in goaop/framework (goaop/framework#603 is the neighbouring, fixed, problem). #259 is another resolver problem found the same day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions