From 814e09eb41471a3d410dbc2cc0c7840bf5f7c81a Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 27 Aug 2024 22:39:38 +0100 Subject: [PATCH] Add daily email for 2024-08-26 Named arguments add context --- source/_daily_emails/2024-08-26.md | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 source/_daily_emails/2024-08-26.md diff --git a/source/_daily_emails/2024-08-26.md b/source/_daily_emails/2024-08-26.md new file mode 100644 index 000000000..93fc0d248 --- /dev/null +++ b/source/_daily_emails/2024-08-26.md @@ -0,0 +1,32 @@ +--- +title: Named arguments add context +date: 2024-08-26 +permalink: daily/2024/08/26/named-arguments-add-context +tags: + - software-development + - drupal + - php + - automated-testing +cta: ~ +snippet: | + As well as types, PHP's named arguments also add context - particularly within tests. +--- + +A couple of weeks ago, I wrote that [using types adds context to code][0], making it easier to read and understand. + +Something else that also adds context is [named arguments][1], which were introduced to PHP in version 8.0.0. + +I particularly like them when making assertions within tests, where getting the expected and actual values in the wrong order can create some confusing output. + +For example: + +```php +self::assertSame(expected: 'My Drupal website', actual: $node->label()); +``` + +While I can tell from the assertion that I'm checking two values are the same, adding the argument names makes it clear which is the expected value and which is the actual value. + +In fact, if you use named arguments, the order no longer matters, so I can put them in whichever order I want and it will work the same way. + +[0]: {{site.url}}/daily/2024/08/17/types-add-context +[1]: https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments