mirror of
https://github.com/jupeter/clean-code-php.git
synced 2025-09-25 21:49:04 +02:00
Merge pull request #188 from andyexeter/remove-declare-strict-types
Remove declare strict_types from code snippets
This commit is contained in:
136
README.md
136
README.md
@@ -69,16 +69,12 @@ 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');
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -89,8 +85,6 @@ $currentDate = $moment->format('y-m-d');
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
getUserInfo();
|
getUserInfo();
|
||||||
getUserData();
|
getUserData();
|
||||||
getUserRecord();
|
getUserRecord();
|
||||||
@@ -100,8 +94,6 @@ getUserProfile();
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
getUser();
|
getUser();
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -117,8 +109,6 @@ 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);
|
||||||
```
|
```
|
||||||
@@ -126,8 +116,6 @@ $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);
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -136,8 +124,6 @@ $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?
|
||||||
@@ -156,8 +142,6 @@ $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;
|
||||||
@@ -187,8 +171,6 @@ $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);
|
||||||
@@ -201,8 +183,6 @@ 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);
|
||||||
@@ -216,8 +196,6 @@ 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);
|
||||||
@@ -235,8 +213,6 @@ than implicit.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
function isShopOpen($day): bool
|
function isShopOpen($day): bool
|
||||||
{
|
{
|
||||||
if ($day) {
|
if ($day) {
|
||||||
@@ -260,8 +236,6 @@ function isShopOpen($day): bool
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
function isShopOpen(string $day): bool
|
function isShopOpen(string $day): bool
|
||||||
{
|
{
|
||||||
if (empty($day)) {
|
if (empty($day)) {
|
||||||
@@ -281,8 +255,6 @@ 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) {
|
||||||
@@ -301,8 +273,6 @@ function fibonacci(int $n)
|
|||||||
**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) {
|
||||||
@@ -344,8 +314,6 @@ 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) {
|
||||||
@@ -368,8 +336,6 @@ variable name.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class Car
|
class Car
|
||||||
{
|
{
|
||||||
public $carMake;
|
public $carMake;
|
||||||
@@ -385,8 +351,6 @@ class Car
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class Car
|
class Car
|
||||||
{
|
{
|
||||||
public $make;
|
public $make;
|
||||||
@@ -448,8 +412,6 @@ 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;
|
||||||
|
|
||||||
@@ -466,8 +428,6 @@ 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;
|
||||||
|
|
||||||
@@ -487,8 +447,6 @@ 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'])) {
|
||||||
@@ -500,8 +458,6 @@ if (isset($_GET['name'])) {
|
|||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
$name = $_GET['name'] ?? $_POST['name'] ?? 'nobody';
|
$name = $_GET['name'] ?? $_POST['name'] ?? 'nobody';
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -523,8 +479,6 @@ 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(
|
||||||
@@ -545,8 +499,6 @@ class Questionnaire
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class Name
|
class Name
|
||||||
{
|
{
|
||||||
private $firstname;
|
private $firstname;
|
||||||
@@ -658,8 +610,6 @@ testing.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
function parseBetterPHPAlternative(string $code): void
|
function parseBetterPHPAlternative(string $code): void
|
||||||
{
|
{
|
||||||
$regexes = [
|
$regexes = [
|
||||||
@@ -798,8 +748,6 @@ 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) {
|
||||||
@@ -813,8 +761,6 @@ 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);
|
||||||
@@ -847,8 +793,6 @@ 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';
|
||||||
@@ -869,8 +813,6 @@ 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);
|
||||||
@@ -899,8 +841,6 @@ that tried to do the same thing.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
function config(): array
|
function config(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@@ -912,8 +852,6 @@ function config(): array
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class Configuration
|
class Configuration
|
||||||
{
|
{
|
||||||
private $configuration = [];
|
private $configuration = [];
|
||||||
@@ -934,8 +872,6 @@ 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',
|
||||||
]);
|
]);
|
||||||
@@ -958,8 +894,6 @@ 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;
|
||||||
@@ -987,8 +921,6 @@ $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)
|
||||||
@@ -1003,8 +935,6 @@ 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);
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1017,8 +947,6 @@ 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') {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
@@ -1027,8 +955,6 @@ if ($article->state === 'published') {
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
if ($article->isPublished()) {
|
if ($article->isPublished()) {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
@@ -1041,8 +967,6 @@ if ($article->isPublished()) {
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
function isDOMNodeNotPresent(DOMNode $node): bool
|
function isDOMNodeNotPresent(DOMNode $node): bool
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@@ -1056,8 +980,6 @@ if (! isDOMNodeNotPresent($node)) {
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
function isDOMNodePresent(DOMNode $node): bool
|
function isDOMNodePresent(DOMNode $node): bool
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@@ -1084,8 +1006,6 @@ just do one thing.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class Airplane
|
class Airplane
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@@ -1107,8 +1027,6 @@ class Airplane
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
interface Airplane
|
interface Airplane
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@@ -1159,8 +1077,6 @@ 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) {
|
||||||
@@ -1174,8 +1090,6 @@ 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'));
|
||||||
@@ -1199,8 +1113,6 @@ 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)) {
|
||||||
@@ -1214,8 +1126,6 @@ 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;
|
||||||
@@ -1233,8 +1143,6 @@ 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
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@@ -1252,8 +1160,6 @@ 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
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@@ -1287,8 +1193,6 @@ 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;
|
||||||
@@ -1356,8 +1260,6 @@ 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;
|
||||||
@@ -1376,8 +1278,6 @@ echo 'Employee name: ' . $employee->name;
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class Employee
|
class Employee
|
||||||
{
|
{
|
||||||
private $name;
|
private $name;
|
||||||
@@ -1424,8 +1324,6 @@ relationship (Human->Animal vs. User->UserDetails).
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class Employee
|
class Employee
|
||||||
{
|
{
|
||||||
private $name;
|
private $name;
|
||||||
@@ -1465,8 +1363,6 @@ class EmployeeTaxData extends Employee
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class EmployeeTaxData
|
class EmployeeTaxData
|
||||||
{
|
{
|
||||||
private $ssn;
|
private $ssn;
|
||||||
@@ -1529,8 +1425,6 @@ 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';
|
||||||
@@ -1579,8 +1473,6 @@ $car = (new Car())
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class Car
|
class Car
|
||||||
{
|
{
|
||||||
private $make = 'Honda';
|
private $make = 'Honda';
|
||||||
@@ -1636,8 +1528,6 @@ 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;
|
||||||
@@ -1660,8 +1550,6 @@ final class Car
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
interface Vehicle
|
interface Vehicle
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -1712,8 +1600,6 @@ your codebase.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class UserSettings
|
class UserSettings
|
||||||
{
|
{
|
||||||
private $user;
|
private $user;
|
||||||
@@ -1740,8 +1626,6 @@ class UserSettings
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class UserAuth
|
class UserAuth
|
||||||
{
|
{
|
||||||
private $user;
|
private $user;
|
||||||
@@ -1790,8 +1674,6 @@ 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;
|
||||||
@@ -1857,8 +1739,6 @@ 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;
|
||||||
@@ -1916,8 +1796,6 @@ get into trouble.
|
|||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
class Rectangle
|
class Rectangle
|
||||||
{
|
{
|
||||||
protected $width = 0;
|
protected $width = 0;
|
||||||
@@ -2042,8 +1920,6 @@ 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;
|
||||||
@@ -2083,8 +1959,6 @@ 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;
|
||||||
@@ -2142,8 +2016,6 @@ 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
|
||||||
@@ -2179,8 +2051,6 @@ class Manager
|
|||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
interface Employee
|
interface Employee
|
||||||
{
|
{
|
||||||
public function work(): void;
|
public function work(): void;
|
||||||
@@ -2248,8 +2118,6 @@ 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) {
|
||||||
@@ -2278,8 +2146,6 @@ 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) {
|
||||||
@@ -2298,8 +2164,6 @@ 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) {
|
||||||
|
2
ecs.php
2
ecs.php
@@ -3,6 +3,7 @@
|
|||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer;
|
use PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer;
|
||||||
|
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer;
|
||||||
use PhpCsFixer\Fixer\Strict\StrictComparisonFixer;
|
use PhpCsFixer\Fixer\Strict\StrictComparisonFixer;
|
||||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||||
use Symplify\EasyCodingStandard\ValueObject\Option;
|
use Symplify\EasyCodingStandard\ValueObject\Option;
|
||||||
@@ -26,5 +27,6 @@ return static function (ContainerConfigurator $containerConfigurator): void
|
|||||||
$parameters->set(Option::SKIP, [
|
$parameters->set(Option::SKIP, [
|
||||||
BlankLineAfterOpeningTagFixer::class => null,
|
BlankLineAfterOpeningTagFixer::class => null,
|
||||||
StrictComparisonFixer::class => null,
|
StrictComparisonFixer::class => null,
|
||||||
|
DeclareStrictTypesFixer::class => null,
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user