Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documented
callable
parameter structure for Assert::callback()
`Assert::callback()` accepts a `$callable` that, **AT MOST**, accepts **1** parameter and returns `bool`. The number of times I've written `->with(self::callback(function ($a, $b, $c) : void {` is very high: * I generally forget that `->with()` is variadic (that's OK) * I forget that `self::callback()` applies to a single parameter * I forget that the `callable` must return a `bool` in order to operate In order to avoid these mistakes, the `Callback` constraint is now templated, and the given `callable` is enforced to require at most one parameter, and to always return `bool`. As a counter-example to verify that this type signature works, I've run this against `tests/static-analysis/TestUsingCallbacks.php` with following snippet: ```php public function testInvalid(): void { $mock = $this->createMock(SayHello::class); $mock ->expects(self::once()) ->method('hey') ->with(self::callback(static function (string $a, string $b): bool { return true; })) ->willReturn('Hey Joe!'); self::assertSame('Hey Joe!', $mock->hey('Joe')); } ``` The above now raises: ``` ERROR: InvalidArgument - tests/static-analysis/TestUsingCallbacks.php:62:35 - Argument 1 of PHPUnit\StaticAnalysis\TestUsingCallbacks::callback expects callable(mixed):bool, pure-Closure(string, string):true provided (see https://psalm.dev/004) ->with(self::callback(static function (string $a, string $b): bool { return true; })) ``` This produces
- Loading branch information