Skip to content

Commit ad87a07

Browse files
committed
:octocat: mark nullable types explicitly (PHP 8.4 deprecation)
1 parent ee00916 commit ad87a07

7 files changed

+25
-23
lines changed

src/Authenticator.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ class Authenticator{
3939
/**
4040
* Authenticator constructor
4141
*/
42-
public function __construct(SettingsContainerInterface|AuthenticatorOptions $options = null, string $secret = null){
42+
public function __construct(
43+
SettingsContainerInterface|AuthenticatorOptions $options = new AuthenticatorOptions,
44+
string|null $secret = null
45+
){
4346
// phpcs:ignore
44-
$this->setOptions($options ?? new AuthenticatorOptions);
47+
$this->setOptions($options);
4548

4649
if($secret !== null){
4750
$this->setSecret($secret);
@@ -94,7 +97,7 @@ public function getSecret():string{
9497
*
9598
* @codeCoverageIgnore
9699
*/
97-
public function createSecret(int $length = null):string{
100+
public function createSecret(int|null $length = null):string{
98101
return $this->authenticator->createSecret($length);
99102
}
100103

@@ -107,7 +110,7 @@ public function createSecret(int $length = null):string{
107110
*
108111
* @codeCoverageIgnore
109112
*/
110-
public function code(int $data = null):string{
113+
public function code(int|null $data = null):string{
111114
return $this->authenticator->code($data);
112115
}
113116

@@ -120,7 +123,7 @@ public function code(int $data = null):string{
120123
*
121124
* @codeCoverageIgnore
122125
*/
123-
public function verify(#[SensitiveParameter] string $otp, int $data = null):bool{
126+
public function verify(#[SensitiveParameter] string $otp, int|null $data = null):bool{
124127
return $this->authenticator->verify($otp, $data);
125128
}
126129

@@ -131,7 +134,7 @@ public function verify(#[SensitiveParameter] string $otp, int $data = null):bool
131134
*
132135
* @throws \InvalidArgumentException
133136
*/
134-
public function getUri(string $label, string $issuer, int $hotpCounter = null, bool $omitSettings = null):string{
137+
public function getUri(string $label, string $issuer, int|null $hotpCounter = null, bool|null $omitSettings = null):string{
135138
$label = trim($label);
136139
$issuer = trim($issuer);
137140

src/Authenticators/AuthenticatorAbstract.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ abstract class AuthenticatorAbstract implements AuthenticatorInterface{
2929
protected const userAgent = 'chillerlanAuthenticator/5.0 +https://github.com/chillerlan/php-authenticator';
3030

3131
protected SettingsContainerInterface|AuthenticatorOptions $options;
32-
protected ?string $secret = null;
32+
protected string|null $secret = null;
3333
protected int $serverTime = 0;
3434
protected int $lastRequestTime = 0;
3535

3636
/**
3737
* AuthenticatorInterface constructor
3838
*/
39-
public function __construct(SettingsContainerInterface|AuthenticatorOptions $options = null){
40-
// phpcs:ignore
41-
$this->setOptions($options ?? new AuthenticatorOptions);
39+
public function __construct(SettingsContainerInterface|AuthenticatorOptions $options = new AuthenticatorOptions){
40+
$this->setOptions($options);
4241
}
4342

4443
/**
@@ -74,7 +73,7 @@ public function getSecret():string{
7473
/**
7574
* @inheritDoc
7675
*/
77-
public function createSecret(int $length = null):string{
76+
public function createSecret(int|null $length = null):string{
7877
$length ??= $this->options->secret_length;
7978

8079
if($length < 16){

src/Authenticators/AuthenticatorInterface.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function getSecret():string;
6363
*
6464
* @throws \InvalidArgumentException
6565
*/
66-
public function createSecret(int $length = null):string;
66+
public function createSecret(int|null $length = null):string;
6767

6868
/**
6969
* Returns the current server time as UNIX timestamp for the given application (or `time()` if not applicable)
@@ -75,7 +75,7 @@ public function getServertime():int;
7575
*
7676
* @internal
7777
*/
78-
public function getCounter(int $data = null):int;
78+
public function getCounter(int|null $data = null):int;
7979

8080
/**
8181
* HMAC hashes the given $data integer with the given secret
@@ -105,7 +105,7 @@ public function getOTP(#[SensitiveParameter] int $code):string;
105105
* - a UNIX timestamp (TOTP)
106106
* - a counter value (HOTP)
107107
*/
108-
public function code(int $data = null):string;
108+
public function code(int|null $data = null):string;
109109

110110
/**
111111
* Checks the given $code against the secret
@@ -114,6 +114,6 @@ public function code(int $data = null):string;
114114
* - a UNIX timestamp (TOTP)
115115
* - a counter value (HOTP)
116116
*/
117-
public function verify(#[SensitiveParameter] string $otp, int $data = null):bool;
117+
public function verify(#[SensitiveParameter] string $otp, int|null $data = null):bool;
118118

119119
}

src/Authenticators/HOTP.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class HOTP extends AuthenticatorAbstract{
3030
/**
3131
* @inheritDoc
3232
*/
33-
public function getCounter(int $data = null):int{
33+
public function getCounter(int|null $data = null):int{
3434
return ($data ?? 0);
3535
}
3636

@@ -77,7 +77,7 @@ public function getOTP(#[SensitiveParameter] int $code):string{
7777
/**
7878
* @inheritDoc
7979
*/
80-
public function code(int $data = null):string{
80+
public function code(int|null $data = null):string{
8181
$hmac = $this->getHMAC($this->getCounter($data));
8282

8383
return $this->getOTP($this->getCode($hmac));
@@ -86,7 +86,7 @@ public function code(int $data = null):string{
8686
/**
8787
* @inheritDoc
8888
*/
89-
public function verify(#[SensitiveParameter] string $otp, int $data = null):bool{
89+
public function verify(#[SensitiveParameter] string $otp, int|null $data = null):bool{
9090
return hash_equals($this->code($data), $otp);
9191
}
9292

src/Authenticators/SteamGuard.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ public function getSecret():string{
6868
* @inheritDoc
6969
* @codeCoverageIgnore
7070
*/
71-
public function createSecret(int $length = null):string{
71+
public function createSecret(int|null $length = null):string{
7272
throw new RuntimeException('Not implemented');
7373
}
7474

7575
/**
7676
* @inheritDoc
7777
*/
78-
public function getCounter(int $data = null):int{
78+
public function getCounter(int|null $data = null):int{
7979
// the period is fixed to 30 seconds for Steam Guard
8080
$this->options->period = 30;
8181

src/Authenticators/TOTP.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TOTP extends HOTP{
2424
/**
2525
* @inheritDoc
2626
*/
27-
public function getCounter(int $data = null):int{
27+
public function getCounter(int|null $data = null):int{
2828
$data ??= time();
2929

3030
if($this->options->useLocalTime === false){
@@ -37,7 +37,7 @@ public function getCounter(int $data = null):int{
3737
/**
3838
* @inheritDoc
3939
*/
40-
public function verify(#[SensitiveParameter] string $otp, int $data = null):bool{
40+
public function verify(#[SensitiveParameter] string $otp, int|null $data = null):bool{
4141
$limit = $this->options->adjacent;
4242

4343
if($limit === 0){

tests/AuthenticatorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function setUp():void{
3232
}
3333

3434
public function testSetSecretViaConstruct():void{
35-
$this->authenticator = new Authenticator(null, self::secret);
35+
$this->authenticator = new Authenticator(secret: self::secret);
3636

3737
$this::assertSame(self::secret, $this->authenticator->getSecret());
3838
}

0 commit comments

Comments
 (0)