## 原文摘自:https://github.com/php-cpm/clean-code-php
## 1、 命名
#### 1.1、变量命名
~~~
#Bad
$ymdstr = $moment->format('y-m-d');
--------------------------------------------------------
#good
$currentDate = $moment->format('y-m-d');
~~~
#### 1.2、方法命名
~~~
同一个实体要用相同的变量名
#Bad
getUserInfo();
getUserData();
getUserRecord();
getUserProfile();
--------------------------------------------------------
#Good
getUser();
~~~
#### 1.3、使用定义好的常量
~~~
#Bad
//谁知道4是啥?
if ($user->access & 4) {
// ...
}
--------------------------------------------------------
#Good
class User
{
const ACCESS_READ = 1;
const ACCESS_CREATE = 2;
const ACCESS_UPDATE = 4;
const ACCESS_DELETE = 8;
}
if ($user->access & User::ACCESS_UPDATE) {
// do edit ...
}
~~~
#### 1.4 、无意义的变量名
~~~
#Bad
$l = ['Austin', 'New York', 'San Francisco'];
for ($i = 0; $i < count($l); $i++) {
$li = $l[$i];
doStuff();
doSomeOtherStuff();
// ...
// ...
// ...
// `$li` 代表什么来着?
dispatch($li);
}
--------------------------------------------------------
#Good
$locations = ['Austin', 'New York', 'San Francisco'];
foreach ($locations as $location) {
doStuff();
doSomeOtherStuff();
// ...
// ...
// ...
dispatch($location);
}
~~~
#### 1.5 不必要的上下文
~~~
#Bad
class Car
{
public $carMake;
public $carModel;
public $carColor;
//...
}
--------------------------------------------------------
#Good
class Car
{
public $make;
public $model;
public $color;
//...
}
~~~
## 2、简化写
#### 2.1 太多的 if else
~~~
#Bad
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;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
--------------------------------------------------------
#Good
function isShopOpen(string $day): bool
{
if (empty($day)) {
return false;
}
$openingDays = [
'friday', 'saturday', 'sunday'
];
return in_array(strtolower($day), $openingDays, true);
}
~~~
~~~
#Bad
function fibonacci(int $n)
{
if ($n < 50) {
if ($n !== 0) {
if ($n !== 1) {
return fibonacci($n - 1) + fibonacci($n - 2);
} else {
return 1;
}
} else {
return 0;
}
} else {
return 'Not supported';
}
}
--------------------------------------------------------
#Good
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);
}
~~~
## 3、方法
## 3.1、参数最好少于3个
~~~
#Bad
function createMenu(string $title, string $body, string $buttonText, bool $cancellable): void
{
// ...
}
--------------------------------------------------------
#Good
class MenuConfig
{
public $title;
public $body;
public $buttonText;
public $cancellable = false;
}
$config = new MenuConfig();
$config->title = 'Foo';
$config->body = 'Bar';
$config->buttonText = 'Baz';
$config->cancellable = true;
function createMenu(MenuConfig $config): void
{
// ...
}
~~~
## 3.2、函数应该只做一件事
~~~
#Bad
function emailClients(array $clients): void
{
foreach ($clients as $client) {
$clientRecord = $db->find($client);
if ($clientRecord->isActive()) {
email($client);
}
}
}
--------------------------------------------------------
#Good
function emailClients(array $clients): void
{
$activeClients = activeClients($clients);
array_walk($activeClients, 'email');
}
function activeClients(array $clients): array
{
return array_filter($clients, 'isClientActive');
}
function isClientActive(int $client): bool
{
$clientRecord = $db->find($client);
return $clientRecord->isActive();
}
~~~