mirror of
https://github.com/jupeter/clean-code-php.git
synced 2025-09-25 21:49:04 +02:00
Correct CS in objects
This commit is contained in:
186
README.md
186
README.md
@@ -1041,20 +1041,24 @@ your codebase.
|
|||||||
|
|
||||||
**Bad:**
|
**Bad:**
|
||||||
```php
|
```php
|
||||||
class UserSettings {
|
class UserSettings
|
||||||
|
{
|
||||||
private $user;
|
private $user;
|
||||||
|
|
||||||
public function __construct($user) {
|
public function __construct($user)
|
||||||
|
{
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function changeSettings($settings) {
|
public function changeSettings($settings)
|
||||||
|
{
|
||||||
if ($this->verifyCredentials()) {
|
if ($this->verifyCredentials()) {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function verifyCredentials() {
|
private function verifyCredentials()
|
||||||
|
{
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1062,28 +1066,35 @@ class UserSettings {
|
|||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
```php
|
```php
|
||||||
class UserAuth {
|
class UserAuth
|
||||||
|
{
|
||||||
private $user;
|
private $user;
|
||||||
|
|
||||||
public function __construct($user) {
|
public function __construct($user)
|
||||||
|
{
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function verifyCredentials() {
|
public function verifyCredentials()
|
||||||
|
{
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class UserSettings {
|
class UserSettings
|
||||||
|
{
|
||||||
private $user;
|
private $user;
|
||||||
|
private $auth;
|
||||||
|
|
||||||
public function __construct($user) {
|
public function __construct($user)
|
||||||
|
{
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
$this->auth = new UserAuth($user);
|
$this->auth = new UserAuth($user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function changeSettings($settings) {
|
public function changeSettings($settings)
|
||||||
|
{
|
||||||
if ($this->auth->verifyCredentials()) {
|
if ($this->auth->verifyCredentials()) {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
@@ -1117,6 +1128,7 @@ class AjaxAdapter extends Adapter
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->name = 'ajaxAdapter';
|
$this->name = 'ajaxAdapter';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1126,6 +1138,7 @@ class NodeAdapter extends Adapter
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->name = 'nodeAdapter';
|
$this->name = 'nodeAdapter';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1138,7 +1151,7 @@ class HttpRequester
|
|||||||
{
|
{
|
||||||
$this->adapter = $adapter;
|
$this->adapter = $adapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fetch($url)
|
public function fetch($url)
|
||||||
{
|
{
|
||||||
$adapterName = $this->adapter->getName();
|
$adapterName = $this->adapter->getName();
|
||||||
@@ -1149,12 +1162,12 @@ class HttpRequester
|
|||||||
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
|
||||||
@@ -1224,14 +1237,8 @@ get into trouble.
|
|||||||
```php
|
```php
|
||||||
class Rectangle
|
class Rectangle
|
||||||
{
|
{
|
||||||
protected $width;
|
protected $width = 0;
|
||||||
protected $height;
|
protected $height = 0;
|
||||||
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
$this->width = 0;
|
|
||||||
$this->height = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function render($area)
|
public function render($area)
|
||||||
{
|
{
|
||||||
@@ -1286,8 +1293,8 @@ renderLargeRectangles($rectangles);
|
|||||||
```php
|
```php
|
||||||
abstract class Shape
|
abstract class Shape
|
||||||
{
|
{
|
||||||
protected $width;
|
protected $width = 0;
|
||||||
protected $height;
|
protected $height = 0;
|
||||||
|
|
||||||
abstract public function getArea();
|
abstract public function getArea();
|
||||||
|
|
||||||
@@ -1299,13 +1306,6 @@ abstract class Shape
|
|||||||
|
|
||||||
class Rectangle extends Shape
|
class Rectangle extends Shape
|
||||||
{
|
{
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
parent::__construct();
|
|
||||||
$this->width = 0;
|
|
||||||
$this->height = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setWidth($width)
|
public function setWidth($width)
|
||||||
{
|
{
|
||||||
$this->width = $width;
|
$this->width = $width;
|
||||||
@@ -1324,11 +1324,7 @@ class Rectangle extends Shape
|
|||||||
|
|
||||||
class Square extends Shape
|
class Square extends Shape
|
||||||
{
|
{
|
||||||
public function __construct()
|
protected $length = 0;
|
||||||
{
|
|
||||||
parent::__construct();
|
|
||||||
$this->length = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setLength($length)
|
public function setLength($length)
|
||||||
{
|
{
|
||||||
@@ -1554,28 +1550,29 @@ and you can chain further class methods onto it.
|
|||||||
|
|
||||||
**Bad:**
|
**Bad:**
|
||||||
```php
|
```php
|
||||||
class Car {
|
class Car
|
||||||
private $make, $model, $color;
|
{
|
||||||
|
private $make = 'Honda';
|
||||||
public function __construct() {
|
private $model = 'Accord';
|
||||||
$this->make = 'Honda';
|
private $color = 'white';
|
||||||
$this->model = 'Accord';
|
|
||||||
$this->color = 'white';
|
public function setMake($make)
|
||||||
}
|
{
|
||||||
|
|
||||||
public function setMake($make) {
|
|
||||||
$this->make = $make;
|
$this->make = $make;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setModel($model) {
|
public function setModel($model)
|
||||||
|
{
|
||||||
$this->model = $model;
|
$this->model = $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setColor($color) {
|
public function setColor($color)
|
||||||
|
{
|
||||||
$this->color = $color;
|
$this->color = $color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dump() {
|
public function dump()
|
||||||
|
{
|
||||||
var_dump($this->make, $this->model, $this->color);
|
var_dump($this->make, $this->model, $this->color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1589,37 +1586,38 @@ $car->dump();
|
|||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
```php
|
```php
|
||||||
class Car {
|
class Car
|
||||||
private $make, $model, $color;
|
{
|
||||||
|
private $make = 'Honda';
|
||||||
public function __construct() {
|
private $model = 'Accord';
|
||||||
$this->make = 'Honda';
|
private $color = 'white';
|
||||||
$this->model = 'Accord';
|
|
||||||
$this->color = 'white';
|
public function setMake($make)
|
||||||
}
|
{
|
||||||
|
|
||||||
public function setMake($make) {
|
|
||||||
$this->make = $make;
|
$this->make = $make;
|
||||||
|
|
||||||
// NOTE: Returning this for chaining
|
// NOTE: Returning this for chaining
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setModel($model) {
|
public function setModel($model)
|
||||||
|
{
|
||||||
$this->model = $model;
|
$this->model = $model;
|
||||||
|
|
||||||
// NOTE: Returning this for chaining
|
// NOTE: Returning this for chaining
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setColor($color) {
|
public function setColor($color)
|
||||||
|
{
|
||||||
$this->color = $color;
|
$this->color = $color;
|
||||||
|
|
||||||
// NOTE: Returning this for chaining
|
// NOTE: Returning this for chaining
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dump() {
|
public function dump()
|
||||||
|
{
|
||||||
var_dump($this->make, $this->model, $this->color);
|
var_dump($this->make, $this->model, $this->color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1652,54 +1650,68 @@ relationship (Human->Animal vs. User->UserDetails).
|
|||||||
|
|
||||||
**Bad:**
|
**Bad:**
|
||||||
```php
|
```php
|
||||||
class Employee {
|
class Employee
|
||||||
private $name, $email;
|
{
|
||||||
|
private $name;
|
||||||
|
private $email;
|
||||||
|
|
||||||
public function __construct($name, $email) {
|
public function __construct($name, $email)
|
||||||
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
$this->email = $email;
|
$this->email = $email;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bad because Employees "have" tax data.
|
// Bad because Employees "have" tax data.
|
||||||
// EmployeeTaxData is not a type of Employee
|
// EmployeeTaxData is not a type of Employee
|
||||||
|
|
||||||
class EmployeeTaxData extends Employee {
|
class EmployeeTaxData extends Employee
|
||||||
private $ssn, $salary;
|
{
|
||||||
|
private $ssn;
|
||||||
|
private $salary;
|
||||||
|
|
||||||
public function __construct($name, $email, $ssn, $salary) {
|
public function __construct($name, $email, $ssn, $salary)
|
||||||
|
{
|
||||||
parent::__construct($name, $email);
|
parent::__construct($name, $email);
|
||||||
|
|
||||||
$this->ssn = $ssn;
|
$this->ssn = $ssn;
|
||||||
$this->salary = $salary;
|
$this->salary = $salary;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
```php
|
```php
|
||||||
class EmployeeTaxData {
|
class EmployeeTaxData
|
||||||
private $ssn, $salary;
|
{
|
||||||
|
private $ssn;
|
||||||
|
private $salary;
|
||||||
|
|
||||||
public function __construct($ssn, $salary) {
|
public function __construct($ssn, $salary)
|
||||||
|
{
|
||||||
$this->ssn = $ssn;
|
$this->ssn = $ssn;
|
||||||
$this->salary = $salary;
|
$this->salary = $salary;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
class Employee {
|
class Employee
|
||||||
private $name, $email, $taxData;
|
{
|
||||||
|
private $name;
|
||||||
public function __construct($name, $email) {
|
private $email;
|
||||||
|
private $taxData;
|
||||||
|
|
||||||
|
public function __construct($name, $email)
|
||||||
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
$this->email = $email;
|
$this->email = $email;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTaxData($ssn, $salary) {
|
public function setTaxData($ssn, $salary) {
|
||||||
$this->taxData = new EmployeeTaxData($ssn, $salary);
|
$this->taxData = new EmployeeTaxData($ssn, $salary);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user