Skip to content

Commit 0802f39

Browse files
authored
Merge pull request #1027 from kenjis/remove-hard-coded-datetime-format
refactor: remove hard coded `'Y-m-d H:i:s'`
2 parents 0b55fa2 + b05dbdc commit 0802f39

File tree

12 files changed

+20
-21
lines changed

12 files changed

+20
-21
lines changed

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function check(array $credentials): Result
170170
]);
171171
}
172172

173-
$token->last_used_at = Time::now()->format('Y-m-d H:i:s');
173+
$token->last_used_at = Time::now();
174174

175175
if ($token->hasChanged()) {
176176
$identityModel->save($token);

src/Authentication/Authenticators/HmacSha256.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function check(array $credentials): Result
186186
]);
187187
}
188188

189-
$token->last_used_at = Time::now()->format('Y-m-d H:i:s');
189+
$token->last_used_at = Time::now();
190190

191191
if ($token->hasChanged()) {
192192
$identityModel->save($token);

src/Authentication/Authenticators/Session.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -922,17 +922,17 @@ protected function rememberUser(User $user): void
922922
$user,
923923
$selector,
924924
$this->hashValidator($validator),
925-
$expires
925+
$expires->format('Y-m-d H:i:s')
926926
);
927927

928928
$this->setRememberMeCookie($rawToken);
929929
}
930930

931-
private function calcExpires(): string
931+
private function calcExpires(): Time
932932
{
933933
$timestamp = Time::now()->getTimestamp() + setting('Auth.sessionConfig')['rememberLength'];
934934

935-
return Time::createFromTimestamp($timestamp)->format('Y-m-d H:i:s');
935+
return Time::createFromTimestamp($timestamp);
936936
}
937937

938938
private function setRememberMeCookie(string $rawToken): void

src/Authorization/Traits/Authorizable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ private function saveGroupsOrPermissions(string $type, $model, array $cache): vo
390390
$inserts[] = [
391391
'user_id' => $this->id,
392392
$type => $item,
393-
'created_at' => Time::now()->format('Y-m-d H:i:s'),
393+
'created_at' => Time::now(),
394394
];
395395
}
396396

src/Controllers/MagicLinkController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function loginAction()
109109
'user_id' => $user->id,
110110
'type' => Session::ID_TYPE_MAGIC_LINK,
111111
'secret' => $token,
112-
'expires' => Time::now()->addSeconds(setting('Auth.magicLinkLifetime'))->format('Y-m-d H:i:s'),
112+
'expires' => Time::now()->addSeconds(setting('Auth.magicLinkLifetime')),
113113
]);
114114

115115
/** @var IncomingRequest $request */

src/Models/LoginModel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class LoginModel extends BaseModel
4040
'identifier' => 'permit_empty|string',
4141
'user_agent' => 'permit_empty|string',
4242
'user_id' => 'permit_empty|integer',
43-
'date' => 'required|valid_date',
43+
'date' => 'required',
4444
];
4545
protected $validationMessages = [];
4646
protected $skipValidation = false;
@@ -80,7 +80,7 @@ public function recordLoginAttempt(
8080
'id_type' => $idType,
8181
'identifier' => $identifier,
8282
'user_id' => $userId,
83-
'date' => Time::now()->format('Y-m-d H:i:s'),
83+
'date' => Time::now(),
8484
'success' => (int) $success,
8585
]);
8686

@@ -121,7 +121,7 @@ public function fake(Generator &$faker): Login
121121
'id_type' => Session::ID_TYPE_EMAIL_PASSWORD,
122122
'identifier' => $faker->email(),
123123
'user_id' => null,
124-
'date' => Time::parse('-1 day')->format('Y-m-d H:i:s'),
124+
'date' => Time::parse('-1 day'),
125125
'success' => true,
126126
]);
127127
}

src/Models/RememberModel.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\I18n\Time;
1717
use CodeIgniter\Shield\Entities\User;
18-
use DateTime;
1918
use Faker\Generator;
2019
use stdClass;
2120

@@ -45,22 +44,22 @@ public function fake(Generator &$faker): stdClass
4544
'user_id' => 1,
4645
'selector' => 'selector',
4746
'hashedValidator' => 'validator',
48-
'expires' => Time::parse('+1 day')->format('Y-m-d H:i:s'),
47+
'expires' => Time::parse('+1 day'),
4948
];
5049
}
5150

5251
/**
5352
* Stores a remember-me token for the user.
53+
*
54+
* @TODO `string $expires` → `Time $expires`
5455
*/
5556
public function rememberUser(User $user, string $selector, string $hashedValidator, string $expires): void
5657
{
57-
$expires = new DateTime($expires);
58-
5958
$return = $this->insert([
6059
'user_id' => $user->id,
6160
'selector' => $selector,
6261
'hashedValidator' => $hashedValidator,
63-
'expires' => $expires->format('Y-m-d H:i:s'),
62+
'expires' => Time::parse($expires),
6463
]);
6564

6665
$this->checkQueryReturn($return);

src/Models/TokenLoginModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function fake(Generator &$faker): Login
3535
'ip_address' => $faker->ipv4(),
3636
'identifier' => 'token: ' . random_string('crypto', 64),
3737
'user_id' => fake(UserModel::class)->id,
38-
'date' => Time::parse('-1 day')->format('Y-m-d H:i:s'),
38+
'date' => Time::parse('-1 day'),
3939
'success' => true,
4040
]);
4141
}

src/Models/UserIdentityModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ public function getIdentitiesByTypes(User $user, array $types): array
442442
*/
443443
public function touchIdentity(UserIdentity $identity): void
444444
{
445-
$identity->last_used_at = Time::now()->format('Y-m-d H:i:s');
445+
$identity->last_used_at = Time::now();
446446

447447
$return = $this->save($identity);
448448

src/Models/UserModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public function updateActiveDate(User $user): void
376376
assert($user->last_active instanceof Time);
377377

378378
// Safe date string for database
379-
$last_active = $user->last_active->format('Y-m-d H:i:s');
379+
$last_active = $user->last_active;
380380

381381
$this->builder()
382382
->set('last_active', $last_active)

0 commit comments

Comments
 (0)