Php_codesniffer: switch/case indent issue

Created on 5 Nov 2015  路  4Comments  路  Source: squizlabs/PHP_CodeSniffer

Hi there!
I have a problem about indents in switch/case.
I wrote some code like below

switch ($httpMethod)
{
    case 'POST':
        $result = self::getInstance()->post(config::API_SERVER . $endpoint, $params);
        break;

    case 'GET':
        $result = self::getInstance()->get(config::API_SERVER . $endpoint, $params);
        break;

    case 'PUT':
        $result = self::getInstance()->put(config::API_SERVER . $endpoint, $params);
        break;

    case 'DEL':
        $result = self::getInstance()->del(config::API_SERVER . $endpoint, $params);
        break;

    case 'PATCH':
        $result = self::getInstance()->patch(config::API_SERVER . $endpoint, $params);
        break;
    default:
        throw new \Exception('HTTP method is invalid.');
}

Then, I run ./vendor/bin/phpcs --standard=some.xml some.php
CMD output ERROR like this:

FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 88 | ERROR | [x] Line indented incorrectly; expected 8 spaces, found
    |       |     12
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

88 line is case 'POST':. So, I fixed this like this:

switch ($httpMethod)
{
case 'POST':
    $result = self::getInstance()->post(config::API_SERVER . $endpoint, $params);
    break;

case 'GET':
    $result = self::getInstance()->get(config::API_SERVER . $endpoint, $params);
    break;

case 'PUT':
    $result = self::getInstance()->put(config::API_SERVER . $endpoint, $params);
    break;

case 'DEL':
    $result = self::getInstance()->del(config::API_SERVER . $endpoint, $params);
    break;

case 'PATCH':
    $result = self::getInstance()->patch(config::API_SERVER . $endpoint, $params);
    break;
default:
    throw new \Exception('HTTP method is invalid.');
}

Then, I run.
But, CMD output ERROR like this:

FOUND 8 ERRORS AFFECTING 8 LINES
----------------------------------------------------------------------
  88 | ERROR | [x] Line indented incorrectly; expected 12 spaces,
     |       |     found 8
  89 | ERROR | [x] Line indented incorrectly; expected at least 16
     |       |     spaces, found 12
  90 | ERROR | [x] Terminating statement must be indented to the same
     |       |     level as the CASE body
  94 | ERROR | [x] Terminating statement must be indented to the same
     |       |     level as the CASE body
  98 | ERROR | [x] Terminating statement must be indented to the same
     |       |     level as the CASE body
 102 | ERROR | [x] Terminating statement must be indented to the same
     |       |     level as the CASE body
 106 | ERROR | [x] Terminating statement must be indented to the same
     |       |     level as the CASE body
 108 | ERROR | [x] Terminating statement must be indented to the same
     |       |     level as the CASE body
----------------------------------------------------------------------
PHPCBF CAN FIX THE 8 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

I've thought my phpcs rule have a problem but I can't find incorrect rule.
My rule contains PSR2.ControlStructures.SwitchDeclaration.
Can i get help?

Awaiting Feedback

Most helpful comment

Your standard has a conflict between PEAR.WhiteSpace.ScopeIndent and Generic.WhiteSpace.ScopeIndent. You'll need to remove one of them.

The Generic one wants the SWITCH statement formatted like you have in your first block (CASE indented one level). If you that code, remove the PEAR reference.

If you prefer your code like the second code block (CASE indented to same level as SWITCH) then remove the Generic reference and keep the PEAR one.

All 4 comments

When I run your second bit of code through the PSR2 sniff, it works fine, so maybe there is another sniff in your custom standard that is conflicting.

Please post your custom standard, or re-run the commands with the -s command line argument to show what sniffs are reporting errors.

It also might be a problem causes by code higher up in your file that I can't see. Try removing that block of code and putting it in a temp file and then checking that. If it doesn't report any errors when tested alone, the error is probably caused by other parts of the file. I'd need the contents of the file to be able to debug the problem in this case.

oh! you're right! Maybe, My ruleset xml file has a problem! But, I don't know What is wrong.
I've tested my code. I made a test file. and attach my second switch/case code to it. then, i run ./vendor/bin/phpcs -s test.php. A result is in below:

FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 2 | ERROR | Missing file doc comment
   |       | (PEAR.Commenting.FileComment.Missing)
----------------------------------------------------------------------

An error exists, but It is not related.

My ruleset xml file is this:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="sample code sniffer">
    <description>
        sample code sniffer
    </description>
    <rule ref="Generic.Classes.DuplicateClassName"/>
    <rule ref="Generic.CodeAnalysis.EmptyStatement"/>
    <rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop"/>
    <rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
    <rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
    <rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
    <rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
    <rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
    <rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>
    <rule ref="Generic.Commenting.Todo"/>
    <rule ref="Generic.Commenting.Todo.CommentFound">
        <message>Please review this TODO comment: %s</message>
        <severity>3</severity>
    </rule>

    <rule ref="Generic.Commenting.Fixme"/>
    <rule ref="Generic.ControlStructures.InlineControlStructure"/>
    <rule ref="Generic.Files.LineEndings"/>
    <rule ref="Generic.Formatting.DisallowMultipleStatements"/>
    <rule ref="Generic.Formatting.MultipleStatementAlignment"/>
    <rule ref="Generic.Formatting.NoSpaceAfterCast"/>
    <rule ref="Generic.Functions.CallTimePassByReference"/>
    <rule ref="Generic.Functions.OpeningFunctionBraceBsdAllman"/>
    <rule ref="Generic.Metrics.CyclomaticComplexity"/>
    <rule ref="Generic.Metrics.NestingLevel">
        <properties>
            <property name="nestingLevel" value="3"/>
            <property name="absoluteNestingLevel" value="5"/>
        </properties>
    </rule>
    <rule ref="Generic.NamingConventions.ConstructorName"/>
    <rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
    <rule ref="Generic.NamingConventions.CamelCapsFunctionName"/>
    <rule ref="Generic.PHP.DeprecatedFunctions"/>
    <rule ref="Generic.PHP.DisallowShortOpenTag"/>
    <rule ref="Generic.PHP.ForbiddenFunctions"/>
    <rule ref="Generic.PHP.NoSilencedErrors"/>
    <rule ref="Generic.PHP.UpperCaseConstant"/>
    <rule ref="Generic.Strings.UnnecessaryStringConcat"/>
    <rule ref="Generic.WhiteSpace.DisallowTabIndent"/>
    <rule ref="Generic.WhiteSpace.ScopeIndent"/>
    <rule ref="MySource.PHP.EvalObjectFactory"/>
    <rule ref="MySource.PHP.GetRequestData"/>
    <!-- <rule ref="MySource.PHP.ReturnFunctionValue"/> -->
    <rule ref="MySource.PHP.AjaxNullComparison"/>
    <rule ref="PEAR.Classes.ClassDeclaration"/>
    <!-- <rule ref="PEAR.Commenting.ClassComment"/> -->
    <rule ref="PEAR.Commenting.FunctionComment"/>
    <rule ref="PEAR.Commenting.InlineComment"/>
    <rule ref="PEAR.ControlStructures.ControlSignature"/>
    <rule ref="PEAR.ControlStructures.MultiLineCondition"/>
    <rule ref="PEAR.Files.IncludingFile"/>
    <rule ref="PEAR.Formatting.MultiLineAssignment"/>
    <rule ref="PEAR.Functions.FunctionCallSignature"/>
    <rule ref="PEAR.Functions.FunctionDeclaration"/>
    <rule ref="PEAR.Functions.ValidDefaultValue"/>
    <!-- <rule ref="PEAR.NamingConventions.ValidClassName"/> -->
    <!-- <rule ref="PEAR.NamingConventions.ValidFunctionName"/> -->
    <!-- <rule ref="PEAR.NamingConventions.ValidVariableName"/> -->
    <rule ref="PEAR.WhiteSpace.ObjectOperatorIndent"/>
    <rule ref="PEAR.WhiteSpace.ScopeClosingBrace"/>
    <rule ref="PEAR.WhiteSpace.ScopeIndent"/>
    <rule ref="PSR1.Classes.ClassDeclaration"/>
    <rule ref="PSR1.Files.SideEffects"/>
    <rule ref="PSR2.Classes.ClassDeclaration"/>
    <rule ref="PSR2.Classes.PropertyDeclaration"/>
    <rule ref="PSR2.ControlStructures.ControlStructureSpacing"/>
    <rule ref="PSR2.ControlStructures.ElseIfDeclaration"/>
    <rule ref="PSR2.ControlStructures.SwitchDeclaration"/>
    <rule ref="PSR2.Files.EndFileNewline"/>
    <rule ref="PSR2.Methods.MethodDeclaration"/>
    <rule ref="PSR2.Namespaces.NamespaceDeclaration"/>
    <rule ref="PSR2.Namespaces.UseDeclaration"/>
    <rule ref="Squiz.PHP.DisallowMultipleAssignments"/>
    <rule ref="Squiz.PHP.DisallowObEndFlush"/>
    <rule ref="Squiz.PHP.DisallowSizeFunctionsInLoops"/>
    <rule ref="Squiz.PHP.DiscouragedFunctions"/>
    <rule ref="Squiz.PHP.EmbeddedPhp"/>
    <rule ref="Squiz.PHP.Eval"/>
    <rule ref="Squiz.PHP.ForbiddenFunctions"/>
    <rule ref="Squiz.PHP.GlobalKeyword"/>
    <rule ref="Squiz.PHP.Heredoc"/>
    <rule ref="Squiz.PHP.InnerFunctions"/>
    <rule ref="Squiz.PHP.LowercasePHPFunctions"/>
    <rule ref="Squiz.PHP.NonExecutableCode"/>
    <rule ref="Squiz.Scope.MemberVarScope"/>
    <rule ref="Squiz.Scope.MethodScope"/>
    <rule ref="Squiz.Scope.StaticThisUsage"/>
    <rule ref="Squiz.WhiteSpace.CastSpacing"/>
    <!-- <rule ref="Squiz.WhiteSpace.ControlStructureSpacing"/> -->
    <!-- <rule ref="Squiz.WhiteSpace.FunctionSpacing">
        <properties>
            <property name="spacing" value="1"/>
        </properties>
    </rule> -->
    <rule ref="Squiz.WhiteSpace.LanguageConstructSpacing"/>
    <rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
    <!-- <rule ref="Squiz.WhiteSpace.ObjectOperatorSpacing"/> -->
    <rule ref="Squiz.WhiteSpace.OperatorSpacing"/>
    <rule ref="Squiz.WhiteSpace.PropertyLabelSpacing"/>
    <rule ref="Squiz.WhiteSpace.ScopeClosingBrace"/>
    <rule ref="Squiz.WhiteSpace.ScopeKeywordSpacing"/>
    <rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
    <rule ref="Zend.Files.ClosingTag"/>
    <!-- <rule ref="Zend.NamingConventions.ValidVariableName"/> -->
</ruleset>

Your standard has a conflict between PEAR.WhiteSpace.ScopeIndent and Generic.WhiteSpace.ScopeIndent. You'll need to remove one of them.

The Generic one wants the SWITCH statement formatted like you have in your first block (CASE indented one level). If you that code, remove the PEAR reference.

If you prefer your code like the second code block (CASE indented to same level as SWITCH) then remove the Generic reference and keep the PEAR one.

Oh, Great! As your advise, I tested it after removing one of them.
Result is a success. I resolved this problem thanks to you.
Your response is awesome.

Was this page helpful?
0 / 5 - 0 ratings