Skip to content

Commit d4057fc

Browse files
committed
Backport nullOr bugfix
1 parent 5e37097 commit d4057fc

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

src/Assert.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,15 +347,19 @@ public static function __callStatic(string $name, array $arguments): void
347347
// Putting Webmozart first, since the most calls will be to their native assertions
348348
if (method_exists(Webmozart::class, $name)) {
349349
call_user_func_array([Webmozart::class, $name], $arguments);
350-
} else if (method_exists(static::class, $name)) {
350+
} elseif (method_exists(static::class, $name)) {
351351
call_user_func_array([static::class, $name], $arguments);
352-
} else if (preg_match('/^nullOr(.*)$/i', $name, $matches)) {
352+
} elseif (preg_match('/^nullOr(.*)$/i', $name, $matches)) {
353353
$method = lcfirst($matches[1]);
354-
if (method_exists(static::class, $method)) {
355-
call_user_func_array([static::class, 'nullOr'], [$method, $arguments]);
354+
if (method_exists(Webmozart::class, $method)) {
355+
call_user_func_array([static::class, 'nullOr'], [[Webmozart::class, $method], $arguments]);
356+
} elseif (method_exists(static::class, $method)) {
357+
call_user_func_array([static::class, 'nullOr'], [[static::class, $method], $arguments]);
358+
} else {
359+
throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $method));
356360
}
357361
} else {
358-
throw new BadMethodCallException();
362+
throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $name));
359363
}
360364
} catch (InvalidArgumentException $e) {
361365
throw new $exception($e->getMessage());
@@ -375,14 +379,14 @@ public static function __callStatic(string $name, array $arguments): void
375379
/**
376380
* nullOr* for our custom assertions
377381
*
378-
* @param string $method
382+
* @param callable $method
379383
* @param array $arguments
380384
* @return void
381385
*/
382-
private static function nullOr(string $method, array $arguments): void
386+
private static function nullOr(callable $method, array $arguments): void
383387
{
384388
$value = reset($arguments);
385-
($value === null) || call_user_func_array([static::class, $method], $arguments);
389+
($value === null) || call_user_func_array($method, $arguments);
386390
}
387391

388392

tests/Assert/AssertTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ public function testUnknownAssertionRaisesBadMethodCallException(): void
6161
}
6262

6363

64+
/**
65+
*/
66+
public function testUnknownNullOrAssertionRaisesBadMethodCallException(): void
67+
{
68+
$this->expectException(BadMethodCallException::class);
69+
Assert::nullOrThisAssertionDoesNotExist('a', 'b', LogicException::class);
70+
}
71+
72+
6473
/**
6574
* @doesNotPerformAssertions
6675
*/

0 commit comments

Comments
 (0)