Permalink
Browse files

merged branch JEDIBC/fixReturnIfNoBraces (PR #120)

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...
2 parents 4c4bc75 + fb141b9 commit 55701d65622b72cbe0db8d8f9136d36e45b2f7f4 @fabpot committed Jul 25, 2012
Showing with 69 additions and 5 deletions.
  1. +6 −5 Symfony/CS/Fixer/ReturnStatementsFixer.php
  2. +63 −0 Symfony/CS/Tests/Fixer/ReturnStatementsFixerTest.php
@@ -21,13 +21,14 @@ class ReturnStatementsFixer implements FixerInterface
public function fix(\SplFileInfo $file, $content)
{
// [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 ...
if (
- 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]) || // ... an inline comment
- preg_match('/^$/m', $match[1]) // ... already blank
+ 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]) || // ... an inline comment
+ 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];
}
@@ -60,10 +60,73 @@ public function testFixProvider()
return;
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(
array($return1, $returnFixed1),
array($return2, $returnFixed2),
array($return3, $returnFixed3),
+ array($return4, $returnFixed4),
+ array($return5, $returnFixed5),
+ array($return6, $returnFixed6),
+ array($return7, $returnFixed7),
+ array($return8, $returnFixed8),
);
}

0 comments on commit 55701d6

Please sign in to comment.