From be25d2805aba14861d3771243cd279281255313c Mon Sep 17 00:00:00 2001 From: George Nicolaou Date: Sun, 21 Jun 2026 14:38:52 +0300 Subject: [PATCH] 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() ); + } }