Skip to content

Commit

Permalink
Refactoring and Code Duplication Reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
mukeshpanchal27 committed Oct 18, 2023
1 parent 6b28da3 commit b40e7b8
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 237 deletions.
34 changes: 24 additions & 10 deletions includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -20,6 +21,8 @@
*/
abstract class Abstract_PHP_CodeSniffer_Check implements Static_Check {

use Amend_Check_Result;

/**
* List of allowed PHPCS arguments.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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']
);
}
}
}
}
Expand Down
47 changes: 20 additions & 27 deletions includes/Checker/Checks/Code_Obfuscation_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -19,6 +20,7 @@
*/
class Code_Obfuscation_Check extends Abstract_File_Check {

use Amend_Check_Result;
use Stable_Check;

const TYPE_ZEND = 1;
Expand Down Expand Up @@ -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
);
}
}

Expand All @@ -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
);
}
}

Expand All @@ -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 ),
)
);
}
}
14 changes: 7 additions & 7 deletions includes/Checker/Checks/Enqueued_Scripts_Size_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -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']
);
}
}
Expand Down
14 changes: 7 additions & 7 deletions includes/Checker/Checks/Enqueued_Styles_Scope_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -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']
);
}
}
Expand Down
74 changes: 41 additions & 33 deletions includes/Checker/Checks/File_Type_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -19,6 +20,7 @@
*/
class File_Type_Check extends Abstract_File_Check {

use Amend_Check_Result;
use Stable_Check;

const TYPE_COMPRESSED = 1;
Expand Down Expand Up @@ -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
);
}
}
}
Expand All @@ -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
);
}
}
}
Expand Down Expand Up @@ -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 )
);
}
}
}
}
Expand All @@ -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
);
}
}
}
Expand All @@ -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 ),
)
);
}
}
12 changes: 6 additions & 6 deletions includes/Checker/Checks/Localhost_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -18,6 +19,7 @@
*/
class Localhost_Check extends Abstract_File_Check {

use Amend_Check_Result;
use Stable_Check;

/**
Expand Down Expand Up @@ -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
);
}
}
Expand Down
Loading

0 comments on commit b40e7b8

Please sign in to comment.