From 0fab26d206e44c1f446638608a815e8df73d7c13 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Aug 2017 00:21:31 +0300 Subject: [PATCH 1/3] use default arguments in PHP 7+ --- README.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index eb072e6..6307b22 100644 --- a/README.md +++ b/README.md @@ -171,19 +171,23 @@ function paintCar(&$car) { ### Use default arguments instead of short circuiting or conditionals -**Bad:** +**Not bad:** + ```php -function createMicrobrewery($name = null) { - $breweryName = $name ?: 'Hipster Brew Co.'; +function createMicrobrewery($name = null) +{ +    $breweryName = $name ?: 'Hipster Brew Co.'; // ... } ``` -**Good**: +**Good for PHP 7+**: + ```php -function createMicrobrewery($breweryName = 'Hipster Brew Co.') { - // ... +function createMicrobrewery(string $breweryName = 'Hipster Brew Co.') +{ +    // ... } ``` From 47d6f95f43fc0150b192a6179baf1858a953e6b0 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Aug 2017 08:41:34 +0300 Subject: [PATCH 2/3] Add origin bad example --- README.md | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6307b22..7958a1b 100644 --- a/README.md +++ b/README.md @@ -171,26 +171,40 @@ function paintCar(&$car) { ### Use default arguments instead of short circuiting or conditionals +**Not good:** + +This is not good because `$breweryName` can be `NULL`. + +```php +function createMicrobrewery($breweryName = 'Hipster Brew Co.') +{ +    // ... +} +``` + **Not bad:** +This opinion is understandable than the previous version, but it better controls the value of the variable. + ```php function createMicrobrewery($name = null) {    $breweryName = $name ?: 'Hipster Brew Co.'; // ... } - ``` -**Good for PHP 7+**: +**Good**: + +If you support only PHP 7+, then you can use [type hinting](http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration) and be sure that the `$breweryName` will not be `NULL`. ```php function createMicrobrewery(string $breweryName = 'Hipster Brew Co.') {    // ... } - ``` + **[⬆ back to top](#table-of-contents)** ## **Functions** ### Function arguments (2 or fewer ideally) From aa24debbac9160c31e805ad8edafb07a27e3925f Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 5 Sep 2017 14:08:13 +0300 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7958a1b..0530f2f 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,7 @@ function createMicrobrewery($breweryName = 'Hipster Brew Co.') **Not bad:** -This opinion is understandable than the previous version, but it better controls the value of the variable. +This opinion is more understandable than the previous version, but it better controls the value of the variable. ```php function createMicrobrewery($name = null)