From e15b99fb59349c237a8c47a4f89caf3e1b8fbb14 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 31 Aug 2017 16:59:29 +0300 Subject: [PATCH 1/9] correct CS --- README.md | 83 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index c8e650c..266f817 100644 --- a/README.md +++ b/README.md @@ -1176,6 +1176,7 @@ renderLargeRectangles($shapes); **[⬆ back to top](#table-of-contents)** ### Interface Segregation Principle (ISP) + ISP states that "Clients should not be forced to depend upon interfaces that they do not use." @@ -1185,97 +1186,123 @@ huge amounts of options is beneficial, because most of the time they won't need all of the settings. Making them optional helps prevent having a "fat interface". **Bad:** + ```php -interface WorkerInterface { +interface WorkerInterface +{ public function work(); public function eat(); } -class Worker implements WorkerInterface { - public function work() { +class Worker implements WorkerInterface +{ + public function work() + { // ....working } - public function eat() { + + public function eat() + { // ...... eating in launch break } } -class SuperWorker implements WorkerInterface { - public function work() { +class SuperWorker implements WorkerInterface +{ + public function work() + { //.... working much more } - public function eat() { + public function eat() + { //.... eating in launch break } } -class Manager { - /** @var WorkerInterface $worker **/ - private $worker; - - public function setWorker(WorkerInterface $worker) { +class Manager +{ + private $worker; + + public function setWorker(WorkerInterface $worker) + { $this->worker = $worker; } - public function manage() { + public function manage() + { $this->worker->work(); } } ``` **Good:** + ```php -interface WorkerInterface extends FeedableInterface, WorkableInterface { +interface WorkerInterface extends FeedableInterface, WorkableInterface +{ } -interface WorkableInterface { +interface WorkableInterface +{ public function work(); } -interface FeedableInterface { +interface FeedableInterface +{ public function eat(); } -class Worker implements WorkableInterface, FeedableInterface { - public function work() { +class Worker implements WorkableInterface, FeedableInterface +{ + public function work() + { // ....working } - public function eat() { + public function eat() + { //.... eating in launch break } } -class Robot implements WorkableInterface { - public function work() { +class Robot implements WorkableInterface +{ + public function work() + { // ....working } } -class SuperWorker implements WorkerInterface { - public function work() { +class SuperWorker implements WorkerInterface +{ + public function work() + { //.... working much more } - public function eat() { + public function eat() + { //.... eating in launch break } } -class Manager { - /** @var $worker WorkableInterface **/ +class Manager +{ private $worker; - public function setWorker(WorkableInterface $w) { + public function setWorker(WorkableInterface $w) + { $this->worker = $w; } - public function manage() { + public function manage() + { $this->worker->work(); } } ``` + **[⬆ back to top](#table-of-contents)** ### Dependency Inversion Principle (DIP) From 83eef477e9d8d53ea4165e528734930e41fc03c8 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 31 Aug 2017 17:06:55 +0300 Subject: [PATCH 2/9] rename Worker to Employe --- README.md | 53 ++++++++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 266f817..1f2d2e9 100644 --- a/README.md +++ b/README.md @@ -1188,13 +1188,13 @@ all of the settings. Making them optional helps prevent having a "fat interface" **Bad:** ```php -interface WorkerInterface +interface Employe { public function work(); public function eat(); } -class Worker implements WorkerInterface +class Human implements Employe { public function work() { @@ -1207,7 +1207,7 @@ class Worker implements WorkerInterface } } -class SuperWorker implements WorkerInterface +class Robot implements Employe { public function work() { @@ -1216,22 +1216,22 @@ class SuperWorker implements WorkerInterface public function eat() { - //.... eating in launch break + //.... robot can't eating, but it must implement this method } } class Manager { - private $worker; + private $employe; - public function setWorker(WorkerInterface $worker) + public function setWorker(Employe $employe) { - $this->worker = $worker; + $this->employe = $employe; } public function manage() { - $this->worker->work(); + $this->employe->work(); } } ``` @@ -1239,21 +1239,21 @@ class Manager **Good:** ```php -interface WorkerInterface extends FeedableInterface, WorkableInterface -{ -} - -interface WorkableInterface +interface Workable { public function work(); } -interface FeedableInterface +interface Feedable { public function eat(); } -class Worker implements WorkableInterface, FeedableInterface +interface Employe extends Feedable, Workable +{ +} + +class Human implements Employe { public function work() { @@ -1266,7 +1266,7 @@ class Worker implements WorkableInterface, FeedableInterface } } -class Robot implements WorkableInterface +class Robot implements Workable { public function work() { @@ -1274,31 +1274,18 @@ class Robot implements WorkableInterface } } -class SuperWorker implements WorkerInterface -{ - public function work() - { - //.... working much more - } - - public function eat() - { - //.... eating in launch break - } -} - class Manager { - private $worker; + private $employe; - public function setWorker(WorkableInterface $w) + public function setWorker(Workable $employe) { - $this->worker = $w; + $this->employe = $employe; } public function manage() { - $this->worker->work(); + $this->employe->work(); } } ``` From d2d59eae49929c4323869a6783d2939d75ef5543 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 31 Aug 2017 17:11:03 +0300 Subject: [PATCH 3/9] rename method Manager::setWorker() to Manager::subdue() --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1f2d2e9..c680535 100644 --- a/README.md +++ b/README.md @@ -1224,7 +1224,7 @@ class Manager { private $employe; - public function setWorker(Employe $employe) + public function subdue(Employe $employe) { $this->employe = $employe; } @@ -1266,6 +1266,7 @@ class Human implements Employe } } +// robot can only work class Robot implements Workable { public function work() @@ -1278,7 +1279,7 @@ class Manager { private $employe; - public function setWorker(Workable $employe) + public function subdue(Workable $employe) { $this->employe = $employe; } From 84ca341bfa44121db2e62d3347d4ca3211c18e29 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 31 Aug 2017 17:13:13 +0300 Subject: [PATCH 4/9] manager can manage several employees --- README.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c680535..dfc6845 100644 --- a/README.md +++ b/README.md @@ -1222,16 +1222,18 @@ class Robot implements Employe class Manager { - private $employe; + private $employees; public function subdue(Employe $employe) { - $this->employe = $employe; + $this->employees[] = $employe; } public function manage() { - $this->employe->work(); + foreach ($this->employees as $employe) { + $employe->work(); + } } } ``` @@ -1277,16 +1279,18 @@ class Robot implements Workable class Manager { - private $employe; + private $employees; public function subdue(Workable $employe) { - $this->employe = $employe; + $this->employees[] = $employe; } public function manage() { - $this->employe->work(); + foreach ($this->employees as $employe) { + $employe->work(); + } } } ``` From e24c70fe7c04af7f0302347ac5ce615beff1016e Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 31 Aug 2017 17:16:51 +0300 Subject: [PATCH 5/9] manager is also an employe. his work is to manage others --- README.md | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index dfc6845..73937b2 100644 --- a/README.md +++ b/README.md @@ -1191,6 +1191,7 @@ all of the settings. Making them optional helps prevent having a "fat interface" interface Employe { public function work(); + public function eat(); } @@ -1220,21 +1221,26 @@ class Robot implements Employe } } -class Manager +class Manager implements Employe { - private $employees; + private $employees = []; public function subdue(Employe $employe) { $this->employees[] = $employe; } - public function manage() + public function work() { foreach ($this->employees as $employe) { $employe->work(); } } + + public function eat() + { + // ...... eating in launch break + } } ``` @@ -1277,21 +1283,26 @@ class Robot implements Workable } } -class Manager +class Manager implements Employe { - private $employees; + private $employees = []; public function subdue(Workable $employe) { $this->employees[] = $employe; } - public function manage() + public function work() { foreach ($this->employees as $employe) { $employe->work(); } } + + public function eat() + { + // ...... eating in launch break + } } ``` From a64e611fa6a4fc384f438ed7afbfded5ce4ccaa8 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 31 Aug 2017 17:25:11 +0300 Subject: [PATCH 6/9] correct word "employee" --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 73937b2..294839b 100644 --- a/README.md +++ b/README.md @@ -1188,14 +1188,14 @@ all of the settings. Making them optional helps prevent having a "fat interface" **Bad:** ```php -interface Employe +interface Employee { public function work(); public function eat(); } -class Human implements Employe +class Human implements Employee { public function work() { @@ -1208,7 +1208,7 @@ class Human implements Employe } } -class Robot implements Employe +class Robot implements Employee { public function work() { @@ -1221,19 +1221,19 @@ class Robot implements Employe } } -class Manager implements Employe +class Manager implements Employee { private $employees = []; - public function subdue(Employe $employe) + public function subdue(Employee $employee) { - $this->employees[] = $employe; + $this->employees[] = $employee; } public function work() { - foreach ($this->employees as $employe) { - $employe->work(); + foreach ($this->employees as $employee) { + $employee->work(); } } @@ -1257,11 +1257,11 @@ interface Feedable public function eat(); } -interface Employe extends Feedable, Workable +interface Employee extends Feedable, Workable { } -class Human implements Employe +class Human implements Employee { public function work() { @@ -1283,19 +1283,19 @@ class Robot implements Workable } } -class Manager implements Employe +class Manager implements Employee { private $employees = []; - public function subdue(Workable $employe) + public function subdue(Workable $employee) { - $this->employees[] = $employe; + $this->employees[] = $employee; } public function work() { - foreach ($this->employees as $employe) { - $employe->work(); + foreach ($this->employees as $employee) { + $employee->work(); } } From 80bd47f9fa8ca52682841e43948dac5bb5196354 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 31 Aug 2017 18:27:33 +0300 Subject: [PATCH 7/9] Manager manage workers --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 294839b..16e339c 100644 --- a/README.md +++ b/README.md @@ -1285,17 +1285,17 @@ class Robot implements Workable class Manager implements Employee { - private $employees = []; + private $workers = []; - public function subdue(Workable $employee) + public function subdue(Workable $worker) { - $this->employees[] = $employee; + $this->workers[] = $worker; } public function work() { - foreach ($this->employees as $employee) { - $employee->work(); + foreach ($this->workers as $worker) { + $worker->work(); } } From e31b5b86cee98d1ee40a8ba06d94c2e8c5aefa72 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 31 Aug 2017 18:33:00 +0300 Subject: [PATCH 8/9] add comment --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 16e339c..ad4a175 100644 --- a/README.md +++ b/README.md @@ -1246,6 +1246,8 @@ class Manager implements Employee **Good:** +Not every worker is an employee, but every employee is an worker. + ```php interface Workable { From 2992a4f3fd1d9cd9d673391d363ade0e15b1daea Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 1 Sep 2017 17:34:44 +0300 Subject: [PATCH 9/9] remove Manager in ISP --- README.md | 44 -------------------------------------------- 1 file changed, 44 deletions(-) diff --git a/README.md b/README.md index ad4a175..839d3b5 100644 --- a/README.md +++ b/README.md @@ -1220,28 +1220,6 @@ class Robot implements Employee //.... robot can't eating, but it must implement this method } } - -class Manager implements Employee -{ - private $employees = []; - - public function subdue(Employee $employee) - { - $this->employees[] = $employee; - } - - public function work() - { - foreach ($this->employees as $employee) { - $employee->work(); - } - } - - public function eat() - { - // ...... eating in launch break - } -} ``` **Good:** @@ -1284,28 +1262,6 @@ class Robot implements Workable // ....working } } - -class Manager implements Employee -{ - private $workers = []; - - public function subdue(Workable $worker) - { - $this->workers[] = $worker; - } - - public function work() - { - foreach ($this->workers as $worker) { - $worker->work(); - } - } - - public function eat() - { - // ...... eating in launch break - } -} ``` **[⬆ back to top](#table-of-contents)**