-
Notifications
You must be signed in to change notification settings - Fork 8
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 #8 from bmitch/functionLength
Added Function Length Sniff
- Loading branch information
Showing
3 changed files
with
122 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,38 @@ | ||
<?php | ||
|
||
namespace Codor\Sniffs\Files; | ||
|
||
use PHP_CodeSniffer_Sniff; | ||
use PHP_CodeSniffer_File; | ||
|
||
class FunctionLengthSniff implements PHP_CodeSniffer_Sniff | ||
{ | ||
|
||
protected $maxLength = 20; | ||
|
||
public function register() | ||
{ | ||
return [T_FUNCTION]; | ||
} | ||
|
||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->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))); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
tests/Sniffs/Files/Assets/FunctionLengthSniff/FunctionLengthSniff.inc
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,62 @@ | ||
<?php | ||
|
||
function someFunctionLongerThan21Lines() | ||
{ | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
function someFunctionLessThan21Lines() | ||
{ | ||
|
||
} | ||
|
||
class SomeClass | ||
{ | ||
public function someMethodLongerThan21Lines() | ||
{ | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
public function someMethodLessThan21Lines() | ||
{ | ||
|
||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace Codor\Tests\Sniffs\ControlStructures; | ||
|
||
use Codor\Tests\CodeSnifferRunner; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** @group Files */ | ||
class FunctionLengthSniffTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_detects_functions_that_are_over_the_max_allowed() | ||
{ | ||
$codeSnifferRunner = new CodeSnifferRunner(); | ||
$errorCount = $codeSnifferRunner->detectErrorCountInFileForSniff( | ||
__DIR__.'/Assets/FunctionLengthSniff/FunctionLengthSniff.inc', | ||
'Codor.Files.FunctionLength' | ||
); | ||
|
||
$this->assertSame(2, $errorCount); | ||
} | ||
} |