Skip to content

Commit

Permalink
Helpers::improveException() refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 3, 2024
1 parent c7306c7 commit bb82fbd
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/Tracy/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit bb82fbd

Please sign in to comment.