PrestaShop / ADR

Architecture Decision Records for the PrestaShop project
11 stars 15 forks source link

0016 - Use variadic to type-hint PHP arrays #24

Open Amoifr opened 2 years ago

Amoifr commented 2 years ago
Questions Answers
Description? New ADR for Variadic
Type? improvement
BC breaks? no
Deprecations? no
Fixed ticket?
How to test?
Possible impacts?
NeOMakinG commented 2 years ago

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 Array<Products> in PHP?

Amoifr commented 2 years ago

@NeOMakinG The best way is to pass a DTO as a parameter, but this is a workaround if you just have one dimension.

matks commented 2 years ago

ADR added to README table https://github.com/PrestaShop/ADR/blob/master/README.md

sowbiba commented 2 years ago

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) ?

saulaski commented 2 years ago

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 Array<Products> in PHP?

Not yet unfortunately :/

saulaski commented 2 years ago

In case BC break is an issue, I suggest a simple trick:

public function setItems(array $items): self
{
    $this->items = [];

    return $this->addItem(...$items);
}

protected function addItem(Item ...$items): self
{
    foreach ($items as $item) {
        $this->items[] = $item;
    }

    return $this;
}
saulaski commented 2 years ago

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) ?

Variadic arguments are always received as an array in the function body. This array contains as many items as variadic arguments provided. If you need to pass items in an array as variadic arguments, just use the spread operator : f(1, 2, ...$products)

matks commented 2 years ago

Let's try to push this topic this week so what we can vote next week \o/