Completed
Pull Request — master (#19)
by
unknown
06:03
created

AbstractSniff::getBaseFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 109
    public function register(): array
59
    {
60 109
        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 109
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr): void
72
    {
73 109
        $this->baseFile = $phpcsFile;
74 109
        $this->file = new File($phpcsFile);
75 109
        $this->tokens = $phpcsFile->getTokens();
76 109
        $this->listenerPtr = $stackPtr;
77 109
        $this->listenerToken = $this->tokens[$stackPtr];
78
79 109
        $this->processToken();
80 109
    }
81
82
    /**
83
     * Getter for CodeSniffer file
84
     *
85
     * @return PHP_CodeSniffer_File The CodeSniffer file
86
     */
87
    public function getBaseFile(): PHP_CodeSniffer_File
88
    {
89
        return $this->baseFile;
90
    }
91
92
    /**
93
     * Getter for deferred PHP_CodeSniffer class.
94
     *
95
     * @return File The deferred CodeSniffer file
96
     */
97 109
    public function getFile(): File
98
    {
99 109
        return $this->file;
100
    }
101
102
    /**
103
     * Getter for token stack.
104
     *
105
     * @return array Array of token data indexed by pointer
106
     */
107
    public function getTokens(): array
108
    {
109
        return $this->tokens;
110
    }
111
112
    /**
113
     * Getter for listener pointer.
114
     *
115
     * @return int Returns pointer of the listened token
116
     */
117 109
    public function getListenerPointer(): int
118
    {
119 109
        return $this->listenerPtr;
120
    }
121
122
    /**
123
     * Getter for listener token data.
124
     *
125
     * @return array Returns token data of the listened token
126
     */
127 109
    public function getListenerToken(): array
128
    {
129 109
        return $this->listenerToken;
130
    }
131
132
    /**
133
     * Returns an array of registered tokens.
134
     *
135
     * @return int[] Returns array of tokens to listen for
136
     */
137
    abstract public function getRegisteredTokens(): array;
138
139
    /**
140
     * Processes a found registered token.
141
     *
142
     * @return void
143
     */
144
    abstract public function processToken(): void;
145
}
146