Skip to content

Conversation

ahilke
Copy link
Contributor

@ahilke ahilke commented Nov 28, 2019

This adjusts the return type of json_decode/json_encode for arbitrary bitwise-or-expressions containing JSON_THROW_ON_ERROR.
Not sure if this is the best approach, but I do think there is a valid use case for this.
For example, passing through other options and adding JSON_THROW_ON_ERROR like this:

function encode(string $input, int $options): string { return json_encode($input, JSON_THROW_ON_ERROR | $options); }

Playground

Problem: JSON_THROW_ON_ERROR option is not correctly detected when combined with an arbitrary integer. A use case for this could look like this: <?php function encodeWithOptions(string $input, int $options): string {	return json_encode($input, JSON_THROW_ON_ERROR | $options); } ?>
@ahilke ahilke force-pushed the allow-json-throw-on-error-with-any-int branch from 49b0409 to 017f728 Compare November 28, 2019 01:12
If the options argument is a (nested) bitwise or expression containing JSON_THROW_ON_ERROR, constrict the return type as expected.
@ahilke ahilke force-pushed the allow-json-throw-on-error-with-any-int branch from 017f728 to b9e6878 Compare November 28, 2019 01:19
@ondrejmirtes ondrejmirtes merged commit ecce7b7 into phpstan:master Nov 28, 2019
@ondrejmirtes
Copy link
Member

Awesome, thanks!

@ahilke ahilke deleted the allow-json-throw-on-error-with-any-int branch November 28, 2019 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants