Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed braces fixer for else if statements. #109

Merged
merged 1 commit into from

2 participants

@pylebecq

Hey there,

I found out a weird case with some "else if" statements. It is related to the braces fixer. When it encounters something like

if (...)
{ 
    // ...
}
else if (...)
{
    // ...
}

It will convert it to

if (...) {
    // ...
} else { if (...)
    // ...
}

Which is totally wrong. If you have several "else if" statements, you end up with successive "else" statements, resulting in a parse error.

@fabpot fabpot referenced this pull request from a commit
@fabpot fabpot merged branch pylebecq/fix-braces (PR #109)
Commits
-------

65d2887 Fixed braces fixer for else if statements.

Discussion
----------

Fixed braces fixer for else if statements.

Hey there,

I found out a weird case with some "else if" statements. It is related to the braces fixer. When it encounters something like
```php
if (...)
{
    // ...
}
else if (...)
{
    // ...
}
```
It will convert it to
```
if (...) {
    // ...
} else { if (...)
    // ...
}
```

Which is totally wrong. If you have several "else if" statements, you end up with successive "else" statements, resulting in a parse error.
9b13373
@fabpot fabpot merged commit 65d2887 into FriendsOfPHP:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 6, 2012
  1. Fixed braces fixer for else if statements.

    pierreyves.lebecq authored
This page is out of date. Refresh to see the latest.
View
1  Symfony/CS/Fixer/CurlyBracketsNewlineFixer.php
@@ -83,6 +83,7 @@ private function controlStatementsFix($content)
{
$statements = array(
'\bif\s*\(.*\)',
+ '\belse\s*if\s*\(.*\)',
'\belse\b',
'\bfor\s*\(.*\)',
'\bdo\b',
View
5 Symfony/CS/Tests/Fixer/CurlyBracketsNewlineFixerTest.php
@@ -80,6 +80,11 @@ public function testControlStatements()
$this->assertEquals($ifFixed, $fixer->fix($this->getFileMock(), $if));
$this->assertEquals($ifFixed, $fixer->fix($this->getFileMock(), $ifFixed));
+ $elseif = "else if (...)\n{";
+ $elseifFixed = "else if (...) {";
+ $this->assertEquals($elseifFixed, $fixer->fix($this->getFileMock(), $elseif));
+ $this->assertEquals($elseifFixed, $fixer->fix($this->getFileMock(), $elseifFixed));
+
$func = "function download() {\n}";
$funcFixed = "function download()\n{\n}";
$this->assertEquals($funcFixed, $fixer->fix($this->getFileMock(), $func));
Something went wrong with that request. Please try again.