Skip to content

Commit b4403cf

Browse files
✨ Check for blank line before return comment
1 parent 4732c3c commit b4403cf

File tree

4 files changed

+107
-21
lines changed

4 files changed

+107
-21
lines changed

SymfonyCustom/Sniffs/Formatting/BlankLineBeforeReturnSniff.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,34 @@ public function register()
3535
public function process(File $phpcsFile, $stackPtr)
3636
{
3737
$tokens = $phpcsFile->getTokens();
38-
$current = $stackPtr;
39-
$previousLine = $tokens[$stackPtr]['line'] - 1;
40-
$prevLineTokens = array();
38+
$current = $stackPtr - 1;
39+
$prevToken = null;
40+
$returnOrCommentLine = $tokens[$stackPtr]['line'];
4141

42-
while ($current >= 0 && $tokens[$current]['line'] >= $previousLine) {
43-
if ($tokens[$current]['line'] === $previousLine
44-
&& 'T_WHITESPACE' !== $tokens[$current]['type']
45-
&& 'T_COMMENT' !== $tokens[$current]['type']
46-
&& 'T_DOC_COMMENT_STRING' !== $tokens[$current]['type']
47-
&& 'T_DOC_COMMENT_OPEN_TAG' !== $tokens[$current]['type']
48-
&& 'T_DOC_COMMENT_TAG' !== $tokens[$current]['type']
49-
&& 'T_DOC_COMMENT_CLOSE_TAG' !== $tokens[$current]['type']
50-
&& 'T_DOC_COMMENT_WHITESPACE' !== $tokens[$current]['type']
51-
) {
52-
$prevLineTokens[] = $tokens[$current]['type'];
42+
while ($current >= 0 && null === $prevToken) {
43+
if ('T_WHITESPACE' !== $tokens[$current]['type']) {
44+
if ($this->isComment($tokens[$current])) {
45+
if ($returnOrCommentLine > $tokens[$current]['line'] + 1) {
46+
$prevToken = $tokens[$current];
47+
} else {
48+
$returnOrCommentLine = $tokens[$current]['line'];
49+
}
50+
} else {
51+
$prevToken = $tokens[$current];
52+
}
5353
}
5454
$current--;
5555
}
5656

57-
if (isset($prevLineTokens[0])
58-
&& ('T_OPEN_CURLY_BRACKET' === $prevLineTokens[0]
59-
|| 'T_COLON' === $prevLineTokens[0])
60-
) {
57+
if (!$prevToken) {
6158
return;
6259
}
6360

64-
if (count($prevLineTokens) > 0) {
61+
if ('T_OPEN_CURLY_BRACKET' === $prevToken['type'] || 'T_COLON' === $prevToken['type']) {
62+
return;
63+
}
64+
65+
if ($returnOrCommentLine - 1 === $prevToken['line']) {
6566
$fix = $phpcsFile->addFixableError(
6667
'Missing blank line before return statement',
6768
$stackPtr,
@@ -71,7 +72,9 @@ public function process(File $phpcsFile, $stackPtr)
7172
if (true === $fix) {
7273
$phpcsFile->fixer->beginChangeset();
7374
$i = 1;
74-
while ('T_WHITESPACE' === $tokens[$stackPtr - $i]['type']) {
75+
while ('T_WHITESPACE' === $tokens[$stackPtr - $i]['type']
76+
|| $this->isComment($tokens[$stackPtr - $i])
77+
) {
7578
$i++;
7679
}
7780
$phpcsFile->fixer->addNewLine($stackPtr - $i);
@@ -81,4 +84,19 @@ public function process(File $phpcsFile, $stackPtr)
8184

8285
return;
8386
}
87+
88+
/**
89+
* @param array $token
90+
*
91+
* @return bool
92+
*/
93+
private function isComment(array $token)
94+
{
95+
return 'T_COMMENT' === $token['type']
96+
|| 'T_DOC_COMMENT_STRING' === $token['type']
97+
|| 'T_DOC_COMMENT_OPEN_TAG' === $token['type']
98+
|| 'T_DOC_COMMENT_TAG' === $token['type']
99+
|| 'T_DOC_COMMENT_CLOSE_TAG' === $token['type']
100+
|| 'T_DOC_COMMENT_WHITESPACE' === $token['type'];
101+
}
84102
}

SymfonyCustom/Tests/Formatting/BlankLineBeforeReturnUnitTest.inc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,45 @@ function validFunctionReturnSix()
3737
return;
3838
}
3939

40+
function validFunctionReturnSeven()
41+
{
42+
echo "";
43+
/** comment */
44+
45+
return;
46+
}
47+
48+
function validFunctionReturnEight()
49+
{
50+
echo "";
51+
// comment
52+
53+
// comment
54+
return;
55+
}
56+
4057
function invalidFunctionReturnOne()
4158
{
4259
echo "";
4360
return;
4461
}
4562

63+
function invalidFunctionReturnTwo()
64+
{
65+
echo "";
66+
/** comment */
67+
return;
68+
}
69+
70+
function invalidFunctionReturnThree()
71+
{
72+
echo "";
73+
/**
74+
* comment
75+
*/
76+
return;
77+
}
78+
4679
switch ($condition) {
4780
case 'foo':
4881
return true;

SymfonyCustom/Tests/Formatting/BlankLineBeforeReturnUnitTest.inc.fixed

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,47 @@ function validFunctionReturnSix()
3737
return;
3838
}
3939

40+
function validFunctionReturnSeven()
41+
{
42+
echo "";
43+
/** comment */
44+
45+
return;
46+
}
47+
48+
function validFunctionReturnEight()
49+
{
50+
echo "";
51+
// comment
52+
53+
// comment
54+
return;
55+
}
56+
4057
function invalidFunctionReturnOne()
4158
{
4259
echo "";
4360

4461
return;
4562
}
4663

64+
function invalidFunctionReturnTwo()
65+
{
66+
echo "";
67+
68+
/** comment */
69+
return;
70+
}
71+
72+
function invalidFunctionReturnThree()
73+
{
74+
echo "";
75+
/**
76+
* comment
77+
*/
78+
return;
79+
}
80+
4781
switch ($condition) {
4882
case 'foo':
4983
return true;

SymfonyCustom/Tests/Formatting/BlankLineBeforeReturnUnitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class BlankLineBeforeReturnUnitTest extends AbstractSniffUnitTest
2222
public function getErrorList()
2323
{
2424
return array(
25-
43 => 1,
25+
60 => 1,
26+
67 => 1,
2627
);
2728
}
2829

0 commit comments

Comments
 (0)