Skip to content

Commit

Permalink
Test argument really is a string before doing a filter_var. (webmozar…
Browse files Browse the repository at this point in the history
  • Loading branch information
mveldman authored and BackEndTea committed Oct 15, 2019
1 parent a3a574a commit 8c707ac
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,16 @@ public static function false($value, $message = '')
}

/**
* @psalm-assert string $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function ip($value, $message = '')
{
static::string($value, $message);
if (false === \filter_var($value, \FILTER_VALIDATE_IP)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value to be an IP. Got: %s',
Expand All @@ -699,13 +702,16 @@ public static function ip($value, $message = '')
}

/**
* @psalm-assert string $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function ipv4($value, $message = '')
{
static::string($value, $message);
if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value to be an IPv4. Got: %s',
Expand All @@ -715,13 +721,16 @@ public static function ipv4($value, $message = '')
}

/**
* @psalm-assert string $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function ipv6($value, $message = '')
{
static::string($value, $message);
if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value to be an IPv6. Got %s',
Expand All @@ -731,13 +740,16 @@ public static function ipv6($value, $message = '')
}

/**
* @psalm-assert string $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function email($value, $message = '')
{
static::string($value, $message);
if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value to be a valid e-mail address. Got %s',
Expand Down
4 changes: 4 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ public function getTests()
array('throws', array(function() { trigger_error('test'); }, 'Unthrowable'), false, false, 70000),
array('throws', array(function() { throw new Error(); }, 'Throwable'), true, true, 70000),
array('ip', array('192.168.0.1'), true),
array('ip', array(new ToStringClass('192.168.0.1')), false),
array('ip', array('255.255.255.255'), true),
array('ip', array('0.0.0.0'), true),
array('ip', array('2001:0db8:0000:0042:0000:8a2e:0370:7334'), true),
Expand All @@ -455,6 +456,7 @@ public function getTests()
array('ip', array(null), false),
array('ip', array(false), false),
array('ipv4', array('192.168.0.1'), true),
array('ipv4', array(new ToStringClass('192.168.0.1')), false),
array('ipv4', array('255.255.255.255'), true),
array('ipv4', array('0.0.0.0'), true),
array('ipv4', array('2001:0db8:0000:0042:0000:8a2e:0370:7334'), false),
Expand All @@ -470,6 +472,7 @@ public function getTests()
array('ipv6', array('255.255.255.255'), false),
array('ipv6', array('0.0.0.0'), false),
array('ipv6', array('2001:0db8:0000:0042:0000:8a2e:0370:7334'), true),
array('ipv6', array(new ToStringClass('2001:0db8:0000:0042:0000:8a2e:0370:7334')), false),
array('ipv6', array('::ffff:192.0.2.1'), true),
array('ipv6', array('::1'), true),
array('ipv6', array('::'), true),
Expand All @@ -482,6 +485,7 @@ public function getTests()
array('email', array(123), false),
array('email', array('foo.com'), false),
array('email', array('[email protected]'), true),
array('email', array(new ToStringClass('[email protected]')), false),
array('uniqueValues', array(array('qwerty', 'qwerty')), false),
array('uniqueValues', array(array('asdfg', 'qwerty')), true),
array('uniqueValues', array(array(123, '123')), false),
Expand Down
15 changes: 15 additions & 0 deletions tests/static-analysis/assert-email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-param mixed $value
*/
function consume($value): string
{
Assert::email($value);

return $value;
}
15 changes: 15 additions & 0 deletions tests/static-analysis/assert-ip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-param mixed $value
*/
function consume($value): string
{
Assert::ip($value);

return $value;
}
15 changes: 15 additions & 0 deletions tests/static-analysis/assert-ipv4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-param mixed $value
*/
function consume($value): string
{
Assert::ipv4($value);

return $value;
}
15 changes: 15 additions & 0 deletions tests/static-analysis/assert-ipv6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-param mixed $value
*/
function consume($value): string
{
Assert::ipv6($value);

return $value;
}

0 comments on commit 8c707ac

Please sign in to comment.