diff --git a/DotBlue/Sniffs/WhiteSpace/ReturnSpacingSniff.php b/DotBlue/Sniffs/WhiteSpace/ReturnSpacingSniff.php new file mode 100644 index 0000000..a3358ac --- /dev/null +++ b/DotBlue/Sniffs/WhiteSpace/ReturnSpacingSniff.php @@ -0,0 +1,58 @@ +getTokens(); + $prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, $stackPtr - 10, $exclude = TRUE); + + if (!$prev) { + return; + } + + $lines = abs($tokens[$stackPtr]['line'] - $tokens[$prev]['line']); + if ($tokens[$prev]['code'] === T_OPEN_CURLY_BRACKET) { + if ($lines > 1) { + $fix = $phpcsFile->addFixableError('There must be no empty lines between the opening brace and the return statement. Found ' . ($lines - 1), $stackPtr); + if ($fix) { + self::fixSpacing($phpcsFile, $prev, $lines, 3); + } + } + } elseif (in_array($tokens[$prev]['code'], [T_CLOSE_CURLY_BRACKET, T_SEMICOLON])) { + if ($lines !== 2) { + $fix = $phpcsFile->addFixableError('There must be one empty line between the closing brace or semicolon and the return statement. Found ' . ($lines - 1), $stackPtr); + if ($fix) { + self::fixSpacing($phpcsFile, $prev, $lines, 2); + } + } + } + } + +} diff --git a/DotBlue/tests/ReturnSpacingSniff.phpt b/DotBlue/tests/ReturnSpacingSniff.phpt new file mode 100644 index 0000000..8690e5e --- /dev/null +++ b/DotBlue/tests/ReturnSpacingSniff.phpt @@ -0,0 +1,39 @@ +setFile('ReturnSpacing_1') + ->setSniff('WhiteSpace.ReturnSpacing') + ->expectMessage('There must be no empty lines between the opening brace and the return statement. Found 1') + ->onLine(5) + ->isFixable() + ->getFile() + ->expectMessage('There must be no empty lines between the opening brace and the return statement. Found 2') + ->onLine(13) + ->isFixable(); + +$tester->setFile('ReturnSpacing_2') + ->setSniff('WhiteSpace.ReturnSpacing') + ->expectMessage('There must be one empty line between the closing brace or semicolon and the return statement. Found 0') + ->onLine(6) + ->isFixable() + ->getFile() + ->expectMessage('There must be one empty line between the closing brace or semicolon and the return statement. Found 0') + ->onLine(13) + ->isFixable(); + +$tester->setFile('ReturnSpacing_3') + ->setSniff('WhiteSpace.ReturnSpacing') + ->expectMessage('There must be one empty line between the closing brace or semicolon and the return statement. Found 2') + ->onLine(8) + ->isFixable() + ->getFile() + ->expectMessage('There must be one empty line between the closing brace or semicolon and the return statement. Found 2') + ->onLine(17) + ->isFixable(); + +$tester->test(); diff --git a/DotBlue/tests/invalid/ReturnSpacing_1.php b/DotBlue/tests/invalid/ReturnSpacing_1.php new file mode 100644 index 0000000..41ae09d --- /dev/null +++ b/DotBlue/tests/invalid/ReturnSpacing_1.php @@ -0,0 +1,14 @@ +