Skip to content

Autowiring tries to wire unused/non-service classes #22977

@samsch

Description

@samsch
Q A
Bug report? yes
Feature request? no
BC Break report? Not sure
RFC? no
Symfony version 3.3.0

It seems autowiring tries to auto create the dependencies for a service before checking that the service is actually used.

It seems like it should not attempt to retrieve a dependency for a service which isn't used. If this error is actually expected or a limitation of the system, then I think there should be at least a note in the documentation, both in the autowiring doc and in the upgrade guide.

For my actual use case, I have a data class which is used as a form data object. The constructor for this class takes a service which I directly pass to it where I instantiate the object. Autowiring tries to find this service during container compile and fails.

Steps to reproduce

  1. Create new 3.3 app symfony new test-app 3.3
  2. Add new src/AppBundle/Class1 and src/AppBundle/Class2 with source as below.
  3. Run app (php bin/console server:start)
  4. Open webpage to error

Error

AutowiringFailedException
Cannot autowire service "AppBundle\Services\DataThings\OtherData": argument "$data" of method "__construct()" must have a type-hint or be given a value explicitly.

Class1 source

<?php

namespace AppBundle;

class Class1
{
    public function __construct(Class2 $c2)
    {}
}

Class2 source

<?php

namespace AppBundle;

class Class2
{
    public function __construct($something)
    {}
}

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