10000 [DependencyInjection] PhpDumper crashes if getProxyCode returns an empty string · Issue #28852 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[DependencyInjection] PhpDumper crashes if getProxyCode returns an empty string #28852
Closed
@olvlvl

Description

@olvlvl

Symfony version(s) affected: 4.1.x (maybe older versions too)

Description

PhpDumper crashes if DumperInterface::getProxyCode() returns an empty string.

How to reproduce

I'm writing a proxy dumper that leverages anonymous classes to produce code similar to the following example:

    protected function getTest_BroadcasterService($lazyLoad = true)
    {
        if ($lazyLoad) {
            return new class(
                function () {
                    return $this->getTest_BroadcasterService(false);
                }
            ) implements \HelloFresh\MenuService\Application\Contract\Broadcaster
            {
           
75F2
     private $factory;
                private $service;

                public function __construct(callable $factory)
                {
                    $this->factory = $factory;
                }

                public function broadcast(object $message, string $routingKey, array $headers = []): void
                {
                    $this->getService()->broadcast($message, $routingKey, $headers);
                }

                private function getService(): \HelloFresh\MenuService\Application\Contract\Broadcaster
                {
                    return $this->service ?: $this->service = ($this->factory)();
                }
            };
        }
        // …

Because it's using anonymous classes, my dumper doesn't have anything to provide during getProxyCode(). But if it returns an empty string, PhpDumper crashes with the following message:

Fatal error: Uncaught ErrorException: Undefined offset: 1 in /app/vendor/symfony/dependency-injection/Dumper/PhpDumper.php:381

That's because of the following code:

            $proxyCode = "\n".$proxyDumper->getProxyCode($definition);
            if ($strip) {
                $proxyCode = "<?php\n".$proxyCode;
                $proxyCode = substr(Kernel::stripComments($proxyCode), 5);
            }
            yield sprintf('%s.php', explode(' ', $proxyCode, 3)[1]) => $proxyCode;

As a workaround, I need to define getProxyCode() as follows, which is not ideal:

    public function getProxyCode(Definition $definition)
    {
        return '// nothing to do';
    }

Possible Solution

To fix the problem we could have the following code:

            $proxyCode = $proxyDumper->getProxyCode($definition);
            if (!$proxyCode) {
                continue;
            }
            $proxyCode = "\n".$proxyCode;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0