forked from WordPress/WordPress-Coding-Standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectDatabaseQuerySniff.php
More file actions
314 lines (268 loc) · 8.83 KB
/
Copy pathDirectDatabaseQuerySniff.php
File metadata and controls
314 lines (268 loc) · 8.83 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* WordPress Coding Standard.
*
* @package WPCS\WordPressCodingStandards
* @link https://github.com/WordPress/WordPress-Coding-Standards
* @license https://opensource.org/licenses/MIT MIT
*/
namespace WordPressCS\WordPress\Sniffs\DB;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\Conditions;
use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Sniff;
/**
* Flag Database direct queries.
*
* @link https://docs.wpvip.com/php_codesniffer/warnings/#h-direct-database-queries
*
* @since 0.3.0
* @since 0.6.0 Removed the add_unique_message() function as it is no longer needed.
* @since 0.11.0 This class now extends the WordPressCS native `Sniff` class.
* @since 0.13.0 Class name changed: this class is now namespaced.
* @since 1.0.0 This sniff has been moved from the `VIP` category to the `DB` category.
* @since 3.0.0 Support for the very sniff specific WPCS native ignore comment syntax has been removed.
*/
final class DirectDatabaseQuerySniff extends Sniff {
/**
* List of custom cache get functions.
*
* @since 0.6.0
*
* @var string[]
*/
public $customCacheGetFunctions = array();
/**
* List of custom cache set functions.
*
* @since 0.6.0
*
* @var string[]
*/
public $customCacheSetFunctions = array();
/**
* List of custom cache delete functions.
*
* @since 0.6.0
*
* @var string[]
*/
public $customCacheDeleteFunctions = array();
/**
* Cache of previously added custom functions.
*
* Prevents having to do the same merges over and over again.
*
* @since 0.11.0
*
* @var array
*/
protected $addedCustomFunctions = array(
'cacheget' => array(),
'cacheset' => array(),
'cachedelete' => array(),
);
/**
* A list of functions that get data from the cache.
*
* @since 0.6.0
* @since 0.11.0 Changed from public static to protected non-static.
* @since 3.0.0 Moved from the generic `Sniff` class to this class.
*
* @var array
*/
protected $cacheGetFunctions = array(
'wp_cache_get' => true,
);
/**
* A list of functions that set data in the cache.
*
* @since 0.6.0
* @since 0.11.0 Changed from public static to protected non-static.
* @since 3.0.0 Moved from the generic `Sniff` class to this class.
*
* @var array
*/
protected $cacheSetFunctions = array(
'wp_cache_set' => true,
'wp_cache_add' => true,
);
/**
* A list of functions that delete data from the cache.
*
* @since 0.6.0
* @since 0.11.0 Changed from public static to protected non-static.
* @since 3.0.0 Moved from the generic `Sniff` class to this class.
*
* @var array
*/
protected $cacheDeleteFunctions = array(
'wp_cache_delete' => true,
'clean_attachment_cache' => true,
'clean_blog_cache' => true,
'clean_bookmark_cache' => true,
'clean_category_cache' => true,
'clean_comment_cache' => true,
'clean_network_cache' => true,
'clean_object_term_cache' => true,
'clean_page_cache' => true,
'clean_post_cache' => true,
'clean_term_cache' => true,
'clean_user_cache' => true,
);
/**
* The lists of $wpdb methods.
*
* @since 0.6.0
* @since 0.11.0 Changed from static to non-static.
*
* @var array[]
*/
protected $methods = array(
'cachable' => array(
'delete' => true,
'get_var' => true,
'get_col' => true,
'get_row' => true,
'get_results' => true,
'query' => true,
'replace' => true,
'update' => true,
),
'noncachable' => array(
'insert' => true,
),
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register() {
return array(
\T_VARIABLE,
);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @param int $stackPtr The position of the current token in the stack.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process_token( $stackPtr ) {
// Check for $wpdb variable.
if ( '$wpdb' !== $this->tokens[ $stackPtr ]['content'] ) {
return;
}
$is_object_call = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
if ( false === $is_object_call
|| ( \T_OBJECT_OPERATOR !== $this->tokens[ $is_object_call ]['code']
&& \T_NULLSAFE_OBJECT_OPERATOR !== $this->tokens[ $is_object_call ]['code'] )
) {
// This is not a call to the wpdb object.
return;
}
$methodPtr = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $is_object_call + 1 ), null, true );
$method = strtolower( $this->tokens[ $methodPtr ]['content'] );
$this->mergeFunctionLists();
if ( ! isset( $this->methods['all'][ $method ] ) ) {
return;
}
$endOfStatement = $this->phpcsFile->findNext( array( \T_SEMICOLON, \T_CLOSE_TAG ), ( $stackPtr + 1 ) );
if ( false === $endOfStatement ) {
return;
}
// Check for Database Schema Changes/ table truncation.
for ( $_pos = ( $stackPtr + 1 ); $_pos < $endOfStatement; $_pos++ ) {
$_pos = $this->phpcsFile->findNext( Tokens::$textStringTokens, $_pos, $endOfStatement );
if ( false === $_pos ) {
break;
}
if ( strpos( strtoupper( TextStrings::stripQuotes( $this->tokens[ $_pos ]['content'] ) ), 'TRUNCATE ' ) === 0 ) {
// Ignore queries to truncate the database as caching those is irrelevant and they need a direct db query.
return;
}
if ( preg_match( '#\b(?:ALTER|CREATE|DROP)\b#i', $this->tokens[ $_pos ]['content'] ) > 0 ) {
$this->phpcsFile->addWarning( 'Attempting a database schema change is discouraged.', $_pos, 'SchemaChange' );
}
}
$this->phpcsFile->addWarning( 'Use of a direct database call is discouraged.', $stackPtr, 'DirectQuery' );
if ( ! isset( $this->methods['cachable'][ $method ] ) ) {
return $endOfStatement;
}
$cached = false;
$wp_cache_get = false;
$scope_function = Conditions::getLastCondition( $this->phpcsFile, $stackPtr, Collections::functionDeclarationTokens() );
if ( false !== $scope_function ) {
$scopeStart = $this->tokens[ $scope_function ]['scope_opener'];
$scopeEnd = $this->tokens[ $scope_function ]['scope_closer'];
for ( $i = ( $scopeStart + 1 ); $i < $scopeEnd; $i++ ) {
if ( \T_STRING === $this->tokens[ $i ]['code'] ) {
$nextNonEmpty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $i + 1 ), null, true );
if ( \T_OPEN_PARENTHESIS !== $this->tokens[ $nextNonEmpty ]['code'] ) {
continue;
}
$content = strtolower( $this->tokens[ $i ]['content'] );
if ( isset( $this->cacheDeleteFunctions[ $content ] ) ) {
if ( \in_array( $method, array( 'query', 'update', 'replace', 'delete' ), true ) ) {
$cached = true;
break;
}
} elseif ( isset( $this->cacheGetFunctions[ $content ] ) ) {
$wp_cache_get = true;
} elseif ( isset( $this->cacheSetFunctions[ $content ] ) ) {
if ( $wp_cache_get ) {
$cached = true;
break;
}
}
}
}
}
if ( ! $cached ) {
$message = 'Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().';
$this->phpcsFile->addWarning( $message, $stackPtr, 'NoCaching' );
}
return $endOfStatement;
}
/**
* Merge custom functions provided via a custom ruleset with the defaults, if we haven't already.
*
* @since 0.11.0 Split out from the `process()` method.
*
* @return void
*/
protected function mergeFunctionLists() {
if ( ! isset( $this->methods['all'] ) ) {
$this->methods['all'] = array_merge( $this->methods['cachable'], $this->methods['noncachable'] );
}
if ( $this->customCacheGetFunctions !== $this->addedCustomFunctions['cacheget'] ) {
$this->cacheGetFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customCacheGetFunctions,
$this->cacheGetFunctions
);
$this->cacheGetFunctions = array_change_key_case( $this->cacheGetFunctions );
$this->addedCustomFunctions['cacheget'] = $this->customCacheGetFunctions;
}
if ( $this->customCacheSetFunctions !== $this->addedCustomFunctions['cacheset'] ) {
$this->cacheSetFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customCacheSetFunctions,
$this->cacheSetFunctions
);
$this->cacheSetFunctions = array_change_key_case( $this->cacheSetFunctions );
$this->addedCustomFunctions['cacheset'] = $this->customCacheSetFunctions;
}
if ( $this->customCacheDeleteFunctions !== $this->addedCustomFunctions['cachedelete'] ) {
$this->cacheDeleteFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customCacheDeleteFunctions,
$this->cacheDeleteFunctions
);
$this->cacheDeleteFunctions = array_change_key_case( $this->cacheDeleteFunctions );
$this->addedCustomFunctions['cachedelete'] = $this->customCacheDeleteFunctions;
}
}
}