From eb31d2d2aadb7e32a027708567c471c7e98fb46f Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 23 Jul 2019 14:01:43 +0300 Subject: [PATCH 1/3] use VO in example of "Function arguments (2 or fewer ideally)" --- README.md | 70 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index a3c10d8..82c7233 100644 --- a/README.md +++ b/README.md @@ -443,32 +443,72 @@ of the time a higher-level object will suffice as an argument. **Bad:** ```php -function createMenu(string $title, string $body, string $buttonText, bool $cancellable): void +class Questionnaire { - // ... + public function __construct( + string $firstname, + string $lastname, + string $patronymic, + string $region, + string $district, + string $city, + string $phone, + string $email + ) { + // ... + } } ``` **Good:** ```php -class MenuConfig +class Name { - public $title; - public $body; - public $buttonText; - public $cancellable = false; + private $firstname = ''; + private $lastname = ''; + private $patronymic = ''; + + public function __construct(string $firstname, string $lastname, string $patronymic) + { + $this->firstname = $firstname; + $this->lastname = $lastname; + $this->patronymic = $patronymic; + } } -$config = new MenuConfig(); -$config->title = 'Foo'; -$config->body = 'Bar'; -$config->buttonText = 'Baz'; -$config->cancellable = true; - -function createMenu(MenuConfig $config): void +class City { - // ... + private $region = ''; + private $district = ''; + private $city = ''; + + public function __construct(string $region, string $district, string $city) + { + $this->region = $region; + $this->district = $district; + $this->city = $city; + } +} + +class Contact +{ + private $phone = ''; + private $email = ''; + + public function __construct(string $phone, string $email) + { + $this->phone = $phone; + $this->email = $email; + } +} + +class Questionnaire +{ + public function __construct(Name $name, City $city, Contact $contact) + { + // ... + } } ``` From 4f11e5324e709f14868956273e30a981a4c76b66 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 23 Jul 2019 16:05:10 +0300 Subject: [PATCH 2/3] add getters comment --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 82c7233..ed9e37d 100644 --- a/README.md +++ b/README.md @@ -475,6 +475,8 @@ class Name $this->lastname = $lastname; $this->patronymic = $patronymic; } + + // getters ... } class City @@ -489,6 +491,8 @@ class City $this->district = $district; $this->city = $city; } + + // getters ... } class Contact @@ -501,6 +505,8 @@ class Contact $this->phone = $phone; $this->email = $email; } + + // getters ... } class Questionnaire From 5969ca97e63e7055548cb93830689cb789e829b8 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 26 Jul 2019 12:20:40 +0300 Subject: [PATCH 3/3] not set default values of string VO properties --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ed9e37d..50219cb 100644 --- a/README.md +++ b/README.md @@ -465,9 +465,9 @@ class Questionnaire ```php class Name { - private $firstname = ''; - private $lastname = ''; - private $patronymic = ''; + private $firstname; + private $lastname; + private $patronymic; public function __construct(string $firstname, string $lastname, string $patronymic) { @@ -481,9 +481,9 @@ class Name class City { - private $region = ''; - private $district = ''; - private $city = ''; + private $region; + private $district; + private $city; public function __construct(string $region, string $district, string $city) { @@ -497,8 +497,8 @@ class City class Contact { - private $phone = ''; - private $email = ''; + private $phone; + private $email; public function __construct(string $phone, string $email) {