Skip to content

[PropertyAccess] Throw an UnexpectedTypeException when the type do not match #18032

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 2 commits into from

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Mar 6, 2016

Q A
Branch 2.3
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #17738
License MIT
Doc PR n/a

This an alternative to #17738.

After discussing with @nicolas-grekas, it appears that the current implementation is error prone because it throws a \TypeError that is an Error in PHP 7 but a regular Exception in PHP 5 because it uses the Symfony polyfill.
Programs wrote in PHP 5 and catching all exceptions will catch this "custom" \TypeError but not those wrote in PHP 7. It can be very hard to debug.

In this alternative implementation:

  • catching type mismatch is considered a bug fix and applied to Symfony 2.3
  • for every PHP versions, a domain exception is thrown

* @throws \Exception
*/
private function callMethod($object, $method, $value) {
if (PHP_MAJOR_VERSION >= 7) {
Copy link
Member

Choose a reason for hiding this comment

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

we usually prefer comparing against PHP_VERSION_ID

@webmozart
Copy link
Contributor

Is this really necessary? This looks like adding a lot of overhead to a very sensitive part of the component with little benefit.

@dunglas
Copy link
Member Author

dunglas commented Mar 7, 2016

@webmozart the component is used in the Serializer (ObjectNormalizer) and without that it's not possible to catch type errors like the following:

POST /url

{"foo": 2}
class MyEntity
{
    public function setFoo(string $foo)
    {
        // ...
    }
}

It currently fail with a 500 HTTP code, but it should be properly caught and throw a 400 HTTP code.

@webmozart
Copy link
Contributor

What about doing the check and setting the error handler in the serializer (or at some other, higher level) instead of doing it for every single property access?

@dunglas
Copy link
Member Author

dunglas commented Mar 7, 2016

Of course we can do that but IMO it's the responsibility of the PropertyAccess component to do that (it can be useful in many contexts).

@webmozart
Copy link
Contributor

@dunglas Technically I agree. My concern is that the code in question can be called a considerable number of times for big forms. I think we should avoid adding (even more) code here unless absolutely necessary.

@dunglas
Copy link
Member Author

dunglas commented Mar 7, 2016

The heavy code (the reflection part) will be called only when a problem occurs isnt'it?

@nicolas-grekas
Copy link
Member

wdyt about moving the type-error logic in setValue, out of the for loop?

@nicolas-grekas
Copy link
Member

Replaced by #18210

fabpot added a commit that referenced this pull request Mar 18, 2016
… type do not match (dunglas, nicolas-grekas)

This PR was merged into the 2.3 branch.

Discussion
----------

[PropertyAccess] Throw an UnexpectedTypeException when the type do not match

| Q             | A
| ------------- | ---
| Branch?       | 2.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17738, #18032
| License       | MIT
| Doc PR        | -

Made in coordination with @dunglas,
Diff best viewed [ignoring whitspaces](https://github.com/symfony/symfony/pull/18210/files?w=1).

Quoting #18032:

> it appears that the current implementation is error prone because it throws a `\TypeError` that is an `Error` in PHP 7 but a regular `Exception` in PHP 5 because it uses the Symfony polyfill.
Programs wrote in PHP 5 and catching all exceptions will catch this "custom"  `\TypeError ` but not those wrote in PHP 7. It can be very hard to debug.

> In this alternative implementation:

> * catching type mismatch is considered a bug fix and applied to Symfony 2.3
> * for every PHP versions, a domain exception is thrown

Commits
-------

5fe2b06 [PropertyAccess] Reduce overhead of UnexpectedTypeException tracking
10c8d5e [PropertyAccess] Throw an UnexpectedTypeException when the type do not match
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