首页 > 代码库 > php代码规范 sonar版本
php代码规范 sonar版本
类的大括号在后面 不是另起一行
变量名首字母小写 驼峰模式 [a-z][a-zA-Z0-9]*
注释要另起一行,而不是跟在代码后面,
移除注释的代码段要
swtich 至少包含3个case 否则就用if吧
if等不能嵌套超过3次
类中的方法不能超过20个,超过的话 就拆分把
移除没有用的参数
移除没用的变量
if必须要跟else
if总是跟着大括号
代码中不要有太多的return
switch 要加default
如下代码
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);
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。