-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Debug] UndefinedMethodFatalErrorHandler - Handle anonymous classes #21010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,8 +36,12 @@ public function handleError(array $error, FatalErrorException $exception) | |
|
||
$message = sprintf('Attempted to call an undefined method named "%s" of class "%s".', $methodName, $className); | ||
|
||
if (null === $methods = get_class_methods($className)) { | ||
return new UndefinedMethodException($message, $exception); | ||
} | ||
|
||
$candidates = array(); | ||
foreach (get_class_methods($className) as $definedMethodName) { | ||
foreach ($methods as $definedMethodName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could simply be foreach ((array) get_class_methods($className) as $definedMethodName) { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👎 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Casting to an array is something commonly used in the Symfony codebase, even in more critical paths where micro optimizations would matter more. To me the intention is clear, but I hear your arguments. Also the issue here is more about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, micro optimizations are not needed here as it is already in error/exception path so fast execution is not that important. Yes, sadly we cannot access the instance of the anonymous class here to figure out what methods are on there, so I guess this is the best we can do. Let me know if the |
||
$lev = levenshtein($methodName, $definedMethodName); | ||
if ($lev <= strlen($methodName) / 3 || false !== strpos($definedMethodName, $methodName)) { | ||
$candidates[] = $definedMethodName; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking at the comments below, this could be turned to:
if (!class_exists($className, false))
, isn'it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would decrease the number of failures indeed.
However the docs of get_class_methods state it will return
null
on error, not that it will only returnnull
when passing a "class name" of an anonymous class. Checking the return value ofget-class-methods
fornull
seems to me the safe bet.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we know any other condition that could make this return null? if not, it'd be more readable to use
class_exists
, don't you thing?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't check the implementation of
get_class_methods
, however the API docs. leave it open on when an error can happen. I've update the code in a way we've the best of the two solutions, i.e. we do theclass_exists
check first but still check the result ofget_class_methods
. This way the code will keep working even ifget_class_methods
might returnnull
later on because of changes in the implementation. Let me know what you think!