-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
[Debug] UndefinedMethodFatalErrorHandler - Handle anonymous classes #21010
Conversation
$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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
👎
I think code should be as explicit as possible. Here we want to iterate methods if there are any. We don't want to cast the result to an array and iterate that and be depended on the result of the cast. To me it makes no sense that (array) null
result in an empty array, I would expect an error. Another side effect of always casting is that it would be done even when not needed.
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.
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 class@anonymous
not being a class name, which is why get_class_methods
returns null
as a way to notify there is an issue with the function call, not as I first thought a PHP inconsistency due to an anonymous class without any method. You don't access the generated internal class name here nor the instance, so it's expected to get null
.
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.
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 array
cast is a deal breaker for the PR to be merged, always happy to agree to disagree and update a PR if the end-result is better than the current state :)
@@ -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)) { |
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 return null
when passing a "class name" of an anonymous class. Checking the return value of get-class-methods
for null
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?
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 the class_exists
check first but still check the result of get_class_methods
. This way the code will keep working even if get_class_methods
might return null
later on because of changes in the implementation. Let me know what you think!
ca46f5e
to
591b2ca
Compare
👍 |
Thank you @SpacePossum. |
…s classes (SpacePossum) This PR was squashed before being merged into the 2.7 branch (closes #21010). Discussion ---------- [Debug] UndefinedMethodFatalErrorHandler - Handle anonymous classes | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT When trying to call a method on an anonymous class (with or without methods) the `UndefinedMethodFatalErrorHandler` tries to iterate `null`. For ref.: https://3v4l.org/l26u1 Commits ------- ed713ae [Debug] UndefinedMethodFatalErrorHandler - Handle anonymous classes
thanks reviewers :) |
When trying to call a method on an anonymous class (with or without methods) the
UndefinedMethodFatalErrorHandler
tries to iteratenull
.For ref.: https://3v4l.org/l26u1