Skip to content

[DependencyInjection] Wrong factory method reported in exception message during container compilation #29678

Closed
@zanbaldwin

Description

@zanbaldwin

Symfony version(s) affected: at least 3.4.20, 4.1.9, 4.2.1

Description

When a service definition with a factory defines invalid arguments, the resulting exception message incorrectly specifies the factory constructor instead of the factory method as not having the specified named arguments.

In ResolveNamedArgumentsPass.php line 70:

  Invalid service "GuzzleHttp\ClientInterface": method "App\Factory\ApiHttpClientFactory::__construct()" has no argument named "$baseUri". Check your service definition.

How to reproduce

  • git clone git://github.com/symfony/skeleton.git && cd skeleton
  • Add files config/packages/app.yaml and src/Factory/ApiClientFactory.php (see below).
  • composer install

Additional context

config/packages/app.yaml

parameters:
    env(API_AUTH_SECRET): 'password'
    env(API_BASE_URI): 'localhost:8080'

services:

    App\Factory\ApiClientFactory:
        arguments:
            $baseUri: '%env(API_AUTH_SECRET)%'
            $authSecret: '%env(API_BASE_URI)%'

    GuzzleHttp\ClientInterface:
        class: 'GuzzleHttp\ClientInterface'
        factory: [ 'App\Factory\ApiClientFactory', 'create' ]
        arguments:
            $baseUri: '%env(API_AUTH_SECRET)%'
            $authSecret: '%env(API_BASE_URI)%'

src/Factory/ApiClientFactory.php

<?php declare(strict_types=1);

namespace App\Factory;

use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\ClientInterface as GuzzleInterface;

class ApiClientFactory
{
    /** @var string $baseUri */
    private $baseUri;
    /** @var string $authSecret */
    private $authSecret;

    public function __construct(string $baseUri, string $authSecret)
    {
        $this->baseUri = $baseUri;
        $this->authSecret = $authSecret;
    }

    public function create(): GuzzleInterface
    {
        return new Guzzle([
            'base_uri' => $this->baseUri,
            'headers' => ['Authorization' => sprintf('Bearer %s', $this->authSecret)]
        ]);
    }
}

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