ClassLengthSniff   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 10
c 1
b 0
f 0
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 12 2
A register() 0 3 1
1
<?php declare(strict_types = 1);
2
3
namespace Codor\Sniffs\Classes;
4
5
use PHP_CodeSniffer\Sniffs\Sniff as PHP_CodeSniffer_Sniff;
6
use PHP_CodeSniffer\Files\File as 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
    public $maxLength = 200;
17
18
    /**
19
     * Returns the token types that this sniff is interested in.
20
     * @return array
21
     */
22
    public function register(): array
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 int                  $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
        
40
        $openParenthesis = $tokens[$token['scope_opener']];
41
        $closedParenthesis = $tokens[$token['scope_closer']];
42
43
        $length = $closedParenthesis['line'] - $openParenthesis['line'];
44
45
        if ($length > $this->maxLength) {
46
            $phpcsFile->addError("Class is {$length} lines. Must be {$this->maxLength} lines or fewer.", $stackPtr, __CLASS__);
47
        }
48
    }
49
}
50