Checks for direct database queries within the plugin codebase. These should be avoided as these queries can negatively impact performance and bypass inbuilt caches. This sniff will evaluate $wpdb usage and warn the developer about its use. This will use the existing WordPress.DB.DirectDatabaseQuery sniff in the WordPress Coding Standards.
Acceptance Criteria
Class Direct_DB_Queries_Check should be created and exists at includes/Checker/Checks
Class Direct_DB_Queries_Check should extend the Abstract_PHP_CodeSniffer_Check abstract class
Class should implement the public get_args() and return an associative array of the required PHPCS arguments
'sniffs' => 'WordPress.DB.DirectDatabaseQuery'
'standard' => 'WordPress'
Add a sample code in the tests/testdata/plugins/test-plugin-with-errors\test-plugin-with-errors.php file, which will come under the WordPress.DB.DirectDatabaseQuery error - we can use DB query by using $wpdb global variable.
Tests Coverage
get_args() method returns the expected array of PHPCS arguments for the check
run() method adds warnings to the $check_result when checking an invalid file - check reference here
run() method does not add any warnings to the $check_result when checking a valid file - check reference here
Description
Checks for direct database queries within the plugin codebase. These should be avoided as these queries can negatively impact performance and bypass inbuilt caches. This sniff will evaluate $wpdb usage and warn the developer about its use. This will use the existing
WordPress.DB.DirectDatabaseQuery
sniff in the WordPress Coding Standards.Acceptance Criteria
Direct_DB_Queries_Check
should be created and exists atincludes/Checker/Checks
Direct_DB_Queries_Check
should extend theAbstract_PHP_CodeSniffer_Check
abstract classget_args()
and return an associative array of the required PHPCS arguments'sniffs' => 'WordPress.DB.DirectDatabaseQuery'
'standard' => 'WordPress'
tests/testdata/plugins/test-plugin-with-errors\test-plugin-with-errors.php
file, which will come under theWordPress.DB.DirectDatabaseQuery
error - we can use DB query by using$wpdb
global variable.Tests Coverage
get_args()
method returns the expected array of PHPCS arguments for the checkrun()
method adds warnings to the$check_result
when checking an invalid file - check reference hererun()
method does not add any warnings to the$check_result
when checking a valid file - check reference here