Skip to content

Commit 79155f9

Browse files
authored
Ensure helpful message for instanceof checks (#350)
1 parent b39f187 commit 79155f9

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
## 2.1.5
5+
6+
### Fixed
7+
8+
- Fixed regression of `instanceOf` messages
9+
410
## 2.1.4
511

612
### Fixed

src/Assert.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ public static function isIterable(mixed $value, string $message = ''): iterable
484484
*/
485485
public static function isInstanceOf(mixed $value, mixed $class, string $message = ''): object
486486
{
487-
static::object($value, $message);
488487
static::string($class, 'Expected class as a string. Got: %s');
489488

490489
if (!($value instanceof $class)) {
@@ -510,10 +509,9 @@ public static function isInstanceOf(mixed $value, mixed $class, string $message
510509
*/
511510
public static function notInstanceOf(mixed $value, mixed $class, string $message = ''): object
512511
{
513-
static::object($value, $message);
514512
static::string($class, 'Expected class as a string. Got: %s');
515513

516-
if ($value instanceof $class) {
514+
if (!\is_object($value) || $value instanceof $class) {
517515
static::reportInvalidArgument(\sprintf(
518516
$message ?: 'Expected an instance other than %2$s. Got: %s',
519517
static::typeToString($value),
@@ -537,7 +535,6 @@ public static function notInstanceOf(mixed $value, mixed $class, string $message
537535
*/
538536
public static function isInstanceOfAny(mixed $value, mixed $classes, string $message = ''): object
539537
{
540-
static::object($value, $message);
541538
static::isIterable($classes);
542539

543540
foreach ($classes as $class) {

tests/AssertTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ public static function getTests(): array
188188
['isInstanceOf', [null, 'stdClass'], false],
189189
['notInstanceOf', [new stdClass(), 'stdClass'], false],
190190
['notInstanceOf', [new Exception(), 'stdClass'], true],
191+
['notInstanceOf', [123, 'stdClass'], false],
191192
['notInstanceOf', [[], 'stdClass'], false],
193+
['notInstanceOf', [null, 'stdClass'], false],
192194
['isInstanceOfAny', [new ArrayIterator(), ['Iterator', 'ArrayAccess']], true],
193195
['isInstanceOfAny', [new Exception(), ['Exception', 'Countable']], true],
194196
['isInstanceOfAny', [new Exception(), ['ArrayAccess', 'Countable']], false],
@@ -803,6 +805,38 @@ public function testConvertValuesToStrings(string $method, array $args, string $
803805
call_user_func_array(['Webmozart\Assert\Assert', $method], $args);
804806
}
805807

808+
public static function getInvalidInstanceOfCases(): iterable
809+
{
810+
yield [
811+
[null, 'stdClass'],
812+
'Expected an instance of stdClass. Got: NULL',
813+
];
814+
815+
yield [
816+
[123, 'stdClass'],
817+
'Expected an instance of stdClass. Got: integer',
818+
];
819+
820+
yield [
821+
[[], 'Iterator'],
822+
'Expected an instance of Iterator. Got: array',
823+
];
824+
825+
yield [
826+
[new stdClass(), 'Iterator'],
827+
'Expected an instance of Iterator. Got: stdClass',
828+
];
829+
}
830+
831+
#[DataProvider('getInvalidInstanceOfCases')]
832+
public function testInstanceOfExceptionMessages(array $args, string $exceptionMessage): void
833+
{
834+
$this->expectException('\InvalidArgumentException');
835+
$this->expectExceptionMessage($exceptionMessage);
836+
837+
call_user_func_array(['Webmozart\Assert\Assert', 'isInstanceOf'], $args);
838+
}
839+
806840
public static function getInvalidIsAOfCases(): iterable
807841
{
808842
yield [
@@ -955,7 +989,7 @@ public static function getMethodsThatUseOtherMethods(): array
955989
],
956990
[
957991
'method' => 'isInstanceOfAny',
958-
'args' => [111, 'stdClass', 'Value must be an instance of stdClass. Got: %s'],
992+
'args' => [111, ['stdClass'], 'Value must be an instance of stdClass. Got: %s'],
959993
'exceptionMessage' => 'Value must be an instance of stdClass. Got: integer',
960994
],
961995
[
@@ -965,7 +999,7 @@ public static function getMethodsThatUseOtherMethods(): array
965999
],
9661000
[
9671001
'method' => 'isAnyOf',
968-
'args' => [111, 'stdClass', 'Value must be an instance of stdClass. Got: %s'],
1002+
'args' => [111, ['stdClass'], 'Value must be an instance of stdClass. Got: %s'],
9691003
'exceptionMessage' => 'Value must be an instance of stdClass. Got: integer',
9701004
],
9711005
];

0 commit comments

Comments
 (0)