Skip to content

Commit

Permalink
:octocat: mark nullable types explicitly (PHP 8.4 deprecation)
Browse files Browse the repository at this point in the history
(cherry picked from commit ad87a07)
  • Loading branch information
codemasher committed Jul 16, 2024
1 parent aff36f5 commit 040318f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 23 deletions.
15 changes: 9 additions & 6 deletions src/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ class Authenticator{
/**
* Authenticator constructor
*/
public function __construct(SettingsContainerInterface|AuthenticatorOptions $options = null, string $secret = null){
public function __construct(
SettingsContainerInterface|AuthenticatorOptions $options = new AuthenticatorOptions,
string|null $secret = null
){
// phpcs:ignore
$this->setOptions($options ?? new AuthenticatorOptions);
$this->setOptions($options);

if($secret !== null){
$this->setSecret($secret);
Expand Down Expand Up @@ -94,7 +97,7 @@ public function getSecret():string{
*
* @codeCoverageIgnore
*/
public function createSecret(int $length = null):string{
public function createSecret(int|null $length = null):string{
return $this->authenticator->createSecret($length);
}

Expand All @@ -107,7 +110,7 @@ public function createSecret(int $length = null):string{
*
* @codeCoverageIgnore
*/
public function code(int $data = null):string{
public function code(int|null $data = null):string{
return $this->authenticator->code($data);
}

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

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

Expand Down
9 changes: 4 additions & 5 deletions src/Authenticators/AuthenticatorAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ abstract class AuthenticatorAbstract implements AuthenticatorInterface{
protected const userAgent = 'chillerlanAuthenticator/5.0 +https://github.com/chillerlan/php-authenticator';

protected SettingsContainerInterface|AuthenticatorOptions $options;
protected ?string $secret = null;
protected string|null $secret = null;
protected int $serverTime = 0;
protected int $lastRequestTime = 0;

/**
* AuthenticatorInterface constructor
*/
public function __construct(SettingsContainerInterface|AuthenticatorOptions $options = null){
// phpcs:ignore
$this->setOptions($options ?? new AuthenticatorOptions);
public function __construct(SettingsContainerInterface|AuthenticatorOptions $options = new AuthenticatorOptions){
$this->setOptions($options);
}

/**
Expand Down Expand Up @@ -74,7 +73,7 @@ public function getSecret():string{
/**
* @inheritDoc
*/
public function createSecret(int $length = null):string{
public function createSecret(int|null $length = null):string{
$length ??= $this->options->secret_length;

if($length < 16){
Expand Down
8 changes: 4 additions & 4 deletions src/Authenticators/AuthenticatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getSecret():string;
*
* @throws \InvalidArgumentException
*/
public function createSecret(int $length = null):string;
public function createSecret(int|null $length = null):string;

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

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

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

}
6 changes: 3 additions & 3 deletions src/Authenticators/HOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class HOTP extends AuthenticatorAbstract{
/**
* @inheritDoc
*/
public function getCounter(int $data = null):int{
public function getCounter(int|null $data = null):int{
return ($data ?? 0);
}

Expand Down Expand Up @@ -77,7 +77,7 @@ public function getOTP(#[SensitiveParameter] int $code):string{
/**
* @inheritDoc
*/
public function code(int $data = null):string{
public function code(int|null $data = null):string{
$hmac = $this->getHMAC($this->getCounter($data));

return $this->getOTP($this->getCode($hmac));
Expand All @@ -86,7 +86,7 @@ public function code(int $data = null):string{
/**
* @inheritDoc
*/
public function verify(#[SensitiveParameter] string $otp, int $data = null):bool{
public function verify(#[SensitiveParameter] string $otp, int|null $data = null):bool{
return hash_equals($this->code($data), $otp);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Authenticators/SteamGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ public function getSecret():string{
* @inheritDoc
* @codeCoverageIgnore
*/
public function createSecret(int $length = null):string{
public function createSecret(int|null $length = null):string{
throw new RuntimeException('Not implemented');
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/Authenticators/TOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TOTP extends HOTP{
/**
* @inheritDoc
*/
public function getCounter(int $data = null):int{
public function getCounter(int|null $data = null):int{
$data ??= time();

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

if($limit === 0){
Expand Down
2 changes: 1 addition & 1 deletion tests/AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function setUp():void{
}

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

$this::assertSame(self::secret, $this->authenticator->getSecret());
}
Expand Down

0 comments on commit 040318f

Please sign in to comment.