diff --git a/includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php b/includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php new file mode 100644 index 000000000..47b52176d --- /dev/null +++ b/includes/Checker/Checks/Plugin_Repo/Generic_Names_Check.php @@ -0,0 +1,88 @@ + 'php', + 'standard' => 'PluginCheck', + 'sniffs' => 'PluginCheck.CodeAnalysis.GenericNames', + ); + } + + /** + * Gets the description for the check. + * + * Every check must have a short description explaining what the check does. + * + * @since 1.3.0 + * + * @return string Description. + */ + public function get_description(): string { + return __( 'Checks the use of prefixes in all generic functions and 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'; + } +} diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index 7b0421863..c4f16051d 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -61,6 +61,7 @@ private function register_default_checks() { '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(), ) ); diff --git a/phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php b/phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php new file mode 100644 index 000000000..d5f8c90f8 --- /dev/null +++ b/phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/GenericNamesSniff.php @@ -0,0 +1,59 @@ +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 @@ + => + */ + 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 ) { + } +} diff --git a/phpcs-sniffs/PluginCheck/ruleset.xml b/phpcs-sniffs/PluginCheck/ruleset.xml index d1a7fecf4..d443d2866 100644 --- a/phpcs-sniffs/PluginCheck/ruleset.xml +++ b/phpcs-sniffs/PluginCheck/ruleset.xml @@ -6,5 +6,6 @@ + diff --git a/tests/phpunit/testdata/plugins/test-plugin-generic-names-with-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-generic-names-with-errors/load.php new file mode 100644 index 000000000..dfcbba908 --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin-generic-names-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 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 ); + + $errors = $check_result->get_errors(); + + $this->assertEmpty( $errors ); + $this->assertEquals( 0, $check_result->get_error_count() ); + } +}