johnbillion / query-monitor

The developer tools panel for WordPress
https://querymonitor.com
GNU General Public License v2.0
1.58k stars 207 forks source link

Query Monitor emits notices when 'file' and 'line' elements are not present in a stack trace #827

Open siliconforks opened 9 months ago

siliconforks commented 9 months ago

Consider the following shortcode:

function buggy_shortcode( $atts, $content, $shortcode_tag ) {
    throw new \Exception();
}

add_shortcode( 'buggy_shortcode', 'buggy_shortcode' );

If I visit a page with [buggy_shortcode], it will throw an exception and Query Monitor will display a stack trace, as expected, but if I look in the error log I see some additional notices (note that I have WP_DEBUG set to true):

PHP Notice:  Undefined index: file in .../query-monitor/collectors/php_errors.php on line 364
...
PHP Notice:  Undefined index: line in .../query-monitor/collectors/php_errors.php on line 364

If you look at the stack trace itself you can see the problem:

buggy_shortcode()
wp-includes/shortcodes.php:433

do_shortcode_tag()

preg_replace_callback()
wp-includes/shortcodes.php:273

do_shortcode()
wp-includes/class-wp-hook.php:310

...

Here do_shortcode() calls preg_replace_callback(), which calls do_shortcode_tag() as its callback. Therefore, do_shortcode_tag() is not being called from PHP code - it is being called from the PHP interpreter itself, so there is no .php file and line number. As a result, the stack trace is missing 'file' and 'line' elements.

This behavior does not appear to be explicitly documented anywhere, although it seems it is hinted at in https://www.php.net/manual/en/function.debug-backtrace.php where it says, "The possible [my emphasis] returned elements are as follows ... " - which suggests that the elements may or may not be present.

johnbillion commented 9 months ago

Thanks for the report!