php代码规范 sonar版本

  1. 类的大括号在后面 不是另起一行php

  2. 变量名首字母小写 驼峰模式 [a-z][a-zA-Z0-9]*spa

  3. 注释要另起一行,而不是跟在代码后面,code

  4. 移除注释的代码段要it

  5. swtich 至少包含3个case 不然就用if吧io

  6. if等不能嵌套超过3次function

  7. 类中的方法不能超过20个,超过的话 就拆分把class

  8. 移除没有用的参数变量

  9. 移除没用的变量方法

  10. if必需要跟elseim

  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 Solution
function 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 Solution
if ($booleanVariable) { /* ... */ }
if (!$booleanVariable) { /* ... */ }
if ($booleanVariable) { /* ... */ }
doSomething(true);
相关文章
相关标签/搜索