Skip to content

Commit 9e09063

Browse files
authored
Merge pull request #6860 from ping-yee/refactor-replace-time
refactor: replace `time()` with Time into `Debug` and `Cookie` class.
2 parents 5de3a98 + 90a8860 commit 9e09063

4 files changed

Lines changed: 11 additions & 6 deletions

File tree

deptrac.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ parameters:
160160
Controller:
161161
- HTTP
162162
- Validation
163+
Cookie:
164+
- I18n
163165
Database:
164166
- Entity
165167
- Events

system/Cookie/Cookie.php

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

1414
use ArrayAccess;
1515
use CodeIgniter\Cookie\Exceptions\CookieException;
16+
use CodeIgniter\I18n\Time;
1617
use Config\Cookie as CookieConfig;
1718
use DateTimeInterface;
1819
use InvalidArgumentException;
@@ -206,7 +207,7 @@ final public function __construct(string $name, string $value = '', array $optio
206207

207208
// If both `Expires` and `Max-Age` are set, `Max-Age` has precedence.
208209
if (isset($options['max-age']) && is_numeric($options['max-age'])) {
209-
$options['expires'] = time() + (int) $options['max-age'];
210+
$options['expires'] = Time::now()->getTimestamp() + (int) $options['max-age'];
210211
unset($options['max-age']);
211212
}
212213

@@ -314,15 +315,15 @@ public function getExpiresString(): string
314315
*/
315316
public function isExpired(): bool
316317
{
317-
return $this->expires === 0 || $this->expires < time();
318+
return $this->expires === 0 || $this->expires < Time::now()->getTimestamp();
318319
}
319320

320321
/**
321322
* {@inheritDoc}
322323
*/
323324
public function getMaxAge(): int
324325
{
325-
$maxAge = $this->expires - time();
326+
$maxAge = $this->expires - Time::now()->getTimestamp();
326327

327328
return $maxAge >= 0 ? $maxAge : 0;
328329
}
@@ -466,7 +467,7 @@ public function withNeverExpiring()
466467
{
467468
$cookie = clone $this;
468469

469-
$cookie->expires = time() + 5 * YEAR;
470+
$cookie->expires = Time::now()->getTimestamp() + 5 * YEAR;
470471

471472
return $cookie;
472473
}

system/Debug/Toolbar.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use CodeIgniter\HTTP\RequestInterface;
2323
use CodeIgniter\HTTP\Response;
2424
use CodeIgniter\HTTP\ResponseInterface;
25+
use CodeIgniter\I18n\Time;
2526
use Config\Services;
2627
use Config\Toolbar as ToolbarConfig;
2728
use Kint\Kint;
@@ -380,7 +381,7 @@ public function prepare(?RequestInterface $request = null, ?ResponseInterface $r
380381
helper('filesystem');
381382

382383
// Updated to microtime() so we can get history
383-
$time = sprintf('%.6f', microtime(true));
384+
$time = sprintf('%.6f', Time::now()->format('U.u'));
384385

385386
if (! is_dir(WRITEPATH . 'debugbar')) {
386387
mkdir(WRITEPATH . 'debugbar', 0777);

system/Debug/Toolbar/Collectors/Database.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Debug\Toolbar\Collectors;
1313

1414
use CodeIgniter\Database\Query;
15+
use CodeIgniter\I18n\Time;
1516

1617
/**
1718
* Collector for the Database tab of the Debug Toolbar.
@@ -184,7 +185,7 @@ public function display(): array
184185
'sql' => $query['query']->debugToolbarDisplay(),
185186
'trace' => $query['trace'],
186187
'trace-file' => $firstNonSystemLine,
187-
'qid' => md5($query['query'] . microtime()),
188+
'qid' => md5($query['query'] . Time::now()->format('0.u00 U')),
188189
];
189190
}, static::$queries);
190191

0 commit comments

Comments
 (0)