Completed
Pull Request — master (#19)
by
unknown
02:45
created

AbstractSniff::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\CodeSniffer;
6
7
use PHP_CodeSniffer_File;
8
use PHP_CodeSniffer_Sniff;
9
10
/**
11
 * Class AbstractSniff
12
 *
13
 * @package BestIt\Sniffs
14
 * @author Nick Lubisch <[email protected]>
15
 */
16
abstract class AbstractSniff implements PHP_CodeSniffer_Sniff
17
{
18
    /**
19
     * The CodeSniffer file.
20
     *
21
     * @var PHP_CodeSniffer_File
22
     */
23
    private $baseFile;
24
25
    /**
26
     * A deferred class of PHP_CodeSniffer_File
27
     *
28
     * @var File
29
     */
30
    private $file;
31
32
    /**
33
     * Stack of all tokens found in the file
34
     *
35
     * @var array
36
     */
37
    private $tokens;
38
39
    /**
40
     * Pointer of the listened token.
41
     *
42
     * @var int
43
     */
44
    private $listenerPtr;
45
46
    /**
47
     * Token data of listened token
48
     *
49
     * @var array
50
     */
51
    private $listenerToken;
52
53
    /**
54
     * Registers the tokens that this sniff wants to listen for.
55
     *
56
     * @return int[] Returns an array of tokens
57
     */
58 121
    public function register(): array
59
    {
60 121
        return $this->getRegisteredTokens();
61
    }
62
63
    /**
64
     * Called when one of the token types that this sniff is listening for is found.
65
     *
66
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the token was found.
67
     * @param int $stackPtr The position in the PHP_CodeSniffer file's token stack where the token was found.
68
     *
69
     * @return void
70
     */
71 121
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr): void
72
    {
73 121
        $this->baseFile = $phpcsFile;
74 121
        $this->file = new File($phpcsFile);
75 121
        $this->tokens = $phpcsFile->getTokens();
76 121
        $this->listenerPtr = $stackPtr;
77 121
        $this->listenerToken = $this->tokens[$stackPtr];
78
79 121
        $this->processToken();
80 121
    }
81
82
    /**
83
     * Getter for deferred PHP_CodeSniffer class.
84
     *
85
     * @return File The deferred CodeSniffer file
86
     */
87 121
    public function getFile(): File
88
    {
89 121
        return $this->file;
90
    }
91
92
    /**
93
     * Getter for listener pointer.
94
     *
95
     * @return int Returns pointer of the listened token
96
     */
97 121
    public function getListenerPointer(): int
98
    {
99 121
        return $this->listenerPtr;
100
    }
101
102
    /**
103
     * Getter for listener token data.
104
     *
105
     * @return array Returns token data of the listened token
106
     */
107 121
    public function getListenerToken(): array
108
    {
109 121
        return $this->listenerToken;
110
    }
111
112
    /**
113
     * Returns an array of registered tokens.
114
     *
115
     * @return int[] Returns array of tokens to listen for
116
     */
117
    abstract public function getRegisteredTokens(): array;
118
119
    /**
120
     * Processes a found registered token.
121
     *
122
     * @return void
123
     */
124
    abstract public function processToken(): void;
125
}
126