From a33a6566e883fa9d7c35c1e3ca7a947a843a48e3 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Sat, 20 Jul 2024 10:26:11 +0200 Subject: [PATCH 1/8] making check prefix all globals --- .../Plugin_Repo/Prefix_All_Globals_Check.php | 50 +++++++++++++++++++ includes/Checker/Default_Check_Repository.php | 1 + .../load.php | 25 ++++++++++ .../load.php | 21 ++++++++ .../Checks/Prefix_Globals_Check_Tests.php | 40 +++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php create mode 100644 tests/phpunit/testdata/plugins/test-plugin-prefix-all-globals-with-errors/load.php create mode 100644 tests/phpunit/testdata/plugins/test-plugin-prefix-all-globals-without-errors/load.php create mode 100644 tests/phpunit/tests/Checker/Checks/Prefix_Globals_Check_Tests.php diff --git a/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php b/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php new file mode 100644 index 000000000..e00099baa --- /dev/null +++ b/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php @@ -0,0 +1,50 @@ + 'php', + 'standard' => 'WordPress', + 'sniffs' => 'WordPress.NamingConventions.PrefixAllGlobals', + ); + } +} diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index 4d68f1793..02ffa305e 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -58,6 +58,7 @@ private function register_default_checks() { 'localhost' => new Checks\Plugin_Repo\Localhost_Check(), 'no_unfiltered_uploads' => new Checks\Plugin_Repo\No_Unfiltered_Uploads_Check(), 'trademarks' => new Checks\Plugin_Repo\Trademarks_Check(), + 'prefix_all_globals' => new Checks\Plugin_Repo\Prefix_All_Globals_Check(), ) ); diff --git a/tests/phpunit/testdata/plugins/test-plugin-prefix-all-globals-with-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-prefix-all-globals-with-errors/load.php new file mode 100644 index 000000000..dfcbba908 --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin-prefix-all-globals-with-errors/load.php @@ -0,0 +1,25 @@ +run( $check_result ); + + $errors = $check_result->get_errors(); + + $this->assertNotEmpty( $errors ); + $this->assertArrayHasKey( 'load.php', $errors ); + $this->assertEquals( 2, $check_result->get_error_count() ); + } + + public function test_run_without_errors() { + $check = new Prefix_All_Globals_Check(); + $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-prefix-all-globals-without-errors/load.php' ); + $check_result = new Check_Result( $check_context ); + + $check->run( $check_result ); + + $errors = $check_result->get_errors(); + + $this->assertEmpty( $errors ); + $this->assertEquals( 0, $check_result->get_error_count() ); + } +} From 605e53b429e70c7afd51a00f71d7898b08e9d962 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Sun, 4 Aug 2024 11:10:19 +0200 Subject: [PATCH 2/8] change the way how to detect it --- .../Plugin_Repo/Prefix_All_Globals_Check.php | 69 ++++++++++++++++--- 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php b/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php index e00099baa..8f8126781 100644 --- a/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php @@ -8,7 +8,9 @@ namespace WordPress\Plugin_Check\Checker\Checks\Plugin_Repo; use WordPress\Plugin_Check\Checker\Check_Categories; -use WordPress\Plugin_Check\Checker\Checks\Abstract_PHP_CodeSniffer_Check; +use WordPress\Plugin_Check\Checker\Check_Result; +use WordPress\Plugin_Check\Checker\Checks\Abstract_File_Check; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; use WordPress\Plugin_Check\Traits\Stable_Check; /** @@ -16,8 +18,9 @@ * * @since 1.0.2 */ -class Prefix_All_Globals_Check extends Abstract_PHP_CodeSniffer_Check { +class Prefix_All_Globals_Check extends Abstract_File_Check { + use Amend_Check_Result; use Stable_Check; /** @@ -34,17 +37,61 @@ public function get_categories() { } /** - * Returns an associative array of arguments to pass to PHPCS. + * Amends the given result by running the check on the given list of files. * - * @since 1.0.2 + * @since 1.0.0 + * + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param array $files List of absolute file paths. * - * @return array An associative array of PHPCS CLI arguments. + * @throws Exception Thrown when the check fails with a critical error (unrelated to any errors detected as part of + * the check). */ - protected function get_args() { - return array( - 'extensions' => 'php', - 'standard' => 'WordPress', - 'sniffs' => 'WordPress.NamingConventions.PrefixAllGlobals', - ); + protected function check_files( Check_Result $result, array $files ) { + $php_files = self::filter_files_by_extension( $files, 'php' ); + + // Looks for saved plugin options. + $this->look_for_name_variables( $result, $php_files ); } + + /** + * Looks for plugin updater routines in plugin files and amends the given result with an error if found. + * + * @since 1.0.0 + * + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param array $php_files List of absolute PHP file paths. + */ + protected function look_for_name_variables( Check_Result $result, array $php_files ) { + + foreach ( $php_files as $php_file ) { + $lines = file( $php_file ); + foreach ( $lines as $line_number => $line ) { + $matches = array(); + + if ( preg_match( '/\$[a-zA-Z0-9_]*\s*=\s*(get_option|add_option|add_site_option|update_option|update_site_option)\(/', $line, $matches ) ) { + + + } + } + } + + foreach ( $look_for_regex as $regex ) { + $matches = array(); + $updater_file = self::file_preg_match( $regex, $php_files, $matches ); + if ( $updater_file ) { + $this->add_result_error_for_file( + $result, + sprintf( + /* translators: %s: The match file name. */ + __( 'Detected code which may be altering WordPress update routines. Detected: %s', 'plugin-check' ), + esc_html( $matches[0] ) + ), + 'update_modification_detected', + $updater_file + ); + } + } + } + } From da770952f5a8ae7af8b3f523faf1114508371547 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Sat, 24 Aug 2024 11:58:57 +0200 Subject: [PATCH 3/8] moved to WPCS again --- .../Plugin_Repo/Prefix_All_Globals_Check.php | 81 +++++++------------ 1 file changed, 30 insertions(+), 51 deletions(-) diff --git a/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php b/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php index 8f8126781..044fb44ed 100644 --- a/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php @@ -8,9 +8,7 @@ namespace WordPress\Plugin_Check\Checker\Checks\Plugin_Repo; use WordPress\Plugin_Check\Checker\Check_Categories; -use WordPress\Plugin_Check\Checker\Check_Result; -use WordPress\Plugin_Check\Checker\Checks\Abstract_File_Check; -use WordPress\Plugin_Check\Traits\Amend_Check_Result; +use WordPress\Plugin_Check\Checker\Checks\Abstract_PHP_CodeSniffer_Check; use WordPress\Plugin_Check\Traits\Stable_Check; /** @@ -18,9 +16,8 @@ * * @since 1.0.2 */ -class Prefix_All_Globals_Check extends Abstract_File_Check { +class Prefix_All_Globals_Check extends Abstract_PHP_CodeSniffer_Check { - use Amend_Check_Result; use Stable_Check; /** @@ -37,61 +34,43 @@ public function get_categories() { } /** - * Amends the given result by running the check on the given list of files. + * Returns an associative array of arguments to pass to PHPCS. * - * @since 1.0.0 - * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param array $files List of absolute file paths. + * @since 1.0.2 * - * @throws Exception Thrown when the check fails with a critical error (unrelated to any errors detected as part of - * the check). + * @return array An associative array of PHPCS CLI arguments. */ - protected function check_files( Check_Result $result, array $files ) { - $php_files = self::filter_files_by_extension( $files, 'php' ); - - // Looks for saved plugin options. - $this->look_for_name_variables( $result, $php_files ); + protected function get_args() { + return array( + 'extensions' => 'php', + 'standard' => 'WordPress', + 'sniffs' => 'WordPress.NamingConventions.PrefixAllGlobals', + ); } /** - * Looks for plugin updater routines in plugin files and amends the given result with an error if found. + * Gets the description for the check. + * + * Every check must have a short description explaining what the check does. * - * @since 1.0.0 + * @since 1.1.0 * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param array $php_files List of absolute PHP file paths. + * @return string Description. */ - protected function look_for_name_variables( Check_Result $result, array $php_files ) { - - foreach ( $php_files as $php_file ) { - $lines = file( $php_file ); - foreach ( $lines as $line_number => $line ) { - $matches = array(); - - if ( preg_match( '/\$[a-zA-Z0-9_]*\s*=\s*(get_option|add_option|add_site_option|update_option|update_site_option)\(/', $line, $matches ) ) { - - - } - } - } - - foreach ( $look_for_regex as $regex ) { - $matches = array(); - $updater_file = self::file_preg_match( $regex, $php_files, $matches ); - if ( $updater_file ) { - $this->add_result_error_for_file( - $result, - sprintf( - /* translators: %s: The match file name. */ - __( 'Detected code which may be altering WordPress update routines. Detected: %s', 'plugin-check' ), - esc_html( $matches[0] ) - ), - 'update_modification_detected', - $updater_file - ); - } - } + public function get_description(): string { + return __( 'Checks the use of prefixes in all globals.', 'plugin-check' ); } + /** + * Gets the documentation URL for the check. + * + * Every check must have a URL with further information about the check. + * + * @since 1.1.0 + * + * @return string The documentation URL. + */ + public function get_documentation_url(): string { + return 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#prefixing'; + } } From a6c6734faca4c189c71efb0b45693c7450c21405 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Mon, 21 Oct 2024 23:14:50 +0200 Subject: [PATCH 4/8] start sniff --- ...bals_Check.php => Generic_Names_Check.php} | 34 +++++++---- includes/Checker/Default_Check_Repository.php | 2 +- .../Sniffs/CodeAnalysis/GenericNamesSniff.php | 58 +++++++++++++++++++ .../CodeAnalysis/GenericNamesUnitTest.inc | 13 +++++ phpcs-sniffs/PluginCheck/ruleset.xml | 1 + 5 files changed, 96 insertions(+), 12 deletions(-) rename includes/Checker/Checks/Plugin_Repo/{Prefix_All_Globals_Check.php => Generic_Names_Check.php} (62%) create mode 100644 phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php create mode 100644 phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/GenericNamesUnitTest.inc diff --git a/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php b/includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php similarity index 62% rename from includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php rename to includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php index 044fb44ed..47b52176d 100644 --- a/includes/Checker/Checks/Plugin_Repo/Prefix_All_Globals_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php @@ -1,6 +1,6 @@ 'php', - 'standard' => 'WordPress', - 'sniffs' => 'WordPress.NamingConventions.PrefixAllGlobals', + 'standard' => 'PluginCheck', + 'sniffs' => 'PluginCheck.CodeAnalysis.GenericNames', ); } @@ -53,12 +65,12 @@ protected function get_args() { * * Every check must have a short description explaining what the check does. * - * @since 1.1.0 + * @since 1.3.0 * * @return string Description. */ public function get_description(): string { - return __( 'Checks the use of prefixes in all globals.', 'plugin-check' ); + return __( 'Checks the use of prefixes in all generic functions and globals.', 'plugin-check' ); } /** diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index 8a729cf8e..f144722f3 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -58,7 +58,7 @@ private function register_default_checks() { 'localhost' => new Checks\Plugin_Repo\Localhost_Check(), 'no_unfiltered_uploads' => new Checks\Plugin_Repo\No_Unfiltered_Uploads_Check(), 'trademarks' => new Checks\Plugin_Repo\Trademarks_Check(), - 'prefix_all_globals' => new Checks\Plugin_Repo\Prefix_All_Globals_Check(), + 'generic_names' => new Checks\Plugin_Repo\Generic_Names_Check(), 'non_blocking_scripts' => new Checks\Performance\Non_Blocking_Scripts_Check(), 'offloading_files' => new Checks\Plugin_Repo\Offloading_Files_Check(), 'image_functions' => new Checks\Performance\Image_Functions_Check(), diff --git a/phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php b/phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php new file mode 100644 index 000000000..372d86f23 --- /dev/null +++ b/phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php @@ -0,0 +1,58 @@ +tokens[ $stackPtr ]['content']; + + if ( empty( trim( $content ) ) ) { + return; + } + + return ( $end_ptr + 1 ); + } +} diff --git a/phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/GenericNamesUnitTest.inc b/phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/GenericNamesUnitTest.inc new file mode 100644 index 000000000..906a38575 --- /dev/null +++ b/phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/GenericNamesUnitTest.inc @@ -0,0 +1,13 @@ + + From 425605e2e9a6ac3b22d99d587f0eb2323ad8cd05 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Mon, 21 Oct 2024 23:20:07 +0200 Subject: [PATCH 5/8] order checks --- includes/Checker/Default_Check_Repository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index f144722f3..c4f16051d 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -58,10 +58,10 @@ private function register_default_checks() { 'localhost' => new Checks\Plugin_Repo\Localhost_Check(), 'no_unfiltered_uploads' => new Checks\Plugin_Repo\No_Unfiltered_Uploads_Check(), 'trademarks' => new Checks\Plugin_Repo\Trademarks_Check(), - 'generic_names' => new Checks\Plugin_Repo\Generic_Names_Check(), 'non_blocking_scripts' => new Checks\Performance\Non_Blocking_Scripts_Check(), 'offloading_files' => new Checks\Plugin_Repo\Offloading_Files_Check(), 'image_functions' => new Checks\Performance\Image_Functions_Check(), + 'generic_names' => new Checks\Plugin_Repo\Generic_Names_Check(), ) ); From d904b19b3426d3832e4223ed91b762f34d61554d Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Tue, 22 Oct 2024 00:05:07 +0200 Subject: [PATCH 6/8] tclass --- .../PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php | 1 + 1 file changed, 1 insertion(+) diff --git a/phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php b/phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php index 372d86f23..d5f8c90f8 100644 --- a/phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php +++ b/phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php @@ -33,6 +33,7 @@ final class GenericNamesSniff extends Sniff { public function register() { $targets = Collections::textStringStartTokens(); $targets[] = \T_FUNCTION; + $targets[] = \T_CLASS; return $targets; } From 08bcc4ea74983165fe0c5565d68752c062f7c1d2 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Tue, 22 Oct 2024 00:09:51 +0200 Subject: [PATCH 7/8] updated tests --- .../load.php | 0 .../load.php | 0 ...eck_Tests.php => Generic_Names_Check_Tests.php} | 14 +++++++------- 3 files changed, 7 insertions(+), 7 deletions(-) rename tests/phpunit/testdata/plugins/{test-plugin-prefix-all-globals-with-errors => test-plugin-generic-names-with-errors}/load.php (100%) rename tests/phpunit/testdata/plugins/{test-plugin-prefix-all-globals-without-errors => test-plugin-generic-names-without-errors}/load.php (100%) rename tests/phpunit/tests/Checker/Checks/{Prefix_Globals_Check_Tests.php => Generic_Names_Check_Tests.php} (68%) diff --git a/tests/phpunit/testdata/plugins/test-plugin-prefix-all-globals-with-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-generic-names-with-errors/load.php similarity index 100% rename from tests/phpunit/testdata/plugins/test-plugin-prefix-all-globals-with-errors/load.php rename to tests/phpunit/testdata/plugins/test-plugin-generic-names-with-errors/load.php diff --git a/tests/phpunit/testdata/plugins/test-plugin-prefix-all-globals-without-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-generic-names-without-errors/load.php similarity index 100% rename from tests/phpunit/testdata/plugins/test-plugin-prefix-all-globals-without-errors/load.php rename to tests/phpunit/testdata/plugins/test-plugin-generic-names-without-errors/load.php diff --git a/tests/phpunit/tests/Checker/Checks/Prefix_Globals_Check_Tests.php b/tests/phpunit/tests/Checker/Checks/Generic_Names_Check_Tests.php similarity index 68% rename from tests/phpunit/tests/Checker/Checks/Prefix_Globals_Check_Tests.php rename to tests/phpunit/tests/Checker/Checks/Generic_Names_Check_Tests.php index 7eb2207a6..c620f1747 100644 --- a/tests/phpunit/tests/Checker/Checks/Prefix_Globals_Check_Tests.php +++ b/tests/phpunit/tests/Checker/Checks/Generic_Names_Check_Tests.php @@ -1,19 +1,19 @@ run( $check_result ); @@ -26,8 +26,8 @@ public function test_run_with_errors() { } public function test_run_without_errors() { - $check = new Prefix_All_Globals_Check(); - $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-prefix-all-globals-without-errors/load.php' ); + $check = new Generic_Names_Check(); + $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-generic-names-without-errors/load.php' ); $check_result = new Check_Result( $check_context ); $check->run( $check_result ); From b79fff9536a1f27e4b623d4b2992b9e86b8ca770 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Wed, 23 Oct 2024 23:47:13 +0200 Subject: [PATCH 8/8] unit test --- .../CodeAnalysis/GenericNamesUnitTest.php | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/GenericNamesUnitTest.php diff --git a/phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/GenericNamesUnitTest.php b/phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/GenericNamesUnitTest.php new file mode 100644 index 000000000..8b00a6fb2 --- /dev/null +++ b/phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/GenericNamesUnitTest.php @@ -0,0 +1,57 @@ + => + */ + public function getErrorList() { + return array( + 3 => 1, + ); + } + + /** + * Returns the lines where warnings should occur. + * + * @return array => + */ + public function getWarningList() { + return array(); + } + + /** + * Returns the fully qualified class name (FQCN) of the sniff. + * + * @return string The fully qualified class name of the sniff. + */ + protected function get_sniff_fqcn() { + return GenericNamesSniff::class; + } + + /** + * Sets the parameters for the sniff. + * + * @throws \RuntimeException If unable to set the ruleset parameters required for the test. + * + * @param Sniff $sniff The sniff being tested. + */ + public function set_sniff_parameters( Sniff $sniff ) { + } +}