Skip to content

Commit

Permalink
Merge pull request #30 from bmitch/Issue#22
Browse files Browse the repository at this point in the history
Fixes #22
  • Loading branch information
bmitch authored Dec 6, 2016
2 parents a3d40e0 + 02674ed commit f89faa1
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ Functions/methods must have no more than 3 parameters.

### Codor.Files.ReturnNull ###
Functions/methods must not return `null`.

### Codor.Classes.ClassLength ###
Classes must be 200 lines of code or fewer.
49 changes: 49 additions & 0 deletions src/Codor/Sniffs/Classes/ClassLengthSniff.php
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);
}
}
}
221 changes: 221 additions & 0 deletions tests/Sniffs/Classes/ClassLengthSniff.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
<?php

class foo {}

class bar
{

}

class baz
{





}

class foobar {









































































































































































































}
24 changes: 24 additions & 0 deletions tests/Sniffs/Classes/ClassLengthSniffTest.php
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());
}
}

0 comments on commit f89faa1

Please sign in to comment.