From b40e7b88471b85acb547b1eed204aa30c756c4d4 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 18 Oct 2023 14:47:50 +0530 Subject: [PATCH] Refactoring and Code Duplication Reduction --- .../Checks/Abstract_PHP_CodeSniffer_Check.php | 34 ++++--- .../Checker/Checks/Code_Obfuscation_Check.php | 47 +++++----- .../Checks/Enqueued_Scripts_Size_Check.php | 14 +-- .../Checks/Enqueued_Styles_Scope_Check.php | 14 +-- includes/Checker/Checks/File_Type_Check.php | 74 +++++++++------- includes/Checker/Checks/Localhost_Check.php | 12 +-- .../Checks/No_Unfiltered_Uploads_Check.php | 12 +-- .../Plugin_Header_Text_Domain_Check.php | 12 +-- .../Checker/Checks/Plugin_Readme_Check.php | 88 ++++++------------- .../Checker/Checks/Plugin_Updater_Check.php | 30 +------ includes/Checker/Checks/Trademarks_Check.php | 70 ++++++--------- includes/Traits/Amend_Check_Result.php | 68 ++++++++++++++ includes/Traits/Find_Readme.php | 54 ++++++++++++ phpstan.neon.dist | 2 +- 14 files changed, 294 insertions(+), 237 deletions(-) create mode 100644 includes/Traits/Amend_Check_Result.php create mode 100644 includes/Traits/Find_Readme.php diff --git a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php index 734d79b41..ce426c276 100644 --- a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php +++ b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php @@ -11,6 +11,7 @@ use PHP_CodeSniffer\Runner; use WordPress\Plugin_Check\Checker\Check_Result; use WordPress\Plugin_Check\Checker\Static_Check; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; /** @@ -20,6 +21,8 @@ */ abstract class Abstract_PHP_CodeSniffer_Check implements Static_Check { + use Amend_Check_Result; + /** * List of allowed PHPCS arguments. * @@ -58,6 +61,8 @@ abstract protected function get_args(); * * @throws Exception Thrown when the check fails with a critical error (unrelated to any errors detected as part of * the check). + * + * @SuppressWarnings(PHPMD.NPathComplexity) */ final public function run( Check_Result $result ) { // Include the PHPCS autoloader. @@ -122,16 +127,25 @@ final public function run( Check_Result $result ) { } foreach ( $file_results['messages'] as $file_message ) { - $result->add_message( - strtoupper( $file_message['type'] ) === 'ERROR', - $file_message['message'], - array( - 'code' => $file_message['source'], - 'file' => $file_name, - 'line' => $file_message['line'], - 'column' => $file_message['column'], - ) - ); + if ( strtoupper( $file_message['type'] ) === 'ERROR' ) { + $this->add_result_error_for_file( + $result, + $file_message['message'], + $file_message['source'], + $file_name, + $file_message['line'], + $file_message['column'] + ); + } else { + $this->add_result_warning_for_file( + $result, + $file_message['message'], + $file_message['source'], + $file_name, + $file_message['line'], + $file_message['column'] + ); + } } } } diff --git a/includes/Checker/Checks/Code_Obfuscation_Check.php b/includes/Checker/Checks/Code_Obfuscation_Check.php index 0b63f4ccc..a9390ffb0 100644 --- a/includes/Checker/Checks/Code_Obfuscation_Check.php +++ b/includes/Checker/Checks/Code_Obfuscation_Check.php @@ -10,6 +10,7 @@ use Exception; use WordPress\Plugin_Check\Checker\Check_Categories; use WordPress\Plugin_Check\Checker\Check_Result; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; use WordPress\Plugin_Check\Traits\Stable_Check; /** @@ -19,6 +20,7 @@ */ class Code_Obfuscation_Check extends Abstract_File_Check { + use Amend_Check_Result; use Stable_Check; const TYPE_ZEND = 1; @@ -94,7 +96,12 @@ protected function check_files( Check_Result $result, array $files ) { protected function look_for_zendguard( Check_Result $result, array $php_files ) { $obfuscated_file = self::file_preg_match( '/(<\?php \@Zend;)|(This file was encoded by)/', $php_files ); if ( $obfuscated_file ) { - $this->add_result_error_for_file( $result, $obfuscated_file, 'Zend Guard' ); + $this->add_result_error_for_file( + $result, + __( 'Code Obfuscation tools are not permitted. Detected: Zend Guard', 'plugin-check' ), + 'obfuscated_code_detected', + $obfuscated_file + ); } } @@ -109,7 +116,12 @@ protected function look_for_zendguard( Check_Result $result, array $php_files ) protected function look_for_sourceguardian( Check_Result $result, array $php_files ) { $obfuscated_file = self::file_preg_match( "/(sourceguardian\.com)|(function_exists\('sg_load'\))|(\$__x=)/", $php_files ); if ( $obfuscated_file ) { - $this->add_result_error_for_file( $result, $obfuscated_file, 'Source Guardian' ); + $this->add_result_error_for_file( + $result, + __( 'Code Obfuscation tools are not permitted. Detected: Source Guardian', 'plugin-check' ), + 'obfuscated_code_detected', + $obfuscated_file + ); } } @@ -124,31 +136,12 @@ protected function look_for_sourceguardian( Check_Result $result, array $php_fil protected function look_for_ioncube( Check_Result $result, array $php_files ) { $obfuscated_file = self::file_str_contains( $php_files, 'ionCube' ); if ( $obfuscated_file ) { - $this->add_result_error_for_file( $result, $obfuscated_file, 'ionCube' ); + $this->add_result_error_for_file( + $result, + __( 'Code Obfuscation tools are not permitted. Detected: ionCube', 'plugin-check' ), + 'obfuscated_code_detected', + $obfuscated_file + ); } } - - /** - * Amends the given result with an error for the given obfuscated file and tool name. - * - * @since n.e.x.t - * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param string $obfuscated_file Absolute path to the obfuscated file found. - * @param string $tool_name Human-readable name of the tool detected for obfuscation. - */ - private function add_result_error_for_file( Check_Result $result, $obfuscated_file, $tool_name ) { - $result->add_message( - true, - sprintf( - /* translators: %s: tool name */ - __( 'Code Obfuscation tools are not permitted. Detected: %s', 'plugin-check' ), - $tool_name - ), - array( - 'code' => 'obfuscated_code_detected', - 'file' => str_replace( $result->plugin()->path(), '', $obfuscated_file ), - ) - ); - } } diff --git a/includes/Checker/Checks/Enqueued_Scripts_Size_Check.php b/includes/Checker/Checks/Enqueued_Scripts_Size_Check.php index b952f4001..d540ffe85 100644 --- a/includes/Checker/Checks/Enqueued_Scripts_Size_Check.php +++ b/includes/Checker/Checks/Enqueued_Scripts_Size_Check.php @@ -12,6 +12,7 @@ use WordPress\Plugin_Check\Checker\Check_Result; use WordPress\Plugin_Check\Checker\Preparations\Demo_Posts_Creation_Preparation; use WordPress\Plugin_Check\Checker\With_Shared_Preparations; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; use WordPress\Plugin_Check\Traits\Stable_Check; use WordPress\Plugin_Check\Traits\URL_Aware; @@ -22,8 +23,9 @@ */ class Enqueued_Scripts_Size_Check extends Abstract_Runtime_Check implements With_Shared_Preparations { - use URL_Aware; + use Amend_Check_Result; use Stable_Check; + use URL_Aware; /** * Threshold for script size to surface a warning for. @@ -231,18 +233,16 @@ protected function check_url( Check_Result $result, $url ) { if ( $plugin_script_size > $this->threshold_size ) { foreach ( $plugin_scripts as $plugin_script ) { - $result->add_message( - false, + $this->add_result_warning_for_file( + $result, sprintf( 'This script has a size of %1$s which in combination with the other scripts enqueued on %2$s exceeds the script size threshold of %3$s.', size_format( $plugin_script['size'] ), $url, size_format( $this->threshold_size ) ), - array( - 'code' => 'EnqueuedScriptsSize.ScriptSizeGreaterThanThreshold', - 'file' => $plugin_script['path'], - ) + 'EnqueuedScriptsSize.ScriptSizeGreaterThanThreshold', + $plugin_script['path'] ); } } diff --git a/includes/Checker/Checks/Enqueued_Styles_Scope_Check.php b/includes/Checker/Checks/Enqueued_Styles_Scope_Check.php index a7b2b86cf..8fa726413 100644 --- a/includes/Checker/Checks/Enqueued_Styles_Scope_Check.php +++ b/includes/Checker/Checks/Enqueued_Styles_Scope_Check.php @@ -12,6 +12,7 @@ use WordPress\Plugin_Check\Checker\Check_Result; use WordPress\Plugin_Check\Checker\Preparations\Demo_Posts_Creation_Preparation; use WordPress\Plugin_Check\Checker\With_Shared_Preparations; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; use WordPress\Plugin_Check\Traits\Stable_Check; use WordPress\Plugin_Check\Traits\URL_Aware; @@ -22,8 +23,9 @@ */ class Enqueued_Styles_Scope_Check extends Abstract_Runtime_Check implements With_Shared_Preparations { - use URL_Aware; + use Amend_Check_Result; use Stable_Check; + use URL_Aware; /** * List of viewable post types. @@ -134,13 +136,11 @@ function () use ( $result ) { $url_count = count( $urls ); foreach ( $this->plugin_styles as $plugin_style ) { if ( isset( $plugin_style['count'] ) && ( $url_count === $plugin_style['count'] ) ) { - $result->add_message( - false, + $this->add_result_warning_for_file( + $result, __( 'This style is being loaded in all contexts.', 'plugin-check' ), - array( - 'code' => 'EnqueuedStylesScope.StyleLoadedInAllContext', - 'file' => $plugin_style['path'], - ) + 'EnqueuedStylesScope', + $plugin_style['path'] ); } } diff --git a/includes/Checker/Checks/File_Type_Check.php b/includes/Checker/Checks/File_Type_Check.php index 2e5bdaede..4ac9b5286 100644 --- a/includes/Checker/Checks/File_Type_Check.php +++ b/includes/Checker/Checks/File_Type_Check.php @@ -10,6 +10,7 @@ use Exception; use WordPress\Plugin_Check\Checker\Check_Categories; use WordPress\Plugin_Check\Checker\Check_Result; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; use WordPress\Plugin_Check\Traits\Stable_Check; /** @@ -19,6 +20,7 @@ */ class File_Type_Check extends Abstract_File_Check { + use Amend_Check_Result; use Stable_Check; const TYPE_COMPRESSED = 1; @@ -101,7 +103,12 @@ protected function look_for_compressed_files( Check_Result $result, array $files $compressed_files = self::filter_files_by_extensions( $files, array( 'zip', 'gz', 'tgz', 'rar', 'tar', '7z' ) ); if ( $compressed_files ) { foreach ( $compressed_files as $file ) { - $this->add_result_error_for_file( $result, $file, 'compressed_files', __( 'Compressed files are not permitted.', 'plugin-check' ) ); + $this->add_result_error_for_file( + $result, + __( 'Compressed files are not permitted.', 'plugin-check' ), + 'compressed_files', + $file + ); } } } @@ -118,7 +125,12 @@ protected function look_for_phar_files( Check_Result $result, array $files ) { $phar_files = self::filter_files_by_extension( $files, 'phar' ); if ( $phar_files ) { foreach ( $phar_files as $file ) { - $this->add_result_error_for_file( $result, $file, 'phar_files', __( 'Phar files are not permitted.', 'plugin-check' ) ); + $this->add_result_error_for_file( + $result, + __( 'Phar files are not permitted.', 'plugin-check' ), + 'phar_files', + $file + ); } } } @@ -152,14 +164,21 @@ function ( $directory ) use ( $directories ) { // Only use an error in production, otherwise a warning. $is_error = ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && 'production' === wp_get_environment_type(); foreach ( $vcs_directories as $dir ) { - $result->add_message( - $is_error, - __( 'Version control checkouts should not be present.', 'plugin-check' ), - array( - 'code' => 'vcs_present', - 'file' => str_replace( $result->plugin()->path(), '', $dir ), - ) - ); + if ( $is_error ) { + $this->add_result_error_for_file( + $result, + __( 'Version control checkouts should not be present.', 'plugin-check' ), + 'vcs_present', + str_replace( $result->plugin()->path(), '', $dir ) + ); + } else { + $this->add_result_warning_for_file( + $result, + __( 'Version control checkouts should not be present.', 'plugin-check' ), + 'vcs_present', + str_replace( $result->plugin()->path(), '', $dir ) + ); + } } } } @@ -177,7 +196,12 @@ protected function look_for_hidden_files( Check_Result $result, array $files ) { $hidden_files = self::filter_files_by_regex( $files, '/^((?!\/vendor\/|\/node_modules\/).)*\/\.\w+(\.\w+)*$/' ); if ( $hidden_files ) { foreach ( $hidden_files as $file ) { - $this->add_result_error_for_file( $result, $file, 'hidden_files', __( 'Hidden files are not permitted.', 'plugin-check' ) ); + $this->add_result_error_for_file( + $result, + __( 'Hidden files are not permitted.', 'plugin-check' ), + 'hidden_files', + $file + ); } } } @@ -197,29 +221,13 @@ protected function look_for_application_files( Check_Result $result, array $file ); if ( $application_files ) { foreach ( $application_files as $file ) { - $this->add_result_error_for_file( $result, $file, 'application_detected', __( 'Application files are not permitted.', 'plugin-check' ) ); + $this->add_result_error_for_file( + $result, + __( 'Application files are not permitted.', 'plugin-check' ), + 'application_detected', + $file + ); } } } - - /** - * Amends the given result with an error for the given file, code, and message. - * - * @since n.e.x.t - * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param string $file Absolute path to the file found. - * @param string $code Error code. - * @param string $message Error message. - */ - private function add_result_error_for_file( Check_Result $result, $file, $code, $message ) { - $result->add_message( - true, - $message, - array( - 'code' => $code, - 'file' => str_replace( $result->plugin()->path(), '', $file ), - ) - ); - } } diff --git a/includes/Checker/Checks/Localhost_Check.php b/includes/Checker/Checks/Localhost_Check.php index b700859a8..ed6b30e5b 100644 --- a/includes/Checker/Checks/Localhost_Check.php +++ b/includes/Checker/Checks/Localhost_Check.php @@ -9,6 +9,7 @@ use WordPress\Plugin_Check\Checker\Check_Categories; use WordPress\Plugin_Check\Checker\Check_Result; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; use WordPress\Plugin_Check\Traits\Stable_Check; /** @@ -18,6 +19,7 @@ */ class Localhost_Check extends Abstract_File_Check { + use Amend_Check_Result; use Stable_Check; /** @@ -45,13 +47,11 @@ protected function check_files( Check_Result $result, array $files ) { $php_files = self::filter_files_by_extension( $files, 'php' ); $file = self::file_preg_match( '#https?://(localhost|127.0.0.1)#', $php_files ); if ( $file ) { - $result->add_message( - true, + $this->add_result_error_for_file( + $result, __( 'Do not use Localhost/127.0.0.1 in your code.', 'plugin-check' ), - array( - 'code' => 'localhost_code_detected', - 'file' => $file, - ) + 'localhost_code_detected', + $file ); } } diff --git a/includes/Checker/Checks/No_Unfiltered_Uploads_Check.php b/includes/Checker/Checks/No_Unfiltered_Uploads_Check.php index 7933bf72a..0156f692e 100644 --- a/includes/Checker/Checks/No_Unfiltered_Uploads_Check.php +++ b/includes/Checker/Checks/No_Unfiltered_Uploads_Check.php @@ -9,6 +9,7 @@ use WordPress\Plugin_Check\Checker\Check_Categories; use WordPress\Plugin_Check\Checker\Check_Result; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; use WordPress\Plugin_Check\Traits\Stable_Check; /** @@ -18,6 +19,7 @@ */ class No_Unfiltered_Uploads_Check extends Abstract_File_Check { + use Amend_Check_Result; use Stable_Check; /** @@ -45,13 +47,11 @@ protected function check_files( Check_Result $result, array $files ) { $php_files = self::filter_files_by_extension( $files, 'php' ); $file = self::file_str_contains( $php_files, 'ALLOW_UNFILTERED_UPLOADS' ); if ( $file ) { - $result->add_message( - true, + $this->add_result_error_for_file( + $result, __( 'ALLOW_UNFILTERED_UPLOADS is not permitted.', 'plugin-check' ), - array( - 'code' => 'allow_unfiltered_uploads_detected', - 'file' => $file, - ) + 'allow_unfiltered_uploads_detected', + $file ); } } diff --git a/includes/Checker/Checks/Plugin_Header_Text_Domain_Check.php b/includes/Checker/Checks/Plugin_Header_Text_Domain_Check.php index 2939e8014..6832f4298 100644 --- a/includes/Checker/Checks/Plugin_Header_Text_Domain_Check.php +++ b/includes/Checker/Checks/Plugin_Header_Text_Domain_Check.php @@ -11,6 +11,7 @@ use WordPress\Plugin_Check\Checker\Check_Categories; use WordPress\Plugin_Check\Checker\Check_Result; use WordPress\Plugin_Check\Checker\Static_Check; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; use WordPress\Plugin_Check\Traits\Stable_Check; /** @@ -20,6 +21,7 @@ */ class Plugin_Header_Text_Domain_Check implements Static_Check { + use Amend_Check_Result; use Stable_Check; /** @@ -60,18 +62,16 @@ public function run( Check_Result $result ) { ! empty( $plugin_header['TextDomain'] ) && $plugin_slug !== $plugin_header['TextDomain'] ) { - $result->add_message( - false, + $this->add_result_warning_for_file( + $result, sprintf( /* translators: 1: plugin header text domain, 2: plugin slug */ __( 'The TextDomain header in the plugin file does not match the slug. Found "%1$s", expected "%2$s".', 'plugin-check' ), esc_html( $plugin_header['TextDomain'] ), esc_html( $plugin_slug ) ), - array( - 'code' => 'textdomain_mismatch', - 'file' => $plugin_main_file, - ) + 'textdomain_mismatch', + $plugin_main_file ); } } diff --git a/includes/Checker/Checks/Plugin_Readme_Check.php b/includes/Checker/Checks/Plugin_Readme_Check.php index 63937e953..5e638f295 100644 --- a/includes/Checker/Checks/Plugin_Readme_Check.php +++ b/includes/Checker/Checks/Plugin_Readme_Check.php @@ -9,6 +9,8 @@ use WordPress\Plugin_Check\Checker\Check_Categories; use WordPress\Plugin_Check\Checker\Check_Result; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; +use WordPress\Plugin_Check\Traits\Find_Readme; use WordPress\Plugin_Check\Traits\Stable_Check; /** @@ -18,6 +20,8 @@ */ class Plugin_Readme_Check extends Abstract_File_Check { + use Amend_Check_Result; + use Find_Readme; use Stable_Check; /** @@ -45,48 +49,16 @@ protected function check_files( Check_Result $result, array $files ) { $plugin_relative_path = $result->plugin()->path(); - // Find the readme file. - $readme_list = self::filter_files_by_regex( $files, '/readme\.(txt|md)$/i' ); - - // Filter the readme files located at root. - $potential_readme_files = array_filter( - $readme_list, - function ( $file ) use ( $plugin_relative_path ) { - $file = str_replace( $plugin_relative_path, '', $file ); - if ( ! strpos( $file, '/' ) ) { - return true; - } - } - ); - - // Find the .txt versions of the readme files. - $readme_txt = array_filter( - $potential_readme_files, - function ( $file ) { - return preg_match( '/^readme\.txt$/i', basename( $file ) ); - } - ); - - // Find the .md versions of the readme files. - $readme_md = array_filter( - $potential_readme_files, - function ( $file ) { - return preg_match( '/^readme\.md$/i', basename( $file ) ); - } - ); - - // If there's a .txt version, ignore .md versions. - $readme = ( ! empty( $readme_txt ) ) ? $readme_txt : $readme_md; + // Filter the readme files. + $readme = $this->filter_files_for_readme( $files, $plugin_relative_path ); // If the readme file does not exist, add a warning and skip other tests. if ( empty( $readme ) ) { - $result->add_message( - false, + $this->add_result_warning_for_file( + $result, __( 'The plugin readme.txt does not exist.', 'plugin-check' ), - array( - 'code' => 'no_plugin_readme', - 'file' => 'readme.txt', - ) + 'no_plugin_readme', + 'readme.txt' ); return; @@ -120,13 +92,11 @@ private function check_default_text( Check_Result $result, array $files ) { foreach ( $default_text_patterns as $pattern ) { $file = self::file_str_contains( $files, $pattern ); if ( $file ) { - $result->add_message( - false, + $this->add_result_warning_for_file( + $result, __( 'The readme appears to contain default text.', 'plugin-check' ), - array( - 'code' => 'default_readme_text', - 'file' => str_replace( $result->plugin()->path(), '', $file ), - ) + 'default_readme_text', + $file ); break; } @@ -152,13 +122,11 @@ private function check_license( Check_Result $result, array $files ) { // Test for a valid SPDX license identifier. if ( ! preg_match( '/^([a-z0-9\-\+\.]+)(\sor\s([a-z0-9\-\+\.]+))*$/i', $matches[2] ) ) { - $result->add_message( - false, + $this->add_result_warning_for_file( + $result, __( 'Your plugin has an invalid license declared. Please update your readme with a valid SPDX license identifier.', 'plugin-check' ), - array( - 'code' => 'invalid_license', - 'file' => str_replace( $result->plugin()->path(), '', $file ), - ) + 'invalid_license', + $file ); } } @@ -182,13 +150,11 @@ private function check_stable_tag( Check_Result $result, array $files ) { $stable_tag = isset( $matches[1] ) ? $matches[1] : ''; if ( 'trunk' === $stable_tag ) { - $result->add_message( - true, + $this->add_result_error_for_file( + $result, __( "It's recommended not to use 'Stable Tag: trunk'.", 'plugin-check' ), - array( - 'code' => 'trunk_stable_tag', - 'file' => str_replace( $result->plugin()->path(), '', $file ), - ) + 'trunk_stable_tag', + $file ); } @@ -199,13 +165,11 @@ private function check_stable_tag( Check_Result $result, array $files ) { $stable_tag && ! empty( $plugin_data['Version'] ) && $stable_tag !== $plugin_data['Version'] ) { - $result->add_message( - true, + $this->add_result_error_for_file( + $result, __( 'The Stable Tag in your readme file does not match the version in your main plugin file.', 'plugin-check' ), - array( - 'code' => 'stable_tag_mismatch', - 'file' => str_replace( $result->plugin()->path(), '', $file ), - ) + 'stable_tag_mismatch', + $file ); } } diff --git a/includes/Checker/Checks/Plugin_Updater_Check.php b/includes/Checker/Checks/Plugin_Updater_Check.php index befd82f2c..fe6a84b2d 100644 --- a/includes/Checker/Checks/Plugin_Updater_Check.php +++ b/includes/Checker/Checks/Plugin_Updater_Check.php @@ -10,6 +10,7 @@ use Exception; use WordPress\Plugin_Check\Checker\Check_Categories; use WordPress\Plugin_Check\Checker\Check_Result; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; use WordPress\Plugin_Check\Traits\Stable_Check; /** @@ -19,6 +20,7 @@ */ class Plugin_Updater_Check extends Abstract_File_Check { + use Amend_Check_Result; use Stable_Check; const TYPE_PLUGIN_UPDATE_URI_HEADER = 1; @@ -111,7 +113,6 @@ protected function look_for_update_uri_header( Check_Result $result ) { if ( ! empty( $plugin_header['UpdateURI'] ) ) { $this->add_result_error_for_file( $result, - true, __( 'Plugin Updater detected. Use of the Update URI header is not helpful in plugins hosted on WordPress.org.', 'plugin-check' ), 'plugin_updater_detected', $plugin_main_file @@ -135,7 +136,6 @@ protected function look_for_updater_file( Check_Result $result, array $php_files foreach ( $plugin_update_files as $file ) { $this->add_result_error_for_file( $result, - true, sprintf( /* translators: %s: The match updater file name. */ __( 'Plugin Updater detected. These are not permitted in WordPress.org hosted plugins. Detected: %s', 'plugin-check' ), @@ -173,7 +173,6 @@ protected function look_for_plugin_updaters( Check_Result $result, array $php_fi if ( $updater_file ) { $this->add_result_error_for_file( $result, - true, sprintf( /* translators: %s: The match updater string. */ __( 'Plugin Updater detected. These are not permitted in WordPress.org hosted plugins. Detected: %s', 'plugin-check' ), @@ -206,9 +205,8 @@ protected function look_for_updater_routines( Check_Result $result, array $php_f $matches = array(); $updater_file = self::file_preg_match( $regex, $php_files, $matches ); if ( $updater_file ) { - $this->add_result_error_for_file( + $this->add_result_warning_for_file( $result, - false, sprintf( /* translators: %s: The match file name. */ __( 'Detected code which may be altering WordPress update routines. Detected: %s', 'plugin-check' ), @@ -220,26 +218,4 @@ protected function look_for_updater_routines( Check_Result $result, array $php_f } } } - - /** - * Amends the given result with an error for the given obfuscated file and tool name. - * - * @since n.e.x.t - * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param bool $error Whether it is an error message. - * @param string $message Error message for updater. - * @param string $code Violation code according to the message. - * @param string $updater_file Absolute path to the updater file found. - */ - private function add_result_error_for_file( Check_Result $result, $error, $message, $code, $updater_file ) { - $result->add_message( - $error, - $message, - array( - 'code' => $code, - 'file' => str_replace( $result->plugin()->path(), '', $updater_file ), - ) - ); - } } diff --git a/includes/Checker/Checks/Trademarks_Check.php b/includes/Checker/Checks/Trademarks_Check.php index cc9a7a0b5..c0febafdf 100644 --- a/includes/Checker/Checks/Trademarks_Check.php +++ b/includes/Checker/Checks/Trademarks_Check.php @@ -10,6 +10,8 @@ use Exception; use WordPress\Plugin_Check\Checker\Check_Categories; use WordPress\Plugin_Check\Checker\Check_Result; +use WordPress\Plugin_Check\Traits\Amend_Check_Result; +use WordPress\Plugin_Check\Traits\Find_Readme; use WordPress\Plugin_Check\Traits\Stable_Check; /** @@ -19,6 +21,8 @@ */ class Trademarks_Check extends Abstract_File_Check { + use Amend_Check_Result; + use Find_Readme; use Stable_Check; /** @@ -236,33 +240,14 @@ protected function check_files( Check_Result $result, array $files ) { private function check_for_name_in_readme( Check_Result $result, array $files ) { $plugin_relative_path = $result->plugin()->path(); - // Find the readme file. - $readme_list = self::filter_files_by_regex( $files, '/readme\.(txt|md)$/i' ); - - // Filter the readme files located at root. - $potential_readme_files = array_filter( - $readme_list, - function ( $file ) use ( $plugin_relative_path ) { - $file = str_replace( $plugin_relative_path, '', $file ); - return ! str_contains( $file, '/' ); - } - ); + // Filter the readme files. + $readme = $this->filter_files_for_readme( $files, $plugin_relative_path ); // If the readme file does not exist, then skip test. - if ( empty( $potential_readme_files ) ) { + if ( empty( $readme ) ) { return; } - // Find the .txt versions of the readme files. - $readme_txt = array_filter( - $potential_readme_files, - function ( $file ) { - return preg_match( '/^readme\.txt$/i', basename( $file ) ); - } - ); - - $readme = $readme_txt ? $readme_txt : $potential_readme_files; - $matches = array(); // Get the plugin name from readme file. $file = self::file_preg_match( '/===(.*)===/i', $readme, $matches ); @@ -276,7 +261,12 @@ function ( $file ) { try { $this->validate_name_has_no_trademarks( $name ); } catch ( Exception $e ) { - $this->add_result_error_for_file( $result, $file, $e->getMessage() ); + $this->add_result_error_for_file( + $result, + $e->getMessage(), + 'trademarked_term', + $file + ); } } @@ -299,7 +289,12 @@ private function check_for_name_in_main_file( Check_Result $result ) { try { $this->validate_name_has_no_trademarks( $plugin_header['Name'] ); } catch ( Exception $e ) { - $this->add_result_error_for_file( $result, $plugin_main_file, $e->getMessage() ); + $this->add_result_error_for_file( + $result, + $e->getMessage(), + 'trademarked_term', + $plugin_main_file + ); } } } @@ -317,7 +312,12 @@ private function check_for_slug( Check_Result $result ) { try { $this->validate_slug_has_no_trademarks( $plugin_slug ); } catch ( Exception $e ) { - $this->add_result_error_for_file( $result, WP_PLUGIN_DIR . '/' . $result->plugin()->basename(), $e->getMessage() ); + $this->add_result_error_for_file( + $result, + $e->getMessage(), + 'trademarked_term', + WP_PLUGIN_DIR . '/' . $result->plugin()->basename() + ); } } @@ -503,24 +503,4 @@ private function is_valid_for_use_exception( $slug, $trademark ) { // If the trademark still doesn't exist in the slug, it's OK. return false === strpos( $short_slug, $trademark ); } - - /** - * Amends the given result with an error for the given file, code, and message. - * - * @since n.e.x.t - * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param string $file Absolute path to the file found. - * @param string $message Error message. - */ - private function add_result_error_for_file( Check_Result $result, $file, $message ) { - $result->add_message( - true, - $message, - array( - 'code' => 'trademarked_term', - 'file' => str_replace( $result->plugin()->path(), '', $file ), - ) - ); - } } diff --git a/includes/Traits/Amend_Check_Result.php b/includes/Traits/Amend_Check_Result.php new file mode 100644 index 000000000..fde5ace04 --- /dev/null +++ b/includes/Traits/Amend_Check_Result.php @@ -0,0 +1,68 @@ +add_message( + true, + $message, + array( + 'code' => $code, + 'file' => str_replace( $result->plugin()->path(), '', $file ), + 'line' => $line, + 'column' => $column, + ) + ); + } + + /** + * Amends the given result with a warning for the given file, code, and message. + * + * @since n.e.x.t + * + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param string $message Error message. + * @param string $code Error code. + * @param string $file Absolute path to the file found. + * @param int $line The line on which the message occurred. Default 0 (unknown line). + * @param int $column The column on which the message occurred. Default 0 (unknown column). + */ + protected function add_result_warning_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0 ) { + $result->add_message( + false, + $message, + array( + 'code' => $code, + 'file' => str_replace( $result->plugin()->path(), '', $file ), + 'line' => $line, + 'column' => $column, + ) + ); + } +} diff --git a/includes/Traits/Find_Readme.php b/includes/Traits/Find_Readme.php new file mode 100644 index 000000000..54b7b6bee --- /dev/null +++ b/includes/Traits/Find_Readme.php @@ -0,0 +1,54 @@ +