diff --git a/src/Codor/Sniffs/Files/FunctionLengthSniff.php b/src/Codor/Sniffs/Files/FunctionLengthSniff.php new file mode 100644 index 0000000..1e56028 --- /dev/null +++ b/src/Codor/Sniffs/Files/FunctionLengthSniff.php @@ -0,0 +1,38 @@ +getTokens(); + $token = $tokens[$stackPtr]; + + // Skip function without body. + if (isset($token['scope_opener']) === false) { + return 0; + } + + $firstToken = $tokens[$token['scope_opener']]; + $lastToken = $tokens[$token['scope_closer']]; + $length = $lastToken['line'] - $firstToken['line']; + + if ($length > $this->maxLength) { + $tokenType = strtolower(substr($token['type'], 2)); + $error = "Function is {$length} lines. Must be {$this->maxLength} lines or fewer."; + $phpcsFile->addError($error, $stackPtr, sprintf('%sTooBig', ucfirst($tokenType))); + } + } +} diff --git a/tests/Sniffs/Files/Assets/FunctionLengthSniff/FunctionLengthSniff.inc b/tests/Sniffs/Files/Assets/FunctionLengthSniff/FunctionLengthSniff.inc new file mode 100644 index 0000000..3fd20db --- /dev/null +++ b/tests/Sniffs/Files/Assets/FunctionLengthSniff/FunctionLengthSniff.inc @@ -0,0 +1,62 @@ +detectErrorCountInFileForSniff( + __DIR__.'/Assets/FunctionLengthSniff/FunctionLengthSniff.inc', + 'Codor.Files.FunctionLength' + ); + + $this->assertSame(2, $errorCount); + } +} \ No newline at end of file