Skip to content

Commit

Permalink
Merge pull request #155 from cakephp/blank-line-before-return
Browse files Browse the repository at this point in the history
Add sniff to ensure blank line before return statement.
  • Loading branch information
ADmad authored Jul 13, 2016
2 parents fe95558 + db81dc8 commit 77d8483
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
73 changes: 73 additions & 0 deletions CakePHP/Sniffs/Formatting/BlankLineBeforeReturnSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Throws errors if there's no blank line before return statements.
*
* @author Authors <[email protected]>
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/escapestudios/Symfony2-coding-standard
*/
class CakePHP_Sniffs_Formatting_BlankLineBeforeReturnSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);

/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_RETURN);
}

/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$current = $stackPtr;
$previousLine = $tokens[$stackPtr]['line'] - 1;
$prevLineTokens = array();

while ($current >= 0 && $tokens[$current]['line'] >= $previousLine) {
if ($tokens[$current]['line'] == $previousLine
&& $tokens[$current]['type'] !== 'T_WHITESPACE'
&& $tokens[$current]['type'] !== 'T_COMMENT'
&& $tokens[$current]['type'] !== 'T_DOC_COMMENT_CLOSE_TAG'
&& $tokens[$current]['type'] !== 'T_DOC_COMMENT_WHITESPACE'
) {
$prevLineTokens[] = $tokens[$current]['type'];
}
$current--;
}

if (isset($prevLineTokens[0])
&& ($prevLineTokens[0] === 'T_OPEN_CURLY_BRACKET'
|| $prevLineTokens[0] === 'T_COLON')
) {
return;
} elseif (count($prevLineTokens) > 0) {
$phpcsFile->addError(
'Missing blank line before return statement',
$stackPtr
);
}

return;
}
}
11 changes: 11 additions & 0 deletions CakePHP/tests/files/blank_line_before_return_fail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* [invalidFunctionReturnOne description]
*
* @return null
*/
function invalidFunctionReturnOne()
{
echo "";
return null;
}
75 changes: 75 additions & 0 deletions CakePHP/tests/files/blank_line_before_return_pass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* [validFunctionReturnOne description]
*
* @return null
*/
function validFunctionReturnOne()
{
return null;
}

/**
* [validFunctionReturnTwo description]
*
* @return null|bool
*/
function validFunctionReturnTwo()
{
if ($a) {
return true;
}

return null;
}

/**
* [validFunctionReturnThree description]
*
* @return null
*/
function validFunctionReturnThree()
{
echo "";

return null;
}

/**
* [validFunctionReturnFour description]
*
* @return null
*/
function validFunctionReturnFour()
{
// comment
return null;
}

/**
* [validFunctionReturnFive description]
*
* @return null
*/
function validFunctionReturnFive()
{
/**
* multi-line
*/
return null;
}

/**
* [validFunctionReturnSix description]
*
* @return null|bool
*/
function validFunctionReturnSix()
{
switch ($condition) {
case 'foo':
return true;
default:
return false;
}
}

0 comments on commit 77d8483

Please sign in to comment.