From b82c80d371c306b2608de20181cec235e16cfe8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emin=20=C5=9Een?= Date: Wed, 6 Sep 2017 09:20:12 +0300 Subject: [PATCH] Correct CS in objects --- README.md | 186 +++++++++++++++++++++++++++++------------------------- 1 file changed, 99 insertions(+), 87 deletions(-) diff --git a/README.md b/README.md index bf69a48..32af8d5 100644 --- a/README.md +++ b/README.md @@ -1041,20 +1041,24 @@ your codebase. **Bad:** ```php -class UserSettings { +class UserSettings +{ private $user; - public function __construct($user) { + public function __construct($user) + { $this->user = $user; } - public function changeSettings($settings) { + public function changeSettings($settings) + { if ($this->verifyCredentials()) { // ... } } - private function verifyCredentials() { + private function verifyCredentials() + { // ... } } @@ -1062,28 +1066,35 @@ class UserSettings { **Good:** ```php -class UserAuth { +class UserAuth +{ private $user; - public function __construct($user) { + public function __construct($user) + { $this->user = $user; } - public function verifyCredentials() { + public function verifyCredentials() + { // ... } } -class UserSettings { +class UserSettings +{ private $user; + private $auth; - public function __construct($user) { + public function __construct($user) + { $this->user = $user; $this->auth = new UserAuth($user); } - - public function changeSettings($settings) { + + public function changeSettings($settings) + { if ($this->auth->verifyCredentials()) { // ... } @@ -1117,6 +1128,7 @@ class AjaxAdapter extends Adapter public function __construct() { parent::__construct(); + $this->name = 'ajaxAdapter'; } } @@ -1126,6 +1138,7 @@ class NodeAdapter extends Adapter public function __construct() { parent::__construct(); + $this->name = 'nodeAdapter'; } } @@ -1138,7 +1151,7 @@ class HttpRequester { $this->adapter = $adapter; } - + public function fetch($url) { $adapterName = $this->adapter->getName(); @@ -1149,12 +1162,12 @@ class HttpRequester return $this->makeHttpCall($url); } } - + protected function makeAjaxCall($url) { // request and return promise } - + protected function makeHttpCall($url) { // request and return promise @@ -1224,14 +1237,8 @@ get into trouble. ```php class Rectangle { - protected $width; - protected $height; - - public function __construct() - { - $this->width = 0; - $this->height = 0; - } + protected $width = 0; + protected $height = 0; public function render($area) { @@ -1286,8 +1293,8 @@ renderLargeRectangles($rectangles); ```php abstract class Shape { - protected $width; - protected $height; + protected $width = 0; + protected $height = 0; abstract public function getArea(); @@ -1299,13 +1306,6 @@ abstract class Shape class Rectangle extends Shape { - public function __construct() - { - parent::__construct(); - $this->width = 0; - $this->height = 0; - } - public function setWidth($width) { $this->width = $width; @@ -1324,11 +1324,7 @@ class Rectangle extends Shape class Square extends Shape { - public function __construct() - { - parent::__construct(); - $this->length = 0; - } + protected $length = 0; public function setLength($length) { @@ -1554,28 +1550,29 @@ and you can chain further class methods onto it. **Bad:** ```php -class Car { - private $make, $model, $color; - - public function __construct() { - $this->make = 'Honda'; - $this->model = 'Accord'; - $this->color = 'white'; - } - - public function setMake($make) { +class Car +{ + private $make = 'Honda'; + private $model = 'Accord'; + private $color = 'white'; + + public function setMake($make) + { $this->make = $make; } - - public function setModel($model) { + + public function setModel($model) + { $this->model = $model; } - - public function setColor($color) { + + public function setColor($color) + { $this->color = $color; } - - public function dump() { + + public function dump() + { var_dump($this->make, $this->model, $this->color); } } @@ -1589,37 +1586,38 @@ $car->dump(); **Good:** ```php -class Car { - private $make, $model, $color; - - public function __construct() { - $this->make = 'Honda'; - $this->model = 'Accord'; - $this->color = 'white'; - } - - public function setMake($make) { +class Car +{ + private $make = 'Honda'; + private $model = 'Accord'; + private $color = 'white'; + + public function setMake($make) + { $this->make = $make; // NOTE: Returning this for chaining return $this; } - - public function setModel($model) { + + public function setModel($model) + { $this->model = $model; - + // NOTE: Returning this for chaining return $this; } - - public function setColor($color) { + + public function setColor($color) + { $this->color = $color; - + // NOTE: Returning this for chaining return $this; } - - public function dump() { + + public function dump() + { var_dump($this->make, $this->model, $this->color); } } @@ -1652,54 +1650,68 @@ relationship (Human->Animal vs. User->UserDetails). **Bad:** ```php -class Employee { - private $name, $email; +class Employee +{ + private $name; + private $email; - public function __construct($name, $email) { + public function __construct($name, $email) + { $this->name = $name; $this->email = $email; } - + // ... } // Bad because Employees "have" tax data. // EmployeeTaxData is not a type of Employee -class EmployeeTaxData extends Employee { - private $ssn, $salary; +class EmployeeTaxData extends Employee +{ + private $ssn; + private $salary; - public function __construct($name, $email, $ssn, $salary) { + public function __construct($name, $email, $ssn, $salary) + { parent::__construct($name, $email); + $this->ssn = $ssn; $this->salary = $salary; } - + // ... } ``` **Good:** ```php -class EmployeeTaxData { - private $ssn, $salary; +class EmployeeTaxData +{ + private $ssn; + private $salary; - public function __construct($ssn, $salary) { + public function __construct($ssn, $salary) + { $this->ssn = $ssn; $this->salary = $salary; } - + // ... } -class Employee { - private $name, $email, $taxData; - - public function __construct($name, $email) { +class Employee +{ + private $name; + private $email; + private $taxData; + + public function __construct($name, $email) + { $this->name = $name; $this->email = $email; } - + public function setTaxData($ssn, $salary) { $this->taxData = new EmployeeTaxData($ssn, $salary); }