From e8473e4f6da9b4e0351add8f43523eef7d0693da Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Tue, 10 Dec 2019 14:02:26 +0100 Subject: [PATCH] `filter_var()` accepts `string|object`, so the input is asserted to be `string|object` An object implementing `#__toString()` suffices for `filter_var()` to work, so we can't assume that after the assertion, the value being asserted upon is a `string`. Instead, `string|object` is our closest bet. Ref: https://github.com/webmozart/assert/pull/160#discussion_r355964035 Note: will fail until https://github.com/vimeo/psalm/issues/2452 has some sort of resolution. --- src/Assert.php | 12 ++++++++---- tests/static-analysis/assert-email.php | 12 ++++++++---- tests/static-analysis/assert-ip.php | 13 +++++++++---- tests/static-analysis/assert-ipv4.php | 13 +++++++++---- tests/static-analysis/assert-ipv6.php | 13 +++++++++---- 5 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/Assert.php b/src/Assert.php index d65dd8a9..8e3f83f0 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -701,7 +701,8 @@ public static function false($value, $message = '') } /** - * @psalm-assert string $value + * @psalm-assert string|object $value note: can be an object implementing __toString + * @psalm-assert !empty $value * * @param mixed $value * @param string $message @@ -719,7 +720,8 @@ public static function ip($value, $message = '') } /** - * @psalm-assert string $value + * @psalm-assert string|object $value note: can be an object implementing __toString + * @psalm-assert !empty $value * * @param mixed $value * @param string $message @@ -737,7 +739,8 @@ public static function ipv4($value, $message = '') } /** - * @psalm-assert string $value + * @psalm-assert string|object $value note: can be an object implementing __toString + * @psalm-assert !empty $value * * @param mixed $value * @param string $message @@ -755,7 +758,8 @@ public static function ipv6($value, $message = '') } /** - * @psalm-assert string $value + * @psalm-assert string|object $value note: can be an object implementing __toString + * @psalm-assert !empty $value * * @param mixed $value * @param string $message diff --git a/tests/static-analysis/assert-email.php b/tests/static-analysis/assert-email.php index ed4a297f..dcb12bfc 100644 --- a/tests/static-analysis/assert-email.php +++ b/tests/static-analysis/assert-email.php @@ -4,10 +4,14 @@ use Webmozart\Assert\Assert; -/** - * @psalm-param mixed $value - */ -function consume($value): string +function consume(?string $value): string +{ + Assert::email($value); + + return $value; +} + +function consumeObject(?object $value): object { Assert::email($value); diff --git a/tests/static-analysis/assert-ip.php b/tests/static-analysis/assert-ip.php index d8df3372..534266ef 100644 --- a/tests/static-analysis/assert-ip.php +++ b/tests/static-analysis/assert-ip.php @@ -4,12 +4,17 @@ use Webmozart\Assert\Assert; -/** - * @psalm-param mixed $value - */ -function consume($value): string +function consume(?string $value): string { Assert::ip($value); return $value; } + +function consumeObject(?object $value): object +{ + Assert::ip($value); + + return $value; +} + diff --git a/tests/static-analysis/assert-ipv4.php b/tests/static-analysis/assert-ipv4.php index 338f8444..5c3b92dd 100644 --- a/tests/static-analysis/assert-ipv4.php +++ b/tests/static-analysis/assert-ipv4.php @@ -4,12 +4,17 @@ use Webmozart\Assert\Assert; -/** - * @psalm-param mixed $value - */ -function consume($value): string +function consume(?string $value): string { Assert::ipv4($value); return $value; } + +function consumeObject(?object $value): object +{ + Assert::ipv4($value); + + return $value; +} + diff --git a/tests/static-analysis/assert-ipv6.php b/tests/static-analysis/assert-ipv6.php index 374c577e..c2b2a6cf 100644 --- a/tests/static-analysis/assert-ipv6.php +++ b/tests/static-analysis/assert-ipv6.php @@ -4,12 +4,17 @@ use Webmozart\Assert\Assert; -/** - * @psalm-param mixed $value - */ -function consume($value): string +function consume(?string $value): string { Assert::ipv6($value); return $value; } + +function consumeObject(?object $value): object +{ + Assert::ipv6($value); + + return $value; +} +