Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPCBF adds brace for ELSE IF split over multiple lines #558

Closed
meijerm15 opened this issue Apr 17, 2015 · 5 comments
Closed

PHPCBF adds brace for ELSE IF split over multiple lines #558

meijerm15 opened this issue Apr 17, 2015 · 5 comments

Comments

@meijerm15
Copy link

PHPCBF is currently fixing bracketless if-else statements incorrectly and causing a fatal error.
Which means that the code won't run, because of the 2 else statements in a row, as seen in my example.

Before:

     public function test($test)
     {
        if ($test) {

        } else
            if ($test) {

            } else {

            }
     }

After:

     public function test($test)
{
    if ($test) {
    } else {
        if ($test) {
        }
    } else {
    }
        }

Log:
Processing test.php [PHP => 53 tokens in 12 lines]... DONE in 3ms (9 fixable violations)
=> Fixing file: 0/9 violations remaining [made 6 passes]... DONE in 61ms
Patched 1 file
Time: 146ms; Memory: 5.25Mb

Version:
PHP_CodeSniffer version 2.3.0 (stable) by Squiz (http://www.squiz.net)

@aik099
Copy link
Contributor

aik099 commented Apr 17, 2015

Can you please specify standard used and error reported in original code.

@meijerm15
Copy link
Author

@aik099

Command
phpcbf --standard=psr2 -n test.php

So I've used the psr2 standard and could you explain what you meant by 'error reported in original code.' because this test snippet does reproduce the fatal code change

@aik099
Copy link
Contributor

aik099 commented Apr 17, 2015

could you explain what you meant by 'error reported in original code.' because this test snippet does reproduce the fatal code change

This is result of phpcs -s --standard=psr2 -n test.php command. The phpcbf is only fixing what phpcs is reporting. The -s option would show sniff code, that is responsible for the fix.

@meijerm15
Copy link
Author

Snippet

<?php
namespace test;

class Test
{
     public function test($test)
     {
        if ($test) {
        } else
            if ($test) {
            } else {
            }
     }
}

Command
phpcs -s --standard=psr2 -n test.php

9 | ERROR | [x] Expected 1 space after ELSE keyword; newline found
| | (Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword)
9 | ERROR | [x] Inline control structures are not allowed
| | (Generic.ControlStructures.InlineControlStructure.NotAllowed)
10 | ERROR | [x] Line indented incorrectly; expected 8 spaces, found
| | 12 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
13 | ERROR | [x] Line indented incorrectly; expected 8 spaces, found
| | 5 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)

Generic.ControlStructures.InlineControlStructure.NotAllowed
Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword

@gsherwood gsherwood changed the title PHPCBF Fatal Bracketless if-else bug PHPCBF adds brace for ELSE IF split over multiple lines Apr 21, 2015
@gsherwood
Copy link
Member

This was a problem with the inline control structure sniff not understanding that the else if split over multiple lines was actually the same statement. This is now fixed.

You need to run PHPCBF a couple of times over this specific code block because the sniff to get it perfect (due to a conflict between the indent, newline and elseif sniffs in this specific case) but you will end up with this:

<?php
namespace test;

class Test
{
     public function test($test)
     {
        if ($test) {
        } elseif ($test) {
        } else {
        }
     }
}

jrfnl added a commit to jrfnl/PHP_CodeSniffer that referenced this issue Jul 29, 2024
…cessary-string-concat

Generic/UnnecessaryStringConcat: improve code coverage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants