简体   繁体   English

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

[英]Error with ternary operator in PHP code sniffer

I have problem with PHP code sniffer and ternary operator. 我有PHP代码嗅探器和三元运算符的问题。 I added rule for checking spaces after and before operators 我在运营商之前和之后添加了检查空格的规则

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

and now I have errors in short if statements: 现在我在if语句中有错误:

 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. 我可以粘贴整个规则集文件但删除后的OperatorSpacing规则一切正常。

Greetings 问候

Ok guys, thanks for help but i found solution, @roberto06, thanks for link m8! 好的,感谢您的帮助,但我找到了解决方案,@ roberto06,感谢链接m8!

After add 添加后

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

it's working perfect :) 它的工作完美:)

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()) : '';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM