diff --git a/.github/workflows/coding_standard.yaml b/.github/workflows/coding_standard.yaml new file mode 100644 index 00000000..262b528b --- /dev/null +++ b/.github/workflows/coding_standard.yaml @@ -0,0 +1,21 @@ +name: Coding Standard + +on: + pull_request: null + push: null + +jobs: + coding_standard: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + # see https://github.com/shivammathur/setup-php + - uses: shivammathur/setup-php@v2 + with: + php-version: 8.0 + coverage: none + + - run: composer install --no-progress --ansi + + - run: vendor/bin/ecs check-markdown README.md --ansi diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..49c63d28 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +composer.lock +/vendor \ No newline at end of file diff --git a/README.md b/README.md index 339e2e29..b5b80b0e 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,51 @@ -# clean-code-php +# Clean Code PHP ## Table of Contents + 1. [Introduction](#introduction) 2. [Variables](#variables) - 3. [Functions](#functions) - 4. [Objects and Data Structures](#objects-and-data-structures) - 5. [Classes](#classes) - 1. [S: Single Responsibility Principle (SRP)](#single-responsibility-principle-srp) - 2. [O: Open/Closed Principle (OCP)](#openclosed-principle-ocp) - 3. [L: Liskov Substitution Principle (LSP)](#liskov-substitution-principle-lsp) - 4. [I: Interface Segregation Principle (ISP)](#interface-segregation-principle-isp) - 5. [D: Dependency Inversion Principle (DIP)](#dependency-inversion-principle-dip) + * [Use meaningful and pronounceable variable names](#use-meaningful-and-pronounceable-variable-names) + * [Use the same vocabulary for the same type of variable](#use-the-same-vocabulary-for-the-same-type-of-variable) + * [Use searchable names (part 1)](#use-searchable-names-part-1) + * [Use searchable names (part 2)](#use-searchable-names-part-2) + * [Use explanatory variables](#use-explanatory-variables) + * [Avoid nesting too deeply and return early (part 1)](#avoid-nesting-too-deeply-and-return-early-part-1) + * [Avoid nesting too deeply and return early (part 2)](#avoid-nesting-too-deeply-and-return-early-part-2) + * [Avoid Mental Mapping](#avoid-mental-mapping) + * [Don't add unneeded context](#dont-add-unneeded-context) + 3. [Comparison](#comparison) + * [Use identical comparison](#use-identical-comparison) + * [Null coalescing operator](#null-coalescing-operator) + 4. [Functions](#functions) + * [Use default arguments instead of short circuiting or conditionals](#use-default-arguments-instead-of-short-circuiting-or-conditionals) + * [Function arguments (2 or fewer ideally)](#function-arguments-2-or-fewer-ideally) + * [Function names should say what they do](#function-names-should-say-what-they-do) + * [Functions should only be one level of abstraction](#functions-should-only-be-one-level-of-abstraction) + * [Don't use flags as function parameters](#dont-use-flags-as-function-parameters) + * [Avoid Side Effects](#avoid-side-effects) + * [Don't write to global functions](#dont-write-to-global-functions) + * [Don't use a Singleton pattern](#dont-use-a-singleton-pattern) + * [Encapsulate conditionals](#encapsulate-conditionals) + * [Avoid negative conditionals](#avoid-negative-conditionals) + * [Avoid conditionals](#avoid-conditionals) + * [Avoid type-checking (part 1)](#avoid-type-checking-part-1) + * [Avoid type-checking (part 2)](#avoid-type-checking-part-2) + * [Remove dead code](#remove-dead-code) + 5. [Objects and Data Structures](#objects-and-data-structures) + * [Use object encapsulation](#use-object-encapsulation) + * [Make objects have private/protected members](#make-objects-have-privateprotected-members) + 6. [Classes](#classes) + * [Prefer composition over inheritance](#prefer-composition-over-inheritance) + * [Avoid fluent interfaces](#avoid-fluent-interfaces) + * [Prefer final classes](#prefer-final-classes) + 7. [SOLID](#solid) + * [Single Responsibility Principle (SRP)](#single-responsibility-principle-srp) + * [Open/Closed Principle (OCP)](#openclosed-principle-ocp) + * [Liskov Substitution Principle (LSP)](#liskov-substitution-principle-lsp) + * [Interface Segregation Principle (ISP)](#interface-segregation-principle-isp) + * [Dependency Inversion Principle (DIP)](#dependency-inversion-principle-dip) + 8. [Don’t repeat yourself (DRY)](#dont-repeat-yourself-dry) + 9. [Translations](#translations) ## Introduction @@ -19,98 +54,254 @@ Software engineering principles, from Robert C. Martin's book adapted for PHP. This is not a style guide. It's a guide to producing readable, reusable, and refactorable software in PHP. -Not every principle herein has to be strictly followed, and even fewer will be universally -agreed upon. These are guidelines and nothing more, but they are ones codified over many +Not every principle herein has to be strictly followed, and even fewer will be universally +agreed upon. These are guidelines and nothing more, but they are ones codified over many years of collective experience by the authors of *Clean Code*. -Inspired from [clean-code-javascript](https://github.com/ryanmcdermott/clean-code-javascript) +Inspired from [clean-code-javascript](https://github.com/ryanmcdermott/clean-code-javascript). + +Although many developers still use PHP 5, most of the examples in this article only work with PHP 7.1+. + +## Variables -## **Variables** ### Use meaningful and pronounceable variable names **Bad:** + ```php $ymdstr = $moment->format('y-m-d'); ``` -**Good**: +**Good:** + ```php $currentDate = $moment->format('y-m-d'); ``` + **[⬆ back to top](#table-of-contents)** ### Use the same vocabulary for the same type of variable **Bad:** + ```php getUserInfo(); -getClientData(); -getCustomerRecord(); +getUserData(); +getUserRecord(); +getUserProfile(); ``` -**Good**: +**Good:** + ```php getUser(); ``` + **[⬆ back to top](#table-of-contents)** -### Use searchable names -We will read more code than we will ever write. It's important that the code we do write is -readable and searchable. By *not* naming variables that end up being meaningful for +### Use searchable names (part 1) + +We will read more code than we will ever write. It's important that the code we do write is +readable and searchable. By *not* naming variables that end up being meaningful for understanding our program, we hurt our readers. Make your names searchable. **Bad:** + ```php -// What the heck is 86400 for? -addExpireAt(86400); +// What the heck is 448 for? +$result = $serializer->serialize($data, 448); +``` + +**Good:** +```php +$json = $serializer->serialize($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); ``` -**Good**: +### Use searchable names (part 2) + +**Bad:** + ```php -// Declare them as capitalized `const` globals. -interface DateGlobal { - const SECONDS_IN_A_DAY = 86400; +class User +{ + // What the heck is 7 for? + public $access = 7; } -addExpireAt(DateGlobal::SECONDS_IN_A_DAY); +// What the heck is 4 for? +if ($user->access & 4) { + // ... +} + +// What's going on here? +$user->access ^= 2; +``` + +**Good:** + +```php +class User +{ + public const ACCESS_READ = 1; + + public const ACCESS_CREATE = 2; + + public const ACCESS_UPDATE = 4; + + public const ACCESS_DELETE = 8; + + // User as default can read, create and update something + public $access = self::ACCESS_READ | self::ACCESS_CREATE | self::ACCESS_UPDATE; +} + +if ($user->access & User::ACCESS_UPDATE) { + // do edit ... +} + +// Deny access rights to create something +$user->access ^= User::ACCESS_CREATE; ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ back to top](#table-of-contents)** ### Use explanatory variables + **Bad:** + ```php $address = 'One Infinite Loop, Cupertino 95014'; -$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/'; +$cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/'; preg_match($cityZipCodeRegex, $address, $matches); saveCityZipCode($matches[1], $matches[2]); ``` -**Good**: +**Not bad:** + +It's better, but we are still heavily dependent on regex. + ```php $address = 'One Infinite Loop, Cupertino 95014'; -$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/'; +$cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/'; preg_match($cityZipCodeRegex, $address, $matches); -list(, $city, $zipCode) = $matchers; +[, $city, $zipCode] = $matches; saveCityZipCode($city, $zipCode); ``` + +**Good:** + +Decrease dependence on regex by naming subpatterns. + +```php +$address = 'One Infinite Loop, Cupertino 95014'; +$cityZipCodeRegex = '/^[^,]+,\s*(?.+?)\s*(?\d{5})$/'; +preg_match($cityZipCodeRegex, $address, $matches); + +saveCityZipCode($matches['city'], $matches['zipCode']); +``` + +**[⬆ back to top](#table-of-contents)** + +### Avoid nesting too deeply and return early (part 1) + +Too many if-else statements can make your code hard to follow. Explicit is better +than implicit. + +**Bad:** + +```php +function isShopOpen($day): bool +{ + if ($day) { + if (is_string($day)) { + $day = strtolower($day); + if ($day === 'friday') { + return true; + } elseif ($day === 'saturday') { + return true; + } elseif ($day === 'sunday') { + return true; + } + return false; + } + return false; + } + return false; +} +``` + +**Good:** + +```php +function isShopOpen(string $day): bool +{ + if (empty($day)) { + return false; + } + + $openingDays = ['friday', 'saturday', 'sunday']; + + return in_array(strtolower($day), $openingDays, true); +} +``` + +**[⬆ back to top](#table-of-contents)** + +### Avoid nesting too deeply and return early (part 2) + +**Bad:** + +```php +function fibonacci(int $n) +{ + if ($n < 50) { + if ($n !== 0) { + if ($n !== 1) { + return fibonacci($n - 1) + fibonacci($n - 2); + } + return 1; + } + return 0; + } + return 'Not supported'; +} +``` + +**Good:** + +```php +function fibonacci(int $n): int +{ + if ($n === 0 || $n === 1) { + return $n; + } + + if ($n >= 50) { + throw new Exception('Not supported'); + } + + return fibonacci($n - 1) + fibonacci($n - 2); +} +``` + **[⬆ back to top](#table-of-contents)** ### Avoid Mental Mapping + Don’t force the reader of your code to translate what the variable means. Explicit is better than implicit. **Bad:** + ```php $l = ['Austin', 'New York', 'San Francisco']; -foreach($i=0; $i 'Honda', - 'carModel' => 'Accord', - 'carColor' => 'Blue', -]; +class Car +{ + public $carMake; -function paintCar(&$car) { - $car['carColor'] = 'Red'; + public $carModel; + + public $carColor; + + //... } ``` -**Good**: +**Good:** + ```php -$car = [ - 'make' => 'Honda', - 'model' => 'Accord', - 'color' => 'Blue', -]; +class Car +{ + public $make; -function paintCar(&$car) { - $car['color'] = 'Red'; + public $model; + + public $color; + + //... } ``` + **[⬆ back to top](#table-of-contents)** -### Use default arguments instead of short circuiting or conditionals +## Comparison + +### Use [identical comparison](http://php.net/manual/en/language.operators.comparison.php) + +**Not good:** + +The simple comparison will convert the string into an integer. -**Bad:** ```php -function createMicrobrewery($name = null) { - $breweryName = $name ?: 'Hipster Brew Co.'; - // ... +$a = '42'; +$b = 42; + +if ($a != $b) { + // The expression will always pass } +``` +The comparison `$a != $b` returns `FALSE` but in fact it's `TRUE`! +The string `42` is different than the integer `42`. + +**Good:** + +The identical comparison will compare type and value. + +```php +$a = '42'; +$b = 42; + +if ($a !== $b) { + // The expression is verified +} ``` -**Good**: +The comparison `$a !== $b` returns `TRUE`. + +**[⬆ back to top](#table-of-contents)** + +### Null coalescing operator + +Null coalescing is a new operator [introduced in PHP 7](https://www.php.net/manual/en/migration70.new-features.php). The null coalescing operator `??` has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with `isset()`. It returns its first operand if it exists and is not `null`; otherwise it returns its second operand. + +**Bad:** + ```php -function createMicrobrewery($breweryName = 'Hipster Brew Co.') { - // ... +if (isset($_GET['name'])) { + $name = $_GET['name']; +} elseif (isset($_POST['name'])) { + $name = $_POST['name']; +} else { + $name = 'nobody'; } +``` +**Good:** +```php +$name = $_GET['name'] ?? $_POST['name'] ?? 'nobody'; ``` + **[⬆ back to top](#table-of-contents)** -## **Functions** -### Function arguments (2 or fewer ideally) -Limiting the amount of function parameters is incredibly important because it makes -testing your function easier. Having more than three leads to a combinatorial explosion -where you have to test tons of different cases with each separate argument. -Zero arguments is the ideal case. One or two arguments is ok, and three should be avoided. -Anything more than that should be consolidated. Usually, if you have more than two -arguments then your function is trying to do too much. In cases where it's not, most -of the time a higher-level object will suffice as an argument. +## Functions + +### Use default arguments instead of short circuiting or conditionals + +**Not good:** + +This is not good because `$breweryName` can be `NULL`. -**Bad:** ```php -function createMenu($title, $body, $buttonText, $cancellable) { +function createMicrobrewery($breweryName = 'Hipster Brew Co.'): void +{ // ... } ``` -**Good**: +**Not bad:** + +This opinion is more understandable than the previous version, but it better controls the value of the variable. + ```php -class menuConfig() { - public $title; - public $body; - public $buttonText; - public $cancellable = false; +function createMicrobrewery($name = null): void +{ + $breweryName = $name ?: 'Hipster Brew Co.'; + // ... } +``` -$config = new MenuConfig(); -$config->title = 'Foo'; -$config->body = 'Bar'; -$config->buttonText = 'Baz'; -$config->cancellable = true; +**Good:** + + You can use [type hinting](https://www.php.net/manual/en/language.types.declarations.php) and be sure that the `$breweryName` will not be `NULL`. -function createMenu(MenuConfig $config) { +```php +function createMicrobrewery(string $breweryName = 'Hipster Brew Co.'): void +{ // ... } - ``` + **[⬆ back to top](#table-of-contents)** +### Function arguments (2 or fewer ideally) + +Limiting the amount of function parameters is incredibly important because it makes +testing your function easier. Having more than three leads to a combinatorial explosion +where you have to test tons of different cases with each separate argument. -### Functions should do one thing -This is by far the most important rule in software engineering. When functions do more -than one thing, they are harder to compose, test, and reason about. When you can isolate -a function to just one action, they can be refactored easily and your code will read much -cleaner. If you take nothing else away from this guide other than this, you'll be ahead -of many developers. +Zero arguments is the ideal case. One or two arguments is ok, and three should be avoided. +Anything more than that should be consolidated. Usually, if you have more than two +arguments then your function is trying to do too much. In cases where it's not, most +of the time a higher-level object will suffice as an argument. **Bad:** + ```php -function emailClients($clients) { - foreach ($clients as $client) { - $clientRecord = $db->find($client); - if ($clientRecord->isActive()) { - email($client); - } +class Questionnaire +{ + public function __construct( + string $firstname, + string $lastname, + string $patronymic, + string $region, + string $district, + string $city, + string $phone, + string $email + ) { + // ... } } ``` -**Good**: +**Good:** + ```php -function emailClients($clients) { - $activeClients = activeClients($clients); - array_walk($activeClients, 'email'); +class Name +{ + private $firstname; + + private $lastname; + + private $patronymic; + + public function __construct(string $firstname, string $lastname, string $patronymic) + { + $this->firstname = $firstname; + $this->lastname = $lastname; + $this->patronymic = $patronymic; + } + + // getters ... +} + +class City +{ + private $region; + + private $district; + + private $city; + + public function __construct(string $region, string $district, string $city) + { + $this->region = $region; + $this->district = $district; + $this->city = $city; + } + + // getters ... } -function activeClients($clients) { - return array_filter($clients, 'isClientActive'); +class Contact +{ + private $phone; + + private $email; + + public function __construct(string $phone, string $email) + { + $this->phone = $phone; + $this->email = $email; + } + + // getters ... } -function isClientActive($client) { - $clientRecord = $db->find($client); - return $clientRecord->isActive(); +class Questionnaire +{ + public function __construct(Name $name, City $city, Contact $contact) + { + // ... + } } ``` + **[⬆ back to top](#table-of-contents)** ### Function names should say what they do **Bad:** + ```php -function addToDate($date, $month) { - // ... -} +class Email +{ + //... -$date = new \DateTime(); + public function handle(): void + { + mail($this->to, $this->subject, $this->body); + } +} -// It's hard to to tell from the function name what is added -addToDate($date, 1); +$message = new Email(...); +// What is this? A handle for the message? Are we writing to a file now? +$message->handle(); ``` -**Good**: +**Good:** + ```php -function addMonthToDate($month, $date) { - // ... +class Email +{ + //... + + public function send(): void + { + mail($this->to, $this->subject, $this->body); + } } -$date = new \DateTime(); -addMonthToDate(1, $date); +$message = new Email(...); +// Clear and obvious +$message->send(); ``` + **[⬆ back to top](#table-of-contents)** ### Functions should only be one level of abstraction + When you have more than one level of abstraction your function is usually doing too much. Splitting up functions leads to reusability and easier testing. **Bad:** + ```php -function parseBetterJSAlternative($code) { +function parseBetterPHPAlternative(string $code): void +{ $regexes = [ // ... ]; - - $statements = split(' ', $code); + + $statements = explode(' ', $code); $tokens = []; - foreach($regexes as $regex) { - foreach($statements as $statement) { + foreach ($regexes as $regex) { + foreach ($statements as $statement) { // ... } } - + $ast = []; - foreach($tokens as $token) { + foreach ($tokens as $token) { // lex... } - - foreach($ast as $node) { + + foreach ($ast as $node) { // parse... } } ``` -**Good**: +**Bad too:** + +We have carried out some of the functionality, but the `parseBetterPHPAlternative()` function is still very complex and not testable. + ```php -function tokenize($code) { +function tokenize(string $code): array +{ $regexes = [ // ... ]; - - $statements = split(' ', $code); + + $statements = explode(' ', $code); $tokens = []; - foreach($regexes as $regex) { - foreach($statements as $statement) { + foreach ($regexes as $regex) { + foreach ($statements as $statement) { $tokens[] = /* ... */; - }); - }); - + } + } + return $tokens; } -function lexer($tokens) { +function lexer(array $tokens): array +{ $ast = []; - foreach($tokens as $token) { + foreach ($tokens as $token) { $ast[] = /* ... */; - }); - + } + return $ast; } -function parseBetterJSAlternative($code) { +function parseBetterPHPAlternative(string $code): void +{ $tokens = tokenize($code); $ast = lexer($tokens); - foreach($ast as $node) { + foreach ($ast as $node) { // parse... - }); + } } ``` -**[⬆ back to top](#table-of-contents)** - -### Remove duplicate code -Do your absolute best to avoid duplicate code. Duplicate code is bad because -it means that there's more than one place to alter something if you need to -change some logic. - -Imagine if you run a restaurant and you keep track of your inventory: all your -tomatoes, onions, garlic, spices, etc. If you have multiple lists that -you keep this on, then all have to be updated when you serve a dish with -tomatoes in them. If you only have one list, there's only one place to update! -Oftentimes you have duplicate code because you have two or more slightly -different things, that share a lot in common, but their differences force you -to have two or more separate functions that do much of the same things. Removing -duplicate code means creating an abstraction that can handle this set of different -things with just one function/module/class. +**Good:** -Getting the abstraction right is critical, that's why you should follow the -SOLID principles laid out in the *Classes* section. Bad abstractions can be -worse than duplicate code, so be careful! Having said this, if you can make -a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself -updating multiple places anytime you want to change one thing. +The best solution is move out the dependencies of `parseBetterPHPAlternative()` function. -**Bad:** ```php -function showDeveloperList($developers) { - foreach($developers as $developer) { - $expectedSalary = $developer->calculateExpectedSalary(); - $experience = $developer->getExperience(); - $githubLink = $developer->getGithubLink(); - $data = [ - $expectedSalary, - $experience, - $githubLink +class Tokenizer +{ + public function tokenize(string $code): array + { + $regexes = [ + // ... ]; - - render($data); + + $statements = explode(' ', $code); + $tokens = []; + foreach ($regexes as $regex) { + foreach ($statements as $statement) { + $tokens[] = /* ... */; + } + } + + return $tokens; } } -function showManagerList($managers) { - foreach($managers as $manager) { - $expectedSalary = $manager->calculateExpectedSalary(); - $experience = $manager->getExperience(); - $githubLink = $manager->getGithubLink(); - $data = [ - $expectedSalary, - $experience, - $githubLink - ]; - - render($data); +class Lexer +{ + public function lexify(array $tokens): array + { + $ast = []; + foreach ($tokens as $token) { + $ast[] = /* ... */; + } + + return $ast; } } -``` -**Good**: -```php -function showList($employees) { - foreach($employees as $employe) { - $expectedSalary = $employe->calculateExpectedSalary(); - $experience = $employe->getExperience(); - $githubLink = $employe->getGithubLink(); - $data = [ - $expectedSalary, - $experience, - $githubLink - ]; - - render($data); +class BetterPHPAlternative +{ + private $tokenizer; + private $lexer; + + public function __construct(Tokenizer $tokenizer, Lexer $lexer) + { + $this->tokenizer = $tokenizer; + $this->lexer = $lexer; + } + + public function parse(string $code): void + { + $tokens = $this->tokenizer->tokenize($code); + $ast = $this->lexer->lexify($tokens); + foreach ($ast as $node) { + // parse... + } } } ``` + **[⬆ back to top](#table-of-contents)** -### Set default objects with Object.assign +### Don't use flags as function parameters + +Flags tell your user that this function does more than one thing. Functions should +do one thing. Split out your functions if they are following different code paths +based on a boolean. **Bad:** -```php -$menuConfig = [ - 'title' => null, - 'body' => 'Bar', - 'buttonText' => null, - 'cancellable' => true, -]; - -function createMenu(&$config) { - $config['title'] = $config['title'] ?: 'Foo'; - $config['body'] = $config['body'] ?: 'Bar'; - $config['buttonText'] = $config['buttonText'] ?: 'Baz'; - $config['cancellable'] = $config['cancellable'] ?: true; -} - -createMenu($menuConfig); -``` - -**Good**: -```php -$menuConfig = [ - 'title' => 'Order', - // User did not include 'body' key - 'buttonText' => 'Send', - 'cancellable' => true, -]; - -function createMenu(&$config) { - $config = array_merge([ - 'title' => 'Foo', - 'body' => 'Bar', - 'buttonText' => 'Baz', - 'cancellable' => true, - ], $config); - - // config now equals: {title: "Order", body: "Bar", buttonText: "Send", cancellable: true} - // ... -} -createMenu($menuConfig); -``` -**[⬆ back to top](#table-of-contents)** - - -### Don't use flags as function parameters -Flags tell your user that this function does more than one thing. Functions should -do one thing. Split out your functions if they are following different code paths -based on a boolean. - -**Bad:** ```php -function createFile($name, $temp = false) { +function createFile(string $name, bool $temp = false): void +{ if ($temp) { - touch('./temp/'.$name); + touch('./temp/' . $name); } else { touch($name); } } ``` -**Good**: +**Good:** + ```php -function createFile($name) { +function createFile(string $name): void +{ touch($name); } -function createTempFile($name) { - touch('./temp/'.$name); +function createTempFile(string $name): void +{ + touch('./temp/' . $name); } ``` + **[⬆ back to top](#table-of-contents)** ### Avoid Side Effects -A function produces a side effect if it does anything other than take a value in and -return another value or values. A side effect could be writing to a file, modifying + +A function produces a side effect if it does anything other than take a value in and +return another value or values. A side effect could be writing to a file, modifying some global variable, or accidentally wiring all your money to a stranger. -Now, you do need to have side effects in a program on occasion. Like the previous -example, you might need to write to a file. What you want to do is to centralize where -you are doing this. Don't have several functions and classes that write to a particular +Now, you do need to have side effects in a program on occasion. Like the previous +example, you might need to write to a file. What you want to do is to centralize where +you are doing this. Don't have several functions and classes that write to a particular file. Have one service that does it. One and only one. The main point is to avoid common pitfalls like sharing state between objects without -any structure, using mutable data types that can be written to by anything, and not -centralizing where your side effects occur. If you can do this, you will be happier +any structure, using mutable data types that can be written to by anything, and not +centralizing where your side effects occur. If you can do this, you will be happier than the vast majority of other programmers. **Bad:** + ```php // Global variable referenced by following function. // If we had another function that used this name, now it'd be an array and it could break it. $name = 'Ryan McDermott'; -function splitIntoFirstAndLastName() { - $name = preg_split('/ /', $name); +function splitIntoFirstAndLastName(): void +{ + global $name; + + $name = explode(' ', $name); } splitIntoFirstAndLastName(); -var_dump($name); // ['Ryan', 'McDermott']; +var_dump($name); +// ['Ryan', 'McDermott']; ``` -**Good**: -```php -$name = 'Ryan McDermott'; +**Good:** -function splitIntoFirstAndLastName($name) { - return preg_split('/ /', $name); +```php +function splitIntoFirstAndLastName(string $name): array +{ + return explode(' ', $name); } $name = 'Ryan McDermott'; $newName = splitIntoFirstAndLastName($name); -var_dump($name); // 'Ryan McDermott'; -var_dump($newName); // ['Ryan', 'McDermott']; +var_dump($name); +// 'Ryan McDermott'; + +var_dump($newName); +// ['Ryan', 'McDermott']; ``` + **[⬆ back to top](#table-of-contents)** ### Don't write to global functions -Polluting globals is a bad practice in very languages because you could clash with another -library and the user of your API would be none-the-wiser until they get an exception in -production. Let's think about an example: what if you wanted to have configuration array. -You could write global function like `config()`, but it could clash with another library -that tried to do the same thing. This is why it -would be much better to use singleton design pattern and simple set configuration. + +Polluting globals is a bad practice in many languages because you could clash with another +library and the user of your API would be none-the-wiser until they get an exception in +production. Let's think about an example: what if you wanted to have configuration array? +You could write global function like `config()`, but it could clash with another library +that tried to do the same thing. **Bad:** + ```php -function config() { - return [ - 'foo': 'bar', - ] +function config(): array +{ + return [ + 'foo' => 'bar', + ]; } ``` **Good:** + +```php +class Configuration +{ + private $configuration = []; + + public function __construct(array $configuration) + { + $this->configuration = $configuration; + } + + public function get(string $key): ?string + { + // null coalescing operator + return $this->configuration[$key] ?? null; + } +} +``` + +Load configuration and create instance of `Configuration` class + +```php +$configuration = new Configuration([ + 'foo' => 'bar', +]); +``` + +And now you must use instance of `Configuration` in your application. + +**[⬆ back to top](#table-of-contents)** + +### Don't use a Singleton pattern + +Singleton is an [anti-pattern](https://en.wikipedia.org/wiki/Singleton_pattern). Paraphrased from Brian Button: + 1. They are generally used as a **global instance**, why is that so bad? Because **you hide the dependencies** of your application in your code, instead of exposing them through the interfaces. Making something global to avoid passing it around is a [code smell](https://en.wikipedia.org/wiki/Code_smell). + 2. They violate the [single responsibility principle](#single-responsibility-principle-srp): by virtue of the fact that **they control their own creation and lifecycle**. + 3. They inherently cause code to be tightly [coupled](https://en.wikipedia.org/wiki/Coupling_%28computer_programming%29). This makes faking them out under **test rather difficult** in many cases. + 4. They carry state around for the lifetime of the application. Another hit to testing since **you can end up with a situation where tests need to be ordered** which is a big no for unit tests. Why? Because each unit test should be independent from the other. + +There is also very good thoughts by [Misko Hevery](http://misko.hevery.com/about/) about the [root of problem](http://misko.hevery.com/2008/08/25/root-cause-of-singletons/). + +**Bad:** + ```php -class Configuration { +class DBConnection +{ private static $instance; - private function __construct($configuration) {/* */} - public static function getInstance() { + + private function __construct(string $dsn) + { + // ... + } + + public static function getInstance(): self + { if (self::$instance === null) { - self::$instance = new Configuration(); + self::$instance = new self(); } + return self::$instance; } - public function get($key) {/* */} - public function getAll() {/* */} + + // ... } -$singleton = Configuration::getInstance(); +$singleton = DBConnection::getInstance(); ``` + +**Good:** + +```php +class DBConnection +{ + public function __construct(string $dsn) + { + // ... + } + + // ... +} +``` + +Create instance of `DBConnection` class and configure it with [DSN](http://php.net/manual/en/pdo.construct.php#refsect1-pdo.construct-parameters). + +```php +$connection = new DBConnection($dsn); +``` + +And now you must use instance of `DBConnection` in your application. + **[⬆ back to top](#table-of-contents)** ### Encapsulate conditionals **Bad:** + ```php -if ($fsm->state === 'fetching' && is_empty($listNode)) { +if ($article->state === 'published') { // ... } ``` -**Good**: -```php -function shouldShowSpinner($fsm, $listNode) { - return $fsm->state === 'fetching' && is_empty($listNode); -} +**Good:** -if (shouldShowSpinner($fsmInstance, $listNodeInstance)) { - // ... +```php +if ($article->isPublished()) { + // ... } ``` + **[⬆ back to top](#table-of-contents)** ### Avoid negative conditionals **Bad:** + ```php -function isDOMNodeNotPresent($node) { +function isDOMNodeNotPresent(DOMNode $node): bool +{ // ... } -if (!isDOMNodeNotPresent($node)) { +if (! isDOMNodeNotPresent($node)) { // ... } ``` -**Good**: +**Good:** + ```php -function isDOMNodePresent($node) { +function isDOMNodePresent(DOMNode $node): bool +{ // ... } @@ -634,9 +989,11 @@ if (isDOMNodePresent($node)) { // ... } ``` + **[⬆ back to top](#table-of-contents)** ### Avoid conditionals + This seems like an impossible task. Upon first hearing this, most people say, "how am I supposed to do anything without an `if` statement?" The answer is that you can use polymorphism to achieve the same task in many cases. The second @@ -647,10 +1004,14 @@ are telling your user that your function does more than one thing. Remember, just do one thing. **Bad:** + ```php -class Airplane { +class Airplane +{ // ... - public function getCruisingAltitude() { + + public function getCruisingAltitude(): int + { switch ($this->type) { case '777': return $this->getMaxAltitude() - $this->getPassengerCount(); @@ -663,125 +1024,160 @@ class Airplane { } ``` -**Good**: +**Good:** + ```php -class Airplane { +interface Airplane +{ // ... + + public function getCruisingAltitude(): int; } -class Boeing777 extends Airplane { +class Boeing777 implements Airplane +{ // ... - public function getCruisingAltitude() { + + public function getCruisingAltitude(): int + { return $this->getMaxAltitude() - $this->getPassengerCount(); } } -class AirForceOne extends Airplane { +class AirForceOne implements Airplane +{ // ... - public function getCruisingAltitude() { + + public function getCruisingAltitude(): int + { return $this->getMaxAltitude(); } } -class Cessna extends Airplane { +class Cessna implements Airplane +{ // ... - public function getCruisingAltitude() { + + public function getCruisingAltitude(): int + { return $this->getMaxAltitude() - $this->getFuelExpenditure(); } } ``` + **[⬆ back to top](#table-of-contents)** ### Avoid type-checking (part 1) + PHP is untyped, which means your functions can take any type of argument. Sometimes you are bitten by this freedom and it becomes tempting to do type-checking in your functions. There are many ways to avoid having to do this. The first thing to consider is consistent APIs. **Bad:** + ```php -function travelToTexas($vehicle) { +function travelToTexas($vehicle): void +{ if ($vehicle instanceof Bicycle) { - $vehicle->peddle($this->currentLocation, new Location('texas')); - } else if ($vehicle instanceof Car) { - $vehicle->drive($this->currentLocation, new Location('texas')); + $vehicle->pedalTo(new Location('texas')); + } elseif ($vehicle instanceof Car) { + $vehicle->driveTo(new Location('texas')); } } ``` -**Good**: +**Good:** + ```php -function travelToTexas($vehicle) { - $vehicle->move($this->currentLocation, new Location('texas')); +function travelToTexas(Vehicle $vehicle): void +{ + $vehicle->travelTo(new Location('texas')); } ``` + **[⬆ back to top](#table-of-contents)** ### Avoid type-checking (part 2) + If you are working with basic primitive values like strings, integers, and arrays, -and you can't use polymorphism but you still feel the need to type-check, -you should consider type declaration or strict mode. It provides you with static -typing on top of standard PHP syntax. The problem with manually type-checking is -that doing it well requires so much extra verbiage that the faux "type-safety" -you get doesn't make up for the lost readability. Keep your PHP clean, write good -tests, and have good code reviews. Otherwise, do all of that but with PHP strict -type declaration or strict mode. +and you use PHP 7+ and you can't use polymorphism but you still feel the need to +type-check, you should consider +[type declaration](https://www.php.net/manual/en/language.types.declarations.php) +or strict mode. It provides you with static typing on top of standard PHP syntax. +The problem with manually type-checking is that doing it will require so much +extra verbiage that the faux "type-safety" you get doesn't make up for the lost +readability. Keep your PHP clean, write good tests, and have good code reviews. +Otherwise, do all of that but with PHP strict type declaration or strict mode. **Bad:** + ```php -function combine($val1, $val2) { - if (is_numeric($val1) && is_numeric($val2)) { - return $val1 + $val2; +function combine($val1, $val2): int +{ + if (! is_numeric($val1) || ! is_numeric($val2)) { + throw new Exception('Must be of type Number'); } - - throw new \Exception('Must be of type Number'); + + return $val1 + $val2; } ``` -**Good**: +**Good:** + ```php -function combine(int $val1, int $val2) { +function combine(int $val1, int $val2): int +{ return $val1 + $val2; } ``` + **[⬆ back to top](#table-of-contents)** ### Remove dead code + Dead code is just as bad as duplicate code. There's no reason to keep it in your codebase. If it's not being called, get rid of it! It will still be safe in your version history if you still need it. **Bad:** + ```php -function oldRequestModule($url) { +function oldRequestModule(string $url): void +{ // ... } -function newRequestModule($url) { +function newRequestModule(string $url): void +{ // ... } -$req = new newRequestModule(); -inventoryTracker('apples', $req, 'www.inventory-awesome.io'); - +$request = newRequestModule($requestUrl); +inventoryTracker('apples', $request, 'www.inventory-awesome.io'); ``` -**Good**: +**Good:** + ```php -function newRequestModule($url) { +function requestModule(string $url): void +{ // ... } -$req = new newRequestModule(); -inventoryTracker('apples', $req, 'www.inventory-awesome.io'); +$request = requestModule($requestUrl); +inventoryTracker('apples', $request, 'www.inventory-awesome.io'); ``` + **[⬆ back to top](#table-of-contents)** -## **Objects and Data Structures** -### Use getters and setters -In PHP you can set `public`, `protected` and `private` keywords for methods. -Using it, you can control properties modification on an object. +## Objects and Data Structures + +### Use object encapsulation + +In PHP you can set `public`, `protected` and `private` keywords for methods. +Using it, you can control properties modification on an object. * When you want to do more beyond getting an object property, you don't have to look up and change every accessor in your codebase. @@ -792,12 +1188,13 @@ to look up and change every accessor in your codebase. * You can lazy load your object's properties, let's say getting it from a server. -Additionally, this is part of Open/Closed principle, from object-oriented -design principles. +Additionally, this is part of [Open/Closed](#openclosed-principle-ocp) principle. **Bad:** + ```php -class BankAccount { +class BankAccount +{ public $balance = 1000; } @@ -807,82 +1204,390 @@ $bankAccount = new BankAccount(); $bankAccount->balance -= 100; ``` -**Good**: +**Good:** + ```php -class BankAccount { +class BankAccount +{ private $balance; - - public function __construct($balance = 1000) { + + public function __construct(int $balance = 1000) + { $this->balance = $balance; } - - public function withdrawBalance($amount) { + + public function withdraw(int $amount): void + { if ($amount > $this->balance) { throw new \Exception('Amount greater than available balance.'); } + $this->balance -= $amount; } - - public function depositBalance($amount) { - $this->balance += $amount; + + public function deposit(int $amount): void + { + $this->balance += $amount; + } + +    public function getBalance(): int + { + return $this->balance; + } +} + +$bankAccount = new BankAccount(); + +// Buy shoes... +$bankAccount->withdraw($shoesPrice); + +// Get balance +$balance = $bankAccount->getBalance(); +``` + +**[⬆ back to top](#table-of-contents)** + +### Make objects have private/protected members + +* `public` methods and properties are most dangerous for changes, because some outside code may easily rely on them and you can't control what code relies on them. **Modifications in class are dangerous for all users of class.** +* `protected` modifier are as dangerous as public, because they are available in scope of any child class. This effectively means that difference between public and protected is only in access mechanism, but encapsulation guarantee remains the same. **Modifications in class are dangerous for all descendant classes.** +* `private` modifier guarantees that code is **dangerous to modify only in boundaries of single class** (you are safe for modifications and you won't have [Jenga effect](http://www.urbandictionary.com/define.php?term=Jengaphobia&defid=2494196)). + +Therefore, use `private` by default and `public/protected` when you need to provide access for external classes. + +For more information you can read the [blog post](http://fabien.potencier.org/pragmatism-over-theory-protected-vs-private.html) on this topic written by [Fabien Potencier](https://github.com/fabpot). + +**Bad:** + +```php +class Employee +{ + public $name; + + public function __construct(string $name) + { + $this->name = $name; + } +} + +$employee = new Employee('John Doe'); +// Employee name: John Doe +echo 'Employee name: ' . $employee->name; +``` + +**Good:** + +```php +class Employee +{ + private $name; + + public function __construct(string $name) + { + $this->name = $name; + } + + public function getName(): string + { + return $this->name; + } +} + +$employee = new Employee('John Doe'); +// Employee name: John Doe +echo 'Employee name: ' . $employee->getName(); +``` + +**[⬆ back to top](#table-of-contents)** + +## Classes + +### Prefer composition over inheritance + +As stated famously in [*Design Patterns*](https://en.wikipedia.org/wiki/Design_Patterns) by the Gang of Four, +you should prefer composition over inheritance where you can. There are lots of +good reasons to use inheritance and lots of good reasons to use composition. +The main point for this maxim is that if your mind instinctively goes for +inheritance, try to think if composition could model your problem better. In some +cases it can. + +You might be wondering then, "when should I use inheritance?" It +depends on your problem at hand, but this is a decent list of when inheritance +makes more sense than composition: + +1. Your inheritance represents an "is-a" relationship and not a "has-a" +relationship (Human->Animal vs. User->UserDetails). +2. You can reuse code from the base classes (Humans can move like all animals). +3. You want to make global changes to derived classes by changing a base class. +(Change the caloric expenditure of all animals when they move). + +**Bad:** + +```php +class Employee +{ + private $name; + + private $email; + + public function __construct(string $name, string $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; + + private $salary; + + public function __construct(string $name, string $email, string $ssn, string $salary) + { + parent::__construct($name, $email); + + $this->ssn = $ssn; + $this->salary = $salary; + } + + // ... +} +``` + +**Good:** + +```php +class EmployeeTaxData +{ + private $ssn; + + private $salary; + + public function __construct(string $ssn, string $salary) + { + $this->ssn = $ssn; + $this->salary = $salary; + } + + // ... +} + +class Employee +{ + private $name; + + private $email; + + private $taxData; + + public function __construct(string $name, string $email) + { + $this->name = $name; + $this->email = $email; + } + + public function setTaxData(EmployeeTaxData $taxData): void + { + $this->taxData = $taxData; + } + + // ... +} +``` + +**[⬆ back to top](#table-of-contents)** + +### Avoid fluent interfaces + +A [Fluent interface](https://en.wikipedia.org/wiki/Fluent_interface) is an object +oriented API that aims to improve the readability of the source code by using +[Method chaining](https://en.wikipedia.org/wiki/Method_chaining). + +While there can be some contexts, frequently builder objects, where this +pattern reduces the verbosity of the code (for example the [PHPUnit Mock Builder](https://phpunit.de/manual/current/en/test-doubles.html) +or the [Doctrine Query Builder](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/query-builder.html)), +more often it comes at some costs: + +1. Breaks [Encapsulation](https://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29). +2. Breaks [Decorators](https://en.wikipedia.org/wiki/Decorator_pattern). +3. Is harder to [mock](https://en.wikipedia.org/wiki/Mock_object) in a test suite. +4. Makes diffs of commits harder to read. + +For more information you can read the full [blog post](https://ocramius.github.io/blog/fluent-interfaces-are-evil/) +on this topic written by [Marco Pivetta](https://github.com/Ocramius). + +**Bad:** + +```php +class Car +{ + private $make = 'Honda'; + + private $model = 'Accord'; + + private $color = 'white'; + + public function setMake(string $make): self + { + $this->make = $make; + + // NOTE: Returning this for chaining + return $this; + } + + public function setModel(string $model): self + { + $this->model = $model; + + // NOTE: Returning this for chaining + return $this; + } + + public function setColor(string $color): self + { + $this->color = $color; + + // NOTE: Returning this for chaining + return $this; + } + + public function dump(): void + { + var_dump($this->make, $this->model, $this->color); + } +} + +$car = (new Car()) + ->setColor('pink') + ->setMake('Ford') + ->setModel('F-150') + ->dump(); +``` + +**Good:** + +```php +class Car +{ + private $make = 'Honda'; + + private $model = 'Accord'; + + private $color = 'white'; + + public function setMake(string $make): void + { + $this->make = $make; + } + + public function setModel(string $model): void + { + $this->model = $model; } - - public function getBalance() { - return $this->balance; + + public function setColor(string $color): void + { + $this->color = $color; + } + + public function dump(): void + { + var_dump($this->make, $this->model, $this->color); } } -$bankAccount = new BankAccount(); +$car = new Car(); +$car->setColor('pink'); +$car->setMake('Ford'); +$car->setModel('F-150'); +$car->dump(); +``` -// Buy shoes... -$bankAccount->withdrawBalance(-$shoesPrice); +**[⬆ back to top](#table-of-contents)** -// Get balance -$balance = $bankAccount->getBalance(); +### Prefer final classes -``` -**[⬆ back to top](#table-of-contents)** +The `final` keyword should be used whenever possible: +1. It prevents an uncontrolled inheritance chain. +2. It encourages [composition](#prefer-composition-over-inheritance). +3. It encourages the [Single Responsibility Principle](#single-responsibility-principle-srp). +4. It encourages developers to use your public methods instead of extending the class to get access to protected ones. +5. It allows you to change your code without breaking applications that use your class. -### Make objects have private/protected members +The only condition is that your class should implement an interface and no other public methods are defined. + +For more informations you can read [the blog post](https://ocramius.github.io/blog/when-to-declare-classes-final/) on this topic written by [Marco Pivetta (Ocramius)](https://ocramius.github.io/). **Bad:** + ```php -class Employee { - public $name; - - public function __construct($name) { - $this->name = $name; +final class Car +{ + private $color; + + public function __construct($color) + { + $this->color = $color; } -} -$employee = new Employee('John Doe'); -echo 'Employee name: '.$employee->name; // Employee name: John Doe + /** + * @return string The color of the vehicle + */ + public function getColor() + { + return $this->color; + } +} ``` -**Good**: +**Good:** + ```php -class Employee { - protected $name; - - public function __construct($name) { - $this->name = $name; +interface Vehicle +{ + /** + * @return string The color of the vehicle + */ + public function getColor(); +} + +final class Car implements Vehicle +{ + private $color; + + public function __construct($color) + { + $this->color = $color; } - - public function getName() { - return $this->name; + + public function getColor() + { + return $this->color; } } - -$employee = new Employee('John Doe'); -echo 'Employee name: '.$employee->getName(); // Employee name: John Doe ``` + **[⬆ back to top](#table-of-contents)** +## SOLID + +**SOLID** is the mnemonic acronym introduced by Michael Feathers for the first five principles named by Robert Martin, which meant five basic principles of object-oriented programming and design. -## **Classes** + * [S: Single Responsibility Principle (SRP)](#single-responsibility-principle-srp) + * [O: Open/Closed Principle (OCP)](#openclosed-principle-ocp) + * [L: Liskov Substitution Principle (LSP)](#liskov-substitution-principle-lsp) + * [I: Interface Segregation Principle (ISP)](#interface-segregation-principle-isp) + * [D: Dependency Inversion Principle (DIP)](#dependency-inversion-principle-dip) ### Single Responsibility Principle (SRP) + As stated in Clean Code, "There should never be more than one reason for a class to change". It's tempting to jam-pack a class with a lot of functionality, like when you can only take one suitcase on your flight. The issue with this is @@ -893,152 +1598,188 @@ it can be difficult to understand how that will affect other dependent modules i your codebase. **Bad:** + ```php -class UserSettings { +class UserSettings +{ private $user; - public function __construct($user) { - $this->user = user; + + public function __construct(User $user) + { + $this->user = $user; } - - public function changeSettings($settings) { + + public function changeSettings(array $settings): void + { if ($this->verifyCredentials()) { // ... } } - - private function verifyCredentials() { + + private function verifyCredentials(): bool + { // ... } } ``` **Good:** + ```php -class UserAuth { +class UserAuth +{ private $user; - public function __construct($user) { - $this->user = user; + + public function __construct(User $user) + { + $this->user = $user; } - - protected function verifyCredentials() { + + public function verifyCredentials(): bool + { // ... } } - -class UserSettings { +class UserSettings +{ private $user; - public function __construct($user) { + + private $auth; + + public function __construct(User $user) + { $this->user = $user; $this->auth = new UserAuth($user); } - - public function changeSettings($settings) { + + public function changeSettings(array $settings): void + { if ($this->auth->verifyCredentials()) { // ... } } } ``` + **[⬆ back to top](#table-of-contents)** ### Open/Closed Principle (OCP) + As stated by Bertrand Meyer, "software entities (classes, modules, functions, 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 add new functionalities without changing existing code. **Bad:** + ```php -abstract class Adapter { +abstract class Adapter +{ protected $name; - public function getName() { + + public function getName(): string + { return $this->name; } } -class AjaxAdapter extends Adapter { - public function __construct() { - parent::__construct(); +class AjaxAdapter extends Adapter +{ + public function __construct() + { + parent::__construct(); + $this->name = 'ajaxAdapter'; } } -class NodeAdapter extends Adapter { - public function __construct() { +class NodeAdapter extends Adapter +{ + public function __construct() + { parent::__construct(); + $this->name = 'nodeAdapter'; } } -class HttpRequester { +class HttpRequester +{ private $adapter; - public function __construct($adapter) { + + public function __construct(Adapter $adapter) + { $this->adapter = $adapter; } - - public function fetch($url) { + + public function fetch(string $url): Promise + { $adapterName = $this->adapter->getName(); + if ($adapterName === 'ajaxAdapter') { return $this->makeAjaxCall($url); - } else if ($adapterName === 'httpNodeAdapter') { + } elseif ($adapterName === 'httpNodeAdapter') { return $this->makeHttpCall($url); } } - - protected function makeAjaxCall($url) { + + private function makeAjaxCall(string $url): Promise + { // request and return promise } - - protected function makeHttpCall($url) { + + private function makeHttpCall(string $url): Promise + { // request and return promise } } ``` **Good:** + ```php -abstract class Adapter { - abstract protected function getName(); - abstract public function request($url); +interface Adapter +{ + public function request(string $url): Promise; } -class AjaxAdapter extends Adapter { - protected function getName() { - return 'ajaxAdapter'; - } - - public function request($url) { +class AjaxAdapter implements Adapter +{ + public function request(string $url): Promise + { // request and return promise } } -class NodeAdapter extends Adapter { - protected function getName() { - return 'nodeAdapter'; - } - - public function request($url) { +class NodeAdapter implements Adapter +{ + public function request(string $url): Promise + { // request and return promise } } -class HttpRequester { +class HttpRequester +{ private $adapter; - public function __construct(Adapter $adapter) { + + public function __construct(Adapter $adapter) + { $this->adapter = $adapter; } - - public function fetch($url) { + + public function fetch(string $url): Promise + { return $this->adapter->request($url); } } - ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ back to top](#table-of-contents)** ### Liskov Substitution Principle (LSP) + This is a scary term for a very simple concept. It's formally defined as "If S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any @@ -1053,233 +1794,212 @@ if you model it using the "is-a" relationship via inheritance, you quickly get into trouble. **Bad:** + ```php -class Rectangle { - private $width, $height; - - public function __construct() { - $this->width = 0; - $this->height = 0; - } - - public function setColor($color) { - // ... - } - - public function render($area) { - // ... - } - - public function setWidth($width) { +class Rectangle +{ + protected $width = 0; + + protected $height = 0; + + public function setWidth(int $width): void + { $this->width = $width; } - - public function setHeight($height) { + + public function setHeight(int $height): void + { $this->height = $height; } - - public function getArea() { + + public function getArea(): int + { return $this->width * $this->height; } } -class Square extends Rectangle { - public function setWidth($width) { +class Square extends Rectangle +{ + public function setWidth(int $width): void + { $this->width = $this->height = $width; } - - public function setHeight(height) { + + public function setHeight(int $height): void + { $this->width = $this->height = $height; } } -function renderLargeRectangles($rectangles) { - foreach($rectangle in $rectangles) { - $rectangle->setWidth(4); - $rectangle->setHeight(5); - $area = $rectangle->getArea(); // BAD: Will return 25 for Square. Should be 20. - $rectangle->render($area); - }); +function printArea(Rectangle $rectangle): void +{ + $rectangle->setWidth(4); + $rectangle->setHeight(5); + + // BAD: Will return 25 for Square. Should be 20. + echo sprintf('%s has area %d.', get_class($rectangle), $rectangle->getArea()) . PHP_EOL; } -$rectangles = [new Rectangle(), new Rectangle(), new Square()]; -renderLargeRectangles($rectangles); +$rectangles = [new Rectangle(), new Square()]; + +foreach ($rectangles as $rectangle) { + printArea($rectangle); +} ``` **Good:** + +The best way is separate the quadrangles and allocation of a more general subtype for both shapes. + +Despite the apparent similarity of the square and the rectangle, they are different. +A square has much in common with a rhombus, and a rectangle with a parallelogram, but they are not subtypes. +A square, a rectangle, a rhombus and a parallelogram are separate shapes with their own properties, albeit similar. + ```php -abstract class Shape { - private $width, $height; - - abstract public function getArea(); - - public function setColor($color) { - // ... - } - - public function render($area) { - // ... - } +interface Shape +{ + public function getArea(): int; } -class Rectangle extends Shape { - public function __construct { - parent::__construct(); - $this->width = 0; - $this->height = 0; - } - - public function setWidth($width) { +class Rectangle implements Shape +{ + private $width = 0; + private $height = 0; + + public function __construct(int $width, int $height) + { $this->width = $width; - } - - public function setHeight($height) { $this->height = $height; } - - public function getArea() { + + public function getArea(): int + { return $this->width * $this->height; } } -class Square extends Shape { - public function __construct { - parent::__construct(); - $this->length = 0; - } - - public function setLength($length) { +class Square implements Shape +{ + private $length = 0; + + public function __construct(int $length) + { $this->length = $length; } - - public function getArea() { - return $this->length * $this->length; - } + + public function getArea(): int + { +        return $this->length ** 2; +    } } -function renderLargeRectangles($rectangles) { - foreach($rectangle in $rectangles) { - if ($rectangle instanceof Square) { - $rectangle->setLength(5); - } else if ($rectangle instanceof Rectangle) { - $rectangle->setWidth(4); - $rectangle->setHeight(5); - } - - $area = $rectangle->getArea(); - $rectangle->render($area); - }); +function printArea(Shape $shape): void +{ + echo sprintf('%s has area %d.', get_class($shape), $shape->getArea()).PHP_EOL; } -$shapes = [new Rectangle(), new Rectangle(), new Square()]; -renderLargeRectangles($shapes); +$shapes = [new Rectangle(4, 5), new Square(5)]; + +foreach ($shapes as $shape) { + printArea($shape); +} ``` + **[⬆ back to top](#table-of-contents)** ### Interface Segregation Principle (ISP) + ISP states that "Clients should not be forced to depend upon interfaces that -they do not use." +they do not use." A good example to look at that demonstrates this principle is for -classes that require large settings objects. Not requiring clients to setup +classes that require large settings objects. Not requiring clients to set up 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". **Bad:** + ```php -interface WorkerInterface { - public function work(); - public function eat(); -} +interface Employee +{ + public function work(): void; -class Worker implements WorkerInterface { - public function work() { - // ....working - } - public function eat() { - // ...... eating in launch break - } + public function eat(): void; } -class SuperWorker implements WorkerInterface { - public function work() { - //.... working much more +class HumanEmployee implements Employee +{ + public function work(): void + { + // ....working } - public function eat() { - //.... eating in launch break + public function eat(): void + { + // ...... eating in lunch break } } -class Manager { - /** @var WorkerInterface $worker **/ - private $worker; - - public void setWorker(WorkerInterface $worker) { - $this->worker = $worker; +class RobotEmployee implements Employee +{ + public function work(): void + { + //.... working much more } - public function manage() { - $this->worker->work(); + public function eat(): void + { + //.... robot can't eat, but it must implement this method } } ``` **Good:** + +Not every worker is an employee, but every employee is a worker. + ```php -interface WorkerInterface extends FeedableInterface, WorkableInterface { +interface Workable +{ + public function work(): void; } -interface WorkableInterface { - public function work(); +interface Feedable +{ + public function eat(): void; } -interface FeedableInterface { - public function eat(); +interface Employee extends Feedable, Workable +{ } -class Worker implements WorkableInterface, FeedableInterface { - public function work() { +class HumanEmployee implements Employee +{ + public function work(): void + { // ....working } - public function eat() { - //.... eating in launch break + public function eat(): void + { + //.... eating in lunch break } } -class Robot implements WorkableInterface { - public void work() { +// robot can only work +class RobotEmployee implements Workable +{ + public function work(): void + { // ....working } } - -class SuperWorker implements WorkerInterface { - public function work() { - //.... working much more - } - - public function eat() { - //.... eating in launch break - } -} - -class Manager { - /** @var $worker WorkableInterface **/ - private $worker; - - public function setWorker(WorkableInterface $w) { - $this->worker = $w; - } - - public function manage() { - $this->worker->work(); - } -} ``` + **[⬆ back to top](#table-of-contents)** ### Dependency Inversion Principle (DIP) + This principle states two essential things: 1. High-level modules should not depend on low-level modules. Both should depend on abstractions. @@ -1294,226 +2014,196 @@ the coupling between modules. Coupling is a very bad development pattern because it makes your code hard to refactor. **Bad:** + ```php -class Worker { - public function work() { - // ....working - } +class Employee +{ + public function work(): void + { + // ....working + } } -class Manager { - /** @var Worker $worker **/ - private $worker; - - public function __construct(Worker $worker) { - $this->worker = $worker; - } - - public function manage() { - $this->worker->work(); +class Robot extends Employee +{ + public function work(): void + { + //.... working much more } } -class SuperWorker extends Worker { - public function work() { - //.... working much more +class Manager +{ + private $employee; + + public function __construct(Employee $employee) + { + $this->employee = $employee; + } + + public function manage(): void + { + $this->employee->work(); } } ``` **Good:** + ```php -interface WorkerInterface { - public function work(); +interface Employee +{ + public function work(): void; } -class Worker implements WorkerInterface { - public function work() { +class Human implements Employee +{ + public function work(): void + { // ....working } } -class SuperWorker implements WorkerInterface { - public function work() { +class Robot implements Employee +{ + public function work(): void + { //.... working much more } } -class Manager { - /** @var Worker $worker **/ - private $worker; - - public void __construct(WorkerInterface $worker) { - $this->worker = $worker; +class Manager +{ + private $employee; + + public function __construct(Employee $employee) + { + $this->employee = $employee; } - - public void manage() { - $this->worker->work(); + + public function manage(): void + { + $this->employee->work(); } } - ``` -**[⬆ back to top](#table-of-contents)** -### Use method chaining -This pattern is very useful and commonly used it many libraries such -as PHPUnit and Doctrine. It allows your code to be expressive, and less verbose. -For that reason, I say, use method chaining and take a look at how clean your code -will be. In your class functions, simply return `this` at the end of every function, -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) { - $this->make = $make; - } - - public function setModel($model) { - $this->model = $model; - } - - public function setColor($color) { - $this->color = $color; - } - - public function dump() { - var_dump($this->make, $this->model, $this->color); - } -} +**[⬆ back to top](#table-of-contents)** -$car = new Car(); -$car->setColor('pink'); -$car->setMake('Ford'); -$car->setModel('F-150'); -$car->dump(); -``` +## Don’t repeat yourself (DRY) -**Good:** -```php -class Car { - private $make, $model, $color; - - public function __construct() { - $this->make = 'Honda'; - $this->model = 'Accord'; - $this->color = 'white'; - } - - public function setMake($make) { - $this->make = $make; - - // NOTE: Returning this for chaining - return $this; - } - - public function setModel($model) { - $this->model = $model; - - // NOTE: Returning this for chaining - return $this; - } - - public function setColor($color) { - $this->color = $color; - - // NOTE: Returning this for chaining - return $this; - } - - public function dump() { - var_dump($this->make, $this->model, $this->color); - } -} +Try to observe the [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) principle. -$car = (new Car()) - ->setColor('pink') - ->setMake('Ford') - ->setModel('F-150') - ->dump(); -``` -**[⬆ back to top](#table-of-contents)** +Do your absolute best to avoid duplicate code. Duplicate code is bad because +it means that there's more than one place to alter something if you need to +change some logic. -### Prefer composition over inheritance -As stated famously in [*Design Patterns*](https://en.wikipedia.org/wiki/Design_Patterns) by the Gang of Four, -you should prefer composition over inheritance where you can. There are lots of -good reasons to use inheritance and lots of good reasons to use composition. -The main point for this maxim is that if your mind instinctively goes for -inheritance, try to think if composition could model your problem better. In some -cases it can. +Imagine if you run a restaurant and you keep track of your inventory: all your +tomatoes, onions, garlic, spices, etc. If you have multiple lists that +you keep this on, then all have to be updated when you serve a dish with +tomatoes in them. If you only have one list, there's only one place to update! -You might be wondering then, "when should I use inheritance?" It -depends on your problem at hand, but this is a decent list of when inheritance -makes more sense than composition: +Often you have duplicate code because you have two or more slightly +different things, that share a lot in common, but their differences force you +to have two or more separate functions that do much of the same things. Removing +duplicate code means creating an abstraction that can handle this set of different +things with just one function/module/class. -1. Your inheritance represents an "is-a" relationship and not a "has-a" -relationship (Human->Animal vs. User->UserDetails). -2. You can reuse code from the base classes (Humans can move like all animals). -3. You want to make global changes to derived classes by changing a base class. -(Change the caloric expenditure of all animals when they move). +Getting the abstraction right is critical, that's why you should follow the +SOLID principles laid out in the [Classes](#classes) section. Bad abstractions can be +worse than duplicate code, so be careful! Having said this, if you can make +a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself +updating multiple places any time you want to change one thing. **Bad:** + ```php -class Employee { - private $name, $email; - - public function __construct($name, $email) { - $this->name = $name; - $this->email = $email; +function showDeveloperList(array $developers): void +{ + foreach ($developers as $developer) { + $expectedSalary = $developer->calculateExpectedSalary(); + $experience = $developer->getExperience(); + $githubLink = $developer->getGithubLink(); + $data = [$expectedSalary, $experience, $githubLink]; + + render($data); } - - // ... } -// Bad because Employees "have" tax data. -// EmployeeTaxData is not a type of Employee +function showManagerList(array $managers): void +{ + foreach ($managers as $manager) { + $expectedSalary = $manager->calculateExpectedSalary(); + $experience = $manager->getExperience(); + $githubLink = $manager->getGithubLink(); + $data = [$expectedSalary, $experience, $githubLink]; -class EmployeeTaxData extends Employee { - private $ssn, $salary; - - public function __construct($ssn, $salary) { - parent::__construct(); - $this->ssn = $ssn; - $this->salary = $salary; + render($data); } - - // ... } ``` **Good:** + ```php -class EmployeeTaxData { - private $ssn, $salary; - - public function __construct($ssn, $salary) { - $this->ssn = $ssn; - $this->salary = $salary; +function showList(array $employees): void +{ + foreach ($employees as $employee) { + $expectedSalary = $employee->calculateExpectedSalary(); + $experience = $employee->getExperience(); + $githubLink = $employee->getGithubLink(); + $data = [$expectedSalary, $experience, $githubLink]; + + render($data); } - - // ... } +``` -class Employee { - private $name, $email, $taxData; - - public function __construct($name, $email) { - $this->name = $name; - $this->email = $email; - } - - public function setTaxData($ssn, $salary) { - $this->taxData = new EmployeeTaxData($ssn, $salary); +**Very good:** + +It is better to use a compact version of the code. + +```php +function showList(array $employees): void +{ + foreach ($employees as $employee) { + render([$employee->calculateExpectedSalary(), $employee->getExperience(), $employee->getGithubLink()]); } - // ... } ``` + +**[⬆ back to top](#table-of-contents)** + +## Translations + +This is also available in other languages: + +* :cn: **Chinese:** + * [php-cpm/clean-code-php](https://github.com/php-cpm/clean-code-php) +* :ru: **Russian:** + * [peter-gribanov/clean-code-php](https://github.com/peter-gribanov/clean-code-php) +* :es: **Spanish:** + * [fikoborquez/clean-code-php](https://github.com/fikoborquez/clean-code-php) +* :brazil: **Portuguese:** + * [fabioars/clean-code-php](https://github.com/fabioars/clean-code-php) + * [jeanjar/clean-code-php](https://github.com/jeanjar/clean-code-php/tree/pt-br) +* :thailand: **Thai:** + * [panuwizzle/clean-code-php](https://github.com/panuwizzle/clean-code-php) +* :fr: **French:** + * [errorname/clean-code-php](https://github.com/errorname/clean-code-php) +* :vietnam: **Vietnamese:** + * [viethuongdev/clean-code-php](https://github.com/viethuongdev/clean-code-php) +* :kr: **Korean:** + * [yujineeee/clean-code-php](https://github.com/yujineeee/clean-code-php) +* :tr: **Turkish:** + * [anilozmen/clean-code-php](https://github.com/anilozmen/clean-code-php) +* :iran: **Persian:** + * [amirshnll/clean-code-php](https://github.com/amirshnll/clean-code-php) +* :bangladesh: **Bangla:** + * [nayeemdev/clean-code-php](https://github.com/nayeemdev/clean-code-php) +* :egypt: **Arabic:** + * [ahmedalmory/clean-code-php](https://github.com/ahmedalmory/clean-code-php) +* :jp: **Japanese:** + * [hayato07/clean-code-php](https://github.com/hayato07/clean-code-php) + **[⬆ back to top](#table-of-contents)** diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..9c258aa3 --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "name": "jupeter/clean-code-php", + "description": "Clean Code concepts adapted for PHP", + "require": { + "php": ">=7.2", + "symplify/easy-coding-standard": "^9.3" + }, + "scripts": { + "check-cs": "vendor/bin/ecs check-markdown README.md", + "fix-cs": "vendor/bin/ecs check-markdown README.md --fix" + } +} diff --git a/ecs.php b/ecs.php new file mode 100644 index 00000000..b2e209ef --- /dev/null +++ b/ecs.php @@ -0,0 +1,27 @@ +import(SetList::COMMON); + $containerConfigurator->import(SetList::CLEAN_CODE); + $containerConfigurator->import(SetList::PSR_12); + $containerConfigurator->import(SetList::SYMPLIFY); + + $parameters = $containerConfigurator->parameters(); + $parameters->set(Option::PATHS, [__DIR__ . '/src', __DIR__ . '/config', __DIR__ . '/ecs.php']); + + $parameters->set(Option::SKIP, [ + BlankLineAfterOpeningTagFixer::class => null, + StrictComparisonFixer::class => null, + DeclareStrictTypesFixer::class => null, + ]); +};