8000 [PropertyAccess] Throw an InvalidArgumentException when the type do not match by dunglas · Pull Request #17738 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] Throw an InvalidArgumentException when the type do not match #17738

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

Closed
wants to merge 10 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Test E_RECOVERABLE_ERROR
  • Loading branch information
dunglas committed Feb 16, 2016
commit 834f97030c0c4d3825d2168c02adcbb35c5175a2
6 changes: 5 additions & 1 deletion src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,11 @@ private function callMethod($object, $method, $value) {

// PHP 5
set_error_handler(function ($errno, $errstr) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for disturbing.
Maybe better to use something like this:

set_error_handler(function ($errno, $errstr) use ($object, $method) {
    if (E_RECOVERABLE_ERROR === $errno && false !== strpos($errstr, sprintf('passed to %s::%s() must', get_class($object), $method))) {
        throw new InvalidArgumentException($errstr);
    }

    return false;
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to not test the error message. It's not clean and it still does not handle all cases. Throwing an error (and not an exception) in a mutator should be very very rare in a real code.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What cases it does not handle?
If found no other messages in PHP source that match used pattern:
https://github.com/php/php-src/blob/be607e724c69c92f8cda72a45a13a26e7c439aec/Zend/zend_execute.c#L644
https://github.com/php/php-src/blob/be607e724c69c92f8cda72a45a13a26e7c439aec/Zend/zend_execute.c#L648
https://github.com/php/php-src/blob/be607e724c69c92f8cda72a45a13a26e7c439aec/Zend/zend_execute.c#L651

And it seems that message is same for all supported PHP 5 versions: https://3v4l.org/VZhkg
I prefer to use more bullet proof solution than relying that something wont happen

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A user can trigger an error with the same or a similar message.

What do you think about that @symfony/deciders

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users are not supposed to trigger errors with E_RECOVERABLE_ERROR type (there are specific ones with USER_...). So if there are other E_RECOVERABLE_ERROR errors that could be triggered by PHP here, we need to make sure only type errors are handled.

throw new InvalidArgumentException($errstr);
if (E_RECOVERABLE_ERROR === $errno) {
throw new InvalidArgumentException($errstr);
}

return false;
});

try {
Expand Down
0