WordPress-Coding-Standards
WordPress-Coding-Standards copied to clipboard
DirectDatabaseQuery: don't demand caching or forbid direct DB calls for `TRUNCATE`
Bug Description
Given the following code snippet:
$wpdb->query(
$wpdb->prepare(
'TRUNCATE TABLE `%1$s`',
plugin_get_table_name( 'Name' )
)
);
WPCS will currently throw the following warnings:
# | WARNING | Usage of a direct database call is discouraged. (WordPress.DB.DirectDatabaseQuery.DirectQuery)
# | WARNING | Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
| | (WordPress.DB.DirectDatabaseQuery.NoCaching)
IMO a query to truncate a table may be dangerous and could warrant a warning be thrown for that reason, however, it is a valid query to use a direct database call for and it is also a query for which caching of the query is irrelevant.
The "cache" related issue here is that any existing "old" cache should probably be thrown away, but that can not be done based on the truncate query.
Possibly a recommendation to query the truncated table afterwards with caching to clear any caches could be given, but that still wouldn't catch all potentially set caches.
Environment
| Question | Answer |
|---|---|
| PHP version | 7.4.8 |
| PHP_CodeSniffer version | master |
| WPCS version | develop |
| WPCS install type | git clones |
| IDE (if relevant) | N/A |
Additional Context (optional)
Loosely related to #1903
Tested Against develop branch?
- [x] I have verified the issue still exists in the
developbranch of WPCS.