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


转载于:https://my.oschina.net/u/554046/blog/294098

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值