Skip to content

[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

Conversation

SpacePossum
Copy link
Contributor

@SpacePossum SpacePossum commented Dec 21, 2016

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

$candidates = array();
foreach (get_class_methods($className) as $definedMethodName) {
foreach ($methods as $definedMethodName) {
Copy link
Contributor

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) {

Copy link
Contributor Author

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.

Copy link
Contributor

@ogizanagi ogizanagi Dec 21, 2016

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.

Copy link
Contributor Author

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)) {
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

@SpacePossum SpacePossum Dec 27, 2016

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!

@SpacePossum SpacePossum changed the title [Debug] UndefinedMethodFatalErrorHandler - Handle anonymous class without methods correctly. [Debug] UndefinedMethodFatalErrorHandler - Handle anonymous classes Dec 22, 2016
@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Dec 26, 2016
@SpacePossum SpacePossum force-pushed the 2_7_bug_UndefinedMethodFatalErrorHandler_anonymous_class_without_methods branch from ca46f5e to 591b2ca Compare December 27, 2016 06:12
@xabbuh
Copy link
Member

xabbuh commented Dec 27, 2016

👍

@nicolas-grekas
Copy link
Member

Thank you @SpacePossum.

nicolas-grekas added a commit that referenced this pull request Dec 27, 2016
…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
@SpacePossum
Copy link
Contributor Author

thanks reviewers :)

@SpacePossum SpacePossum deleted the 2_7_bug_UndefinedMethodFatalErrorHandler_anonymous_class_without_methods branch December 27, 2016 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants