mirror of
https://github.com/jupeter/clean-code-php.git
synced 2025-09-25 21:49:04 +02:00
correct CS
This commit is contained in:
52
README.md
52
README.md
@@ -1279,6 +1279,7 @@ class Manager {
|
|||||||
**[⬆ back to top](#table-of-contents)**
|
**[⬆ back to top](#table-of-contents)**
|
||||||
|
|
||||||
### Dependency Inversion Principle (DIP)
|
### Dependency Inversion Principle (DIP)
|
||||||
|
|
||||||
This principle states two essential things:
|
This principle states two essential things:
|
||||||
1. High-level modules should not depend on low-level modules. Both should
|
1. High-level modules should not depend on low-level modules. Both should
|
||||||
depend on abstractions.
|
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.
|
it makes your code hard to refactor.
|
||||||
|
|
||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
class Worker {
|
class Worker
|
||||||
public function work() {
|
{
|
||||||
|
public function work()
|
||||||
|
{
|
||||||
// ....working
|
// ....working
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Manager {
|
class Manager
|
||||||
/** @var Worker $worker **/
|
{
|
||||||
private $worker;
|
private $worker;
|
||||||
|
|
||||||
public function __construct(Worker $worker) {
|
public function __construct(Worker $worker)
|
||||||
|
{
|
||||||
$this->worker = $worker;
|
$this->worker = $worker;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function manage() {
|
public function manage()
|
||||||
|
{
|
||||||
$this->worker->work();
|
$this->worker->work();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SuperWorker extends Worker {
|
class SuperWorker extends Worker
|
||||||
public function work() {
|
{
|
||||||
|
public function work()
|
||||||
|
{
|
||||||
//.... working much more
|
//.... working much more
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
interface WorkerInterface {
|
interface WorkerInterface
|
||||||
|
{
|
||||||
public function work();
|
public function work();
|
||||||
}
|
}
|
||||||
|
|
||||||
class Worker implements WorkerInterface {
|
class Worker implements WorkerInterface
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Manager {
|
class Manager
|
||||||
/** @var Worker $worker **/
|
{
|
||||||
private $worker;
|
private $worker;
|
||||||
|
|
||||||
public function __construct(WorkerInterface $worker) {
|
public function __construct(WorkerInterface $worker)
|
||||||
|
{
|
||||||
$this->worker = $worker;
|
$this->worker = $worker;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function manage() {
|
public function manage()
|
||||||
|
{
|
||||||
$this->worker->work();
|
$this->worker->work();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**[⬆ back to top](#table-of-contents)**
|
**[⬆ back to top](#table-of-contents)**
|
||||||
|
|
||||||
### Use method chaining
|
### Use method chaining
|
||||||
|
Reference in New Issue
Block a user