PHP簡潔之道(轉)

PHP簡潔之道

前言

前幾天在GitHub看到一篇寫PHP簡潔之道的譯文,覺得還不錯,所以轉在了自己的博客中,共同學習進步。

介紹

本文參考自 Robert C. Martin的Clean Code 書中的軟件工程師的原則
,適用於PHP。 這不是風格指南。 這是一個關於開發可讀、可複用並且可重構的PHP軟件指南。

並不是這裏所有的原則都得遵循,甚至很少的能被普遍接受。 這些雖然只是指導,但是都是Clean Code作者多年總結出來的。

本文受到 clean-code-javascript 的啓發

雖然很多開發者還在使用PHP5,但是本文中的大部分示例的運行環境需要PHP 7.1+。

變量

  • 使用見字知意的變量名

壞:

    $ymdstr = $moment->format('y-m-d');

好:

   $currentDate = $moment->format('y-m-d');
  • 同一個實體要用相同的變量名

壞:

getUserInfo();
getUserData();
getUserRecord();
getUserProfile();

好:

   getUser();
  • 使用便於搜索的名稱 (part 1)

寫代碼是用來讀的。所以寫出可讀性高、便於搜索的代碼至關重要。
命名變量時如果沒有有意義、不好理解,那就是在傷害讀者。
請讓你的代碼便於搜索。

壞:

// What the heck is 448 for?
$result = $serializer->serialize($data, 448);

好:

$json = $serializer->serialize($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  • 使用便於搜索的名稱 (part 2)

壞:

    // What the heck is 4 for?
    if ($usRer->access & 4) {
        // ...
    }

好:

    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 ...
    }
  • 使用自解釋型變量

壞:


    $address = 'One Infinite Loop, Cupertino 95014';
    $cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/';
    preg_match($cityZipCodeRegex, $address, $matches);
    
    saveCityZipCode($matches[1], $matches[2]);

不錯:

好一些,但強依賴於正則表達式的熟悉程度

    $address = 'One Infinite Loop, Cupertino 95014';
    $cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/';

    preg_match($cityZipCodeRegex, $address, $matches);
    
    [, $city, $zipCode] = $matches;
    saveCityZipCode($city, $zipCode);

好:

使用帶名字的子規則,不用懂正則也能看的懂

    $address = 'One Infinite Loop, Cupertino 95014';
    $cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(?<city>.+?)\s*(?<zipCode>\d{5})?$/';
    preg_match($cityZipCodeRegex, $address, $matches);
    
    saveCityZipCode($matches['city'], $matches['zipCode']);
  • 避免深層嵌套,儘早返回 (part 1)

太多的if else語句通常會導致你的代碼難以閱讀,直白優於隱晦

糟糕:


    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;
        }
    }

好:

   function isShopOpen(string $day): bool
   {
       if (empty($day)) {
           return false;
       }
   
       $openingDays = [
           'friday', 'saturday', 'sunday'
       ];
   
       return in_array(strtolower($day), $openingDays, true);
   }
   
  • 避免深層嵌套,儘早返回 (part 2)

糟糕:

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';
        }
    }

好:

    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);
    }
  • 少用無意義的變量名

別讓讀你的代碼的人猜你寫的變量是什麼意思。
寫清楚好過模糊不清。

壞:

    $l = ['Austin', 'New York', 'San Francisco'];
    
    for ($i = 0; $i < count($l); $i++) {
        $li = $l[$i];
        doStuff();
        doSomeOtherStuff();
        // ...
        // ...
        // ...
      // 等等, `$li` 又代表什麼?
        dispatch($li);
    }

好:


    $locations = ['Austin', 'New York', 'San Francisco'];
    
    foreach ($locations as $location) {
        doStuff();
        doSomeOtherStuff();
        // ...
        // ...
        // ...
        dispatch($location);
    }
  • 不要添加不必要上下文

如果從你的類名、對象名已經可以得知一些信息,就別再在變量名裏重複。

壞:

    class Car
    {
        public $carMake;
        public $carModel;
        public $carColor;
    
        //...
    }

好:

    class Car
    {
        public $make;
        public $model;
        public $color;
    
        //...
    }
  • 合理使用參數默認值,沒必要在方法裏再做默認值檢測

不好:


    //This is not good because $breweryName can be NULL.
    
    function createMicrobrewery($breweryName = 'Hipster Brew Co.'): void
    {
        // ...
    }
    
**還行:**

    //This opinion is more understandable than the previous version, but it better controls the value of the variable.
    
    function createMicrobrewery($name = null): void
    {
        $breweryName = $name ?: 'Hipster Brew Co.';
        // ...
    }

好:


 
   If you support only PHP 7+, then you can use type hinting and be sure that the $breweryName will not be NULL.
    
    function createMicrobrewery(string $breweryName = 'Hipster Brew Co.'): void
    {
        // ...
    }

函數

  • 函數參數(最好少於2個)

限制函數參數個數極其重要,這樣測試你的函數容易點。有超過3個可選參數參數導致一個爆炸式組合增長,你會有成噸獨立參數情形要測試。

無參數是理想情況。1個或2個都可以,最好避免3個。再多就需要加固了。通常如果你的函數有超過兩個參數,說明他要處理的事太多了。 如果必須要傳入很多數據,建議封裝一個高級別對象作爲參數。

壞:

    function createMenu(string $title, string $body, string $buttonText, bool $cancellable): void
    {
        // ...
    }
    

好:

    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
    {
        // ...
    }
  • 函數應該只做一件事

這是迄今爲止軟件工程裏最重要的一個規則。當一個函數做超過一件事的時候,他們就難於實現、測試和理解。當你把一個函數拆分到只剩一個功能時,他們就容易被重構,然後你的代碼讀起來就更清晰。如果你光遵循這條規則,你就領先於大多數開發者了。

壞:

    function emailClients(array $clients): void
    {
        foreach ($clients as $client) {
            $clientRecord = $db->find($client);
            if ($clientRecord->isActive()) {
                email($client);
            }
        }
    }

好:


    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();
    }
  • 函數名應該是有意義的動詞(或表明具體做了什麼事)

壞:

    class Email
    {
        //...
    
        public function handle(): void
        {
            mail($this->to, $this->subject, $this->body);
        }
    }
    
    $message = new Email(...);
    // 啥?handle處理一個消息幹嘛了?是往一個文件裏寫碼?
    $message->handle();
  

好:

    class Email 
    {
        //...
    
        public function send(): void
        {
            mail($this->to, $this->subject, $this->body);
        }
    }
    
    $message = new Email(...);
    // 簡單明瞭
    $message->send();
  • 函數裏應當只有一層抽象abstraction

當你抽象層次過多時時,函數處理的事情太多了。需要拆分功能來提高可重用性和易用性,以便簡化測試。
(譯者注:這裏從示例代碼看應該是指嵌套過多)

**壞:**

    function parseBetterJSAlternative(string $code): void
    {
        $regexes = [
            // ...
        ];
    
        $statements = explode(' ', $code);
        $tokens = [];
        foreach ($regexes as $regex) {
            foreach ($statements as $statement) {
                // ...
            }
        }
    
        $ast = [];
        foreach ($tokens as $token) {
            // lex...
        }
    
        foreach ($ast as $node) {
            // parse...
        }
    }

壞:

我們把一些方法從循環中提取出來,但是parseBetterJSAlternative()方法還是很複雜,而且不利於測試。


    function tokenize(string $code): array
    {
        $regexes = [
            // ...
        ];
    
        $statements = explode(' ', $code);
        $tokens = [];
        foreach ($regexes as $regex) {
            foreach ($statements as $statement) {
                $tokens[] = /* ... */;
            }
        }
    
        return $tokens;
    }
    
    function lexer(array $tokens): array
    {
        $ast = [];
        foreach ($tokens as $token) {
            $ast[] = /* ... */;
        }
    
        return $ast;
    }
    
    function parseBetterJSAlternative(string $code): void
    {
        $tokens = tokenize($code);
        $ast = lexer($tokens);
        foreach ($ast as $node) {
            // 解析邏輯...
        }
    }

好:

最好的解決方案是把 parseBetterJSAlternative()方法的依賴移除。

    class Tokenizer
    {
        public function tokenize(string $code): array
        {
            $regexes = [
                // ...
            ];
    
            $statements = explode(' ', $code);
            $tokens = [];
            foreach ($regexes as $regex) {
                foreach ($statements as $statement) {
                    $tokens[] = /* ... */;
                }
            }
    
            return $tokens;
        }
    }
    
    class Lexer
    {
        public function lexify(array $tokens): array
        {
            $ast = [];
            foreach ($tokens as $token) {
                $ast[] = /* ... */;
            }
    
            return $ast;
        }
    }
    
    class BetterJSAlternative
    {
        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) {
                // 解析邏輯...
            }
        }
    }
  

這樣我們可以對依賴做mock,並測試BetterJSAlternative::parse()運行是否符合預期。

  • 不要用flag作爲函數的參數

flag就是在告訴大家,這個方法裏處理很多事。前面剛說過,一個函數應當只做一件事。 把不同flag的代碼拆分到多個函數裏。

壞:

    function createFile(string $name, bool $temp = false): void
    {
        if ($temp) {
            touch('./temp/'.$name);
        } else {
            touch($name);
        }
    }

好:

    function createFile(string $name): void
    {
        touch($name);
    }
    
    function createTempFile(string $name): void
    {
        touch('./temp/'.$name);
    }
  • 避免副作用

一個函數做了比獲取一個值然後返回另外一個值或值們會產生副作用如果。副作用可能是寫入一個文件,修改某些全局變量或者偶然的把你全部的錢給了陌生人。

現在,你的確需要在一個程序或者場合裏要有副作用,像之前的例子,你也許需要寫一個文件。你想要做的是把你做這些的地方集中起來。不要用幾個函數和類來寫入一個特定的文件。用一個服務來做它,一個只有一個。

重點是避免常見陷阱比如對象間共享無結構的數據,使用可以寫入任何的可變數據類型,不集中處理副作用發生的地方。如果你做了這些你就會比大多數程序員快樂。

壞:

    // 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(): void
    {
        global $name;
    
        $name = explode(' ', $name);
    }
    
    splitIntoFirstAndLastName();
    
    var_dump($name); // ['Ryan', 'McDermott'];

好:


    function splitIntoFirstAndLastName(string $name): array
    {
        return explode(' ', $name);
    }
    
    $name = 'Ryan McDermott';
    $newName = splitIntoFirstAndLastName($name);
    
    var_dump($name); // 'Ryan McDermott';
    var_dump($newName); // ['Ryan', 'McDermott'];
  • 不要寫全局函數

在大多數語言中污染全局變量是一個壞的實踐,因爲你可能和其他類庫衝突
並且調用你api的人直到他們捕獲異常才知道踩坑了。讓我們思考一種場景:
如果你想配置一個數組,你可能會寫一個全局函數config(),但是他可能
和試着做同樣事的其他類庫衝突。

壞:

    function config(): array
    {
        return  [
            'foo' => 'bar',
        ]
    }

好:

    class Configuration
    {
        private $configuration = [];
    
        public function __construct(array $configuration)
        {
            $this->configuration = $configuration;
        }
    
        public function get(string $key): ?string
        {
            return isset($this->configuration[$key]) ? $this->configuration[$key] : null;
        }
    }
    Load configuration and create instance of Configuration class
    
    $configuration = new Configuration([
        'foo' => 'bar',
    ]);
    And now you must use instance of Configuration in your application.
  • 不要使用單例模式

單例是一種 反模式. 以下是解釋:Paraphrased from Brian Button:

總是被用成全局實例。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.
違反了[單一響應原則]()They violate the single responsibility principle: by virtue of the fact that they control their own creation and lifecycle.
導致代碼強耦合They inherently cause code to be tightly coupled. This makes faking them out under test rather difficult in many cases.
在整個程序的生命週期中始終攜帶狀態。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.
這裏有一篇非常好的討論單例模式的根本問題((http://misko.hevery.com/2008/08/25/root-cause-of-singletons/)的文章,是[Misko Hevery 寫的。

壞:

    class DBConnection
    {
        private static $instance;
    
        private function __construct(string $dsn)
        {
            // ...
        }
    
        public static function getInstance(): DBConnection
        {
            if (self::$instance === null) {
                self::$instance = new self();
            }
    
            return self::$instance;
        }
    
        // ...
    }
    
    $singleton = DBConnection::getInstance();

好:

    class DBConnection
    {
        public function __construct(string $dsn)
        {
            // ...
        }
    
         // ...
    }
    Create instance of DBConnection class and configure it with DSN.
    
    $connection = new DBConnection($dsn);
    And now you must use instance of DBConnection in your application.
  • 封裝條件語句

壞:

    if ($article->state === 'published') {
        // ...
    }

好:

    if ($article->isPublished()) {
        // ...
    }
  • 避免用反義條件判斷

壞:

    function isDOMNodeNotPresent(\DOMNode $node): bool
    {
        // ...
    }
    
    if (!isDOMNodeNotPresent($node))
    {
        // ...
    }

好:

    function isDOMNodePresent(\DOMNode $node): bool
    {
        // ...
    }
    
    if (isDOMNodePresent($node)) {
        // ...
    }
  • 避免條件判斷

這看起來像一個不可能任務。當人們第一次聽到這句話是都會這麼說。
"沒有if語句我還能做啥?" 答案是你可以使用多態來實現多種場景
的相同任務。第二個問題很常見, “這麼做可以,但爲什麼我要這麼做?”
答案是前面我們學過的一個Clean Code原則:一個函數應當只做一件事。
當你有很多含有if語句的類和函數時,你的函數做了不止一件事。
記住,只做一件事。

壞:

    class Airplane
    {
        // ...
    
        public function getCruisingAltitude(): int
        {
            switch ($this->type) {
                case '777':
                    return $this->getMaxAltitude() - $this->getPassengerCount();
                case 'Air Force One':
                    return $this->getMaxAltitude();
                case 'Cessna':
                    return $this->getMaxAltitude() - $this->getFuelExpenditure();
            }
        }
    }

好:

interface Airplane
  {
      // ...
  
      public function getCruisingAltitude(): int;
  }
  
  class Boeing777 implements Airplane
  {
      // ...
  
      public function getCruisingAltitude(): int
      {
          return $this->getMaxAltitude() - $this->getPassengerCount();
      }
  }
  
  class AirForceOne implements Airplane
  {
      // ...
  
      public function getCruisingAltitude(): int
      {
          return $this->getMaxAltitude();
      }
  }
  
  class Cessna implements Airplane
  {
      // ...
  
      public function getCruisingAltitude(): int
      {
          return $this->getMaxAltitude() - $this->getFuelExpenditure();
      }
  }
  • 避免類型檢查 (part 1)

PHP是弱類型的,這意味着你的函數可以接收任何類型的參數。
有時候你爲這自由所痛苦並且在你的函數漸漸嘗試類型檢查。
有很多方法去避免這麼做。第一種是統一API。

壞:

    function travelToTexas($vehicle): void
    {
        if ($vehicle instanceof Bicycle) {
            $vehicle->pedalTo(new Location('texas'));
        } elseif ($vehicle instanceof Car) {
            $vehicle->driveTo(new Location('texas'));
        }
    }

好:

    function travelToTexas(Traveler $vehicle): void
    {
        $vehicle->travelTo(new Location('texas'));
    }
  • 避免類型檢查 (part 2)

如果你正使用基本原始值比如字符串、整形和數組,要求版本是PHP 7+,不用多態,需要類型檢測,
那你應當考慮類型聲明或者嚴格模式。
提供了基於標準PHP語法的靜態類型。 手動檢查類型的問題是做好了需要好多的廢話,好像爲了安全就可以不顧損失可讀性。
保持你的PHP 整潔,寫好測試,做好代碼回顧。做不到就用PHP嚴格類型聲明和嚴格模式來確保安全。

壞:

    function combine($val1, $val2): int
    {
        if (!is_numeric($val1) || !is_numeric($val2)) {
            throw new \Exception('Must be of type Number');
        }
    
        return $val1 + $val2;
    }

好:

    function combine(int $val1, int $val2): int
    {
        return $val1 + $val2;
    }
  • 移除殭屍代碼

殭屍代碼和重複代碼一樣壞。沒有理由保留在你的代碼庫中。如果從來沒被調用過,就刪掉!
因爲還在代碼版本庫裏,因此很安全。

壞:

    function oldRequestModule(string $url): void
    {
        // ...
    }
    
    function newRequestModule(string $url): void
    {
        // ...
    }
    
    $request = newRequestModule($requestUrl);
    inventoryTracker('apples', $request, 'www.inventory-awesome.io');

好:

    function requestModule(string $url): void
    {
        // ...
    }
    
    $request = requestModule($requestUrl);
    inventoryTracker('apples', $request, 'www.inventory-awesome.io');
  • 對象和數據結構

使用 getters 和 setters

在PHP中你可以對方法使用public, protected, private 來控制對象屬性的變更。

當你想對對象屬性做獲取之外的操作時,你不需要在代碼中去尋找並修改每一個該屬性訪問方法
當有set對應的屬性方法時,易於增加參數的驗證
封裝內部的表示
使用set和get時,易於增加日誌和錯誤控制
繼承當前類時,可以複寫默認的方法功能
當對象屬性是從遠端服務器獲取時,get,set易於使用延遲加載
此外,這樣的方式也符合OOP開發中的開閉原則

糟糕:

    class BankAccount
    {
        public $balance = 1000;
    }
    
    $bankAccount = new BankAccount();
    
    // Buy shoes...
    $bankAccount->balance -= 100;

好:

    class BankAccount
    {
        private $balance;
    
        public function __construct(int $balance = 1000)
        {
          $this->balance = $balance;
        }
    
        public function withdrawBalance(int $amount): void
        {
            if ($amount > $this->balance) {
                throw new \Exception('Amount greater than available balance.');
            }
    
            $this->balance -= $amount;
        }
    
        public function depositBalance(int $amount): void
        {
            $this->balance += $amount;
        }
    
        public function getBalance(): int
        {
            return $this->balance;
        }
    }
    
    $bankAccount = new BankAccount();
    
    // Buy shoes...
    $bankAccount->withdrawBalance($shoesPrice);
    
    // Get balance
    $balance = $bankAccount->getBalance();
  • 對象屬性多使用private/protected限定

對public方法和屬性進行修改非常危險,因爲外部代碼容易依賴他,而你沒辦法控制。對之修改影響所有這個類的使用者。 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的修改跟對public修改差不多危險,因爲他們對子類可用,他倆的唯一區別就是可調用的位置不一樣,對之修改影響所有集成這個類的地方。 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的修改保證了這部分代碼只會影響當前類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).
所以,當你需要控制類裏的代碼可以被訪問時才用public/protected,其他時候都用private。

可以讀一讀這篇 博客文章 ,Fabien Potencier寫的.

糟糕:

    class Employee
    {
        public $name;
    
        public function __construct(string $name)
        {
            $this->name = $name;
        }
    }
    
    $employee = new Employee('John Doe');
    echo 'Employee name: '.$employee->name; // Employee name: John Doe

好:

    class Employee
    {
        private $name;
    
        public function __construct(string $name)
        {
            $this->name = $name;
        }
    
        public function getName(): string
        {
            return $this->name;
        }
    }
    
    $employee = new Employee('John Doe');
    echo 'Employee name: '.$employee->getName(); // Employee name: John Doe

  • 組合優於繼承

正如 the Gang of Four 所著的設計模式之前所說,
我們應該儘量優先選擇組合而不是繼承的方式。使用繼承和組合都有很多好處。
這個準則的主要意義在於當你本能的使用繼承時,試着思考一下組合是否能更好對你的需求建模。
在一些情況下,是這樣的。

接下來你或許會想,“那我應該在什麼時候使用繼承?”
答案依賴於你的問題,當然下面有一些何時繼承比組合更好的說明:

你的繼承表達了“是一個”而不是“有一個”的關係(人類-》動物,用戶-》用戶詳情)
你可以複用基類的代碼(人類可以像動物一樣移動)
你想通過修改基類對所有派生類做全局的修改(當動物移動時,修改她們的能量消耗)

糟糕的:

    class Employee 
    {
        private $name;
        private $email;
    
        public function __construct(string $name, string $email)
        {
            $this->name = $name;
            $this->email = $email;
        }
    
        // ...
    }
    
    
    // 不好,因爲Employees "有" taxdata
    // 而EmployeeTaxData不是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;
        }
    
        // ...
    }

好:

    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(string $ssn, string $salary)
        {
            $this->taxData = new EmployeeTaxData($ssn, $salary);
        }
    
        // ...
    }
  • 避免連貫接口

連貫接口Fluent interface是一種
旨在提高面向對象編程時代碼可讀性的API設計模式,他基於方法鏈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
or the Doctrine Query Builder),
more often it comes at some costs:

Breaks Encapsulation
Breaks Decorators
Is harder to mock in a test suite
Makes diffs of commits harder to read
For more informations you can read the full blog post
on this topic written by Marco Pivetta.

壞:

    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();

好:

    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 setColor(string $color): void
        {
            $this->color = $color;
        }
    
        public function dump(): void
        {
            var_dump($this->make, $this->model, $this->color);
        }
    }
    
    $car = new Car();
    $car->setColor('pink');
    $car->setMake('Ford');
    $car->setModel('F-150');
    $car->dump();
  • SOLID

SOLID 是Michael Feathers推薦的便於記憶的首字母簡寫,它代表了Robert Martin命名的最重要的五個面對對象編碼設計原則

S: 職責單一原則 (SRP)
O: 開閉原則 (OCP)
L: 里氏替換原則 (LSP)
I: 接口隔離原則 (ISP)
D: 依賴反轉原則 (DIP)
職責單一原則 Single Responsibility Principle (SRP)

正如在Clean Code所述,"修改一個類應該只爲一個理由"。
人們總是易於用一堆方法塞滿一個類,如同我們只能在飛機上
只能攜帶一個行李箱(把所有的東西都塞到箱子裏)。這樣做
的問題是:從概念上這樣的類不是高內聚的,並且留下了很多
理由去修改它。將你需要修改類的次數降低到最小很重要。
這是因爲,當有很多方法在類中時,修改其中一處,你很難知
曉在代碼庫中哪些依賴的模塊會被影響到。

壞:

    class UserSettings
    {
        private $user;
    
        public function __construct(User $user)
        {
            $this->user = $user;
        }
    
        public function changeSettings(array $settings): void
        {
            if ($this->verifyCredentials()) {
                // ...
            }
        }
    
        private function verifyCredentials(): bool
        {
            // ...
        }
    }

好:

    class UserAuth 
    {
        private $user;
    
        public function __construct(User $user)
        {
            $this->user = $user;
        }
        
        public function verifyCredentials(): bool
        {
            // ...
        }
    }
    
    class UserSettings 
    {
        private $user;
        private $auth;
    
        public function __construct(User $user) 
        {
            $this->user = $user;
            $this->auth = new UserAuth($user);
        }
    
        public function changeSettings(array $settings): void
        {
            if ($this->auth->verifyCredentials()) {
                // ...
            }
        }
    }
  • 開閉原則 Open/Closed Principle (OCP)

正如Bertrand Meyer所述,"軟件的工件(classes, modules, functions,等)
應該對擴展開放,對修改關閉。" 然而這句話意味着什麼呢?這個原則大體上表示你
應該允許在不改變已有代碼的情況下增加新的功能

壞:

    abstract class Adapter
    {
        protected $name;
    
        public function getName(): string
        {
            return $this->name;
        }
    }
    
    class AjaxAdapter extends Adapter
    {
        public function __construct()
        {
            parent::__construct();
    
            $this->name = 'ajaxAdapter';
        }
    }
    
    class NodeAdapter extends Adapter
    {
        public function __construct()
        {
            parent::__construct();
    
            $this->name = 'nodeAdapter';
        }
    }
    
    class HttpRequester
    {
        private $adapter;
    
        public function __construct(Adapter $adapter)
        {
            $this->adapter = $adapter;
        }
    
        public function fetch(string $url): Promise
        {
            $adapterName = $this->adapter->getName();
    
            if ($adapterName === 'ajaxAdapter') {
                return $this->makeAjaxCall($url);
            } elseif ($adapterName === 'httpNodeAdapter') {
                return $this->makeHttpCall($url);
            }
        }
    
        private function makeAjaxCall(string $url): Promise
        {
            // request and return promise
        }
    
        private function makeHttpCall(string $url): Promise
        {
            // request and return promise
        }
    }

好:

    interface Adapter
    {
        public function request(string $url): Promise;
    }
    
    class AjaxAdapter implements Adapter
    {
        public function request(string $url): Promise
        {
            // request and return promise
        }
    }
    
    class NodeAdapter implements Adapter
    {
        public function request(string $url): Promise
        {
            // request and return promise
        }
    }
    
    class HttpRequester
    {
        private $adapter;
    
        public function __construct(Adapter $adapter)
        {
            $this->adapter = $adapter;
        }
    
        public function fetch(string $url): Promise
        {
            return $this->adapter->request($url);
        }
    }
  • 里氏替換原則 Liskov Substitution Principle (LSP)

這是一個簡單的原則,卻用了一個不好理解的術語。它的正式定義是
"如果S是T的子類型,那麼在不改變程序原有既定屬性(檢查、執行
任務等)的前提下,任何T類型的對象都可以使用S類型的對象替代
(例如,使用S的對象可以替代T的對象)" 這個定義更難理解:-)。

對這個概念最好的解釋是:如果你有一個父類和一個子類,在不改變
原有結果正確性的前提下父類和子類可以互換。這個聽起來依舊讓人
有些迷惑,所以讓我們來看一個經典的正方形-長方形的例子。從數學
上講,正方形是一種長方形,但是當你的模型通過繼承使用了"is-a"
的關係時,就不對了。

壞:


    class Rectangle
    {
        protected $width = 0;
        protected $height = 0;
    
        public function render(int $area): void
        {
            // ...
        }
    
        public function setWidth(int $width): void
        {
            $this->width = $width;
        }
    
        public function setHeight(int $height): void
        {
            $this->height = $height;
        }
    
        public function getArea(): int
        {
            return $this->width * $this->height;
        }
    }
    
    class Square extends Rectangle
    {
        public function setWidth(int $width): void
        {
            $this->width = $this->height = $width;
        }
    
        public function setHeight(int $height): void
        {
            $this->width = $this->height = $height;
        }
    }
    
    function renderLargeRectangles(Rectangle $rectangles): void
    {
        foreach ($rectangles as $rectangle) {
            $rectangle->setWidth(4);
            $rectangle->setHeight(5);
            $area = $rectangle->getArea(); // BAD: Will return 25 for Square. Should be 20.
            $rectangle->render($area);
        }
    }
    
    $rectangles = [new Rectangle(), new Rectangle(), new Square()];
    renderLargeRectangles($rectangles);

好:

    abstract class Shape
    {
        protected $width = 0;
        protected $height = 0;
    
        abstract public function getArea(): int;
    
        public function render(int $area): void
        {
            // ...
        }
    }
    
    class Rectangle extends Shape
    {
        public function setWidth(int $width): void
        {
            $this->width = $width;
        }
    
        public function setHeight(int $height): void
        {
            $this->height = $height;
        }
    
        public function getArea(): int
        {
            return $this->width * $this->height;
        }
    }
    
    class Square extends Shape
    {
        private $length = 0;
    
        public function setLength(int $length): void
        {
            $this->length = $length;
        }
    
        public function getArea(): int
        {
            return pow($this->length, 2);
        }
    }
    
    function renderLargeRectangles(Shape $rectangles): void
    {
        foreach ($rectangles as $rectangle) {
            if ($rectangle instanceof Square) {
                $rectangle->setLength(5);
            } elseif ($rectangle instanceof Rectangle) {
                $rectangle->setWidth(4);
                $rectangle->setHeight(5);
            }
    
            $area = $rectangle->getArea(); 
            $rectangle->render($area);
        }
    }
    
    $shapes = [new Rectangle(), new Rectangle(), new Square()];
    renderLargeRectangles($shapes);
  • 接口隔離原則 Interface Segregation Principle (ISP)

接口隔離原則表示:"調用方不應該被強制依賴於他不需要的接口"

有一個清晰的例子來說明示範這條原則。當一個類需要一個大量的設置項,
爲了方便不會要求調用方去設置大量的選項,因爲在通常他們不需要所有的
設置項。使設置項可選有助於我們避免產生"胖接口"

壞:

    interface Employee
    {
        public function work(): void;
    
        public function eat(): void;
    }
    
    class Human implements Employee
    {
        public function work(): void
        {
            // ....working
        }
    
        public function eat(): void
        {
            // ...... eating in lunch break
        }
    }
    
    class Robot implements Employee
    {
        public function work(): void
        {
            //.... working much more
        }
    
        public function eat(): void
        {
            //.... robot can't eat, but it must implement this method
        }
    }

好:

不是每一個工人都是僱員,但是每一個僱員都是一個工人

    interface Workable
    {
        public function work(): void;
    }
    
    interface Feedable
    {
        public function eat(): void;
    }
    
    interface Employee extends Feedable, Workable
    {
    }
    
    class Human implements Employee
    {
        public function work(): void
        {
            // ....working
        }
    
        public function eat(): void
        {
            //.... eating in lunch break
        }
    }
    
    // robot can only work
    class Robot implements Workable
    {
        public function work(): void
        {
            // ....working
        }
    }
  • 依賴反轉原則 Dependency Inversion Principle (DIP)

這條原則說明兩個基本的要點:

高階的模塊不應該依賴低階的模塊,它們都應該依賴於抽象
抽象不應該依賴於實現,實現應該依賴於抽象
這條起初看起來有點晦澀難懂,但是如果你使用過php框架(例如 Symfony),你應該見過
依賴注入(DI)對這個概念的實現。雖然它們不是完全相通的概念,依賴倒置原則使高階模塊
與低階模塊的實現細節和創建分離。可以使用依賴注入(DI)這種方式來實現它。更多的好處
是它使模塊之間解耦。耦合會導致你難於重構,它是一種非常糟糕的的開發模式

壞:

    class Employee
    {
        public function work(): void
        {
            // ....working
        }
    }
    
    class Robot extends Employee
    {
        public function work(): void
        {
            //.... working much more
        }
    }
    
    class Manager
    {
        private $employee;
    
        public function __construct(Employee $employee)
        {
            $this->employee = $employee;
        }
    
        public function manage(): void
        {
            $this->employee->work();
        }
    }

好:

    interface Employee
    {
        public function work(): void;
    }
    
    class Human implements Employee
    {
        public function work(): void
        {
            // ....working
        }
    }
    
    class Robot implements Employee
    {
        public function work(): void
        {
            //.... working much more
        }
    }
    
    class Manager
    {
        private $employee;
    
        public function __construct(Employee $employee)
        {
            $this->employee = $employee;
        }
    
        public function manage(): void
        {
            $this->employee->work();
        }
    }
  • 別寫重複代碼 (DRY)

試着去遵循DRY 原則.

盡你最大的努力去避免複製代碼,它是一種非常糟糕的行爲,複製代碼
通常意味着當你需要變更一些邏輯時,你需要修改不止一處。

試想一下,如果你在經營一家餐廳並且你在記錄你倉庫的進銷記錄:所有
的土豆,洋蔥,大蒜,辣椒等。如果你有多個列表來管理進銷記錄,當你
用其中一些土豆做菜時你需要更新所有的列表。如果你只有一個列表的話
只有一個地方需要更新。

通常情況下你複製代碼是應該有兩個或者多個略微不同的邏輯,它們大多數
都是一樣的,但是由於它們的區別致使你必須有兩個或者多個隔離的但大部
分相同的方法,移除重複的代碼意味着用一個function/module/class創
建一個能處理差異的抽象。

正確的抽象是非常關鍵的,這正是爲什麼你必須學習遵守在Classes章節展開
的SOLID原則,不合理的抽象比複製代碼更糟糕,所有務必謹慎!說到這麼多,
如果你能設計一個合理的抽象,實現它!不要重複,否則你會發現任何時候當你
想修改一個邏輯時你必須修改多個地方。

壞:


    function showDeveloperList(array $developers): void
    {
        foreach ($developers as $developer) {
            $expectedSalary = $developer->calculateExpectedSalary();
            $experience = $developer->getExperience();
            $githubLink = $developer->getGithubLink();
            $data = [
                $expectedSalary,
                $experience,
                $githubLink
            ];
    
            render($data);
        }
    }
    
    function showManagerList(array $managers): void
    {
        foreach ($managers as $manager) {
            $expectedSalary = $manager->calculateExpectedSalary();
            $experience = $manager->getExperience();
            $githubLink = $manager->getGithubLink();
            $data = [
                $expectedSalary,
                $experience,
                $githubLink
            ];
    
            render($data);
        }
    }

好:

    function showList(array $employees): void
    {
        foreach ($employees as $employee) {
            $expectedSalary = $employee->calculateExpectedSalary();
            $experience = $employee->getExperience();
            $githubLink = $employee->getGithubLink();
            $data = [
                $expectedSalary,
                $experience,
                $githubLink
            ];
    
            render($data);
        }
    }

極好:

It is better to use a compact version of the code.

    
    function showList(array $employees): void
    {
        foreach ($employees as $employee) {
            render([
                $employee->calculateExpectedSalary(),
                $employee->getExperience(),
                $employee->getGithubLink()
            ]);
        }
    }

閱讀原文

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章