Skip to content

ReflectionException: Method Nette\DI\Container::createServiceFromStaticFactory() does not exist #314

@zeleznypa

Description

@zeleznypa

Version: 3.2.1

Bug Description

The Nette\DI\Container allows the addition of a service as a static factory. All operations can handle this except the Container::getByType method, which fails when creating a reflection over a non-existent container method.

Steps To Reproduce

class Service {
}

$container = new Nette\DI\Container\Container();
$container->addService('fromStaticFactory', static fn(): Service => new Service());
$container->getByType(Service::class)

Expected Behavior

There should be no exception when attempting to access a non-existent method.

Possible Solution

Use reflection of the method given in $foo

di/src/DI/Container.php

Lines 261 to 262 in 9c4af52

foreach ($this->methods as $method => $foo) {
$methodType = (new \ReflectionMethod(static::class, $method))->getReturnType()->getName();

Something like this:

foreach ($this->metohds as $method) {
    $methodType = \Nette\Utils\Callback::toReflection($method)->getReturnType()->getName();
    if (is_a($methodType, $type, allow_string: true)) {
        throw new MissingServiceException(sprintf(
            "Service of type %s is not autowired or is missing in di\u{a0}\u{a0}export\u{a0}\u{a0}types.",
            $type,
        ));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions