Skip to content

Commit

Permalink
Fix PHP 8.2 compat issue in uses of DateTime::modify()
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Dec 6, 2022
1 parent d05b77c commit a06c98d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Model/AbstractRefreshToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ abstract class AbstractRefreshToken implements RefreshTokenInterface
public static function createForUserWithTtl(string $refreshToken, UserInterface $user, int $ttl): RefreshTokenInterface
{
$valid = new \DateTime();
$valid->modify('+'.$ttl.' seconds');

// Explicitly check for a negative number based on a behavior change in PHP 8.2, see https://github.com/php/php-src/issues/9950
if ($ttl > 0) {
$valid->modify('+'.$ttl.' seconds');
} elseif ($ttl < 0) {
$valid->modify($ttl.' seconds');
}

$model = new static();
$model->setRefreshToken($refreshToken);
Expand Down
9 changes: 8 additions & 1 deletion Security/Http/Authenticator/RefreshTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ public function authenticate(Request $request): Passport

if ($this->options['ttl_update']) {
$expirationDate = new \DateTime();
$expirationDate->modify(sprintf('+%d seconds', $this->options['ttl']));

// Explicitly check for a negative number based on a behavior change in PHP 8.2, see https://github.com/php/php-src/issues/9950
if ($this->options['ttl'] > 0) {
$expirationDate->modify(sprintf('+%d seconds', $this->options['ttl']));
} elseif ($this->options['ttl'] < 0) {
$expirationDate->modify(sprintf('%d seconds', $this->options['ttl']));
}

$refreshToken->setValid($expirationDate);

$this->refreshTokenManager->save($refreshToken);
Expand Down

0 comments on commit a06c98d

Please sign in to comment.