-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractSniff.php
More file actions
217 lines (204 loc) · 7.13 KB
/
Copy pathAbstractSniff.php
File metadata and controls
217 lines (204 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* AbstractSniff
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Marcel Eichner // foobugs <marcel.eichner@foobugs.com>
* @copyright 2012 foobugs oelke & eichner GbR
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @link https://github.com/foobugs/PHP53to54
* @since 1.0-beta
*/
/**
* AbstractSniff
*
* Jaggers base class for Sniffs that provides methods that can be used in
* various Sniffs.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Marcel Eichner // foobugs <marcel.eichner@foobugs.com>
* @author Maik Penz // <maik@phpkuh.de>
* @copyright 2012 foobugs oelke & eichner GbR
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @link https://github.com/foobugs/PHP53to54
* @since 1.0-beta
*/
abstract class PHP53to54_AbstractSniff
implements PHP_CodeSniffer_Sniff
{
/**
* Cache for storing last namespace names found in files while
* parsing them.
*
* @var array(string = string)
*/
protected $lastNamespacesPerFile = null;
/**
*
* @var array
*/
protected $functionCallParametersMap = array(T_CONSTANT_ENCAPSED_STRING, T_VARIABLE, T_NULL, T_LNUMBER, T_DNUMBER);
/**
*
* @param string $propertyName
* @return PHP53to54_AbstractSniff - fluent interface
*/
protected function parseArrayProperty($propertyName)
{
if (!is_string($this->{$propertyName})) {
return true;
}
$this->{$propertyName} = preg_split('/[\s,\r\n]/', $this->{$propertyName});
$this->{$propertyName} = array_map('trim', $this->{$propertyName});
$this->{$propertyName} = array_filter($this->{$propertyName});
return $this;
}
/**
*
* @param PHP_CodeSniffer_File $phpcsFile
* @return string
*/
protected function getLastNamespaceForFile(PHP_CodeSniffer_File $phpcsFile)
{
$filename = $phpcsFile->getFilename();
if (empty($this->lastNamespacesPerFile[$filename])) {
return false;
}
return $this->lastNamespacesPerFile[$filename];
}
/**
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return boolean - always true
*/
protected function processNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
$namspaceToken = $tokens[$phpcsFile->findNext(array(T_STRING), ($stackPtr + 1), null, false)];
$this->lastNamespacesPerFile[$phpcsFile->getFilename()] = strtolower($namspaceToken['content']);
return true;
}
/**
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return array - false on error
*/
public function getFunctionDefinitionParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if (!isset($tokens[$openBracket]['parenthesis_closer'])) {
return false;
}
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
$parameters = array();
$tmpPtr = $openBracket;
$parameterIndex = 1;
while (($tmpPtr = $phpcsFile->findNext(array(T_VARIABLE), $tmpPtr)) !== false) {
if ($tmpPtr > $closeBracket) break;
$parameters[$parameterIndex] = $tokens[$tmpPtr];
$tmpPtr++;
$parameterIndex++;
}
return $parameters;
}
/**
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return boolean
*/
public function isFunction(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
return false;
}
if (isset($tokens[$openBracket]['parenthesis_closer']) === false) {
return false;
}
return true;
}
/**
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return boolean
*/
public function isFunctionCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$search = PHP_CodeSniffer_Tokens::$emptyTokens;
$search[] = T_BITWISE_AND;
$previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true);
if ($tokens[$previous]['code'] === T_FUNCTION) {
return false;
}
return true;
}
/**
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return array - false on error
*/
public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if (!isset($tokens[$openBracket]['parenthesis_closer'])) {
return false;
}
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
$parameters = array();
$tmpPtr = $openBracket;
$parameterIndex = 1;
while (($tmpPtr = $phpcsFile->findNext($this->functionCallParametersMap, $tmpPtr)) !== false) {
if ($tmpPtr > $closeBracket) {
break;
}
$parameters[$parameterIndex] = $tokens[$tmpPtr];
$tmpPtr++;
$parameterIndex++;
}
return $parameters;
}
/**
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
* @param int $index
*
* @return mixed - false on error
*/
public function getFunctionCallParameterByIndex(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $index)
{
$i = 0;
foreach($this->getFunctionCallParameters($phpcsFile, $stackPtr) as $parameter) {
if ($i == $index) {
return $parameter;
}
$i++;
}
return false;
}
}