diff --git a/src/Tracy/Helpers.php b/src/Tracy/Helpers.php index 9eda3f62d..00aa36ac7 100644 --- a/src/Tracy/Helpers.php +++ b/src/Tracy/Helpers.php @@ -170,35 +170,40 @@ public static function improveException(\Throwable $e): void // do nothing } elseif (preg_match('#^Call to undefined function (\S+\\\\)?(\w+)\(#', $message, $m)) { $funcs = array_merge(get_defined_functions()['internal'], get_defined_functions()['user']); - $hint = self::getSuggestion($funcs, $m[1] . $m[2]) ?: self::getSuggestion($funcs, $m[2]); - $message = "Call to undefined function $m[2](), did you mean $hint()?"; - $replace = ["$m[2](", "$hint("]; + if ($hint = self::getSuggestion($funcs, $m[1] . $m[2]) ?: self::getSuggestion($funcs, $m[2])) { + $message = "Call to undefined function $m[2](), did you mean $hint()?"; + $replace = ["$m[2](", "$hint("]; + } } elseif (preg_match('#^Call to undefined method ([\w\\\\]+)::(\w+)#', $message, $m)) { - $hint = self::getSuggestion(get_class_methods($m[1]) ?: [], $m[2]); - $message .= ", did you mean $hint()?"; - $replace = ["$m[2](", "$hint("]; + if ($hint = self::getSuggestion(get_class_methods($m[1]) ?: [], $m[2])) { + $message .= ", did you mean $hint()?"; + $replace = ["$m[2](", "$hint("]; + } } elseif (preg_match('#^Undefined property: ([\w\\\\]+)::\$(\w+)#', $message, $m)) { $rc = new \ReflectionClass($m[1]); $items = array_filter($rc->getProperties(\ReflectionProperty::IS_PUBLIC), fn($prop) => !$prop->isStatic()); - $hint = self::getSuggestion($items, $m[2]); - $message .= ", did you mean $$hint?"; - $replace = ["->$m[2]", "->$hint"]; + if ($hint = self::getSuggestion($items, $m[2])) { + $message .= ", did you mean $$hint?"; + $replace = ["->$m[2]", "->$hint"]; + } } elseif (preg_match('#^Access to undeclared static property:? ([\w\\\\]+)::\$(\w+)#', $message, $m)) { $rc = new \ReflectionClass($m[1]); $items = array_filter($rc->getProperties(\ReflectionProperty::IS_STATIC), fn($prop) => $prop->isPublic()); - $hint = self::getSuggestion($items, $m[2]); - $message .= ", did you mean $$hint?"; - $replace = ["::$$m[2]", "::$$hint"]; + if ($hint = self::getSuggestion($items, $m[2])) { + $message .= ", did you mean $$hint?"; + $replace = ["::$$m[2]", "::$$hint"]; + } } - if (isset($hint)) { + $ref = new \ReflectionProperty($e, 'message'); + $ref->setAccessible(true); + $ref->setValue($e, $message); + + if (isset($replace)) { $loc = Debugger::mapSource($e->getFile(), $e->getLine()) ?? ['file' => $e->getFile(), 'line' => $e->getLine()]; - $ref = new \ReflectionProperty($e, 'message'); - $ref->setAccessible(true); - $ref->setValue($e, $message); @$e->tracyAction = [ // dynamic properties are deprecated since PHP 8.2 'link' => self::editorUri($loc['file'], $loc['line'], 'fix', $replace[0], $replace[1]), 'label' => 'fix it',