mirror of
https://github.com/jupeter/clean-code-php.git
synced 2025-09-25 13:39:04 +02:00
apply coding standard on README
This commit is contained in:
2
.github/workflows/coding_standard.yaml
vendored
2
.github/workflows/coding_standard.yaml
vendored
@@ -17,4 +17,4 @@ jobs:
|
|||||||
|
|
||||||
- run: composer install --no-progress
|
- run: composer install --no-progress
|
||||||
|
|
||||||
- run: vendor/bin/ecs check README.md --ansi
|
- run: vendor/bin/ecs check-markdown README.md --ansi
|
258
README.md
258
README.md
@@ -69,12 +69,16 @@ Although many developers still use PHP 5, most of the examples in this article o
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$ymdstr = $moment->format('y-m-d');
|
$ymdstr = $moment->format('y-m-d');
|
||||||
```
|
```
|
||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$currentDate = $moment->format('y-m-d');
|
$currentDate = $moment->format('y-m-d');
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -85,6 +89,8 @@ $currentDate = $moment->format('y-m-d');
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
getUserInfo();
|
getUserInfo();
|
||||||
getUserData();
|
getUserData();
|
||||||
getUserRecord();
|
getUserRecord();
|
||||||
@@ -94,6 +100,8 @@ getUserProfile();
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
getUser();
|
getUser();
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -109,6 +117,8 @@ Make your names searchable.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
// What the heck is 448 for?
|
// What the heck is 448 for?
|
||||||
$result = $serializer->serialize($data, 448);
|
$result = $serializer->serialize($data, 448);
|
||||||
```
|
```
|
||||||
@@ -116,6 +126,8 @@ $result = $serializer->serialize($data, 448);
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$json = $serializer->serialize($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
$json = $serializer->serialize($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -124,6 +136,8 @@ $json = $serializer->serialize($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class User
|
class User
|
||||||
{
|
{
|
||||||
// What the heck is 7 for?
|
// What the heck is 7 for?
|
||||||
@@ -142,11 +156,16 @@ $user->access ^= 2;
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class User
|
class User
|
||||||
{
|
{
|
||||||
public const ACCESS_READ = 1;
|
public const ACCESS_READ = 1;
|
||||||
|
|
||||||
public const ACCESS_CREATE = 2;
|
public const ACCESS_CREATE = 2;
|
||||||
|
|
||||||
public const ACCESS_UPDATE = 4;
|
public const ACCESS_UPDATE = 4;
|
||||||
|
|
||||||
public const ACCESS_DELETE = 8;
|
public const ACCESS_DELETE = 8;
|
||||||
|
|
||||||
// User as default can read, create and update something
|
// User as default can read, create and update something
|
||||||
@@ -168,6 +187,8 @@ $user->access ^= User::ACCESS_CREATE;
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$address = 'One Infinite Loop, Cupertino 95014';
|
$address = 'One Infinite Loop, Cupertino 95014';
|
||||||
$cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/';
|
$cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/';
|
||||||
preg_match($cityZipCodeRegex, $address, $matches);
|
preg_match($cityZipCodeRegex, $address, $matches);
|
||||||
@@ -180,6 +201,8 @@ saveCityZipCode($matches[1], $matches[2]);
|
|||||||
It's better, but we are still heavily dependent on regex.
|
It's better, but we are still heavily dependent on regex.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$address = 'One Infinite Loop, Cupertino 95014';
|
$address = 'One Infinite Loop, Cupertino 95014';
|
||||||
$cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/';
|
$cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/';
|
||||||
preg_match($cityZipCodeRegex, $address, $matches);
|
preg_match($cityZipCodeRegex, $address, $matches);
|
||||||
@@ -193,6 +216,8 @@ saveCityZipCode($city, $zipCode);
|
|||||||
Decrease dependence on regex by naming subpatterns.
|
Decrease dependence on regex by naming subpatterns.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$address = 'One Infinite Loop, Cupertino 95014';
|
$address = 'One Infinite Loop, Cupertino 95014';
|
||||||
$cityZipCodeRegex = '/^[^,]+,\s*(?<city>.+?)\s*(?<zipCode>\d{5})$/';
|
$cityZipCodeRegex = '/^[^,]+,\s*(?<city>.+?)\s*(?<zipCode>\d{5})$/';
|
||||||
preg_match($cityZipCodeRegex, $address, $matches);
|
preg_match($cityZipCodeRegex, $address, $matches);
|
||||||
@@ -210,6 +235,8 @@ than implicit.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function isShopOpen($day): bool
|
function isShopOpen($day): bool
|
||||||
{
|
{
|
||||||
if ($day) {
|
if ($day) {
|
||||||
@@ -221,30 +248,27 @@ function isShopOpen($day): bool
|
|||||||
return true;
|
return true;
|
||||||
} elseif ($day === 'sunday') {
|
} elseif ($day === 'sunday') {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function isShopOpen(string $day): bool
|
function isShopOpen(string $day): bool
|
||||||
{
|
{
|
||||||
if (empty($day)) {
|
if (empty($day)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$openingDays = [
|
$openingDays = ['friday', 'saturday', 'sunday'];
|
||||||
'friday', 'saturday', 'sunday'
|
|
||||||
];
|
|
||||||
|
|
||||||
return in_array(strtolower($day), $openingDays, true);
|
return in_array(strtolower($day), $openingDays, true);
|
||||||
}
|
}
|
||||||
@@ -257,27 +281,28 @@ function isShopOpen(string $day): bool
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function fibonacci(int $n)
|
function fibonacci(int $n)
|
||||||
{
|
{
|
||||||
if ($n < 50) {
|
if ($n < 50) {
|
||||||
if ($n !== 0) {
|
if ($n !== 0) {
|
||||||
if ($n !== 1) {
|
if ($n !== 1) {
|
||||||
return fibonacci($n - 1) + fibonacci($n - 2);
|
return fibonacci($n - 1) + fibonacci($n - 2);
|
||||||
} else {
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
} else {
|
return 1;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
} else {
|
return 0;
|
||||||
return 'Not supported';
|
|
||||||
}
|
}
|
||||||
|
return 'Not supported';
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function fibonacci(int $n): int
|
function fibonacci(int $n): int
|
||||||
{
|
{
|
||||||
if ($n === 0 || $n === 1) {
|
if ($n === 0 || $n === 1) {
|
||||||
@@ -285,7 +310,7 @@ function fibonacci(int $n): int
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($n >= 50) {
|
if ($n >= 50) {
|
||||||
throw new \Exception('Not supported');
|
throw new Exception('Not supported');
|
||||||
}
|
}
|
||||||
|
|
||||||
return fibonacci($n - 1) + fibonacci($n - 2);
|
return fibonacci($n - 1) + fibonacci($n - 2);
|
||||||
@@ -319,6 +344,8 @@ for ($i = 0; $i < count($l); $i++) {
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$locations = ['Austin', 'New York', 'San Francisco'];
|
$locations = ['Austin', 'New York', 'San Francisco'];
|
||||||
|
|
||||||
foreach ($locations as $location) {
|
foreach ($locations as $location) {
|
||||||
@@ -341,10 +368,14 @@ variable name.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Car
|
class Car
|
||||||
{
|
{
|
||||||
public $carMake;
|
public $carMake;
|
||||||
|
|
||||||
public $carModel;
|
public $carModel;
|
||||||
|
|
||||||
public $carColor;
|
public $carColor;
|
||||||
|
|
||||||
//...
|
//...
|
||||||
@@ -354,10 +385,14 @@ class Car
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Car
|
class Car
|
||||||
{
|
{
|
||||||
public $make;
|
public $make;
|
||||||
|
|
||||||
public $model;
|
public $model;
|
||||||
|
|
||||||
public $color;
|
public $color;
|
||||||
|
|
||||||
//...
|
//...
|
||||||
@@ -413,11 +448,13 @@ function createMicrobrewery(string $breweryName = 'Hipster Brew Co.'): void
|
|||||||
The simple comparison will convert the string in an integer.
|
The simple comparison will convert the string in an integer.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$a = '42';
|
$a = '42';
|
||||||
$b = 42;
|
$b = 42;
|
||||||
|
|
||||||
if ($a != $b) {
|
if ($a !== $b) {
|
||||||
// The expression will always pass
|
// The expression will always pass
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -429,6 +466,8 @@ The string `42` is different than the integer `42`.
|
|||||||
The identical comparison will compare type and value.
|
The identical comparison will compare type and value.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$a = '42';
|
$a = '42';
|
||||||
$b = 42;
|
$b = 42;
|
||||||
|
|
||||||
@@ -448,6 +487,8 @@ Null coalescing is a new operator [introduced in PHP 7](https://www.php.net/manu
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
if (isset($_GET['name'])) {
|
if (isset($_GET['name'])) {
|
||||||
$name = $_GET['name'];
|
$name = $_GET['name'];
|
||||||
} elseif (isset($_POST['name'])) {
|
} elseif (isset($_POST['name'])) {
|
||||||
@@ -459,6 +500,8 @@ if (isset($_GET['name'])) {
|
|||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$name = $_GET['name'] ?? $_POST['name'] ?? 'nobody';
|
$name = $_GET['name'] ?? $_POST['name'] ?? 'nobody';
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -480,6 +523,8 @@ of the time a higher-level object will suffice as an argument.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Questionnaire
|
class Questionnaire
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
@@ -500,10 +545,14 @@ class Questionnaire
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Name
|
class Name
|
||||||
{
|
{
|
||||||
private $firstname;
|
private $firstname;
|
||||||
|
|
||||||
private $lastname;
|
private $lastname;
|
||||||
|
|
||||||
private $patronymic;
|
private $patronymic;
|
||||||
|
|
||||||
public function __construct(string $firstname, string $lastname, string $patronymic)
|
public function __construct(string $firstname, string $lastname, string $patronymic)
|
||||||
@@ -519,7 +568,9 @@ class Name
|
|||||||
class City
|
class City
|
||||||
{
|
{
|
||||||
private $region;
|
private $region;
|
||||||
|
|
||||||
private $district;
|
private $district;
|
||||||
|
|
||||||
private $city;
|
private $city;
|
||||||
|
|
||||||
public function __construct(string $region, string $district, string $city)
|
public function __construct(string $region, string $district, string $city)
|
||||||
@@ -535,6 +586,7 @@ class City
|
|||||||
class Contact
|
class Contact
|
||||||
{
|
{
|
||||||
private $phone;
|
private $phone;
|
||||||
|
|
||||||
private $email;
|
private $email;
|
||||||
|
|
||||||
public function __construct(string $phone, string $email)
|
public function __construct(string $phone, string $email)
|
||||||
@@ -606,6 +658,8 @@ testing.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function parseBetterPHPAlternative(string $code): void
|
function parseBetterPHPAlternative(string $code): void
|
||||||
{
|
{
|
||||||
$regexes = [
|
$regexes = [
|
||||||
@@ -744,10 +798,12 @@ based on a boolean.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function createFile(string $name, bool $temp = false): void
|
function createFile(string $name, bool $temp = false): void
|
||||||
{
|
{
|
||||||
if ($temp) {
|
if ($temp) {
|
||||||
touch('./temp/'.$name);
|
touch('./temp/' . $name);
|
||||||
} else {
|
} else {
|
||||||
touch($name);
|
touch($name);
|
||||||
}
|
}
|
||||||
@@ -757,6 +813,8 @@ function createFile(string $name, bool $temp = false): void
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function createFile(string $name): void
|
function createFile(string $name): void
|
||||||
{
|
{
|
||||||
touch($name);
|
touch($name);
|
||||||
@@ -764,7 +822,7 @@ function createFile(string $name): void
|
|||||||
|
|
||||||
function createTempFile(string $name): void
|
function createTempFile(string $name): void
|
||||||
{
|
{
|
||||||
touch('./temp/'.$name);
|
touch('./temp/' . $name);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -789,6 +847,8 @@ than the vast majority of other programmers.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
// Global variable referenced by following function.
|
// 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.
|
// If we had another function that used this name, now it'd be an array and it could break it.
|
||||||
$name = 'Ryan McDermott';
|
$name = 'Ryan McDermott';
|
||||||
@@ -802,12 +862,15 @@ function splitIntoFirstAndLastName(): void
|
|||||||
|
|
||||||
splitIntoFirstAndLastName();
|
splitIntoFirstAndLastName();
|
||||||
|
|
||||||
var_dump($name); // ['Ryan', 'McDermott'];
|
// ['Ryan', 'McDermott'];
|
||||||
|
var_dump($name);
|
||||||
```
|
```
|
||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function splitIntoFirstAndLastName(string $name): array
|
function splitIntoFirstAndLastName(string $name): array
|
||||||
{
|
{
|
||||||
return explode(' ', $name);
|
return explode(' ', $name);
|
||||||
@@ -816,8 +879,10 @@ function splitIntoFirstAndLastName(string $name): array
|
|||||||
$name = 'Ryan McDermott';
|
$name = 'Ryan McDermott';
|
||||||
$newName = splitIntoFirstAndLastName($name);
|
$newName = splitIntoFirstAndLastName($name);
|
||||||
|
|
||||||
var_dump($name); // 'Ryan McDermott';
|
// 'Ryan McDermott';
|
||||||
var_dump($newName); // ['Ryan', 'McDermott'];
|
var_dump($name);
|
||||||
|
// ['Ryan', 'McDermott'];
|
||||||
|
var_dump($newName);
|
||||||
```
|
```
|
||||||
|
|
||||||
**[⬆ back to top](#table-of-contents)**
|
**[⬆ back to top](#table-of-contents)**
|
||||||
@@ -833,9 +898,11 @@ that tried to do the same thing.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function config(): array
|
function config(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'foo' => 'bar',
|
'foo' => 'bar',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -844,6 +911,8 @@ function config(): array
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Configuration
|
class Configuration
|
||||||
{
|
{
|
||||||
private $configuration = [];
|
private $configuration = [];
|
||||||
@@ -864,6 +933,8 @@ class Configuration
|
|||||||
Load configuration and create instance of `Configuration` class
|
Load configuration and create instance of `Configuration` class
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$configuration = new Configuration([
|
$configuration = new Configuration([
|
||||||
'foo' => 'bar',
|
'foo' => 'bar',
|
||||||
]);
|
]);
|
||||||
@@ -886,6 +957,8 @@ There is also very good thoughts by [Misko Hevery](http://misko.hevery.com/about
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class DBConnection
|
class DBConnection
|
||||||
{
|
{
|
||||||
private static $instance;
|
private static $instance;
|
||||||
@@ -895,7 +968,7 @@ class DBConnection
|
|||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getInstance(): DBConnection
|
public static function getInstance(): self
|
||||||
{
|
{
|
||||||
if (self::$instance === null) {
|
if (self::$instance === null) {
|
||||||
self::$instance = new self();
|
self::$instance = new self();
|
||||||
@@ -913,6 +986,8 @@ $singleton = DBConnection::getInstance();
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class DBConnection
|
class DBConnection
|
||||||
{
|
{
|
||||||
public function __construct(string $dsn)
|
public function __construct(string $dsn)
|
||||||
@@ -920,13 +995,15 @@ class DBConnection
|
|||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Create instance of `DBConnection` class and configure it with [DSN](http://php.net/manual/en/pdo.construct.php#refsect1-pdo.construct-parameters).
|
Create instance of `DBConnection` class and configure it with [DSN](http://php.net/manual/en/pdo.construct.php#refsect1-pdo.construct-parameters).
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
$connection = new DBConnection($dsn);
|
$connection = new DBConnection($dsn);
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -939,6 +1016,8 @@ And now you must use instance of `DBConnection` in your application.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
if ($article->state === 'published') {
|
if ($article->state === 'published') {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
@@ -947,6 +1026,8 @@ if ($article->state === 'published') {
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
if ($article->isPublished()) {
|
if ($article->isPublished()) {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
@@ -959,13 +1040,14 @@ if ($article->isPublished()) {
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
function isDOMNodeNotPresent(\DOMNode $node): bool
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
function isDOMNodeNotPresent(DOMNode $node): bool
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isDOMNodeNotPresent($node))
|
if (! isDOMNodeNotPresent($node)) {
|
||||||
{
|
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -973,7 +1055,9 @@ if (!isDOMNodeNotPresent($node))
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
function isDOMNodePresent(\DOMNode $node): bool
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
function isDOMNodePresent(DOMNode $node): bool
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
@@ -999,6 +1083,8 @@ just do one thing.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Airplane
|
class Airplane
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@@ -1020,6 +1106,8 @@ class Airplane
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
interface Airplane
|
interface Airplane
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@@ -1070,6 +1158,8 @@ The first thing to consider is consistent APIs.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function travelToTexas($vehicle): void
|
function travelToTexas($vehicle): void
|
||||||
{
|
{
|
||||||
if ($vehicle instanceof Bicycle) {
|
if ($vehicle instanceof Bicycle) {
|
||||||
@@ -1083,6 +1173,8 @@ function travelToTexas($vehicle): void
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function travelToTexas(Vehicle $vehicle): void
|
function travelToTexas(Vehicle $vehicle): void
|
||||||
{
|
{
|
||||||
$vehicle->travelTo(new Location('texas'));
|
$vehicle->travelTo(new Location('texas'));
|
||||||
@@ -1106,10 +1198,12 @@ Otherwise, do all of that but with PHP strict type declaration or strict mode.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function combine($val1, $val2): int
|
function combine($val1, $val2): int
|
||||||
{
|
{
|
||||||
if (!is_numeric($val1) || !is_numeric($val2)) {
|
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;
|
return $val1 + $val2;
|
||||||
@@ -1119,6 +1213,8 @@ function combine($val1, $val2): int
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function combine(int $val1, int $val2): int
|
function combine(int $val1, int $val2): int
|
||||||
{
|
{
|
||||||
return $val1 + $val2;
|
return $val1 + $val2;
|
||||||
@@ -1136,6 +1232,8 @@ in your version history if you still need it.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function oldRequestModule(string $url): void
|
function oldRequestModule(string $url): void
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@@ -1153,6 +1251,8 @@ inventoryTracker('apples', $request, 'www.inventory-awesome.io');
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function requestModule(string $url): void
|
function requestModule(string $url): void
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@@ -1186,6 +1286,8 @@ Additionally, this is part of [Open/Closed](#openclosed-principle-ocp) principle
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class BankAccount
|
class BankAccount
|
||||||
{
|
{
|
||||||
public $balance = 1000;
|
public $balance = 1000;
|
||||||
@@ -1253,6 +1355,8 @@ For more informations you can read the [blog post](http://fabien.potencier.org/p
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Employee
|
class Employee
|
||||||
{
|
{
|
||||||
public $name;
|
public $name;
|
||||||
@@ -1264,12 +1368,15 @@ class Employee
|
|||||||
}
|
}
|
||||||
|
|
||||||
$employee = new Employee('John Doe');
|
$employee = new Employee('John Doe');
|
||||||
echo 'Employee name: '.$employee->name; // Employee name: John Doe
|
// Employee name: John Doe
|
||||||
|
echo 'Employee name: ' . $employee->name;
|
||||||
```
|
```
|
||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Employee
|
class Employee
|
||||||
{
|
{
|
||||||
private $name;
|
private $name;
|
||||||
@@ -1286,7 +1393,8 @@ class Employee
|
|||||||
}
|
}
|
||||||
|
|
||||||
$employee = new Employee('John Doe');
|
$employee = new Employee('John Doe');
|
||||||
echo 'Employee name: '.$employee->getName(); // Employee name: John Doe
|
// Employee name: John Doe
|
||||||
|
echo 'Employee name: ' . $employee->getName();
|
||||||
```
|
```
|
||||||
|
|
||||||
**[⬆ back to top](#table-of-contents)**
|
**[⬆ back to top](#table-of-contents)**
|
||||||
@@ -1315,9 +1423,12 @@ relationship (Human->Animal vs. User->UserDetails).
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Employee
|
class Employee
|
||||||
{
|
{
|
||||||
private $name;
|
private $name;
|
||||||
|
|
||||||
private $email;
|
private $email;
|
||||||
|
|
||||||
public function __construct(string $name, string $email)
|
public function __construct(string $name, string $email)
|
||||||
@@ -1335,6 +1446,7 @@ class Employee
|
|||||||
class EmployeeTaxData extends Employee
|
class EmployeeTaxData extends Employee
|
||||||
{
|
{
|
||||||
private $ssn;
|
private $ssn;
|
||||||
|
|
||||||
private $salary;
|
private $salary;
|
||||||
|
|
||||||
public function __construct(string $name, string $email, string $ssn, string $salary)
|
public function __construct(string $name, string $email, string $ssn, string $salary)
|
||||||
@@ -1352,9 +1464,12 @@ class EmployeeTaxData extends Employee
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class EmployeeTaxData
|
class EmployeeTaxData
|
||||||
{
|
{
|
||||||
private $ssn;
|
private $ssn;
|
||||||
|
|
||||||
private $salary;
|
private $salary;
|
||||||
|
|
||||||
public function __construct(string $ssn, string $salary)
|
public function __construct(string $ssn, string $salary)
|
||||||
@@ -1369,7 +1484,9 @@ class EmployeeTaxData
|
|||||||
class Employee
|
class Employee
|
||||||
{
|
{
|
||||||
private $name;
|
private $name;
|
||||||
|
|
||||||
private $email;
|
private $email;
|
||||||
|
|
||||||
private $taxData;
|
private $taxData;
|
||||||
|
|
||||||
public function __construct(string $name, string $email)
|
public function __construct(string $name, string $email)
|
||||||
@@ -1378,7 +1495,7 @@ class Employee
|
|||||||
$this->email = $email;
|
$this->email = $email;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTaxData(EmployeeTaxData $taxData)
|
public function setTaxData(EmployeeTaxData $taxData): void
|
||||||
{
|
{
|
||||||
$this->taxData = $taxData;
|
$this->taxData = $taxData;
|
||||||
}
|
}
|
||||||
@@ -1411,10 +1528,14 @@ on this topic written by [Marco Pivetta](https://github.com/Ocramius).
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Car
|
class Car
|
||||||
{
|
{
|
||||||
private $make = 'Honda';
|
private $make = 'Honda';
|
||||||
|
|
||||||
private $model = 'Accord';
|
private $model = 'Accord';
|
||||||
|
|
||||||
private $color = 'white';
|
private $color = 'white';
|
||||||
|
|
||||||
public function setMake(string $make): self
|
public function setMake(string $make): self
|
||||||
@@ -1448,19 +1569,23 @@ class Car
|
|||||||
}
|
}
|
||||||
|
|
||||||
$car = (new Car())
|
$car = (new Car())
|
||||||
->setColor('pink')
|
->setColor('pink')
|
||||||
->setMake('Ford')
|
->setMake('Ford')
|
||||||
->setModel('F-150')
|
->setModel('F-150')
|
||||||
->dump();
|
->dump();
|
||||||
```
|
```
|
||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Car
|
class Car
|
||||||
{
|
{
|
||||||
private $make = 'Honda';
|
private $make = 'Honda';
|
||||||
|
|
||||||
private $model = 'Accord';
|
private $model = 'Accord';
|
||||||
|
|
||||||
private $color = 'white';
|
private $color = 'white';
|
||||||
|
|
||||||
public function setMake(string $make): void
|
public function setMake(string $make): void
|
||||||
@@ -1510,6 +1635,8 @@ For more informations you can read [the blog post](https://ocramius.github.io/bl
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
final class Car
|
final class Car
|
||||||
{
|
{
|
||||||
private $color;
|
private $color;
|
||||||
@@ -1532,6 +1659,8 @@ final class Car
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
interface Vehicle
|
interface Vehicle
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -1549,9 +1678,6 @@ final class Car implements Vehicle
|
|||||||
$this->color = $color;
|
$this->color = $color;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getColor()
|
public function getColor()
|
||||||
{
|
{
|
||||||
return $this->color;
|
return $this->color;
|
||||||
@@ -1585,6 +1711,8 @@ your codebase.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class UserSettings
|
class UserSettings
|
||||||
{
|
{
|
||||||
private $user;
|
private $user;
|
||||||
@@ -1611,6 +1739,8 @@ class UserSettings
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class UserAuth
|
class UserAuth
|
||||||
{
|
{
|
||||||
private $user;
|
private $user;
|
||||||
@@ -1629,6 +1759,7 @@ class UserAuth
|
|||||||
class UserSettings
|
class UserSettings
|
||||||
{
|
{
|
||||||
private $user;
|
private $user;
|
||||||
|
|
||||||
private $auth;
|
private $auth;
|
||||||
|
|
||||||
public function __construct(User $user)
|
public function __construct(User $user)
|
||||||
@@ -1658,6 +1789,8 @@ add new functionalities without changing existing code.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
abstract class Adapter
|
abstract class Adapter
|
||||||
{
|
{
|
||||||
protected $name;
|
protected $name;
|
||||||
@@ -1723,6 +1856,8 @@ class HttpRequester
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
interface Adapter
|
interface Adapter
|
||||||
{
|
{
|
||||||
public function request(string $url): Promise;
|
public function request(string $url): Promise;
|
||||||
@@ -1780,9 +1915,12 @@ get into trouble.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Rectangle
|
class Rectangle
|
||||||
{
|
{
|
||||||
protected $width = 0;
|
protected $width = 0;
|
||||||
|
|
||||||
protected $height = 0;
|
protected $height = 0;
|
||||||
|
|
||||||
public function setWidth(int $width): void
|
public function setWidth(int $width): void
|
||||||
@@ -1820,7 +1958,7 @@ function printArea(Rectangle $rectangle): void
|
|||||||
$rectangle->setHeight(5);
|
$rectangle->setHeight(5);
|
||||||
|
|
||||||
// BAD: Will return 25 for Square. Should be 20.
|
// BAD: Will return 25 for Square. Should be 20.
|
||||||
echo sprintf('%s has area %d.', get_class($rectangle), $rectangle->getArea()).PHP_EOL;
|
echo sprintf('%s has area %d.', get_class($rectangle), $rectangle->getArea()) . PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
$rectangles = [new Rectangle(), new Square()];
|
$rectangles = [new Rectangle(), new Square()];
|
||||||
@@ -1903,6 +2041,8 @@ all of the settings. Making them optional helps prevent having a "fat interface"
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
interface Employee
|
interface Employee
|
||||||
{
|
{
|
||||||
public function work(): void;
|
public function work(): void;
|
||||||
@@ -1942,6 +2082,8 @@ class RobotEmployee implements Employee
|
|||||||
Not every worker is an employee, but every employee is a worker.
|
Not every worker is an employee, but every employee is a worker.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
interface Workable
|
interface Workable
|
||||||
{
|
{
|
||||||
public function work(): void;
|
public function work(): void;
|
||||||
@@ -1999,6 +2141,8 @@ it makes your code hard to refactor.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
class Employee
|
class Employee
|
||||||
{
|
{
|
||||||
public function work(): void
|
public function work(): void
|
||||||
@@ -2034,6 +2178,8 @@ class Manager
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
interface Employee
|
interface Employee
|
||||||
{
|
{
|
||||||
public function work(): void;
|
public function work(): void;
|
||||||
@@ -2101,17 +2247,15 @@ updating multiple places any time you want to change one thing.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function showDeveloperList(array $developers): void
|
function showDeveloperList(array $developers): void
|
||||||
{
|
{
|
||||||
foreach ($developers as $developer) {
|
foreach ($developers as $developer) {
|
||||||
$expectedSalary = $developer->calculateExpectedSalary();
|
$expectedSalary = $developer->calculateExpectedSalary();
|
||||||
$experience = $developer->getExperience();
|
$experience = $developer->getExperience();
|
||||||
$githubLink = $developer->getGithubLink();
|
$githubLink = $developer->getGithubLink();
|
||||||
$data = [
|
$data = [$expectedSalary, $experience, $githubLink];
|
||||||
$expectedSalary,
|
|
||||||
$experience,
|
|
||||||
$githubLink
|
|
||||||
];
|
|
||||||
|
|
||||||
render($data);
|
render($data);
|
||||||
}
|
}
|
||||||
@@ -2123,11 +2267,7 @@ function showManagerList(array $managers): void
|
|||||||
$expectedSalary = $manager->calculateExpectedSalary();
|
$expectedSalary = $manager->calculateExpectedSalary();
|
||||||
$experience = $manager->getExperience();
|
$experience = $manager->getExperience();
|
||||||
$githubLink = $manager->getGithubLink();
|
$githubLink = $manager->getGithubLink();
|
||||||
$data = [
|
$data = [$expectedSalary, $experience, $githubLink];
|
||||||
$expectedSalary,
|
|
||||||
$experience,
|
|
||||||
$githubLink
|
|
||||||
];
|
|
||||||
|
|
||||||
render($data);
|
render($data);
|
||||||
}
|
}
|
||||||
@@ -2137,17 +2277,15 @@ function showManagerList(array $managers): void
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function showList(array $employees): void
|
function showList(array $employees): void
|
||||||
{
|
{
|
||||||
foreach ($employees as $employee) {
|
foreach ($employees as $employee) {
|
||||||
$expectedSalary = $employee->calculateExpectedSalary();
|
$expectedSalary = $employee->calculateExpectedSalary();
|
||||||
$experience = $employee->getExperience();
|
$experience = $employee->getExperience();
|
||||||
$githubLink = $employee->getGithubLink();
|
$githubLink = $employee->getGithubLink();
|
||||||
$data = [
|
$data = [$expectedSalary, $experience, $githubLink];
|
||||||
$expectedSalary,
|
|
||||||
$experience,
|
|
||||||
$githubLink
|
|
||||||
];
|
|
||||||
|
|
||||||
render($data);
|
render($data);
|
||||||
}
|
}
|
||||||
@@ -2159,14 +2297,12 @@ function showList(array $employees): void
|
|||||||
It is better to use a compact version of the code.
|
It is better to use a compact version of the code.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function showList(array $employees): void
|
function showList(array $employees): void
|
||||||
{
|
{
|
||||||
foreach ($employees as $employee) {
|
foreach ($employees as $employee) {
|
||||||
render([
|
render([$employee->calculateExpectedSalary(), $employee->getExperience(), $employee->getGithubLink()]);
|
||||||
$employee->calculateExpectedSalary(),
|
|
||||||
$employee->getExperience(),
|
|
||||||
$employee->getGithubLink()
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -4,7 +4,7 @@
|
|||||||
"symplify/easy-coding-standard": "^8.3"
|
"symplify/easy-coding-standard": "^8.3"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"check-cs": "vendor/bin/ecs check README.md",
|
"check-cs": "vendor/bin/ecs check-markdown README.md",
|
||||||
"fix-cs": "vendor/bin/ecs check README.md --fix"
|
"fix-cs": "vendor/bin/ecs check-markdown README.md --fix"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
6
ecs.php
6
ecs.php
@@ -3,7 +3,7 @@
|
|||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||||
use Symplify\EasyCodingStandard\Configuration\Option;
|
use Symplify\EasyCodingStandard\ValueObject\Option;
|
||||||
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
|
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
|
||||||
|
|
||||||
return static function (ContainerConfigurator $containerConfigurator): void
|
return static function (ContainerConfigurator $containerConfigurator): void
|
||||||
@@ -20,4 +20,8 @@ return static function (ContainerConfigurator $containerConfigurator): void
|
|||||||
SetList::PHP_71,
|
SetList::PHP_71,
|
||||||
SetList::SYMPLIFY,
|
SetList::SYMPLIFY,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$parameters->set(Option::SKIP, [
|
||||||
|
\PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer::class => null,
|
||||||
|
]);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user