Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,27 @@ final public function run( Check_Result $result ) {
$_SERVER['argv'] = $this->parse_argv( $args, $defaults );

// Run PHPCS.
$this->register_php_codesniffer_error_handler();

try {
ob_start();
$runner = new Runner();
$runner->runPHPCS();
$reports = ob_get_clean();
} catch ( Exception $e ) {
$_SERVER['argv'] = $orig_cmd_args;
if ( ob_get_level() > 0 ) {
ob_end_clean();
}
throw $e;
}
} finally {
restore_error_handler();

// Reset installed_paths.
Config::setConfigData( 'installed_paths', $installed_paths, true );
// Reset installed_paths.
Config::setConfigData( 'installed_paths', $installed_paths, true );

// Restore original arguments.
$_SERVER['argv'] = $orig_cmd_args;
// Restore original arguments.
$_SERVER['argv'] = $orig_cmd_args;
}

// Parse the reports into data to add to the overall $result.
$reports = json_decode( trim( $reports ), true );
Expand Down Expand Up @@ -229,6 +235,35 @@ private function get_argv_defaults( Check_Result $result ): array {
return $defaults;
}

/**
* Registers an error handler for known PHPCS notices.
*
* @since n.e.x.t
*/
private function register_php_codesniffer_error_handler() {
$previous_error_handler = null;

$previous_error_handler = set_error_handler(
static function ( $errno, $errstr, $errfile, $errline ) use ( &$previous_error_handler ) {
$normalized_file = wp_normalize_path( $errfile );

if (
E_DEPRECATED === $errno &&
false !== strpos( $errstr, 'auto_detect_line_endings is deprecated' ) &&
false !== strpos( $normalized_file, 'vendor/squizlabs/php_codesniffer/src/Runner.php' )
) {
return true;
}

if ( is_callable( $previous_error_handler ) ) {
return (bool) call_user_func( $previous_error_handler, $errno, $errstr, $errfile, $errline );
}

return false;
}
);
}

/**
* Resets \PHP_CodeSniffer\Config::$overriddenDefaults to prevent
* incorrect results when running multiple checks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,35 @@ public function test_run_without_errors() {
$this->assertEquals( 0, $check_result->get_error_count() );
$this->assertEquals( 0, $check_result->get_warning_count() );
}

public function test_run_suppresses_phpcs_auto_detect_line_endings_deprecation() {
$plugin_review_phpcs_check = new Plugin_Review_PHPCS_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-review-phpcs-without-errors/load.php' );
$check_result = new Check_Result( $check_context );

$previous_error_handler = null;

$previous_error_handler = set_error_handler(
static function ( $errno, $errstr, $errfile, $errline ) use ( &$previous_error_handler ) {
if ( E_DEPRECATED === $errno && false !== strpos( $errstr, 'auto_detect_line_endings is deprecated' ) ) {
throw new ErrorException( $errstr, 0, $errno, $errfile, $errline );
}

if ( is_callable( $previous_error_handler ) ) {
return (bool) call_user_func( $previous_error_handler, $errno, $errstr, $errfile, $errline );
}

return false;
}
);

try {
$plugin_review_phpcs_check->run( $check_result );
} finally {
restore_error_handler();
}

$this->assertEquals( 0, $check_result->get_error_count() );
$this->assertEquals( 0, $check_result->get_warning_count() );
}
}
Loading