mirror of
https://github.com/jupeter/clean-code-php.git
synced 2025-09-29 23:49:04 +02:00
correct CS
This commit is contained in:
81
README.md
81
README.md
@@ -1176,6 +1176,7 @@ renderLargeRectangles($shapes);
|
|||||||
**[⬆ back to top](#table-of-contents)**
|
**[⬆ back to top](#table-of-contents)**
|
||||||
|
|
||||||
### Interface Segregation Principle (ISP)
|
### Interface Segregation Principle (ISP)
|
||||||
|
|
||||||
ISP states that "Clients should not be forced to depend upon interfaces that
|
ISP states that "Clients should not be forced to depend upon interfaces that
|
||||||
they do not use."
|
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".
|
all of the settings. Making them optional helps prevent having a "fat interface".
|
||||||
|
|
||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
interface WorkerInterface {
|
interface WorkerInterface
|
||||||
|
{
|
||||||
public function work();
|
public function work();
|
||||||
public function eat();
|
public function eat();
|
||||||
}
|
}
|
||||||
|
|
||||||
class Worker implements WorkerInterface {
|
class Worker implements WorkerInterface
|
||||||
public function work() {
|
{
|
||||||
|
public function work()
|
||||||
|
{
|
||||||
// ....working
|
// ....working
|
||||||
}
|
}
|
||||||
public function eat() {
|
|
||||||
|
public function eat()
|
||||||
|
{
|
||||||
// ...... eating in launch break
|
// ...... eating in launch break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SuperWorker implements WorkerInterface {
|
class SuperWorker implements WorkerInterface
|
||||||
public function work() {
|
{
|
||||||
|
public function work()
|
||||||
|
{
|
||||||
//.... working much more
|
//.... working much more
|
||||||
}
|
}
|
||||||
|
|
||||||
public function eat() {
|
public function eat()
|
||||||
|
{
|
||||||
//.... eating in launch break
|
//.... eating in launch break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Manager {
|
class Manager
|
||||||
/** @var WorkerInterface $worker **/
|
{
|
||||||
private $worker;
|
private $worker;
|
||||||
|
|
||||||
public function setWorker(WorkerInterface $worker) {
|
public function setWorker(WorkerInterface $worker)
|
||||||
|
{
|
||||||
$this->worker = $worker;
|
$this->worker = $worker;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function manage() {
|
public function manage()
|
||||||
|
{
|
||||||
$this->worker->work();
|
$this->worker->work();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
interface WorkerInterface extends FeedableInterface, WorkableInterface {
|
interface WorkerInterface extends FeedableInterface, WorkableInterface
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
interface WorkableInterface {
|
interface WorkableInterface
|
||||||
|
{
|
||||||
public function work();
|
public function work();
|
||||||
}
|
}
|
||||||
|
|
||||||
interface FeedableInterface {
|
interface FeedableInterface
|
||||||
|
{
|
||||||
public function eat();
|
public function eat();
|
||||||
}
|
}
|
||||||
|
|
||||||
class Worker implements WorkableInterface, FeedableInterface {
|
class Worker implements WorkableInterface, FeedableInterface
|
||||||
public function work() {
|
{
|
||||||
|
public function work()
|
||||||
|
{
|
||||||
// ....working
|
// ....working
|
||||||
}
|
}
|
||||||
|
|
||||||
public function eat() {
|
public function eat()
|
||||||
|
{
|
||||||
//.... eating in launch break
|
//.... eating in launch break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Robot implements WorkableInterface {
|
class Robot implements WorkableInterface
|
||||||
public function work() {
|
{
|
||||||
|
public function work()
|
||||||
|
{
|
||||||
// ....working
|
// ....working
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SuperWorker implements WorkerInterface {
|
class SuperWorker implements WorkerInterface
|
||||||
public function work() {
|
{
|
||||||
|
public function work()
|
||||||
|
{
|
||||||
//.... working much more
|
//.... working much more
|
||||||
}
|
}
|
||||||
|
|
||||||
public function eat() {
|
public function eat()
|
||||||
|
{
|
||||||
//.... eating in launch break
|
//.... eating in launch break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Manager {
|
class Manager
|
||||||
/** @var $worker WorkableInterface **/
|
{
|
||||||
private $worker;
|
private $worker;
|
||||||
|
|
||||||
public function setWorker(WorkableInterface $w) {
|
public function setWorker(WorkableInterface $w)
|
||||||
|
{
|
||||||
$this->worker = $w;
|
$this->worker = $w;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function manage() {
|
public function manage()
|
||||||
|
{
|
||||||
$this->worker->work();
|
$this->worker->work();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**[⬆ back to top](#table-of-contents)**
|
**[⬆ back to top](#table-of-contents)**
|
||||||
|
|
||||||
### Dependency Inversion Principle (DIP)
|
### Dependency Inversion Principle (DIP)
|
||||||
|
Reference in New Issue
Block a user