doucong8553
doucong8553
2017-05-31 07:47
浏览 74
已采纳

PHP代码嗅探器中的三元运算符出错

I have problem with PHP code sniffer and ternary operator. I added rule for checking spaces after and before operators

<rule ref="Squiz.WhiteSpace.OperatorSpacing"/>

and now I have errors in short if statements:

 37 | ERROR | [x] Expected 1 space before "?"; newline found
 38 | ERROR | [x] Expected 1 space before ":"; newline found

My code looks like:

return ($this->get('router')->getContext()->getHttpPort() == 80)
    ? '//'.$this->get('router')->getContext()->getHost()
    : '//'.$this->get('router')->getContext()->getHost().':'.$this->get('router')->getContext()->getHttpPort();

Anyone know where can be problem? I can paste whole ruleset file but after delete OperatorSpacing rule everything is ok.

Greetings

图片转代码服务由CSDN问答提供 功能建议

我遇到了PHP代码嗅探器和三元运算符的问题。 我添加了用于检查运算符之后和之前的空格的规则

 &lt; rule ref =“Squiz.WhiteSpace.OperatorSpacing”/&gt; 
   
 
 

现在我有错误 short if语句:

  37 | 错误|  [x]“?”之前预计有1个空格;  newline找到
 38 | 错误|  [x]在“:”之前预计有1个空格; 找到换行符
   
 
 

我的代码如下所示:

  return($ this-&gt; get('router')  ) - &gt; getContext() - &gt; getHttpPort()== 80)
?  '//'.$this->get('router')->getContext()->getHost()
:'//'.$this->get('router')-> 的getContext() - &GT;和getHost()。 ':' $这 - &GT;获得( '路由器') - &GT;的getContext() - &GT; getHttpPort(); 
   
  
 

任何人都知道哪里有问题? 我可以粘贴整个规则集文件但删除后的OperatorSpacing规则一切正常。

问候

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drag2458
    drag2458 2017-05-31 08:07
    已采纳

    Ok guys, thanks for help but i found solution, @roberto06, thanks for link m8!

    After add

    <rule ref="Squiz.WhiteSpace.OperatorSpacing">
        <properties>
            <property name="ignoreNewlines" value="true"/>
        </properties>
    </rule>
    

    it's working perfect :)

    点赞 评论
  • drztpx8735
    drztpx8735 2017-05-31 07:58

    It only says everything should be on the same line.

    By the way, something better in your case would be:

    $value = '//'.$this->get('router')->getContext()->getHost();
    
    return $value . ($this->get('router')->getContext()->getHttpPort() !== 80) ? (':'.$this->get('router')->getContext()->getHttpPort()) : '';
    
    点赞 评论