Skip to content

Commit

Permalink
Merge pull request #11 from SalesChamp/matus/return-spacing-sniff
Browse files Browse the repository at this point in the history
Return spacing sniff
  • Loading branch information
mochja authored Oct 20, 2017
2 parents 7e9091f + a9f9db6 commit c64e0b4
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DotBlue/Sniffs/WhiteSpace/FixEmptyLines.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private static function fixSpacing(PHP_CodeSniffer_File $phpcsFile, $updateFrom,
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->replaceToken($i, str_repeat($phpcsFile->eolChar, $expected - 2));
$phpcsFile->fixer->replaceToken($i, str_repeat($phpcsFile->eolChar, max($expected - 2, 0)));
$phpcsFile->fixer->endChangeset();
}
}
Expand Down
1 change: 1 addition & 0 deletions DotBlue/Sniffs/WhiteSpace/PropertySpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$nextCode = $tokens[$nextModifier]['code'];
if ($thisCode !== $nextCode && !in_array($nextCode, self::ALLOWED_NEXT_MODIFIER[$thisCode])) {
$phpcsFile->addError("Property visibility must be ordered from private to protected to public, found %s, next is %s", $stackPtr, 'VisibilityOrder', [token_name($thisCode), token_name($nextCode)]);

return;
}
}
Expand Down
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();
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Bar
public function foo()
{
$bar = NULL;

return function () use ($bar) {};
}

Expand Down
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Bar
public function foo()
{
$bar = NULL;

return function () use ($bar) {};
}

Expand Down
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 c64e0b4

Please sign in to comment.