Skip to content

[DependencyInjection] Bind with type is broken if no typehint used for constructor parameter #33470

@OskarStark

Description

@OskarStark

Symfony version(s) affected: 4.3.4

Description
When binding arguments with a type, types should be valid if no typehint is used for the constructor argument.

How to reproduce

class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface
{
    public function __construct(PhoneNumberUtil $phoneNumberUtil, $region = PhoneNumberUtil::UNKNOWN_REGION)
    {
        // ...
    }
}

register it like:

   App\PhoneNumberNormalizer:
        bind:
            string $region: 'DE'
        tags:
            - { name: serializer.normalizer, priority: 100 }

will raise an error:

  A binding is configured for an argument of type "string" named "$region" for service "Misd
  App\PhoneNumberNormalizer" in file "/Users/oskar.stark/dev/backend/config/services.yaml", but no corresponding argument has been found. It may be unused and should be removed, or it may have a typo.

Workaround

   App\PhoneNumberNormalizer:
        bind:
-            string $region: 'DE'
+            $region: 'DE'
        tags:
            - { name: serializer.normalizer, priority: 100 }

Removing the string typehint on the binding fixes the problem.
For me using string here, if the argument itself has no typehint in the constructor is fine.

WDYT?

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