首页 > 代码库 > php代码规范 sonar版本

php代码规范 sonar版本

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