diff --git a/src/Tracy/Helpers.php b/src/Tracy/Helpers.php index 82daf16fd..2254bf1f2 100644 --- a/src/Tracy/Helpers.php +++ b/src/Tracy/Helpers.php @@ -174,7 +174,7 @@ public static function improveException($e) $hint = self::getSuggestion($funcs, $m[1] . $m[2]) ?: self::getSuggestion($funcs, $m[2]); $message = "Call to undefined function $m[2](), did you mean $hint()?"; - } elseif (preg_match('#^Call to undefined method (\S+)::(\w+)#', $message, $m)) { + } 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()?"; @@ -182,13 +182,13 @@ public static function improveException($e) $hint = self::getSuggestion(array_keys($e->context), $m[1]); $message = "Undefined variable $$m[1], did you mean $$hint?"; - } elseif (preg_match('#^Undefined property: (\S+)::\$(\w+)#', $message, $m)) { + } elseif (preg_match('#^Undefined property: ([\w\\\\]+)::\$(\w+)#', $message, $m)) { $rc = new \ReflectionClass($m[1]); $items = array_diff($rc->getProperties(\ReflectionProperty::IS_PUBLIC), $rc->getProperties(\ReflectionProperty::IS_STATIC)); $hint = self::getSuggestion($items, $m[2]); $message .= ", did you mean $$hint?"; - } elseif (preg_match('#^Access to undeclared static property: (\S+)::\$(\w+)#', $message, $m)) { + } elseif (preg_match('#^Access to undeclared static property: ([\w\\\\]+)::\$(\w+)#', $message, $m)) { $rc = new \ReflectionClass($m[1]); $items = array_intersect($rc->getProperties(\ReflectionProperty::IS_PUBLIC), $rc->getProperties(\ReflectionProperty::IS_STATIC)); $hint = self::getSuggestion($items, $m[2]); diff --git a/tests/Tracy/Helpers.improveException.phpt b/tests/Tracy/Helpers.improveException.phpt index ea519c4d3..04bbdb259 100644 --- a/tests/Tracy/Helpers.improveException.phpt +++ b/tests/Tracy/Helpers.improveException.phpt @@ -97,6 +97,15 @@ test(function () use ($obj) { // suggest only public method Assert::same('Call to undefined method TestClass::protectedMethodX()', $e->getMessage()); }); +test(function () { // do not suggest anything when accessing anonymous class + try { + $obj = new class {}; + $obj->method(); + } catch (\Error $e) {} + Helpers::improveException($e); + Assert::same('Call to undefined method class@anonymous::method()', $e->getMessage()); +}); + // reading test(function () use ($obj) { @@ -136,6 +145,24 @@ test(function () use ($obj) { // suggest only public static property Assert::same('Access to undeclared static property: TestClass::$protectedMethodX', $e->getMessage()); }); +test(function () { // do not suggest anything when accessing anonymous class + $obj = new class {}; + @$val = $obj->property; + $e = new ErrorException(error_get_last()['message'], 0, error_get_last()['type']); + Helpers::improveException($e); + Assert::same('Undefined property: class@anonymous::$property', $e->getMessage()); +}); + +test(function () { // do not suggest anything when accessing anonymous class + try { + $obj = new class {}; + @$val = $obj::$property; + } catch (\Error $e) {} + $e = new ErrorException(error_get_last()['message'], 0, error_get_last()['type']); + Helpers::improveException($e); + Assert::same('Undefined property: class@anonymous::$property', $e->getMessage()); +}); + // variables test(function () use ($obj) {