Skip to content
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

Problematic order of optional constructor parameters in AsContentElement/AsFrontendModule #4055

Closed
m-vo opened this issue Feb 2, 2022 · 12 comments · Fixed by #4065
Closed
Labels
Milestone

Comments

@m-vo
Copy link
Member

m-vo commented Feb 2, 2022

Affected version(s)

4.13

Description

Our AsContentElement attribute currently looks like this:

public function __construct(string $type = null, string $category, string $template = null, string $method = null, string $renderer = null, array ...$attributes)

Having the first non-optional parameter at the second place forces you to define at least two parameters (https://3v4l.org/Y7N4G#v8.1.2), but with the content element attribute, you typically only define the category, which then results in:

Contao\CoreBundle\DependencyInjection\Attribute\AsContentElement::__construct(): Argument #1 ($type) not passed

(Similar reasoning for AsFrontendModule.)

@aschempp What's the reason for this unusual order? Can we change this?

@m-vo m-vo added this to the 4.13 milestone Feb 2, 2022
@m-vo
Copy link
Member Author

m-vo commented Feb 2, 2022

I found a discussion about this very topic #3619 (comment). But I don't agree with the outcome. 🙂

@aschempp
Copy link
Member

aschempp commented Feb 2, 2022

This is not a bug. As mentioned in the comment, this is intentional and makes sense to be consistent with annotations. Always use named parameters with (our) attributes.

@m-vo
Copy link
Member Author

m-vo commented Feb 2, 2022

It does not work, though, even with named parameters unless you pass in at least two. So this is clearly a bug.

@aschempp
Copy link
Member

aschempp commented Feb 3, 2022

What does not work?

@m-vo
Copy link
Member Author

m-vo commented Feb 3, 2022

Did you read anything of what I wrote? You cannot construct the object with a single parameter, no matter if named or not.

Having the first non-optional parameter at the second place forces you to define at least two parameters (https://3v4l.org/Y7N4G#v8.1.2), but with the content element attribute, you typically only define the category, which then results in:

@m-vo
Copy link
Member Author

m-vo commented Feb 3, 2022

Just so that we are on the same page:

image

=

image

@fritzmg
Copy link
Contributor

fritzmg commented Feb 3, 2022

I agree that this needs to be fixed. Only having to define the category and nothing else should be the minimum for the AsContentElement attribute, just as it is already the case with the ContentElement annotation.

@ausi
Copy link
Member

ausi commented Feb 3, 2022

Can this be fixed by making category optional like
public function __construct(string $type = null, string $category = null, …
and throw an InvalidArgumentException if $category is null?

Or use an actual default value like string $category = 'texts' instead?

@m-vo
Copy link
Member Author

m-vo commented Feb 3, 2022

Can this be fixed by making category optional like
public function __construct(string $type = null, string $category = null, …
and throw an InvalidArgumentException if $category is null?

Yes, this would work. https://3v4l.org/TArPo#v8.1.2

But we could IMHO also just change the order - especially, if you should use named arguments anyways.

@ausi
Copy link
Member

ausi commented Feb 3, 2022

As this parameter order was not released as stable yet, I agree we should change the order.

@m-vo
Copy link
Member Author

m-vo commented Feb 3, 2022

By the way, having optional parameters before mandatory ones is deprecated since PHP8.0: https://php.watch/versions/8.0/deprecate-required-param-after-optional

@m-vo
Copy link
Member Author

m-vo commented Feb 3, 2022

see #4065

@m-vo m-vo closed this as completed Feb 3, 2022
leofeyer pushed a commit that referenced this issue Feb 11, 2022
…tructors (see #4065)

Description
-----------

Fixes #4055

This PR swaps the first two parameters of the `AsContentElement` and `AsFrontendModule` constructors. This was not released yet, so no BC issue.

Background info: As of PHP8.0 having optional parameters before mandatory ones is deprecated. Here is an explanation why the current code did not trigger a deprecation warning and behaved just like two mandatory parameters: php/php-src#5067 (comment)

Commits
-------

9058274 fix order of parameters
6df2476 adjust tests
5bd86a5 use miscellaneous as the default type and fix variadic argument type hint
4356ed6 prevent PHP<8.0 jobs from autoloading test cases with PHP8 syntax
8a1d2d8 add 'mixed' type hint for variadic attributes
1f396f9 improve comment
942fc37 Update core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php
e413bf3 🦊
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants