0016 - Use variadic to type-hint PHP arrays#24
0016 - Use variadic to type-hint PHP arrays#24Amoifr wants to merge 12 commits intoPrestaShop:masterfrom
Conversation
Amoifr
commented
Jan 7, 2022
| Questions | Answers |
|---|---|
| Description? | New ADR for Variadic |
| Type? | improvement |
| BC breaks? | no |
| Deprecations? | no |
| Fixed ticket? | |
| How to test? | |
| Possible impacts? |
Co-authored-by: SZCZUPAK Valentin <valentin.szczupak@prestashop.com>
|
Even if it's PHP and I'm a front-end developer, I have to agree with this, no kind of types like in TS with |
|
matks
left a comment
There was a problem hiding this comment.
Some improvements needed about the text and details but I approve the idea
|
ADR added to README table https://github.com/PrestaShop/ADR/blob/master/README.md |
Co-authored-by: Mathieu Ferment <mathieu.ferment@prestashop.com>
Co-authored-by: Mathieu Ferment <mathieu.ferment@prestashop.com>
jolelievre
left a comment
There was a problem hiding this comment.
I'm in favor of this, although I have a few comments
| $req: 1; $opt: 2; Number of arguments : 3 | ||
| ``` | ||
|
|
||
| In our world, mixing the two ways |
There was a problem hiding this comment.
Just a suggestion about redaction, it might be easier to understand if you first display the old way, and then the new way Starting the explanation with the new syntax first, which they may not know, is confusing as you don't have the context If you show the old context first at least people are in their comfort zone ready to accept the new one
| In our world, mixing the two ways | ||
|
|
||
| ``` | ||
| private function formatProductsForAssociation(ProductForAssociation ...$productsForAssociation): array |
There was a problem hiding this comment.
You only described this as method parameters but I believe it's usable for return as well no? The usage of this new feature will only have its real value if we use them both ways: input and output
There was a problem hiding this comment.
Unfortunately variadic arguments are only used as input. But as output we could use a collection DTO that implements \iterable or \IteratorAggregate interface.
There was a problem hiding this comment.
It's kind of confusing since, according to your description, this operator can have 2 different meaning depending if it's typed or not, IF I understand well
function f($a, $b, ...$c)
means that you can pass any number of arguments to the function, when it's more than 2, they will be in the variable $c
f(1, 2, $d, $e, $f, $g) => count($c) in the function will give 4
But with function f($a, $b, Product ...$c)
you cannot do f(1, 2, $d, $e, $f, $g) even if $d, $e, $f, $g are Product
but you can do f(1, 2, [$d, $e, $f, $g])
I will look further to PHP documentation
Or maybe in your 2nd example f(Product ...$products) is not equivalent to f(array $products) ? |
Co-authored-by: Jonathan Lelievre <jo.lelievre@gmail.com>
Not yet unfortunately :/ |
|
In case BC break is an issue, I suggest a simple trick: |
Variadic arguments are always received as an array in the function body. This array contains as many items as variadic arguments provided. |
|
Let's try to push this topic this week so what we can vote next week \o/ |