Skip to content

Commit

Permalink
Helpers::improveException() fixed suggestion when calling undefined m…
Browse files Browse the repository at this point in the history
…ethod of anonymous class [Closes #248]
  • Loading branch information
Pavel Janda authored and dg committed Feb 19, 2017
1 parent 2f4a70d commit a8489ba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Tracy/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,21 @@ 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()?";

} elseif (preg_match('#^Undefined variable: (\w+)#', $message, $m) && !empty($e->context)) {
$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]);
Expand Down
27 changes: 27 additions & 0 deletions tests/Tracy/Helpers.improveException.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a8489ba

Please sign in to comment.