Skip to content

Commit

Permalink
Merge pull request #324 from WordPress/fix/improve-host-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
bordoni authored Dec 14, 2023
2 parents b63967c + 398b569 commit 23cce1e
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 380 deletions.
82 changes: 50 additions & 32 deletions checks/phpcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
use const WordPressdotorg\Plugin_Check\{ PLUGIN_DIR, HAS_VENDOR };
use WordPressdotorg\Plugin_Check\{Error, Guideline_Violation, Message, Notice, Warning};
use WordPressdotorg\Plugin_Check\PHPCS;

include PLUGIN_DIR . '/inc/class-php-cli.php';
include PLUGIN_DIR . '/inc/class-phpcs.php';
use WordPressdotorg\Plugin_Check\PHPCS_Runner;

class PHPCS_Checks extends Check_Base {

const NOTICE_TYPES = [
// This should be an Error, but this is triggered for all variablse with SQL which isn't always a problem.
// This should be an Error, but this is triggered for all variables with SQL which isn't always a problem.
//'WordPress.DB.PreparedSQL.InterpolatedNotPrepared' => Warning::class,
];

Expand All @@ -27,6 +25,7 @@ public function check_against_phpcs() {
);
}


public function check_against_phpcs_review() {
if ( ! HAS_VENDOR ) {
return new Notice(
Expand All @@ -40,38 +39,52 @@ public function check_against_phpcs_review() {
);
}

protected function run_phpcs_standard( string $standard, array $args = [] ) {
$phpcs = new PHPCS();
$phpcs->set_standard( $standard );
/**
* Attempts to load Codesniffer and return a status if it's safe to use the runner.
*
* @since 0.2.2
*
* @return bool
*/
protected function load_codesniffer_runner(): bool {
if ( class_exists( '\PHP_CodeSniffer\Runner' ) ) {
return true;
}

$args = wp_parse_args(
$args,
array(
'extensions' => 'php', // Only check php files.
's' => true, // Show the name of the sniff triggering a violation.
// --ignore-annotations
)
);
// Include the PHPCS autoloader.
$autoloader = PLUGIN_DIR . '/vendor/squizlabs/php_codesniffer/autoload.php';

$report = $phpcs->run_json_report(
$this->path,
$args,
'array'
);
if ( file_exists( $autoloader ) ) {
include_once $autoloader;
}

if ( is_wp_error( $report ) ) {
return new Error(
$report->get_error_code(),
$report->get_error_message()
return class_exists( '\PHP_CodeSniffer\Runner' );
}

protected function run_phpcs_standard( string $standard, array $args = [] ) {
include_once PLUGIN_DIR . '/inc/class-phpcs-runner.php';

if ( ! $this->load_codesniffer_runner() ) {
return new Notice(
'phpcs_runner_not_found',
esc_html__( 'PHP Code Sniffer rulesets have not been tested, as the Code Sniffer Runner class is missing.', 'plugin-check' )
);
}

// If no response, either malformed output or PHP encountered an error.
if ( ! $report || empty( $report['files'] ) ) {
return false;
$phpcs = new PHPCS_Runner();
$phpcs->set_path( $this->path );
$phpcs->set_standard( $standard );

$results = $phpcs->run();

if ( is_wp_error( $results ) ) {
return new Error(
$results->get_error_code(),
$results->get_error_message()
);
}

return $this->phpcs_result_to_warnings( $report );
return $this->phpcs_result_to_warnings( $results );
}

protected function phpcs_result_to_warnings( $result ) {
Expand Down Expand Up @@ -109,7 +122,9 @@ protected function phpcs_result_to_warnings( $result ) {
$notice_class = self::NOTICE_TYPES[ $message['source'] ];
}

$source_code = esc_html( trim( file( $this->path . '/' . $filename )[ $message['line'] - 1 ] ) );
$file_path = dirname( $this->path ) . '/' . $filename;

$source_code = esc_html( trim( file( $file_path )[ $message['line'] - 1 ] ) );

if ( current_user_can( 'edit_plugins' ) ) {
$edit_link = sprintf(
Expand All @@ -128,7 +143,7 @@ protected function phpcs_result_to_warnings( $result ) {
$message['source'],
sprintf(
/* translators: 1: Type of Error 2: Line 3: File 4: Message 5: Code Example 6: Edit Link */
__( '%1$s Line %2$d of file %3$s.<br>%4$s.<br>%5$s%6$s', 'plugin-check' ),
__( '%1$s<br><br>Line %2$d of file <code>%3$s</code>.<br>%4$s.<br>%5$s%6$s', 'plugin-check' ),
"<strong>{$message['source']}</strong>",
$message['line'],
$filename,
Expand All @@ -153,7 +168,7 @@ protected function phpcs_result_to_warnings( $result ) {
*
* @return string|null File editor URL or null if not available.
*/
private function get_file_editor_url( $filename, $line ) {
protected function get_file_editor_url( $filename, $line ) {
if ( ! isset( $filename, $line ) ) {
return null;
}
Expand Down Expand Up @@ -228,12 +243,15 @@ private function get_file_editor_url( $filename, $line ) {
// Fall back to using the plugin editor if no external editor is offered.
if ( ! $edit_url ) {
$plugin_data = get_plugins( '/' . $this->slug );
if ( ! str_starts_with( $filename, $this->slug ) ) {
$filename = $this->slug . '/' . $filename;
}

return esc_url(
add_query_arg(
[
'plugin' => rawurlencode( $this->slug . '/' . array_key_first( $plugin_data ) ),
'file' => rawurlencode( $this->slug . '/' . $filename ),
'file' => rawurlencode( $filename ),
'line' => rawurlencode( $line ),
],
admin_url( 'plugin-editor.php' )
Expand Down
71 changes: 0 additions & 71 deletions inc/class-php-cli.php

This file was deleted.

Loading

0 comments on commit 23cce1e

Please sign in to comment.