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
33 changes: 33 additions & 0 deletions includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ 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();
Expand All @@ -118,6 +120,8 @@ final public function run( Check_Result $result ) {
} catch ( Exception $e ) {
$_SERVER['argv'] = $orig_cmd_args;
throw $e;
} finally {
restore_error_handler();
}
Comment on lines 113 to 125

// Reset installed_paths.
Expand Down Expand Up @@ -229,6 +233,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,29 @@ 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 );

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

return false;
}
);
Comment on lines +170 to +178

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