squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

Switch and Case Indenting #1327

Closed MadCoder253 closed 7 years ago

MadCoder253 commented 7 years ago

Hi, I noticed a possible issue with correct indenting being marked as an issue with PHP CodeSniffer. I was using Visual Studio Code and the phpcs plugin.

Link to bug that has been closed. https://github.com/ikappas/vscode-phpcs/issues/20

Here's the summary.

Hi,

PHPCS is working great for me, very handy to update code as I work on it or making sure new code is formatted correctly.

It appears that the indenting check for the case may be off. If I have a switch statement inside a function, it is indented and then the case statement has an additional indent. However, the PHPCS shows the indentation is wrong, that it was expecting 4 spaces, but found 8. I think the check may not be taking into account the indent for being inside a function.

I double checked the standard. http://www.php-fig.org/psr/psr-2/#5-2-switch-case

Here's my truncated sample code. There is an error message for each case line and the default line.

function createWebsiteImage($orig_name, $new_name, $path, $w, $h, $img)
{
    switch($img) {
        **case 1:**
            $nw = 150;
            $nh = 205;
            break;
        **case 2:**
            $nw = 205;
            $nh = 150;
            break;
        **default:**
            $nw = 220;
            $nh = 220;
            break;    
    } 
}
gsherwood commented 7 years ago

The PSR standard does not show any errors for the code you posted. Only the PEAR standard (which is the default) shows errors because it wants that code formatted like this:

<?php
function createWebsiteImage($orig_name, $new_name, $path, $w, $h, $img)
{
    switch($img) {
    case 1:
        $nw = 150;
        $nh = 205;
        break;
    case 2:
        $nw = 205;
        $nh = 150;
        break;
    default:
        $nw = 220;
        $nh = 220;
        break;    
    } 
}

Please make sure your plugin is executing the coding standard that you want applied to your code. The PEAR standard is similar to the PSR2 standard, but you'd be missing out on some important checks if you are actually wanting to adhere to PSR2.

ikappas commented 7 years ago

@MadCoder253 According to http://www.php-fig.org/psr/psr-2/#5-2-switch-case and using vscode-phpcs 0.7.0 and phpcs 2.7.1 with:

{
    "phpcs.standard": "PSR2"
}

The correct formatting is:

<?php
function createWebsiteImage($orig_name, $new_name, $path, $w, $h, $img)
{
    switch ($img) {
        case 1:
            $nw = 150;
            $nh = 205;
            break;
        case 2:
            $nw = 205;
            $nh = 150;
            break;
        default:
            $nw = 220;
            $nh = 220;
            break;
    }
}

As @gsherwood noted the PEAR standard is the default one so unless you specify the standard setting in your workspace configuration in vscode, the plugin falls back to the phpcs default.

That means that unless you set your default standard to PSR2 like:

$ phpcs --config-set default_standard PSR2

All linting will be performed against PEAR and not PSR2.

I hope this helps.

References

https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-default-coding-standard

MadCoder253 commented 7 years ago

Thank you for the response! After I do a few launches today, I'll try it out.

Ken