Skip to content

Commit

Permalink
Solved webmozarts#270 issue by adding Assert::isNotInstanceOfAny
Browse files Browse the repository at this point in the history
  • Loading branch information
Tautvydaskarvelis committed Oct 1, 2022
1 parent 11cb219 commit 7ba4dd6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,29 @@ public static function isInstanceOfAny($value, array $classes, $message = '')
));
}

/**
* @psalm-pure
* @psalm-param array<class-string> $classes
*
* @param mixed $value
* @param array<object|string> $classes
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isNotInstanceOfAny($value, array $classes, $message = '')
{
foreach ($classes as $class) {
if ($value instanceof $class) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected not an instance of %2$s. Got: %s',
static::typeToString($value),
\implode(', ', \array_map(array(static::class, 'valueToString'), $classes))
));
}
}
}

/**
* @psalm-pure
* @psalm-template ExpectedType of object
Expand Down
5 changes: 5 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ public function getTests()
array('uniqueValues', array(array('qwerty', 'qwerty')), false),
array('uniqueValues', array(array('asdfg', 'qwerty')), true),
array('uniqueValues', array(array(123, '123')), false),
array('isNotInstanceOfAny', array(new Exception(), array('Exception', 'ArrayAccess')), false),
array('isNotInstanceOfAny', array(new Exception(), array('Iterator', 'Countable')), true),
array('isNotInstanceOfAny', array(new Error(), array('Exception', 'Countable')), true),
array('isNotInstanceOfAny', array(123, array('stdClass')), true),
array('isNotInstanceOfAny', array(array(), array('stdClass')), true),
);
}

Expand Down

0 comments on commit 7ba4dd6

Please sign in to comment.