Skip to content

Conversation

EmilMassey
Copy link

@EmilMassey EmilMassey commented Mar 19, 2025

Assertion::isJsonString returns true if the value is not a string but is an accepted argument for the json_decode function. This is possible because the library works in non-strict mode, so PHP does not raise an error if a non-string argument is passed to json_decode.

Current behavior

None of these throw an exception:

Assert::that(true)->isJsonString();
Assert::that(10)->isJsonString();
Assert::that(2.5)->isJsonString();

Expected behavior

All of the above should throw AssertionFailedException

Psalm

Because the function is annotated with @psalm-assert =string $value, Psalm does not raise an issue with this snippet:

declare(strict_types=1);

function test(mixed $value): string {
    Assertion::isJsonString($value);

    return $value;
}

echo test(5);

This will actually fail at runtime with Fatal error: Uncaught TypeError: test(): Return value must be of type string, int returned. My PR changes this, so AssertionFailedException is thrown instead.

Backward compatibility

This will be a BC break if someone used the assertion with non-string arguments. On the other hand, description clearly suggests that this assertion should be performed for strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant