Skip to content

Commit 9aab998

Browse files
committed
fix(Authenticator): Improve argument validation logic
- Update argument validation in Authenticator constructor. - Modify exception message for incorrect argument count. - Enforce that usernameOrToken must be provided when password is set. - Ensure compatibility with updated instantiation in tests.
1 parent d9a4f11 commit 9aab998

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/Ntfy/Authenticator.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ public function __construct(
2828
#[\SensitiveParameter]
2929
?string $password = null
3030
) {
31-
parent::__construct(...match (\func_num_args()) {
32-
0 => [
31+
parent::__construct(...match (true) {
32+
!isset($usernameOrToken) && !isset($password) => [
3333
new NullAuthenticator,
3434
],
35-
1 => [
35+
isset($usernameOrToken) && !isset($password) => [
3636
new BearerAuthenticator($usernameOrToken),
3737
new OptionsAuthenticator([
3838
RequestOptions::QUERY => [RequestOptions::AUTH => $usernameOrToken],
3939
]),
4040
],
41-
2 => [
41+
isset($usernameOrToken, $password) => [
4242
new BasicAuthenticator($usernameOrToken, $password),
4343
],
44-
default => throw new InvalidArgumentException('The number of arguments must be 0, 1 or 2.'),
44+
default => throw new InvalidArgumentException('When the password is not null, the usernameOrToken must be not null.'),
4545
});
4646
}
4747
}

tests/Ntfy/AuthenticatorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
use Guanguans\Notify\Ntfy\Authenticator;
2323

2424
it('will throw an InvalidArgumentException', function (): void {
25-
new Authenticator('foo', 'bar', 'baz');
25+
new Authenticator(password: faker()->password());
2626
})
2727
->group(__DIR__, __FILE__)
28-
->throws(InvalidArgumentException::class, 'The number of arguments must be 0, 1 or 2.');
28+
->throws(InvalidArgumentException::class, 'When the password is not null, the usernameOrToken must be not null.');
2929

3030
it('can create an Authenticator', function (): void {
3131
expect([

0 commit comments

Comments
 (0)