|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Codor\Sniffs\Classes; |
|
4
|
|
|
|
|
5
|
|
|
use PHP_CodeSniffer_Sniff; |
|
6
|
|
|
use PHP_CodeSniffer_File; |
|
7
|
|
|
|
|
8
|
|
|
class FinalPrivateSniff implements PHP_CodeSniffer_Sniff |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Returns the token types that this sniff is interested in. |
|
12
|
|
|
* @return array |
|
13
|
|
|
*/ |
|
14
|
|
|
public function register(): array |
|
15
|
|
|
{ |
|
16
|
|
|
return [T_CLASS]; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Is the class marked as final? |
|
21
|
|
|
* @var boolean |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $classIsMarkedFinal = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* List of protected methods found. |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $protectedMethods = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* List of protected variables found. |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $protectedVariables = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Processes the tokens that this sniff is interested in. |
|
39
|
|
|
* |
|
40
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
41
|
|
|
* @param int $stackPtr The position in the stack where |
|
42
|
|
|
* the token was found. |
|
43
|
|
|
* @return void |
|
44
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
|
45
|
|
|
*/ |
|
46
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
47
|
|
|
{ |
|
48
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($tokens as $index => $token) { |
|
51
|
|
|
$this->handleToken($tokens, $index); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->handleErrors($phpcsFile, $stackPtr); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Handle the incoming token. |
|
59
|
|
|
* @param array $tokens List of tokens. |
|
60
|
|
|
* @param integer $index Current token index. |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function handleToken($tokens, $index) |
|
64
|
|
|
{ |
|
65
|
|
|
$tokenType = $tokens[$index]['type']; |
|
66
|
|
|
|
|
67
|
|
|
if ($tokenType === 'T_FINAL') { |
|
68
|
|
|
$this->classIsMarkedFinal = true; |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (! $this->classIsMarkedFinal) { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($tokenType !== 'T_PROTECTED') { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->handleFoundProtectedElement($tokens, $index); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Handles found protected method or variable within |
|
85
|
|
|
* a final class. |
|
86
|
|
|
* @param array $tokens List of tokens. |
|
87
|
|
|
* @param integer $index Current token index. |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function handleFoundProtectedElement($tokens, $index) |
|
91
|
|
|
{ |
|
92
|
|
|
$type = $tokens[$index+2]['type']; |
|
93
|
|
|
|
|
94
|
|
|
if ($type === 'T_VARIABLE') { |
|
95
|
|
|
$variableName = $tokens[$index+2]['content']; |
|
96
|
|
|
$this->protectedVariables[] = $variableName; |
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$methodName = $tokens[$index+4]['content']; |
|
101
|
|
|
$this->protectedMethods[] = $methodName; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Handle any errors. |
|
106
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
107
|
|
|
* @param int $stackPtr The position in the stack where. |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function handleErrors($phpcsFile, $stackPtr) |
|
111
|
|
|
{ |
|
112
|
|
|
foreach ($this->protectedMethods as $protectedMethod) { |
|
113
|
|
|
$this->handleProtectedMethod($protectedMethod, $phpcsFile, $stackPtr); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
foreach ($this->protectedVariables as $protectedVariable) { |
|
117
|
|
|
$this->handleProtectedVariable($protectedVariable, $phpcsFile, $stackPtr); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Add a protected method found error. |
|
123
|
|
|
* @param string $method Name of the method found. |
|
124
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
125
|
|
|
* @param int $stackPtr The position in the stack where. |
|
126
|
|
|
* @return void |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function handleProtectedMethod($method, $phpcsFile, $stackPtr) |
|
129
|
|
|
{ |
|
130
|
|
|
$phpcsFile->addError("Final Class contains a protected method {$method} - should be private.", $stackPtr); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Add a protected variable found error. |
|
135
|
|
|
* @param string $variable Name of the variable found. |
|
136
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
137
|
|
|
* @param int $stackPtr The position in the stack where. |
|
138
|
|
|
* @return void |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function handleProtectedVariable($variable, $phpcsFile, $stackPtr) |
|
141
|
|
|
{ |
|
142
|
|
|
$phpcsFile->addError("Final Class contains a protected variable {$variable} - should be private.", $stackPtr); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|