From 7ba4dd6836adbb9162bfe27a8be1fe26e2c34157 Mon Sep 17 00:00:00 2001 From: tautvydaskarvelis Date: Sat, 1 Oct 2022 09:09:13 +0300 Subject: [PATCH 1/2] Solved #270 issue by adding Assert::isNotInstanceOfAny --- src/Assert.php | 23 +++++++++++++++++++++++ tests/AssertTest.php | 5 +++++ 2 files changed, 28 insertions(+) diff --git a/src/Assert.php b/src/Assert.php index db1f3a5..d4913e3 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -467,6 +467,29 @@ public static function isInstanceOfAny($value, array $classes, $message = '') )); } + /** + * @psalm-pure + * @psalm-param array $classes + * + * @param mixed $value + * @param array $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 diff --git a/tests/AssertTest.php b/tests/AssertTest.php index 567fddf..e92650a 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -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), ); } From ff6d98c3ed1497efb69f583dd4546b9f8f0b5c4d Mon Sep 17 00:00:00 2001 From: tautvydaskarvelis Date: Sat, 1 Oct 2022 09:11:26 +0300 Subject: [PATCH 2/2] updated CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56c8011..c1b6dd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ Changelog ## UNRELEASED +## 1.12.0 + +### Added + +* added `Assert::isNotInstanceOfAny()` + ## 1.11.0 ### Added