简体   繁体   中英

PSR2 code style and PHP Code Sniffer doesn't agreed?

I have setup my editor code style setup from Editor > Code Style > PHP as Predefined Style >PSR1/PSR2 . I have PHP Code Sniffer and PHP Mess Detector installed and configured as well. Any time I format the code using CTRL+ALT+L I get the following issue:

在此输入图像描述

Why is that? The original code looks like (I think is not so helpful but anyway here it's):

public function myTestFunction()
{
    $is_valid = true;

    if ($this->manual_value && !$this->_InputValidator->isValidString(
            $this->manual_value,
            1,
            2,
            Regex::STRING
        )
    ) {
        $is_valid = false;
    }

    return $is_valid;
}

It's unfortunate; it looks like you've simply hit a bug in either your IDE's or PHPCS's interpretation of the PSR rules. One of them is wrong and is in need of a bug report being raised, but you'll need to read the PSR rules carefully to work out which one. (It may be easier to raise a bug report for both of them and let them work it out)

(I am, of course, assuming that you have the latest versions of both installed already; I note that a new release of PHPStorm has just come out, so if you haven't already upgraded, this might be a good chance to do so)

In the meanwhile, I would suggest refactoring your code to stop your if() statements ending up looking like that -- to be honest, it's not clean-looking code, regardless of whether it meets the PSR rules.

I would refactor it to look something like this:

public function myTestFunction()
{
    $input_is_valid = $this->_InputValidator->isValidString(
        $this->manual_value,
        1,
        2,
        Regex::STRING
    );

    return ($this->manual_value && !$input_is_valid);
}

PSR2 doesn't actually say that a multi-line IF condition needs to be indented, but PHPStorm is obviously putting in 1 indent because your lines are inside an IF condition and 1 additional indent because your lines are inside a multi-line function call.

PSR2 does say that multi-line function calls must be indented, but it says they must be indented once . That is documented here: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md#46-method-and-function-calls

So the correct PSR2 code is probably this:

public function myTestFunction()
{
    $is_valid = true;

    if ($this->manual_value && !$this->_InputValidator->isValidString(
        $this->manual_value,
        1,
        2,
        Regex::STRING
    )
    ) {
        $is_valid = false;
    }

    return $is_valid;
}

But it doesn't look great.

What I tend to do is combine PSR2 with some multi-line condition rules from the PEAR standard, which would give you this valid PSR2 code:

public function myTestFunction()
{
    $is_valid = true;

    if ($this->manual_value
        && !$this->_InputValidator->isValidString(
            $this->manual_value,
            1,
            2,
            Regex::STRING
        )
    ) {
        $is_valid = false;
    }

    return $is_valid;
}

I have no idea if PHPStorm would agree with that, but I think it might given the indent rules it appears to have.

You can also put the && at the end of the first line instead of at the start of the second. The code I posted above is just what the PEAR coding standard uses, but PSR2 doesn't define any rules for this.

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