-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPHPCodeValidator.php
More file actions
243 lines (215 loc) · 6.7 KB
/
PHPCodeValidator.php
File metadata and controls
243 lines (215 loc) · 6.7 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
namespace CleantalkSP\Common\Scanner\HeuristicAnalyser\Modules;
class PHPCodeValidator
{
/**
* The result of checks
*
* @var array
*/
public $check_list_result;
/**
* The tokens to be validated.
*
* @var Tokens
*/
private $tokens;
/**
* PHPCodeValidator constructor.
*
* @param Tokens $tokens The tokens to be validated.
*/
public function __construct($tokens)
{
$this->tokens = $tokens;
}
/**
* Checks if the PHP code is valid.
*
* @return bool Returns true if the PHP code is valid, false otherwise.
* @psalm-suppress PossiblyUnusedMethod
*/
public function isValidPHPCode()
{
$this->hasCorrectPHPOpenTags();
$this->checkBraces();
$this->checkBrackets();
$this->checkParentheses();
$this->checkSingleQuotes();
$this->checkDoubleQuotes();
$this->checkDigitsStartedVariables();
return empty($this->check_list_result);
}
/**
* Checks if the count of left and right braces and brackets are equal.
*
* @return bool Returns true if the count is equal, false otherwise.
*/
private function checkBraces()
{
$braces_l_count = 0;
$braces_r_count = 0;
foreach ( $this->tokens as $token ) {
if ( $token[0] === '__SERV' ) {
if ( $token[1] === '(' ) {
$braces_l_count++;
}
if ( $token[1] === ')' ) {
$braces_r_count++;
}
}
}
if ( $braces_l_count !== $braces_r_count ) {
$this->check_list_result[__FUNCTION__] = 'Braces () count is not equal ' . $braces_l_count . ' != ' . $braces_r_count;
return false;
}
return true;
}
/**
* Checks if the count of left and right brackets are equal.
*
* @return bool Returns true if the count is equal, false otherwise.
*/
private function checkBrackets()
{
$brackets_l_count = 0;
$brackets_r_count = 0;
foreach ( $this->tokens as $token ) {
if ( $token[0] === '__SERV' ) {
if ( $token[1] === '[' ) {
$brackets_l_count++;
}
if ( $token[1] === ']' ) {
$brackets_r_count++;
}
}
}
if ( $brackets_l_count !== $brackets_r_count ) {
$this->check_list_result[__FUNCTION__] = 'Brackets [] count is not equal';
return false;
}
return true;
}
/**
* Checks if the count of left and right parentheses are equal.
*
* @return bool Returns true if the count is equal, false otherwise.
*/
private function checkParentheses()
{
$parentheses_l_count = 0;
$parentheses_r_count = 0;
/**
* init opening and closing tokens, key is token type, value is string to search
*/
$opening_tokens = array(
'__SERV' => '{',
'T_CURLY_OPEN' => '{',
'T_DOLLAR_OPEN_CURLY_BRACES' => '${',
'T_STRING_VARNAME' => '{'
);
$closing_tokens = array(
'__SERV' => '}',
'T_STRING_VARNAME' => '}'
);
foreach ( $this->tokens as $token ) {
if ( isset($opening_tokens[$token[0]]) &&
$token[1] === $opening_tokens[$token[0]] ) {
$parentheses_l_count++;
}
if ( isset($closing_tokens[$token[0]]) &&
$token[1] === $closing_tokens[$token[0]] ) {
$parentheses_r_count++;
}
}
if ( $parentheses_l_count !== $parentheses_r_count ) {
$this->check_list_result[__FUNCTION__] = 'Parentheses {} count is not equal';
return false;
}
return true;
}
/**
* Checks if the count of single quotes are even.
*
* @return bool Returns true if the count is even, false otherwise.
*/
private function checkSingleQuotes()
{
$single_quotes_count = 0;
foreach ( $this->tokens as $token ) {
if ( $token[0] === '__SERV' ) {
if ( $token[1] === "'" ) {
$single_quotes_count++;
}
}
}
if ( $single_quotes_count % 2 !== 0 ) {
$this->check_list_result[__FUNCTION__] = 'Single quotes count is not even';
return false;
}
return true;
}
/**
* Checks if the count of double quotes are even.
*
* @return bool Returns true if the count is even, false otherwise.
*/
private function checkDoubleQuotes()
{
$double_quotes_count = 0;
foreach ( $this->tokens as $token ) {
if ( $token[0] === '__SERV' ) {
if ( $token[1] === '"' ) {
$double_quotes_count++;
}
}
}
if ( $double_quotes_count % 2 !== 0 ) {
$this->check_list_result[__FUNCTION__] = 'Double quotes count is not even';
return false;
}
return true;
}
/**
* Checks if variables contain digits.
*
* @return bool Returns true if no variables contain digits, false otherwise.
*/
private function checkDigitsStartedVariables()
{
foreach ( $this->tokens as $token ) {
if ( $token[0] === 'T_VARIABLE' ) {
if ( preg_match('/^\$\d.+/', $token[1]) ) {
$this->check_list_result[__FUNCTION__] = 'Variable starts with digits [' . $token[1] . ']';
return false;
}
}
}
return true;
}
/**
* Checks if the file does not contain PHP open tags ("<\?php" or `<\?`).
*
* @return bool Returns true if the file does not contain PHP open tags, false otherwise.
*/
public function hasCorrectPHPOpenTags()
{
foreach ( $this->tokens as $_token => $content ) {
if ( isset($content[0]) && isset($this->tokens->next1[0]) ) {
if ( $content[0] === 'T_OPEN_TAG' ) {
//check if open tag is short
$is_short = isset($content[1]) && $content[1] === '<?';
if ( $is_short ) {
if ( $this->tokens->next1[0] !== 'T_WHITESPACE' ) {
$this->check_list_result[__FUNCTION__] = 'PHP open tags are not valid';
return false;
}
} else {
return true;
}
}
}
}
return false;
}
}