ExtendsSniff   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 3 1
A process() 0 5 2
A handleToken() 0 7 2
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 ExtendsSniff implements PHP_CodeSniffer_Sniff
9
{
10
    /**
11
     * The file where the token was found.
12
     * @var PHP_CodeSniffer_File
13
     */
14
    private $phpcsFile;
15
16
    /**
17
     * Returns the token types that this sniff is interested in.
18
     * @return array
19
     */
20
    public function register(): array
21
    {
22
        return [T_CLASS];
23
    }
24
25
    /**
26
     * Processes the tokens that this sniff is interested in.
27
     *
28
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
29
     * @param int                  $stackPtr  The position in the stack where
30
     *                                        the token was found.
31
     * @return void
32
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
33
     */
34
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
35
    {
36
        $this->phpcsFile = $phpcsFile;
37
        foreach ($phpcsFile->getTokens() as $token) {
38
            $this->handleToken($token);
39
        }
40
    }
41
42
    /**
43
     * Handle the incoming token.
44
     * @param  array $token Token data.
45
     * @return void
46
     */
47
    protected function handleToken($token)
48
    {
49
        if ($token['type'] !== 'T_EXTENDS') {
50
            return;
51
        }
52
        $warning = "Class extends another class - consider composition over inheritance.";
53
        $this->phpcsFile->addWarning($warning, $token['line'], __CLASS__);
54
    }
55
}
56