|
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 PropertyDeclarationSniff implements PHP_CodeSniffer_Sniff |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The decalred member variables in the class. |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $memberVars = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The referenced member variables in the class. |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $referencedMemberVars = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Visibility Tokens. |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $visibilityTokens = ['T_PRIVATE', 'T_PUBLIC', 'T_PROTECTED']; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Holds the value if the class is extended or not. |
|
31
|
|
|
* @var boolean |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $isExtendedClass = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns the token types that this sniff is interested in. |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
|
|
public function register(): array |
|
40
|
|
|
{ |
|
41
|
|
|
return [T_CLASS]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Processes the tokens that this sniff is interested in. |
|
46
|
|
|
* |
|
47
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
48
|
|
|
* @param int $stackPtr The position in the stack where |
|
49
|
|
|
* the token was found. |
|
50
|
|
|
* @return void |
|
51
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
|
52
|
|
|
*/ |
|
53
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->memberVars = []; |
|
56
|
|
|
$this->referencedMemberVars = []; |
|
57
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
58
|
|
|
foreach ($tokens as $index => $token) { |
|
59
|
|
|
$this->handleToken($tokens, $index); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$undeclaredMemberVars = array_diff($this->referencedMemberVars, $this->memberVars); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($undeclaredMemberVars as $undeclaredVariable) { |
|
65
|
|
|
$this->handleError($undeclaredVariable, $phpcsFile, $stackPtr); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Handle the incoming token. |
|
71
|
|
|
* @param array $tokens List of tokens. |
|
72
|
|
|
* @param integer $index Current token index. |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function handleToken($tokens, $index) |
|
76
|
|
|
{ |
|
77
|
|
|
$token = $tokens[$index]; |
|
78
|
|
|
|
|
79
|
|
|
if ($token['type'] === 'T_EXTENDS') { |
|
80
|
|
|
$this->isExtendedClass = true; |
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($this->isVisibilityToken($token)) { |
|
85
|
|
|
$this->handleVisibilityToken($tokens, $index); |
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
if ($token['type'] === 'T_OBJECT_OPERATOR') { |
|
91
|
|
|
$this->handleObjectOperatorToken($tokens, $index); |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Determines if the provided token is a visiblity token. |
|
98
|
|
|
* @param array $token Token data. |
|
99
|
|
|
* @return boolean |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function isVisibilityToken($token): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return in_array($token['type'], $this->visibilityTokens); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Handles the logic for a visiblity type token. |
|
108
|
|
|
* @param array $tokens List of tokens. |
|
109
|
|
|
* @param integer $index Current token index. |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function handleVisibilityToken($tokens, $index) |
|
113
|
|
|
{ |
|
114
|
|
|
$possibleVariable = $tokens[$index + 2]; |
|
115
|
|
|
if ($possibleVariable['type'] !== 'T_VARIABLE') { |
|
116
|
|
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
$this->memberVars[] = str_replace('$', '', $possibleVariable['content']); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Handles the logic for an object operator token. |
|
123
|
|
|
* @param array $tokens List of tokens. |
|
124
|
|
|
* @param integer $index Current token index. |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function handleObjectOperatorToken($tokens, $index) |
|
128
|
|
|
{ |
|
129
|
|
|
$previousToken = $tokens[$index-1]; |
|
130
|
|
|
$nextToken = $tokens[$index+1]; |
|
131
|
|
|
$tokenAfterNext = $tokens[$index+2]; |
|
132
|
|
|
|
|
133
|
|
|
if ($previousToken['content'] !== '$this') { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if (($tokenAfterNext['type'] !== 'T_WHITESPACE') && ($tokenAfterNext['type'] !== 'T_EQUAL')) { |
|
138
|
|
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$this->referencedMemberVars[] = $nextToken['content']; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* If the class is extended from another class then a warning is produced. |
|
146
|
|
|
* Otherwise an error is produced. |
|
147
|
|
|
* @param string $undeclaredVariable Name of the undeclared member variable. |
|
148
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile PHP Code Sniffer File. |
|
149
|
|
|
* @param integer $index Index of where the undeclared variable was found in the file. |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function handleError($undeclaredVariable, $phpcsFile, $index) |
|
153
|
|
|
{ |
|
154
|
|
|
if ($this->isExtendedClass) { |
|
155
|
|
|
$phpcsFile->addWarning("Class contains undeclared property {$undeclaredVariable}.", $index); |
|
156
|
|
|
return; |
|
157
|
|
|
} |
|
158
|
|
|
$phpcsFile->addError("Class contains undeclared property {$undeclaredVariable}.", $index); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|