Skip to content

[DependencyInjection] Bad phpdoc for $configurator in class Autoconfigure #59821

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

Closed
jfranclin opened this issue Feb 20, 2025 · 0 comments
Closed

Comments

@jfranclin
Copy link
Contributor

Symfony version(s) affected

7.1.*

Description

In https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/DependencyInjection/Attribute/Autoconfigure.php there is in my opinion an issue on the PHPDOC of $configurator which makes complaining PHPStan for example :

* @param array<class-string, string>|string|null $configurator A PHP function, reference or an array containing a class/Reference and a method to call after the service is fully initialized

The doc is asking for a class-string key and a string value. However, this is not always the case because the first parameter could be a service, like @foo which is not a class-string. Moreover, the second parameter should not be empty.

Did I miss something ?

How to reproduce

Run a static analysis tool on a class holding an attribute autoconfigure like :

use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;

#[Autoconfigure(
    lazy: true,
    configurator: [
        '@my_service',
        'configure',
    ]
)]
class DummyConfigurableClient
{}

Give :


Line DummyConfigurableClient.php


13 Parameter $configurator of attribute class Symfony\Component\DependencyInjection\Attribute\Autoconfigure constructor expects array<class-string, string>|string|null, array<string, string> given.


Possible Solution

For me, the right PHPDoc should be :

@param array{non-empty-string|class-string, non-empty-string}|non-empty-string|null

Additional Context

I've opened a PR for demonstrating the fix : #59820

@jfranclin jfranclin added the Bug label Feb 20, 2025
nicolas-grekas added a commit that referenced this issue Feb 21, 2025
…toconfigure attribute (jfranclin)

This PR was merged into the 7.2 branch.

Discussion
----------

[DependencyInjection] Fix phpdoc for $configurator in Autoconfigure attribute

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #59821
| License       | MIT

Since SF7.1, in Autoconfigure attribute, the $configurator phpdoc seems wrong :

` * `@param` array<class-string, string>|string|null                 $configurator A PHP function, reference or an array containing a class/Reference and a method to call after the service is fully initialized`

The doc is asking for a class-string **key** and a string **value**. However, this is not always the case because the first parameter could be a service, like ``@foo`` which is not a class-string. Moreover, the second parameter should not be empty.

Commits
-------

ce03562 [DependencyInjection] Fix phpdoc for $configurator in Autoconfigure attribute
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

2 participants