2016-12-04 29 views
0

我有下面的代碼片段:明確的變量聲明使用提取物時()

protected function sendEmail($email) 
{ 
    extract($email); 

    $this->transmail->locale($locale) 
        ->timezone($timezone) 
        ->template($template) 
        ->subject($subject) 
        ->send($header, $params); 
} 

此代碼的工作完美(full source code here)。但是,我想確保在旅途中遵循一些良好的做法。我currenlty得到[一些CodeClimate警告(PHPMD)(https://codeclimate.com/github/timegridio/timegrid/app/Listeners/SendBookingNotification.php):

  • 避免未使用的本地變量,如 '$語言環境'。
  • 避免未使用的本地變量,如'$ timezone'。
  • 避免未使用的局部變量,如'$ template'。
  • 避免未使用的局部變量,如'$ subject'。
  • 避免未使用的本地變量,如'$ header'。
  • 避免未使用的局部變量,如'$ params'。

這將是優雅的方式去呢?

我是否應該明確聲明變量list()或類似的東西?

在此先感謝

+0

發現如果您正在使用PHPStorm工作,只是把它在某種程度上所以這個文件(或方法)從PHPMD排除......沒有什麼是完善。它會在最上面追加特殊的phpdocblock,所以沒有其他開發者有相同的警告。 – Kyslik

+0

謝謝,@Kyslik。那麼這個phpdocblock是由phpmd解釋的嗎?在這種情況下,它看起來很公平,因爲你無論如何都明確表示代碼意圖(變量聲明)。 – alariva

+0

只需使用這個'/ ** @noinspection PhpUndefinedVariableInspection * /'$ this-> transmail ...並且探測器會忽略它(或者更好地說:phpstorm不會允許在下一個語句中運行檢查) – Kyslik

回答

1

您可以使用文檔註釋註釋從PHPMD排除方法或類或抑制特殊規則對一些軟件瑕疵。

/** 
* This will suppress all the PMD warnings in 
* this class. 
* 
* @SuppressWarnings(PHPMD) 
*/ 
class Bar { 
    function foo() { 
     $baz = 23; 
    } 
} 

或者,您可以抑制一個規則,像這樣的註釋:

/** 
* 
*/ 
class Bar { 
    /** 
    * This will suppress UnusedLocalVariable 
    * warnings in this method 
    * 
    * @SuppressWarnings(PHPMD.UnusedLocalVariable) 
    */ 
    public function foo() { 
     $baz = 42; 
    } 
} 

來源PHPStorm的https://phpmd.org/documentation/suppress-warnings.html


用戶不使用PHPMD可以使用

/** @noinspection RULE */ 

規則在哪裏都可以在這裏

https://gist.github.com/discordier/ed4b9cba14652e7212f5

+0

所以這是[更新的代碼](https://github.com/timegridio/timegrid/commit/d29096d61aa49ca6ffecc581128154e635488989)。謝謝@Kyslik – alariva