-
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 #30 from bmitch/Issue#22
Fixes #22
- Loading branch information
Showing
4 changed files
with
297 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
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,49 @@ | ||
<?php | ||
|
||
namespace Codor\Sniffs\Classes; | ||
|
||
use PHP_CodeSniffer_Sniff; | ||
use PHP_CodeSniffer_File; | ||
|
||
class ClassLengthSniff implements PHP_CodeSniffer_Sniff | ||
{ | ||
|
||
/** | ||
* The maximum number of lines a class | ||
* should have. | ||
* @var integer | ||
*/ | ||
protected $maxLength = 200; | ||
|
||
/** | ||
* Returns the token types that this sniff is interested in. | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [T_CLASS]; | ||
} | ||
|
||
/** | ||
* Processes the tokens that this sniff is interested in. | ||
* | ||
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. | ||
* @param integer $stackPtr The position in the stack where | ||
* the token was found. | ||
* @return void | ||
*/ | ||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
$token = $tokens[$stackPtr]; | ||
|
||
$openParenthesis = $tokens[$token['scope_opener']]; | ||
$closedParenthesis = $tokens[$token['scope_closer']]; | ||
|
||
$length = $closedParenthesis['line'] - $openParenthesis['line']; | ||
|
||
if ($length > $this->maxLength) { | ||
$phpcsFile->addError("Class is {$length} lines. Must be {$this->maxLength} lines or fewer.", $stackPtr); | ||
} | ||
} | ||
} |
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,221 @@ | ||
<?php | ||
|
||
class foo {} | ||
|
||
class bar | ||
{ | ||
|
||
} | ||
|
||
class baz | ||
{ | ||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
class foobar { | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace Codor\Tests\Sniffs\ControlStructures; | ||
|
||
use Codor\Tests\BaseTestCase; | ||
|
||
/** @group Classes */ | ||
class ClassLengthSniffTest extends BaseTestCase | ||
{ | ||
|
||
public function setup() | ||
{ | ||
parent::setup(); | ||
|
||
$this->runner->setSniff('Codor.Classes.ClassLength')->setFolder(__DIR__.'/'); | ||
} | ||
|
||
public function testSniff() | ||
{ | ||
$results = $this->runner->sniff('ClassLengthSniff.inc'); | ||
$this->assertSame(1, $results->getErrorCount()); | ||
$this->assertSame(0, $results->getWarningCount()); | ||
} | ||
} |