From 9fcad11b0fe631c45a557e6fa1583a229019f925 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 27 Apr 2023 15:22:06 -0700 Subject: [PATCH 1/3] Call previous error handler at end of `QM_Collector_PHP_Errors::error_handler()` --- collectors/php_errors.php | 5 ++- tests/integration/CollectorPhpErrorsTest.php | 32 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/collectors/php_errors.php b/collectors/php_errors.php index 00a184b4d..1d1c71b8a 100644 --- a/collectors/php_errors.php +++ b/collectors/php_errors.php @@ -280,11 +280,14 @@ public function error_handler( $errno, $message, $file = null, $line = null, $co * Filters the PHP error handler return value. This can be used to control whether or not the default error * handler is called after Query Monitor's. * + * Return true to stop further error handling, false to run the normal error handler. + * * @since 2.7.0 * * @param bool $return_value Error handler return value. Default false. */ - return apply_filters( 'qm/collect/php_errors_return_value', false ); + return apply_filters( 'qm/collect/php_errors_return_value', false ) + || ( is_callable( $this->previous_error_handler ) && call_user_func($this->previous_error_handler, func_get_args() ) ); } diff --git a/tests/integration/CollectorPhpErrorsTest.php b/tests/integration/CollectorPhpErrorsTest.php index bc741d080..f1947ec88 100644 --- a/tests/integration/CollectorPhpErrorsTest.php +++ b/tests/integration/CollectorPhpErrorsTest.php @@ -248,4 +248,36 @@ function testItWillFilterNoticesFromPlugin(): void { self::assertArrayNotHasKey( 'warning', $actual->silenced ); self::assertEquals( 1, count( $actual->silenced['notice'] ) ); } + + function testItReturnsFalseWhenNoPreviousErrorHandler(): void { + $result = $this->collector->error_handler( E_WARNING, 'test' ); + + self::assertFalse( $result ); + } + + function testItCallsFilterForErrorHandlerResult(): void { + add_filter( 'qm/collect/php_errors_return_value', '__return_true' ); + + $result = $this->collector->error_handler( E_WARNING, 'test' ); + + self::assertTrue( $result ); + } + + function testItCallsPreviousErrorHandler(): void { + // Test needs the previous error handler set before the sut is set up. + ini_set( 'display_errors', '0' ); + $this->collector->set_up(); + $this->collector->tear_down(); + + set_error_handler(function( $errno, $message, $file = null, $line = null, $context = null, $do_trace = true ) { + return true; + }); + + $this->collector = new \QM_Collector_PHP_Errors(); + $this->collector->set_up(); + + $result = $this->collector->error_handler( E_WARNING, 'test' ); + + self::assertTrue( $result ); + } } From 8b38cc8fd6db380782980c6db80243de2332c35b Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 27 Apr 2023 15:24:13 -0700 Subject: [PATCH 2/3] lint --- collectors/php_errors.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collectors/php_errors.php b/collectors/php_errors.php index 1d1c71b8a..87ab76b55 100644 --- a/collectors/php_errors.php +++ b/collectors/php_errors.php @@ -287,7 +287,7 @@ public function error_handler( $errno, $message, $file = null, $line = null, $co * @param bool $return_value Error handler return value. Default false. */ return apply_filters( 'qm/collect/php_errors_return_value', false ) - || ( is_callable( $this->previous_error_handler ) && call_user_func($this->previous_error_handler, func_get_args() ) ); + || ( is_callable( $this->previous_error_handler ) && call_user_func( $this->previous_error_handler, func_get_args() ) ); } From 83e70e4736664a5c29cd5105502254e1030aa818 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 3 May 2023 09:05:25 -0700 Subject: [PATCH 3/3] Change `call_user_func` to `call_user_func_array`. --- collectors/php_errors.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collectors/php_errors.php b/collectors/php_errors.php index 87ab76b55..297380c4f 100644 --- a/collectors/php_errors.php +++ b/collectors/php_errors.php @@ -287,7 +287,7 @@ public function error_handler( $errno, $message, $file = null, $line = null, $co * @param bool $return_value Error handler return value. Default false. */ return apply_filters( 'qm/collect/php_errors_return_value', false ) - || ( is_callable( $this->previous_error_handler ) && call_user_func( $this->previous_error_handler, func_get_args() ) ); + || ( is_callable( $this->previous_error_handler ) && call_user_func_array( $this->previous_error_handler, func_get_args() ) ); }