Search code examples
phpphpcodesnifferphpcs

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


Solution

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