-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DX] Fix phpdoc for serializer normalizers exceptions #22231
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
Exceptions are a BC Break. Some implementations of the interface may have returned a null or empty value because exceptions are not allowed. Suddenly a new version allow exception in interface, so lots of implementations can change their mind and throw an exception. That break my code. |
This PR doesn't actually break backward compatibility, it just fix the PHPDoc. |
cb1e746
to
f05d0ed
Compare
You fix the PHPDoc on an interface, so my comment is valid. |
* @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException | ||
* @throws \Symfony\Component\Serializer\Exception\ExtraAttributesException | ||
* @throws \Symfony\Component\Serializer\Exception\LogicException | ||
* @throws \Symfony\Component\Serializer\Exception\RuntimeException |
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.
We are using use
statements instead of FQCN.
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.
Also, we don't document exceptions if we don't also add when this can be thrown.
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.
exception should ALWAYS be in PHPDoc, you can't know when an interface throw an exception or not and you can't work properly with interface when you don't know that.
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.
where is the rule to specify the exception for doc in the interface ??? This is specific logic exception, not business exception.
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.
@fabpot that's a good point. I'll document the "why" exceptions are used for. Currently, it's not easy to get it at first glance.
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.
you can't know when an interface throw an exception or not
Interfaces don't throw exceptions, their implementations do. I disagree with putting them into interface, because when you make different implementation which don't throw those exceptions, you are not respecting the interface. Interface specifies what class implementing it MUST implement. I don't see a need here to force the implementation to use these exceptions
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.
throwing an exception is not a mandatory when you implement an interface. For a simple reason, exception is not a mandatory output, it can happen, or not.
A user shouldn't catch LogicExceptions, so you should not need to document them: they are sign that you need to fix something in your code (usually configuration related). |
Arguments is in interface ? Return is in interface ? We have put return in PHPDoc ? Why exceptions must be un PHPDoc for an interface so ? |
@theofidry that's not always true. |
@syrm which is why It's not an absolute rule, but if you were to compare it to Java that's the would somewhat translate to a compilation error and runtime errors. It's just that in PHP we don't have this compilation step. |
DatabaseOffline can be a LogicException, you don't know. |
To me, we just need a single line in the phpdoc, using a |
Should be applied on 2.7 also. |
If you are going to add them then you are missing a few places:
|
+1 for @nicolas-grekas' solution |
What would be the benefit to add those generic exceptions? I'm 👎 |
If you don't want a generic exception you should rewrite the existing exception because they're very specialized : + * @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException
+ * @throws \Symfony\Component\Serializer\Exception\ExtraAttributesException
+ * @throws \Symfony\Component\Serializer\Exception\LogicException
+ * @throws \Symfony\Component\Serializer\Exception\RuntimeException |
@nicolas-grekas disagree. Most of the classes I modify doesn't even exist in 2.7. I also disagree with adding a standard exception in phpdoc because to me the PHPDoc would be useless (and this PR would also be). You can choose to just close it if you think so. I fixed the FQCN, and added a note for cases when exceptions are throw. Please notice that there is a problem of coherence of throwed exceptions... |
@@ -27,6 +34,13 @@ | |||
* @param array $context options available to the denormalizer | |||
* | |||
* @return object | |||
* | |||
* @throws BadMethodCallException Occurs when the normalizer is not call in an expected context |
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.
[...] is not called [...]
* @throws UnexpectedValueException Occurs when the item cannot be hydrated with the given data | ||
* @throws ExtraAttributesException Occurs when the item doesn't have attribute to receive given data | ||
* @throws LogicException Occurs when the normalizer is not supposed to denormalize | ||
* @throws RuntimeException Occurs if the class cannot be instantiate |
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.
instantiated
@@ -26,6 +30,11 @@ | |||
* @param array $context Context options for the normalizer | |||
* | |||
* @return array|scalar | |||
* | |||
* @throws InvalidArgumentException Occurs when the object given is not an attempted type for the normalizer | |||
* @throws CircularReferenceException Occurs when the normalizer detect a circular reference when no circular |
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.
detects
* @throws InvalidArgumentException Occurs when the object given is not an attempted type for the normalizer | ||
* @throws CircularReferenceException Occurs when the normalizer detect a circular reference when no circular | ||
* reference handler can fix it | ||
* @throws LogicException Occurs when the normalizer is not call in an expected context |
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.
called
@syrm unrecoverable exceptions (i.e. without code patch) like Runtime|Logic hierarchies or implementation specific exceptions should not be part of the interface IMHO |
It should @NoiseByNorthwest INPUT/OUTPUT is the part of contract. |
@syrm have fun with all of your interfaces bloated with irrelevant Runtime|Logic like exceptions.... |
@NoiseByNorthwest so interface are bad written and we should continue like this ? |
@syrm what's the point of documenting an exception that is only here to tell you you didn't configure something properly? Your application should not have to deal with them, only you. If you have an edge case where you need that, they you can always catch |
You know the contract of an interface can't be incomplete because you think there is not interest to have a complete contract. And as a developer I won't put a try catch \Exception everywhere because developer are too lazy to describe the contract of interface fully. Then, a wrong configuration can happen only on production and when it's an optional part a webpage you don't want crash the full page. |
It's not a lack of interest in a complete contract, it's that it's different contracts for different targets. A So it's not laziness in the contract description, it's you trying to tweak the contract to use it differently than it's meant to be. You're free to add yourself more work and ensure to have contracts which can be tweaked that way, but don't blame people if they want to be more pragmatic and not take care of this. But if you're not sensible to those arguments let's change the angle and see it the other way around: bring a case where adding those elements to the contract are meaningful. If there is a point, I'm sure no one here will disagree with adding those exceptions to the doc. It's just that as it is now it doesn't provide anything than additional noise and/or confusion. |
RuntimeException is also for developer. Throw new RuntimeException("can't connect to database myprivatedatabase"). When you don't catch this sensitive information can be shown on you page. Every exception is for developer because it can crash your application when you don't handle it. Add these information don't add additional noise or confusion. And when it add noise or confusion that mean this exception is useless or not specific. So you should rethink your interface to remove these exception or rewrite them. |
Here is the problem, you dont get RuntimeException semantic. A database connection error is not a RuntimeException because its origin is not necessarily a program flaw. |
The normalizers throw exceptions. They need to be documented for DX in the normalizer/denormalizer interfaces. [Serializer] fit Symfony phpdoc standard It also adds meaning of each exception throw. Fix grammary in phpdoc
af10b21
to
836759b
Compare
@xabbuh 👍 |
Thank you @Nek-. |
…ek-) This PR was submitted for the master branch but it was merged into the 3.2 branch instead (closes #22231). Discussion ---------- [DX] Fix phpdoc for serializer normalizers exceptions The normalizers throw exceptions. They need to be documented for DX in the normalizer/denormalizer interfaces. | Q | A | ------------- | --- | Branch? | master (rebase on 2.7 if you want?) | Bug fix? | no | New feature? | yes? | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22218 | License | MIT | Doc PR | Not needed Commits ------- ec3cc84 Fix phpdoc for serializer normalizers exceptions
The normalizers throw exceptions. They need to be documented for DX in the normalizer/denormalizer interfaces.