Skip to content

Conversation

@m-vo
Copy link
Member

@m-vo m-vo commented Feb 3, 2022

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)

@m-vo m-vo force-pushed the bugfix/attribute-param-order branch from b61e55f to 6df2476 Compare February 3, 2022 17:21
fritzmg
fritzmg previously approved these changes Feb 3, 2022
ausi
ausi previously approved these changes Feb 4, 2022
@aschempp
Copy link
Member

aschempp commented Feb 4, 2022

I would kinda prefer @ausi's solution in #4055 (comment)
If I ever write an attribute with a type, I would never write

#AsContentElement("texts", "foobar")

The goal should be to work the same as annotatinos did, so to use named paramters (I know we can't enforce that). Either write

#AsContentElement(category: 'texts')

or write

#AsContentElement("foobar", category: 'texts')

if you have a type. Making category default null and throw an exception would solve that.

@leofeyer
Copy link
Member

leofeyer commented Feb 4, 2022

@m-vo Will you update this PR accordingly?

@ausi
Copy link
Member

ausi commented Feb 4, 2022

I’d prefer something that works so that my IDE shows me if something is missing or what will happen.

IMO only the following two possibilities accomplish that:

__construct(string $category, string $type = null, …
__construct(string $type = null, string $category = 'texts',…

With string $category = null my IDE would suggest that this parameter is nullable and not required (which it is not at runtime then 😕)

@leofeyer
Copy link
Member

leofeyer commented Feb 4, 2022

I‘d be fine with string $category = 'texts' as well.

@m-vo
Copy link
Member Author

m-vo commented Feb 4, 2022

But then you could omit all parameters and the element would appear in the text category (why text?). I'm ok with that, but I think I would prefer a solution without adding magic.

@leofeyer
Copy link
Member

leofeyer commented Feb 6, 2022

Why don‘t we just make $type a required parameter, too?

__construct(string $type, string $category, …

The maker bundle will always set the type anyway:

<?php if ($use_attributes): ?>
#[AsContentElement("<?= $elementName ?>", category: "<?= $category ?>")]
<?php else: ?>

@m-vo
Copy link
Member Author

m-vo commented Feb 6, 2022

Why don‘t we just make $type a required parameter, too?

In my opinion it's a good thing, that the type is inferred by default, because it promotes a common naming scheme. And I don't want to type it when creating fragments myself. 🙂

@m-vo
Copy link
Member Author

m-vo commented Feb 6, 2022

We should also discuss the usage of the variadic parameter.

@m-vo m-vo added the up for discussion Issues and PRs which will be discussed in our monthly calls. label Feb 6, 2022
@fritzmg
Copy link
Contributor

fritzmg commented Feb 6, 2022

I usually always end up using

/**
 * @ContentElement(MyContentElement::TYPE, category="texts")
 */
class MyContentElement extends AbstractContentElementController
{
    public const TYPE = 'my_content_element';
}

so that I can reference the type in the DCA for example.

@m-vo
Copy link
Member Author

m-vo commented Feb 6, 2022

I usually always end up using

/**

  • @ContentElement(MyContentElement::TYPE, category="texts")
    */

👍 for constants, that can be automatically refactored. Maybe the FQCN should get the default type in the future?

Other aspect: We currently use the type to identify templates, but with Twig there is no point in injecting a Template in getResponse(). So either we build a special render method that, again, uses the type to identify a template or you would explicitly reference a template in $this->render() anyways and not need the type at all. This is also coupled to the discussion about template selection in the back end.

If we want to alter the behavior in Contao 5, we could maybe already prepare this in 4.13 when using attributes.

@leofeyer leofeyer removed the up for discussion Issues and PRs which will be discussed in our monthly calls. label Feb 10, 2022
Copy link
Member

@leofeyer leofeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in the Contao call, we want to use miscellaneous as default category for both (!) content elements and front end modules. Also, we want to change array ...$attributes to mixed ...$attributes.

@m-vo m-vo dismissed stale reviews from ausi and fritzmg via 5bd86a5 February 10, 2022 20:33
@m-vo
Copy link
Member Author

m-vo commented Feb 10, 2022

The changes are now implemented as discussed.

Because we're now in a situation, where it's required to use named arguments to set arbitrary keys, I had to use some trickery in the tests, so that jobs < PHP8 would not result in a syntax error. If there is a better solution, let me know.

@ausi
Copy link
Member

ausi commented Feb 11, 2022

If there is a better solution, let me know.

new AsContentElement(...[
    'type' => 'content_element/text',
    'template' => 'a_template',
    'method' => 'aMethod',
    'renderer' => 'inline',
    'foo' => 'bar',
    'baz' => 42,
]);

https://3v4l.org/3mVUg

@m-vo m-vo requested review from ausi and leofeyer February 11, 2022 08:50
@leofeyer leofeyer merged commit 3c0617a into contao:4.13 Feb 11, 2022
@leofeyer
Copy link
Member

Thank you @m-vo.

@m-vo m-vo deleted the bugfix/attribute-param-order branch July 5, 2024 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Problematic order of optional constructor parameters in AsContentElement/AsFrontendModule

5 participants