Completed
Pull Request — master (#30)
by Bill
01:45
created

ClassLengthSniff::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Codor\Sniffs\Classes;
4
5
use PHP_CodeSniffer_Sniff;
6
use PHP_CodeSniffer_File;
7
8
class ClassLengthSniff implements PHP_CodeSniffer_Sniff
9
{
10
11
    /**
12
     * The maximum number of lines a class
13
     * should have.
14
     * @var integer
15
     */
16
    protected $maxLength = 200;
17
18
    /**
19
     * Returns the token types that this sniff is interested in.
20
     * @return array
21
     */
22
    public function register()
23
    {
24
        return [T_CLASS];
25
    }
26
27
    /**
28
     * Processes the tokens that this sniff is interested in.
29
     *
30
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
31
     * @param integer              $stackPtr  The position in the stack where
32
     *                                    the token was found.
33
     * @return void
34
     */
35
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
36
    {
37
        $tokens = $phpcsFile->getTokens();
38
        $token = $tokens[$stackPtr];
39
        $classLength = $token['scope_closer'] - $token['scope_opener'] - 2;
40
41
        if ($classLength > $this->maxLength) {
42
            $phpcsFile->addError("Class is {$classLength} lines. Must be {$this->maxLength} lines or fewer.", $stackPtr);
43
        }
44
    }
45
}
46