Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DependencyInjection] PhpDumper crashes if getProxyCode returns an empty string #28852

Closed
olvlvl opened this issue Oct 13, 2018 · 1 comment
Closed

Comments

@olvlvl
Copy link
Contributor

olvlvl commented Oct 13, 2018

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
            {
                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;
@nicolas-grekas
Copy link
Member

Would you mind sending a PR please?

nicolas-grekas added a commit that referenced this issue Oct 20, 2018
This PR was merged into the 2.8 branch.

Discussion
----------

[DependencyInjection] Skip empty proxy code

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28852
| License       | MIT

Fix #28852

@nicolas-grekas I'm not sure which branch this should be applied to, please let me know.

Commits
-------

baf6f8c Skip empty proxy code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants