service_collector method should not be required to provide a type-hint

Created on 30 November 2016, almost 8 years ago
Updated 27 June 2023, over 1 year ago

Problem/Motivation

When collecting services (using the service collector provided by TaggedHandlersPass), the consumer service method called MUST provide a type-hinted parameter (that will receive the tagged services).

First of all, this is not stated anywhere in the documentation (see: https://www.drupal.org/docs/8/api/services-and-dependency-injection/serv... β†’ ). The documentation simply states that the first parameter of the method will be passed the tagged service.

Instead, if you look at the code (https://github.com/drupal/drupal/blob/8.3.x/core/lib/Drupal/Core/Depende...), you will notice that the expected interface is actually the last one in the method signature.

So basically, if you want to call a method like:

public function callMe(Extension $extension, $id, SomeService $service = null)

the TaggedHandlersPass will actually fail (because it will expect that all tagged services are of type SomeService when in reality, they should be of type Extension.

Secondly, this is an "annoyance" to most users. I'm currently trying to create a Stratigility middleware pipe service in Drupal. I want stratigility middleware services to be automatically piped to the middleware pipe. To do this, I wrote:

  stratigility_pipe:
    class:     Zend\Stratigility\MiddlewarePipe
    calls:
     - [raiseThrowables, []]
    tags:
     - { name: service_collector, tag: http_interop_middleware, call: pipe }

But unfortunately, the "pipe" method of MiddlewarePipe has a signature with no type hint (https://github.com/zendframework/zend-stratigility/blob/master/src/Middl...).

And Drupal forces the method handling the signature to have a type hint. IMHO, there is really no good reason for this. Type-hinting the service in the method should not be compulsory (as this forces users relying on third party code to write a useless wrapper).

Proposed resolution

Let's completely get rid of forcing the collector service method to provide a type-hint. This is not documented and is actually harmful as it can require user to write useless boilerplate code.

Also, if there is a type-hint, let's solve the bug that makes the TaggedHandlersPass crash if it is given a method with 2 type hints.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡«πŸ‡·France moufmouf

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡±Netherlands Lendude Amsterdam

    This came up as a bug smash daily triage target.

    Tried to wrap my head around this one and it seems like it mixes a task and a bug fix and fixes the bug by implementing the task.
    Task: Be less strict
    Bug: The strictness is bugged when having multiple hinted parameters
    Fix: Remove strictness

    Feedback from #9 still seems relevant and still needs to be addressed but the general approach seems to have approval to go ahead.

Production build 0.71.5 2024