简体   繁体   中英

Error with ternary operator in PHP code sniffer

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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