-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1f268d
commit a9f9db6
Showing
8 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace DotBlue\Sniffs\WhiteSpace; | ||
|
||
use PHP_CodeSniffer_File; | ||
use PHP_CodeSniffer_Sniff; | ||
|
||
|
||
class ReturnSpacingSniff implements PHP_CodeSniffer_Sniff | ||
{ | ||
|
||
use FixEmptyLines; | ||
|
||
|
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function register() | ||
{ | ||
return [ | ||
T_RETURN, | ||
]; | ||
} | ||
|
||
|
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
use DotBlue\CodeSniffer\Helpers\Tester; | ||
|
||
|
||
require __DIR__ . '/bootstrap.php'; | ||
|
||
$tester = new Tester(); | ||
$tester->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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
function foo() { | ||
|
||
return 1; | ||
} | ||
|
||
|
||
|
||
function bar() { | ||
|
||
|
||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
function foo() { | ||
if (TRUE) { | ||
} | ||
return 1; | ||
} | ||
|
||
|
||
|
||
function bar() { | ||
$a = 1; | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
function foo() { | ||
if (TRUE) { | ||
} | ||
|
||
|
||
return 1; | ||
} | ||
|
||
|
||
|
||
function bar() { | ||
$a = 1; | ||
|
||
|
||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
function foo() { | ||
return 1; | ||
} | ||
|
||
|
||
|
||
function bar() { | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
function foo() { | ||
if (TRUE) { | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
|
||
|
||
function bar() { | ||
$a = 1; | ||
|
||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
function foo() { | ||
if (TRUE) { | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
|
||
|
||
function bar() { | ||
$a = 1; | ||
|
||
return 1; | ||
} |