From be25d2805aba14861d3771243cd279281255313c Mon Sep 17 00:00:00 2001 From: George Nicolaou Date: Sun, 21 Jun 2026 14:38:52 +0300 Subject: [PATCH 1/3] Suppress PHPCS line ending deprecation notice --- .../Checks/Abstract_PHP_CodeSniffer_Check.php | 33 +++++++++++++++++++ .../Plugin_Review_PHPCS_Check_Tests.php | 25 ++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php index d3bde6a40..cf2c23e4c 100644 --- a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php +++ b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php @@ -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(); @@ -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(); } // Reset installed_paths. @@ -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. diff --git a/tests/phpunit/tests/Checker/Checks/Plugin_Review_PHPCS_Check_Tests.php b/tests/phpunit/tests/Checker/Checks/Plugin_Review_PHPCS_Check_Tests.php index 0e0779dba..4fc753dd9 100644 --- a/tests/phpunit/tests/Checker/Checks/Plugin_Review_PHPCS_Check_Tests.php +++ b/tests/phpunit/tests/Checker/Checks/Plugin_Review_PHPCS_Check_Tests.php @@ -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; + } + ); + + 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() ); + } } From fc290f0e9d82825df6f3c7936ad7ee593bff7a30 Mon Sep 17 00:00:00 2001 From: George Nicolaou Date: Tue, 7 Jul 2026 13:27:53 +0300 Subject: [PATCH 2/3] Ensure cleanup always runs after PHPCS, even on exception --- .../Checks/Abstract_PHP_CodeSniffer_Check.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php index cf2c23e4c..bf0c8f966 100644 --- a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php +++ b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php @@ -118,17 +118,19 @@ final public function run( Check_Result $result ) { $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 ); From a45794bbea3424abf539d9a40c85b77996f7fffc Mon Sep 17 00:00:00 2001 From: George Nicolaou Date: Tue, 7 Jul 2026 13:30:31 +0300 Subject: [PATCH 3/3] Update error handler to maintain previous state Refactor error handler to preserve previous handler. --- .../Checker/Checks/Plugin_Review_PHPCS_Check_Tests.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/Checker/Checks/Plugin_Review_PHPCS_Check_Tests.php b/tests/phpunit/tests/Checker/Checks/Plugin_Review_PHPCS_Check_Tests.php index 4fc753dd9..4f2b10d77 100644 --- a/tests/phpunit/tests/Checker/Checks/Plugin_Review_PHPCS_Check_Tests.php +++ b/tests/phpunit/tests/Checker/Checks/Plugin_Review_PHPCS_Check_Tests.php @@ -167,12 +167,18 @@ public function test_run_suppresses_phpcs_auto_detect_line_endings_deprecation() $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 ) { + $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; } );