From 2a04e3d6ac254b13e3a9d3375a645db430c9043c Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 31 Aug 2017 17:28:47 +0300 Subject: [PATCH 1/4] correct CS --- README.md | 60 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index c8e650cc..49039170 100644 --- a/README.md +++ b/README.md @@ -1279,6 +1279,7 @@ class Manager { **[⬆ back to top](#table-of-contents)** ### Dependency Inversion Principle (DIP) + This principle states two essential things: 1. High-level modules should not depend on low-level modules. Both should depend on abstractions. @@ -1293,65 +1294,80 @@ the coupling between modules. Coupling is a very bad development pattern because it makes your code hard to refactor. **Bad:** + ```php -class Worker { - public function work() { +class Worker +{ + public function work() + { // ....working } } -class Manager { - /** @var Worker $worker **/ +class Manager +{ private $worker; - - public function __construct(Worker $worker) { + + public function __construct(Worker $worker) + { $this->worker = $worker; } - - public function manage() { + + public function manage() + { $this->worker->work(); } } -class SuperWorker extends Worker { - public function work() { +class SuperWorker extends Worker +{ + public function work() + { //.... working much more } } ``` **Good:** + ```php -interface WorkerInterface { +interface WorkerInterface +{ public function work(); } -class Worker implements WorkerInterface { - public function work() { +class Worker implements WorkerInterface +{ + public function work() + { // ....working } } -class SuperWorker implements WorkerInterface { - public function work() { +class SuperWorker implements WorkerInterface +{ + public function work() + { //.... working much more } } -class Manager { - /** @var Worker $worker **/ +class Manager +{ private $worker; - - public function __construct(WorkerInterface $worker) { + + public function __construct(WorkerInterface $worker) + { $this->worker = $worker; } - - public function manage() { + + public function manage() + { $this->worker->work(); } } - ``` + **[⬆ back to top](#table-of-contents)** ### Use method chaining From 9c1f626a0e29697f4696e9bba4acc92a40d56c7e Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 31 Aug 2017 17:31:41 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D1=81hange=20dependencies=20without=20chan?= =?UTF-8?q?ging=20manager=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 49039170..6990855b 100644 --- a/README.md +++ b/README.md @@ -1304,6 +1304,14 @@ class Worker } } +class SuperWorker extends Worker +{ + public function work() + { + //.... working much more + } +} + class Manager { private $worker; @@ -1318,25 +1326,17 @@ class Manager $this->worker->work(); } } - -class SuperWorker extends Worker -{ - public function work() - { - //.... working much more - } -} ``` **Good:** ```php -interface WorkerInterface +interface Worker { public function work(); } -class Worker implements WorkerInterface +class SumeWorker implements Worker { public function work() { @@ -1344,7 +1344,7 @@ class Worker implements WorkerInterface } } -class SuperWorker implements WorkerInterface +class SuperWorker implements Worker { public function work() { @@ -1356,7 +1356,7 @@ class Manager { private $worker; - public function __construct(WorkerInterface $worker) + public function __construct(Worker $worker) { $this->worker = $worker; } From 2a9cef35718463b1d3a0f3b668c70adc89af7e37 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 5 Sep 2017 19:43:35 +0300 Subject: [PATCH 3/4] remove invalid comment --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 49fb8814..d89bf872 100644 --- a/README.md +++ b/README.md @@ -1447,8 +1447,6 @@ class SuperWorker implements Worker class Manager { - /** @var WorkerInterface $worker **/ - private $worker; public function __construct(Worker $worker) From 6f59430697b13bb6f40cb9bdf633cdcf3378df1b Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 5 Sep 2017 21:43:43 +0300 Subject: [PATCH 4/4] rename classes and interface --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d89bf872..7b3b4717 100644 --- a/README.md +++ b/README.md @@ -1389,7 +1389,7 @@ it makes your code hard to refactor. **Bad:** ```php -class Worker +class Employee { public function work() { @@ -1397,7 +1397,7 @@ class Worker } } -class SuperWorker extends Worker +class Robot extends Employee { public function work() { @@ -1407,16 +1407,16 @@ class SuperWorker extends Worker class Manager { - private $worker; + private $employee; - public function __construct(Worker $worker) + public function __construct(Employee $employee) { - $this->worker = $worker; + $this->employee = $employee; } public function manage() { - $this->worker->work(); + $this->employee->work(); } } ``` @@ -1424,12 +1424,12 @@ class Manager **Good:** ```php -interface Worker +interface Employee { public function work(); } -class SumeWorker implements Worker +class Human implements Employee { public function work() { @@ -1437,7 +1437,7 @@ class SumeWorker implements Worker } } -class SuperWorker implements Worker +class Robot implements Employee { public function work() { @@ -1447,16 +1447,16 @@ class SuperWorker implements Worker class Manager { - private $worker; + private $employee; - public function __construct(Worker $worker) + public function __construct(Employee $employee) { - $this->worker = $worker; + $this->employee = $employee; } public function manage() { - $this->worker->work(); + $this->employee->work(); } } ```