-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from cakephp/blank-line-before-return
Add sniff to ensure blank line before return statement.
- Loading branch information
Showing
3 changed files
with
159 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,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; | ||
} | ||
} |
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 | ||
/** | ||
* [invalidFunctionReturnOne description] | ||
* | ||
* @return null | ||
*/ | ||
function invalidFunctionReturnOne() | ||
{ | ||
echo ""; | ||
return null; | ||
} |
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,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; | ||
} | ||
} |