php代碼規範 sonar版本

來源:互聯網
上載者:User

標籤:style   color   os   io   re   c   

  1. 類的大括弧在後面 不是另起一行

  2. 變數名首字母小寫 駝峰模式 [a-z][a-zA-Z0-9]*

  3. 注釋要另起一行,而不是跟在代碼後面,

  4. 移除注釋的程式碼片段要

  5. swtich 至少包含3個case 否則就用if吧

  6. if等不能嵌套超過3次

  7. 類中的方法不能超過20個,超過的話 就拆分把

  8. 移除沒有用的參數

  9. 移除沒用的變數

  10. if必須要跟else

  11. if總是跟著大括弧

  12. 代碼中不要有太多的return

  13. switch 要加default

  14. 如下代碼

if (condition) {  return true;} else {  return false;}//或者if(a==b){return true;}else{return false;}應該寫成return condition;return a==b;

//直接返回function compute_duration_in_milliseconds() {  $duration = ((($hours * 60) + $minutes) * 60 + $seconds ) * 1000 ;  return $duration;}Compliant Solutionfunction compute_duration_in_milliseconds() {  return ((($hours * 60) + $minutes) * 60 + $seconds ) * 1000;}

//出現重複參數function run() {  prepare(‘action1‘);          // Non-Compliant - ‘action1‘ is duplicated 3 times  execute(‘action1‘);  release(‘action1‘);}//正確的做法ACTION_1 = ‘action1‘;function run() {  prepare(ACTION_1);  execute(ACTION_1);  release(ACTION_1);}

//布爾值直接判斷if ($booleanVariable == true) { /* ... */ }if ($booleanVariable != true) { /* ... */ }if ($booleanVariable || false) { /* ... */ }doSomething(!false);Compliant Solutionif ($booleanVariable) { /* ... */ }if (!$booleanVariable) { /* ... */ }if ($booleanVariable) { /* ... */ }doSomething(true);


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.