mirror of
https://github.com/jupeter/clean-code-php.git
synced 2025-10-01 08:26:41 +02:00
correct CS
This commit is contained in:
74
README.md
74
README.md
@@ -942,98 +942,128 @@ class UserSettings {
|
|||||||
**[⬆ back to top](#table-of-contents)**
|
**[⬆ back to top](#table-of-contents)**
|
||||||
|
|
||||||
### Open/Closed Principle (OCP)
|
### Open/Closed Principle (OCP)
|
||||||
|
|
||||||
As stated by Bertrand Meyer, "software entities (classes, modules, functions,
|
As stated by Bertrand Meyer, "software entities (classes, modules, functions,
|
||||||
etc.) should be open for extension, but closed for modification." What does that
|
etc.) should be open for extension, but closed for modification." What does that
|
||||||
mean though? This principle basically states that you should allow users to
|
mean though? This principle basically states that you should allow users to
|
||||||
add new functionalities without changing existing code.
|
add new functionalities without changing existing code.
|
||||||
|
|
||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
abstract class Adapter {
|
abstract class Adapter
|
||||||
|
{
|
||||||
protected $name;
|
protected $name;
|
||||||
public function getName() {
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AjaxAdapter extends Adapter {
|
class AjaxAdapter extends Adapter
|
||||||
public function __construct() {
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->name = 'ajaxAdapter';
|
$this->name = 'ajaxAdapter';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NodeAdapter extends Adapter {
|
class NodeAdapter extends Adapter
|
||||||
public function __construct() {
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->name = 'nodeAdapter';
|
$this->name = 'nodeAdapter';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HttpRequester {
|
class HttpRequester
|
||||||
|
{
|
||||||
private $adapter;
|
private $adapter;
|
||||||
public function __construct($adapter) {
|
|
||||||
|
public function __construct($adapter)
|
||||||
|
{
|
||||||
$this->adapter = $adapter;
|
$this->adapter = $adapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fetch($url) {
|
public function fetch($url)
|
||||||
|
{
|
||||||
$adapterName = $this->adapter->getName();
|
$adapterName = $this->adapter->getName();
|
||||||
|
|
||||||
if ($adapterName === 'ajaxAdapter') {
|
if ($adapterName === 'ajaxAdapter') {
|
||||||
return $this->makeAjaxCall($url);
|
return $this->makeAjaxCall($url);
|
||||||
} else if ($adapterName === 'httpNodeAdapter') {
|
} elseif ($adapterName === 'httpNodeAdapter') {
|
||||||
return $this->makeHttpCall($url);
|
return $this->makeHttpCall($url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function makeAjaxCall($url) {
|
protected function makeAjaxCall($url)
|
||||||
|
{
|
||||||
// request and return promise
|
// request and return promise
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function makeHttpCall($url) {
|
protected function makeHttpCall($url)
|
||||||
|
{
|
||||||
// request and return promise
|
// request and return promise
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
abstract class Adapter {
|
abstract class Adapter
|
||||||
|
{
|
||||||
abstract protected function getName();
|
abstract protected function getName();
|
||||||
|
|
||||||
abstract public function request($url);
|
abstract public function request($url);
|
||||||
}
|
}
|
||||||
|
|
||||||
class AjaxAdapter extends Adapter {
|
class AjaxAdapter extends Adapter
|
||||||
protected function getName() {
|
{
|
||||||
|
protected function getName()
|
||||||
|
{
|
||||||
return 'ajaxAdapter';
|
return 'ajaxAdapter';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function request($url) {
|
public function request($url)
|
||||||
|
{
|
||||||
// request and return promise
|
// request and return promise
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NodeAdapter extends Adapter {
|
class NodeAdapter extends Adapter
|
||||||
protected function getName() {
|
{
|
||||||
|
protected function getName()
|
||||||
|
{
|
||||||
return 'nodeAdapter';
|
return 'nodeAdapter';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function request($url) {
|
public function request($url)
|
||||||
|
{
|
||||||
// request and return promise
|
// request and return promise
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HttpRequester {
|
class HttpRequester
|
||||||
|
{
|
||||||
private $adapter;
|
private $adapter;
|
||||||
public function __construct(Adapter $adapter) {
|
|
||||||
|
public function __construct(Adapter $adapter)
|
||||||
|
{
|
||||||
$this->adapter = $adapter;
|
$this->adapter = $adapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fetch($url) {
|
public function fetch($url)
|
||||||
|
{
|
||||||
return $this->adapter->request($url);
|
return $this->adapter->request($url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**[⬆ back to top](#table-of-contents)**
|
**[⬆ back to top](#table-of-contents)**
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user