PHP代码_PHP整洁之道

更新日期: 2018-12-09阅读: 1.8k标签: php

摘录自 Robert C. Martin的Clean Code 书中的软件工程师的原则 ,适用于php。 这不是风格指南。 这是一个关于开发可读、可复用并且可重构的PHP软件指南。 并不是这里所有的原则都得遵循,甚至很少的能被普遍接受。 这些虽然只是指导,但是都是Clean Code作者多年总结出来的。


变量

使用有意义且可拼写的变量名
Bad:
$ymdstr = $moment->format(‘y-m-d‘);
Good:
$currentDate = $moment->format(‘y-m-d‘);

 

同种类型的变量使用相同词汇
Bad:
getUserInfo();
getClientData();
getCustomerRecord();
Good:
getUser();

 

使用易检索的名称
我们会读比写要多的代码。通过是命名易搜索,让我们写出可读性和易搜索代码很重要。
Bad:
// What the heck is 86400 for?
addExpireAt(86400);
Good:
// Declare them as capitalized `const` globals.
interface DateGlobal {
  const SECONDS_IN_A_DAY = 86400;
}
 
addExpireAt(DateGlobal::SECONDS_IN_A_DAY);

 

使用解释型变量
Bad:
$address = ‘One Infinite Loop, Cupertino 95014‘;
$cityZipCodeRegex = ‘/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/‘;
preg_match($cityZipCodeRegex, $address, $matches);
saveCityZipCode($matches[1], $matches[2]);
Good:
$address = ‘One Infinite Loop, Cupertino 95014‘;
$cityZipCodeRegex = ‘/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/‘;
preg_match($cityZipCodeRegex, $address, $matches);
list(, $city, $zipCode) = $matchers;
saveCityZipCode($city, $zipCode);

 

避免心理映射,明确比隐性好
Bad:
$l = [‘Austin‘, ‘New York‘, ‘San Francisco‘];
foreach($i=0; $i<count($l); $i++) {
  oStuff();
  doSomeOtherStuff();
  // ...
  // ...
  // ...
  // 等等`$l` 又代表什么?
  dispatch($l);
}
Good:
$locations = [‘Austin‘, ‘New York‘, ‘San Francisco‘];
foreach($i=0; $i<count($locations); $i++) {
  $location = $locations[$i];
 
  doStuff();
  doSomeOtherStuff();
  // ...
  // ...
  // ...
  dispatch($location);
});

 

不要添加不必要上下文
如果你的class/object 名能告诉你什么,不要把它重复在你变量名里。
Bad:
$car = [
  ‘carMake‘  => ‘Honda‘,
  ‘carModel‘ => ‘Accord‘,
  ‘carColor‘ => ‘Blue‘,
];
 
function paintCar(&$car) {
  $car[‘carColor‘] = ‘Red‘;
}
Good:
$car = [
  ‘make‘  => ‘Honda‘,
  ‘model‘ => ‘Accord‘,
  ‘color‘ => ‘Blue‘,
];
 
function paintCar(&$car) {
  $car[‘color‘] = ‘Red‘;
}

 

使用参数默认值代替短路或条件语句。
Bad:
function createMicrobrewery($name = null) {
  $breweryName = $name ?: ‘Hipster Brew Co.‘;
  // ...
}
Good:
function createMicrobrewery($breweryName = ‘Hipster Brew Co.‘) {
  // ...
}

 

函数

 
函数参数最好少于2个
限制函数参数个数极其重要因为它是你函数测试容易点。有超过3个可选参数参数导致一个爆炸式组合增长,你会有成吨独立参数情形要测试。
无参数是理想情况。1个或2个都可以,最好避免3个。再多旧需要加固了。通常如果你的函数有超过两个参数,说明他多做了一些事。 在参数少的情况里,大多数时候一个高级别对象(数组)作为参数就足够应付。
Bad:
function createMenu($title, $body, $buttonText, $cancellable) {
  // ...
}
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) {
  // ...
}

 

函数应该只做一件事
这是迄今为止软件工程里最重要的一个规则。当函数做超过一件事的时候,他们就难于实现、测试和理解。当你隔离函数只剩一个功能时,他们就容易被重构,然后你的代码读起来就更清晰。如果你光遵循这条规则,你就领先于大多数开发者了。
Bad:
function emailClients($clients) {
  foreach ($clients as $client) {
    $clientRecord = $db->find($client);
    if($clientRecord->isActive()) {
       email($client);
    }
  }
}
Good:
function emailClients($clients) {
  $activeClients = activeClients($clients);
  array_walk($activeClients, ‘email‘);
}
 
function activeClients($clients) {
  return array_filter($clients, ‘isClientActive‘);
}
 
function isClientActive($client) {
  $clientRecord = $db->find($client);
  return $clientRecord->isActive();
}

 

函数名应当描述他们所做的事
Bad:
function addToDate($date, $month) {
  // ...
}
 
$date = new \DateTime();
 
// It‘s hard to to tell from the function name what is added
addToDate($date, 1);
Good:
function addMonthToDate($month, $date) {
  // ...
}
 
$date = new \DateTime();
addMonthToDate(1, $date);

 

函数应当只为一层抽象,当你超过一层抽象时,函数正在做多件事。拆分功能易达到可重用性和易用性。.
Bad:
function parseBetterJSAlternative($code) {
  $regexes = [
    // ...
  ];
 
  $statements = split(‘ ‘, $code);
  $tokens = [];
  foreach($regexes as $regex) {
    foreach($statements as $statement) {
      // ...
    }
  }
 
  $ast = [];
  foreach($tokens as $token) {
     // lex...
  }
 
  foreach($ast as $node) {
   // parse...
  }
}
Good:
function tokenize($code) {
  $regexes = [
    // ...
  ];
 
  $statements = split(‘ ‘, $code);
  $tokens = [];
  foreach($regexes as $regex) {
    foreach($statements as $statement) {
      $tokens[] = /* ... */;
    });
  });
 
  return tokens;
}
 
function lexer($tokens) {
  $ast = [];
  foreach($tokens as $token) {
    $ast[] = /* ... */;
  });
 
  return ast;
}
 
function parseBetterJSAlternative($code) {
  $tokens = tokenize($code);
  $ast = lexer($tokens);
  foreach($ast as $node) {
    // parse...
  });
}

 

删除重复的代码
尽你最大的努力来避免重复的代码。重复代码不好,因为它意味着如果你修改一些逻辑,那就有不止一处地方要同步修改了。
想象一下如果你经营着一家餐厅并跟踪它的库存: 你全部的西红柿、洋葱、大蒜、香料等。如果你保留有多个列表,当你服务一个有着西红柿的菜,那么所有记录都得更新。如果你只有一个列表,那么只需要修改一个地方!
经常你容忍重复代码,因为你有两个或更多有共同部分但是少许差异的东西强制你用两个或更多独立的函数来做相同的事。移除重复代码意味着创造一个处理这组不同事物的一个抽象,只需要一个函数/模块/类。
抽象正确非常重要,这也是为什么你应当遵循SOLID原则(奠定Class基础的原则)。坏的抽象可能比重复代码还要糟,因为要小心。在这个前提下,如果你可以抽象好,那就开始做把!不要重复你自己,否则任何你想改变一件事的时候你都发现在即在更新维护多处。
Bad:
function showDeveloperList($developers) {
  foreach($developers as $developer) {
    $expectedSalary = $developer->calculateExpectedSalary();
    $experience = $developer->getExperience();
    $githubLink = $developer->getGithubLink();
    $data = [
      $expectedSalary,
      $experience,
      $githubLink
    ];
 
    render($data);
  }
}
 
function showManagerList($managers) {
  foreach($managers as $manager) {
    $expectedSalary = $manager->calculateExpectedSalary();
    $experience = $manager->getExperience();
    $githubLink = $manager->getGithubLink();
    $data = [
      $expectedSalary,
      $experience,
      $githubLink
    ];
 
    render($data);
  }
}
Good:
function showList($employees) {
  foreach($employees as $employe) {
    $expectedSalary = $employe->calculateExpectedSalary();
    $experience = $employe->getExperience();
    $githubLink = $employe->getGithubLink();
    $data = [
      $expectedSalary,
      $experience,
      $githubLink
    ];
 
    render($data);
  }
}

 

通过对象赋值设置默认值
Bad:
$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:
$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);

 

不要用标志作为函数的参数,标志告诉你的用户函数做很多事了。函数应当只做一件事。 根据布尔值区别的路径来拆分你的复杂函数。
Bad:
function createFile(name, temp = false) {
  if (temp) {
    touch(‘./temp/‘.$name);
  } else {
    touch($name);
  }
}
Good:
function createFile($name) {
  touch(name);
}
 
function createTempFile($name) {
  touch(‘./temp/‘.$name);
}

 

避免副作用
一个函数做了比获取一个值然后返回另外一个值或值们会产生副作用如果。副作用可能是写入一个文件,修改某些全局变量或者偶然的把你全部的钱给了陌生人。
现在,你的确需要在一个程序或者场合里要有副作用,像之前的例子,你也许需要写一个文件。你想要做的是把你做这些的地方集中起来。不要用几个函数和类来写入一个特定的文件。用一个服务来做它,一个只有一个。
重点是避免常见陷阱比如对象间共享无结构的数据,使用可以写入任何的可变数据类型,不集中处理副作用发生的地方。如果你做了这些你就会比大多数程序员快乐。
Bad:
// 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);
}
 
splitIntoFirstAndLastName();
 
var_dump($name); // [‘Ryan‘, ‘McDermott‘];
Good:
$name = ‘Ryan McDermott‘;
 
function splitIntoFirstAndLastName($name) {
  return preg_split(‘/ /‘, $name);
}
 
$name = ‘Ryan McDermott‘;
$newName = splitIntoFirstAndLastName(name);
 
var_export($name); // ‘Ryan McDermott‘;
var_export($newName); // [‘Ryan‘, ‘McDermott‘];

 

不要写全局函数
在大多数语言中污染全局变量是一个坏的实践,因为你可能和其他类库冲突并且你api的用户不明白为什么直到他们获得产品的一个异常。让我们看一个例子:如果你想配置一个数组,你可能会写一个全局函数像config(),但是可能和试着做同样事的其他类库冲突。这就是为什么单例设计模式和简单配置会更好的原因。
Bad:
function config() {
  return  [
    ‘foo‘: ‘bar‘,
  ]
};
Good:
class Configuration {
  private static $instance;
  private function __construct($configuration) {/* */}
  public static function getInstance() {
     if(self::$instance === null) {
         self::$instance = new Configuration();
     }
     return self::$instance;
 }
 public function get($key) {/* */}
 public function getAll() {/* */}
}
 
$singleton = Configuration::getInstance();

 

封装条件语句
Bad:
if ($fsm->state === ‘fetching‘ && is_empty($listNode)) {
  // ...
}
Good:
function shouldShowSpinner($fsm, $listNode) {
  return $fsm->state === ‘fetching‘ && is_empty(listNode);
}
 
if (shouldShowSpinner($fsmInstance, $listNodeInstance)) {
  // ...
}

 

避免消极条件
Bad:
function isdomNodeNotPresent($node) {
  // ...
}
 
if (!isDOMNodeNotPresent($node)) {
  // ...
}
Good:
function isDOMNodePresent($node) {
  // ...
}
 
if (isDOMNodePresent($node)) {
  // ...
}

 

避免条件声明
这看起来像一个不可能任务。当人们第一次听到这句话是都会这么说。 "没有一个if声明" 答案是你可以使用多态来达到许多case语句里的任务。第二个问题很常见, “那么为什么我要那么做?” 答案是前面我们学过的一个整洁代码原则:一个函数应当只做一件事。当你有类和函数有很多if声明,你自己知道你的函数做了不止一件事。记住,只做一件事。
Bad:
class Airplane {
  // ...
  public function getCruisingAltitude() {
    switch (this.type) {
      case ‘777‘:
        return $this->getMaxAltitude() - $this->getPassengerCount();
      case ‘Air Force One‘:
        return $this->getMaxAltitude();
      case ‘Cessna‘:
        return $this->getMaxAltitude() - $this->getFuelExpenditure();
    }
  }
}
Good:
class Airplane {
  // ...
}
 
class Boeing777 extends Airplane {
  // ...
  public function getCruisingAltitude() {
    return $this->getMaxAltitude() - $this->getPassengerCount();
  }
}
 
class AirForceOne extends Airplane {
  // ...
  public function getCruisingAltitude() {
    return $this->getMaxAltitude();
  }
}
 
class Cessna extends Airplane {
  // ...
  public function getCruisingAltitude() {
    return $this->getMaxAltitude() - $this->getFuelExpenditure();
  }
}

 

Avoid 避免类型检查 (part 1)
PHP是弱类型的,这意味着你的函数可以接收任何类型的参数。 有时候你为这自由所痛苦并且在你的函数渐渐尝试类型检查。有很多方法去避免这么做。第一种是考虑API的一致性。
Bad:
function travelToTexas($vehicle) {
  if ($vehicle instanceof Bicycle) {
    $vehicle->peddle($this->currentLocation, new Location(‘texas‘));
  } else if ($vehicle instanceof Car) {
    $vehicle->drive($this->currentLocation, new Location(‘texas‘));
  }
}
Good:
function travelToTexas($vehicle) {
  $vehicle->move($this->currentLocation, new Location(‘texas‘));
}

 

避免类型检查 (part 2)
如果你正使用基本原始值比如字符串、整形和数组,你不能用多态,你仍然感觉需要类型检测,你应当考虑类型声明或者严格模式。 这给你了基于标准PHP语法的静态类型。 手动检查类型的问题是做好了需要好多的废话,好像为了安全就可以不顾损失可读性。保持你的PHP 整洁,写好测试,做好代码回顾。做不到就用PHP严格类型声明和严格模式来确保安全。
Bad:
function combine($val1, $val2) {
  if (is_numeric($val1) && is_numeric(val2)) {
    return val1 + val2;
  }
 
  throw new \Exception(‘Must be of type Number‘);
}
Good:
function combine(int $val1, int $val2) {
  return $val1 + $val2;
}

 

移除僵尸代码
僵尸代码和重复代码一样坏。没有理由保留在你的代码库中。如果从来被调用过,见鬼去!在你的版本库里是如果你仍然需要他的话,因此这么做很安全。
Bad:
function oldRequestModule($url) {
  // ...
}
 
function newRequestModule($url) {
  // ...
}
 
$req = new newRequestModule();
inventoryTracker(‘apples‘, $req, ‘www.inventory-awesome.io‘);
Good:
function newRequestModule($url) {
  // ...
}
 
$req = new newRequestModule();
inventoryTracker(‘apples‘, $req, ‘www.inventory-awesome.io‘);

链接: https://www.fly63.com/article/detial/1518

PHP 是 Web 开发最好的语言!

PHP 一直受到全球 Web开发人员的青睐,它为人们提供了创建高度交互性和直观的网站和Web应用程序的良好方式,包括语言的广度、深度,且执行简单。以下五个原因,我们来说明PHP是世界 Web开发的最佳语言

PHP中常用加解密方式

PHP中使用OpenSSL生成RSA公钥私钥及进行加密解密示例(非对称加密),php服务端与客户端交互、提供开放api时,通常需要对敏感的部分api数据传输进行数据加密,这时候rsa非对称加密就能派上用处了,下面通过一个例子来说明如何用php来实现数据的加密解密

在PHP7中不要做的 10 件事

PHP7中不要做的 10 件事: 不要使用 mysql_ 函数、不要编写垃圾代码、不要在文件末尾使用 PHP 闭合标签、 不要做不必要的引用传递、不要在循环中执行查询、不要在 SQL 查询中使用 *

PHP如何打造一个高可用高性能的网站呢?

PHP如何打造一个高可用高性能的网站呢?我们来分析分析高性能高可用的系统。简而言之,采用分布式系统,分布式应用和服务,分布式数据和存储,分布式静态资源,分布式计算,分布式配置和分布式锁。负载均衡,故障转移,实现高并发。

php获取客户端ip地址或者服务器ip地址

在PHP获取客户端IP时,常使用REMOTE_ADDR,但如果客户端是使用代理服务器来访问,那取到的是代理服务器的 IP 地址,而不是真正的客户端 IP 地址。要想透过代理服务器取得客户端的真实 IP 地址,就要使用HTTP_X_FORWARDED_FOR

php 守护进程

首先需要解释的是什么是守护进程。守护进程就是在后台一直运行的进程。比如我们启动的httpd,mysqld等进程都是常驻内存内运行的程序。

解决PHP剪切缩略图生成png,gif透明图时,黑色背景问题

后台上传png图片透明底变成黑色的问题,php缩放gif和png图透明背景变成黑色的解决方法,本文讲的是php缩放gif和png图透明背景变成黑色的解决方法, 工作中需要缩放一些gif图然后在去Imagecopymerge

PHP超级全局变量、魔术变量和魔术函数

PHP超级全局变量(9个),$GLOBALS  储存全局作用域中的变量,$_SERVER  获取服务器相关信息;PHP魔术变量(8个)__LINE__文件中的当前行号。__FILE__文件的完整路径和文件名。如果用在被包含文件中,则返回被包含的文件名。PHP魔术函数(13个)

PHP的高效编程

如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍。echo 比 print 快,并且使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接,比如echo $str1,$str2。

PHP多进程引发的msyql连接数问题

业务中有一块采用了PHP的pcntl_fork多进程,希望能提高效率,但是在执行的时候数据库报错,MySQL能有的连接数量。当主要MySQL线程在一个很短时间内得到非常多的连接请求,这就起作用,然后主线程花些时间(尽管很短)检查连接并且启动一个新线程。

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!