Conversation
`$status_code` is declared as `int` but was sometimes null or string. Downstream: FreshRSS/FreshRSS#4301
|
Would not having it type |
@jtojnar That would change the contract, and my personal preference is towards simpler types rather than more complex types, but besides that I do not really mind. In the case of my client, it would add a bit of code to check the returned type and/or cast to int (null to 0) before sending it to the rest of the pipeline that is expecting an HTTP status to be an integer. |
|
Yeah, it would change the contract, but so does changing empty string to zero. Technically, zero would be an okay choice since it is not actually a valid status code but I generally prefer more explicit types for representing exceptional states over magic values. I have not really looked into the API but I would expect any consumer to filter such states out so they would never make it further in the pipeline anyway. |
That is the point of the contract. SimplePie has been promising an I do not really mind, but I believe my current suggestion is marginally more efficient. |
That really depends on what you consider the contract. Observing Hyrum's Law you could just as well say the contract is The only thing that is certain is that there is a discrepancy between the phpdoc annotation and the implementation. As for API design, I generally prefer having fewer uncertainties by making corner cases explicit over code that something that appears to be simpler but the complexity is still there. You will almost certainly still need to have something like Thinking about this more, this is actually a BC break so we might need to change the PHPDoc annotation to |
Strictly speaking, you are right. But the PHPDoc has always indicated that the value must be an integer. But that sometimes In the future you would avoid such bugs by defining the property type. But as long as we can't do that, I would work towards the simplified types. |
Just for the record, in the case of my client downstream, the only assumption was that I should get an |
|
I would expect any function accepting https://github.com/guzzle/psr7/blob/5c693242bede743c23402bc5b9de62da04a882d7/src/Response.php#L101 |
That is precisely going in the same direction than my argumentation, thanks :-)
https://github.com/guzzle/psr7/blob/5c693242bede743c23402bc5b9de62da04a882d7/src/Response.php#L154 So such a code might currently be breaking in some cases, before this PR. |
|
Right, the typehint of the |
|
I think this is clear enough, if you want to propose a change @jtojnar I'm happy to look at it but will to merge this for now. |
This is to distinguish a status_code being set on the type level, making it harder to forget the case – static analysis would notify you. This is technically a BC break but so is simplepie#728 and `null` behaves just like `0` from the perspective of non-strict comparison so I would not expect issues in legacy applications.
|
I opened #732 |
This is to distinguish a status_code being set on the type level, making it harder to forget the case – static analysis would notify you. This is technically a BC break but so is simplepie#728 and `null` behaves just like `0` from the perspective of non-strict comparison so I would not expect issues in legacy applications.
This is to distinguish a status_code being set on the type level, making it harder to forget the case – static analysis would notify you. To make it more consistent, we also change the few cases that could make `status_code` contain a `0` to replace it with `null`: - `CURLINFO_HTTP_CODE` can contain `0`, but only when the request fails with an error such as `CURLE_COULDNT_RESOLVE_HOST`, in which case the File constructor will fail. - HTTP parser, which could return `0` when a web server decides to return it as a status code for some reason. I added a code that will fail for `0` status code (though other invalid HTTP response codes may still appear successful). - Successful local requests were already using `null` correctly. While at it, simplify few conditionals using de Morgan’s laws for easier reasoning. This is technically a BC break but so is simplepie#728 and `null` behaves just like `0` from the perspective of non-strict comparison so I would not expect issues in legacy applications.
This is to distinguish a status_code being set on the type level, making it harder to forget the case – static analysis would notify you. To make it more consistent, we also change the few cases that could make `status_code` contain a `0` to replace it with `null`: - `CURLINFO_HTTP_CODE` can contain `0`, but only when the request fails with an error such as `CURLE_COULDNT_RESOLVE_HOST`, in which case the File constructor will fail. - HTTP parser, which could return `0` when a web server decides to return it as a status code for some reason. I added a code that will fail for `0` status code (though other invalid HTTP response codes may still appear successful). - Successful local requests were already using `null` correctly. While at it, simplify few conditionals using de Morgan’s laws for easier reasoning. This is technically a BC break but so is simplepie#728 and `null` behaves just like `0` from the perspective of non-strict comparison so I would not expect issues in legacy applications.
$status_codeis declared asintbut was sometimesnullorstring, breaking some contracts.Downstream: FreshRSS/FreshRSS#4301