Skip to content

Commit

Permalink
Add return spacing sniff
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusGoljerSalesChamp committed Oct 20, 2017
1 parent b1f268d commit a9f9db6
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 0 deletions.
58 changes: 58 additions & 0 deletions DotBlue/Sniffs/WhiteSpace/ReturnSpacingSniff.php
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);
}
}
}
}

}
39 changes: 39 additions & 0 deletions DotBlue/tests/ReturnSpacingSniff.phpt
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();
14 changes: 14 additions & 0 deletions DotBlue/tests/invalid/ReturnSpacing_1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

function foo() {

return 1;
}



function bar() {


return 1;
}
14 changes: 14 additions & 0 deletions DotBlue/tests/invalid/ReturnSpacing_2.php
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;
}
18 changes: 18 additions & 0 deletions DotBlue/tests/invalid/ReturnSpacing_3.php
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;
}
11 changes: 11 additions & 0 deletions DotBlue/tests/valid/ReturnSpacing_1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

function foo() {
return 1;
}



function bar() {
return 1;
}
16 changes: 16 additions & 0 deletions DotBlue/tests/valid/ReturnSpacing_2.php
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;
}
16 changes: 16 additions & 0 deletions DotBlue/tests/valid/ReturnSpacing_3.php
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;
}

0 comments on commit a9f9db6

Please sign in to comment.