Skip to content

Commit

Permalink
merged branch JEDIBC/fixReturnIfNoBraces (PR PHP-CS-Fixer#120)
Browse files Browse the repository at this point in the history
Commits
-------

fb141b9 Remove unneeded rtrim
3c46626 Fix if there is multiple blank line between the previous instruction and the return statement
8912019 Fix blank line before return if used ine an if/els/else if/else context with no opening brace used

Discussion
----------

Fix blank line before return if used ine an if/els/else if/else context ...

...with no opening brace used

Like :

```
if ($foo == $bar)
    return true;
```

must remain without blank line.
  • Loading branch information
fabpot committed Jul 25, 2012
2 parents 4c4bc75 + fb141b9 commit 55701d6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Symfony/CS/Fixer/ReturnStatementsFixer.php
Expand Up @@ -21,13 +21,14 @@ class ReturnStatementsFixer implements FixerInterface
public function fix(\SplFileInfo $file, $content) public function fix(\SplFileInfo $file, $content)
{ {
// [Structure] Add a blank line before return statements // [Structure] Add a blank line before return statements
return preg_replace_callback('/(^.*$)\n(^ +return\b)/m', function ($match) { return preg_replace_callback('/(^.*$)\n+(^ +return\b)/m', function ($match) {
// don't add it if the previous line is ... // don't add it if the previous line is ...
if ( if (
preg_match('/\{$/m', $match[1]) || // ... ending with an opening brace preg_match('/\{$/m', $match[1]) || // ... ending with an opening brace
preg_match('/\:$/m', $match[1]) || // ... ending with a colon (e.g. a case statement) preg_match('/\:$/m', $match[1]) || // ... ending with a colon (e.g. a case statement)
preg_match('%^ *//%m', $match[1]) || // ... an inline comment preg_match('%^ *//%m', $match[1]) || // ... an inline comment
preg_match('/^$/m', $match[1]) // ... already blank preg_match('/^$/m', $match[1]) || // ... already blank
preg_match('/^(?!.*\{) *(if .+|else.*)/m', $match[1]) // ... if/else/else if/elseif without an opening brace
) { ) {
return $match[1]."\n".$match[2]; return $match[1]."\n".$match[2];
} }
Expand Down
63 changes: 63 additions & 0 deletions Symfony/CS/Tests/Fixer/ReturnStatementsFixerTest.php
Expand Up @@ -60,10 +60,73 @@ public function testFixProvider()
return; return;
TEST; TEST;


$return4 = <<<TEST
if (\$foo == \$bar)
return;
TEST;
$returnFixed4 = <<<TEST
if (\$foo == \$bar)
return;
TEST;

$return5 = <<<TEST
else
return;
TEST;
$returnFixed5 = <<<TEST
else
return;
TEST;

$return6 = <<<TEST
elseif (\$foo == \$bar)
return;
TEST;
$returnFixed6 = <<<TEST
elseif (\$foo == \$bar)
return;
TEST;

$return7 = <<<TEST
if (\$foo == \$bar)
return;
TEST;
$returnFixed7 = <<<TEST
if (\$foo == \$bar)
return;
TEST;

$return8 = <<<TEST
\$foo = \$bar;
return \$foo;
TEST;
$returnFixed8 = <<<TEST
\$foo = \$bar;
return \$foo;
TEST;

return array( return array(
array($return1, $returnFixed1), array($return1, $returnFixed1),
array($return2, $returnFixed2), array($return2, $returnFixed2),
array($return3, $returnFixed3), array($return3, $returnFixed3),
array($return4, $returnFixed4),
array($return5, $returnFixed5),
array($return6, $returnFixed6),
array($return7, $returnFixed7),
array($return8, $returnFixed8),
); );
} }


Expand Down

0 comments on commit 55701d6

Please sign in to comment.