简体   繁体   English

PSR2代码风格和PHP Code Sniffer不同意?

[英]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 . 我已经设置了编辑器代码样式设置,从Editor > Code Style > PHP作为Predefined Style >PSR1/PSR2 I have PHP Code Sniffer and PHP Mess Detector installed and configured as well. 我也安装并配置了PHP Code Sniffer和PHP Mess Detector。 Any time I format the code using CTRL+ALT+L I get the following issue: 每次使用CTRL+ALT+L格式化代码时,都会出现以下问题:

在此输入图像描述

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. 看起来你只是在你的IDE或PHPCS对PSR规则的解释中遇到了一个错误。 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. 其中一个是错误的并且需要提出错误报告,但是您需要仔细阅读PSR规则以确定哪一个。 (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) (我当然,假设你已经安装了最新版本的两个版本;我注意到PHPStorm的新版本刚刚问世,所以如果你还没有升级,那么这可能是一个很好的机会)

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. 同时,我建议重构你的代码以阻止你的if()语句看起来像那样 - 说实话,它不是看起来很干净的代码,无论它是否符合PSR规则。

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实际上并不是说多线IF条件需要缩进,但PHPStorm显然是1缩进,因为你的线在IF条件内并且还有1个缩进,因为你的线在多线函数调用中。

PSR2 does say that multi-line function calls must be indented, but it says they must be indented once . PSR2 确实说多行函数调用必须缩进,但它说它们必须缩进一次 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 这在此处记录: 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: 所以正确的PSR2代码可能是这样的:

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: 我倾向于将PSR2与PEAR标准中的一些多行条件规则结合起来,这将为您提供这个有效的PSR2代码:

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. 我不知道PHPStorm是否同意这一点,但我认为它可能会给出它似乎有的缩进规则。

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. 我上面发布的代码正是PEAR编码标准使用的代码,但PSR2没有为此定义任何规则。

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

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