Last active
December 22, 2016 17:36
-
-
Save khepin/28fb10e27c058b27009085fa8eff8940 to your computer and use it in GitHub Desktop.
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
<?php | |
class CreativeMarket_Sniffs_Functions_FunctionLengthSniff implements PHP_CodeSniffer_Sniff | |
{ | |
const MAX_NUM_LINES = 30; | |
/** | |
* Returns an array of tokens this test wants to listen for. | |
* | |
* @return array | |
*/ | |
public function register() | |
{ | |
return array(T_FUNCTION); | |
} | |
/** | |
* Checks if a function or method is too long | |
* | |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | |
* @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(); | |
$token = $tokens[$stackPtr]; | |
if (!isset($token['scope_closer'])) { | |
// There's no scope. It's either an interface function definition | |
// or an abstract method | |
return; | |
} | |
$startPtr = $token['scope_opener']; | |
$startLine = $tokens[$startPtr]['line']; | |
$endPtr = $token['scope_closer']; | |
$endLine = $tokens[$endPtr]['line']; | |
if (($endLine - $startLine) > self::MAX_NUM_LINES) { | |
$namePtr = $phpcsFile->findNext(T_STRING, $stackPtr); | |
$name = $tokens[$namePtr]['content']; | |
$phpcsFile->addError('Function "%s" is too long with %d lines', $stackPtr, 'Found', [$name, $endLine - $startLine]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment