Skip to content

Commit

Permalink
filter_var() accepts string|object, so the input is asserted to b…
Browse files Browse the repository at this point in the history
…e `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: webmozarts#160 (comment)

Note: will fail until vimeo/psalm#2452 has some
sort of resolution.
  • Loading branch information
Ocramius committed Dec 10, 2019
1 parent 28dcfaa commit e8473e4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
12 changes: 8 additions & 4 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions tests/static-analysis/assert-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 9 additions & 4 deletions tests/static-analysis/assert-ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

13 changes: 9 additions & 4 deletions tests/static-analysis/assert-ipv4.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

13 changes: 9 additions & 4 deletions tests/static-analysis/assert-ipv6.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit e8473e4

Please sign in to comment.