-
Notifications
You must be signed in to change notification settings - Fork 14
0016 - Use variadic to type-hint PHP arrays #24
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
base: master
Are you sure you want to change the base?
Changes from all commits
e0123d3
83d3d36
0faeb1e
b787dc7
48e8292
34c6dcd
f5696a6
f69a2b7
3084079
e7f4d9a
d864dfb
2500197
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # 16. Use variadic to type-hint PHP arrays | ||
|
|
||
| Date: 2021-01-07 | ||
|
|
||
| ## Status | ||
|
|
||
| Pending | ||
|
|
||
| ## Context | ||
|
|
||
| Using methods with array in signature force to type the parameters in phpDocs. It exists a workaround to force the type of the items in the array, and have astronger typing : | ||
|
|
||
| THE [VARIADIC](https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list) | ||
|
|
||
|
|
||
| when you call a function/method | ||
| ``` | ||
| <?php | ||
| function add($a, $b, $c) { | ||
| return $a + $b + $c; | ||
| } | ||
|
|
||
| $operators = [2, 3]; | ||
| echo add(1, ...$operators); | ||
| ?> | ||
|
|
||
| it returns 6 | ||
| ``` | ||
|
|
||
| when you accept variadic | ||
| ``` | ||
| <?php | ||
| function f($req, $opt = null, ...$params) { | ||
| // $params is an array containing the remaining arguments | ||
| printf('$req: %d; $opt: %d; Number of arguments : %d'."\n", | ||
| $req, $opt, count($params)); | ||
| } | ||
|
|
||
| f(1); | ||
| f(1, 2); | ||
| f(1, 2, 3); | ||
| f(1, 2, 3, 4); | ||
| f(1, 2, 3, 4, 5); | ||
| ?> | ||
|
|
||
| it returns | ||
| $req: 1; $opt: 0; Number of arguments : 0 | ||
| $req: 1; $opt: 2; Number of arguments : 0 | ||
| $req: 1; $opt: 2; Number of arguments : 1 | ||
| $req: 1; $opt: 2; Number of arguments : 2 | ||
| $req: 1; $opt: 2; Number of arguments : 3 | ||
| ``` | ||
|
|
||
| In our world, mixing the two ways | ||
|
|
||
| ``` | ||
| private function formatProductsForAssociation(ProductForAssociation ...$productsForAssociation): array | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately variadic arguments are only used as input. But as output we could use a collection DTO that implements |
||
| { | ||
| ... | ||
| } | ||
|
|
||
| $this->json($this->formatProductsForAssociation(...$products)); | ||
|
|
||
| ``` | ||
| instead of | ||
| ``` | ||
| /** | ||
| * @param ProductForAssociation[] $productsForAssociation | ||
| * | ||
| * @return array | ||
| */ | ||
| private function formatProductsForAssociation(array $productsForAssociation): array | ||
| { | ||
| ... | ||
| } | ||
|
|
||
| $this->json($this->formatProductsForAssociation($products)); | ||
|
|
||
| ``` | ||
|
|
||
| It allows to type without creating a DTO for a simple collection ! | ||
|
|
||
|
|
||
| ## Decision | ||
|
|
||
| - we don't use variadic if the method has optional parameters but the variadic is not | ||
| - we don't use multiple variadics with different types because it's not compatible with PHP<8 | ||
|
|
||
|
|
||
| ## First implementation | ||
|
|
||
|
|
||
| ## Consequences | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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