Skip to content

Commit 072fca3

Browse files
committed
fix test for lower PHP version and PHPDoc blocks
1 parent 6880ba2 commit 072fca3

File tree

7 files changed

+23
-37
lines changed

7 files changed

+23
-37
lines changed

phpstan-baseline.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@
258258
];
259259
$ignoreErrors[] = [
260260
'message' => '#^Call to function model with CodeIgniter\\\\Shield\\\\Models\\\\UserIdentityModel\\:\\:class is discouraged\\.$#',
261-
'identifier' => 'codeigniter.factoriesClassConstFetch',
262-
'count' => 19,
261+
'count' => 21,
263262
'path' => __DIR__ . '/src/Entities/User.php',
264263
];
265264
$ignoreErrors[] = [

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function check(array $credentials): Result
156156

157157
// Is expired ?
158158
if (
159-
$token->expires
159+
$token->expires !== null
160160
&& $token->expires->isBefore(
161161
Time::now()
162162
)

src/Authentication/Traits/HasAccessTokens.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,20 @@ public function setAccessToken(?AccessToken $accessToken): self
175175
/**
176176
* Checks if the provided Access Token has expired.
177177
*
178-
* @return false|true|null Returns true if Access Token has expired, false if not, and null if the expire field is null
178+
* @return bool|null Returns true if Access Token has expired, false if not, and null if the expire field is null
179179
*/
180180
public function hasAccessTokenExpired(?AccessToken $accessToken): bool|null
181181
{
182-
if (null === $accessToken->expires) {
183-
return null;
184-
}
185-
186-
return $accessToken->expires->isBefore(Time::now());
182+
return $accessToken->expires !== null ? $accessToken->expires->isBefore(Time::now()) : null;
187183
}
188184

189185
/**
190186
* Returns formatted date to expiration for provided AccessToken
191187
*
192-
* @param AcessToken $accessToken AccessToken
193-
* @param string $format The return format - "date" or "human". Date is 'Y-m-d h:i:s', human is 'in 2 weeks'
188+
* @param AccessToken $accessToken AccessToken
189+
* @param string $format The return format - "date" or "human". Date is 'Y-m-d h:i:s', human is 'in 2 weeks'
194190
*
195-
* @return false|true|null Returns true if Access Token has expired, false if not and null if the expire field is null
191+
* @return string|null Returns a formatted expiration date or null if the expire field is not set.
196192
*
197193
* @throws InvalidArgumentException
198194
*/
@@ -219,6 +215,8 @@ public function getAccessTokenTimeToExpire(?AccessToken $accessToken, string $fo
219215
*
220216
* @param int $id AccessTokens ID
221217
* @param string $expiresAt Expiration date. Accepts DateTime string formatted as 'Y-m-d h:i:s' or DateTime relative formats (1 day, 2 weeks, 6 months, 1 year) to be added to DateTime 'now'
218+
*
219+
* @return bool Returns true if expiration date is set or updated.
222220
*/
223221
public function setAccessTokenExpirationById(int $id, string $expiresAt): bool
224222
{

src/Authentication/Traits/HasHmacTokens.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,24 +165,20 @@ public function setHmacToken(?AccessToken $accessToken): self
165165
/**
166166
* Checks if the provided Access Token has expired.
167167
*
168-
* @return false|true|null Returns true if Access Token has expired, false if not, and null if the expire field is null
168+
* @return bool|null Returns true if Access Token has expired, false if not, and null if the expire field is null
169169
*/
170170
public function hasHmacTokenExpired(?AccessToken $accessToken): bool|null
171171
{
172-
if (null === $accessToken->expires) {
173-
return null;
174-
}
175-
176-
return $accessToken->expires->isBefore(Time::now());
172+
return $accessToken->expires !== null ? $accessToken->expires->isBefore(Time::now()) : null;
177173
}
178174

179175
/**
180176
* Returns formatted date to expiration for provided Hmac Key/Token.
181177
*
182-
* @param AcessToken $accessToken AccessToken
183-
* @param string $format The return format - "date" or "human". Date is 'Y-m-d h:i:s', human is 'in 2 weeks'
178+
* @param AccessToken $accessToken AccessToken
179+
* @param string $format The return format - "date" or "human". Date is 'Y-m-d h:i:s', human is 'in 2 weeks'
184180
*
185-
* @return false|true|null Returns true if Access Token has expired, false if not and null if the expire field is null
181+
* @return string|null Returns a formatted expiration date or null if the expire field is not set.
186182
*
187183
* @throws InvalidArgumentException
188184
*/
@@ -207,10 +203,10 @@ public function getHmacTokenTimeToExpire(?AccessToken $accessToken, string $form
207203
/**
208204
* Sets an expiration for Hmac Key/Token by ID.
209205
*
210-
* @param int $id AccessTokens ID
206+
* @param int $id AccessToken ID
211207
* @param string $expiresAt Expiration date. Accepts DateTime string formatted as 'Y-m-d h:i:s' or DateTime relative formats (1 day, 2 weeks, 6 months, 1 year) to be added to DateTime 'now'
212208
*
213-
* @return false|true|null Returns true if token is updated, false if not.
209+
* @return bool Returns true if expiration date is set or updated.
214210
*/
215211
public function setHmacTokenExpirationById(int $id, string $expiresAt): bool
216212
{

src/Entities/AccessToken.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Represents a single Personal Access Token, used
2323
* for authenticating users for an API.
2424
*
25+
* @property string|Time|null $expires
2526
* @property string|Time|null $last_used_at
2627
*/
2728
class AccessToken extends Entity

src/Models/UserIdentityModel.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ private function checkUserId(User $user): void
106106

107107
private function checkExpiresAtFormat(string $expiresAt): string
108108
{
109-
if (! is_string($expiresAt)) {
110-
throw new InvalidArgumentException('$expiresAt should be a string.');
111-
}
112-
113109
$expireMatch = [];
114110

115111
// Check Y-m-d h:i:s format.
@@ -277,30 +273,26 @@ public function getAllAccessTokens(User $user): array
277273
*
278274
* @param string $expiresAt Expiration date. Accepts DateTime string formatted as 'Y-m-d h:i:s' or DateTime relative formats (1 day, 2 weeks, 6 months, 1 year) to be added to 'Time::now()'
279275
* @param mixed $id
276+
*
277+
* @return bool Returns true if expiration date was set or updated.
280278
*/
281279
public function setIdentityExpirationById($id, User $user, ?string $expiresAt = null, ?string $type_token = null): bool
282280
{
283281
$this->checkUserId($user);
284282

285-
if (! $expiresAt) {
286-
throw new InvalidArgumentException("setIdentityExpirationById(): expiresAt argument can't be null.");
287-
}
288-
289283
$expiresAt = $this->checkExpiresAtFormat($expiresAt);
290284

291-
$currentExpiration = $this->asObject(AccessToken::class)->find($id);
285+
$currentExpiration = $this->where('user_id', $user->id)->where('id', $id)->asObject(AccessToken::class)->first();
292286

293-
// d($currentExpiration);
294-
if ($currentExpiration->expires !== $expiresAt) {
295-
// throw new InvalidArgumentException(sprintf("User-id: %d type: %s id: %d expires:%s", $user->id,$type_token,$id,$expiresAt));
287+
if ($currentExpiration->expires !== null && $currentExpiration->expires !== $expiresAt) {
296288
return $this->where('user_id', $user->id)
297289
->where('type', $type_token)
298290
->where('id', $id)
299291
->set(['expires' => $expiresAt])
300292
->update();
301293
}
302294

303-
throw new InvalidArgumentException('setIdentityExpirationById(): No data to update. ID:' . $id . ' Expires at: ' . $expiresAt . ' curr_expires:' . $currentExpiration->expires . ' Result: ' . ($currentExpiration->expires !== $expiresAt));
295+
return false;
304296
}
305297

306298
// HMAC

tests/Commands/HmacTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function testExpireAll(): void
148148
{
149149
/** @var User $user */
150150
$user = fake(UserModel::class);
151-
$user->generateHmacToken('foo', expiresAt: '2024-10-01 12:20:00');
151+
$user->generateHmacToken('foo', ['*'], '2024-10-01 12:20:00');
152152
$user->generateHmacToken('bar');
153153

154154
$this->setMockIo([]);

0 commit comments

Comments
 (0)