Skip to content

Commit 0723f67

Browse files
authored
Merge pull request #6880 from ping-yee/refactor-time
refactor: replace `time()` with Time into `Cache`, `Database` and `Test` class.
2 parents 39c336a + d5111d5 commit 0723f67

8 files changed

Lines changed: 15 additions & 9 deletions

File tree

deptrac.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ parameters:
165165
Database:
166166
- Entity
167167
- Events
168+
- I18n
168169
Email:
169170
- I18n
170171
- Events

phpstan-baseline.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ parameters:
4141
path: system/Cache/CacheFactory.php
4242

4343
-
44-
message: "#^Comparison operation \"\\>\" between int\\<1, max\\> and \\(array\\|float\\|int\\) results in an error\\.$#"
44+
message: "#^Comparison operation \"\\>\" between int and \\(array\\|float\\|int\\) results in an error\\.$#"
4545
count: 1
4646
path: system/Cache/Handlers/FileHandler.php
4747

system/Cache/Handlers/FileHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ protected function getItem(string $filename)
242242
return false;
243243
}
244244

245-
if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl']) {
245+
if ($data['ttl'] > 0 && Time::now()->getTimestamp() > $data['time'] + $data['ttl']) {
246246
// If the file is still there then try to remove it
247247
if (is_file($this->path . $filename)) {
248248
@unlink($this->path . $filename);

system/Database/MigrationRunner.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\CLI\CLI;
1515
use CodeIgniter\Events\Events;
1616
use CodeIgniter\Exceptions\ConfigException;
17+
use CodeIgniter\I18n\Time;
1718
use Config\Database;
1819
use Config\Migrations as MigrationsConfig;
1920
use Config\Services;
@@ -596,7 +597,7 @@ protected function addHistory($migration, int $batch)
596597
'class' => $migration->class,
597598
'group' => $this->group,
598599
'namespace' => $migration->namespace,
599-
'time' => time(),
600+
'time' => Time::now()->getTimestamp(),
600601
'batch' => $batch,
601602
]);
602603

system/Test/Fabricator.php

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

1414
use CodeIgniter\Exceptions\FrameworkException;
15+
use CodeIgniter\I18n\Time;
1516
use CodeIgniter\Model;
1617
use Faker\Factory;
1718
use Faker\Generator;
@@ -503,7 +504,7 @@ protected function createMock(?int $count = null)
503504
break;
504505

505506
default:
506-
$datetime = time();
507+
$datetime = Time::now()->getTimestamp();
507508
}
508509

509510
// Determine which fields we will need

system/Test/Mock/MockCache.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Closure;
1515
use CodeIgniter\Cache\CacheInterface;
1616
use CodeIgniter\Cache\Handlers\BaseHandler;
17+
use CodeIgniter\I18n\Time;
1718
use PHPUnit\Framework\Assert;
1819

1920
class MockCache extends BaseHandler implements CacheInterface
@@ -100,7 +101,7 @@ public function save(string $key, $value, int $ttl = 60, bool $raw = false)
100101
$key = static::validateKey($key, $this->prefix);
101102

102103
$this->cache[$key] = $value;
103-
$this->expirations[$key] = $ttl > 0 ? time() + $ttl : null;
104+
$this->expirations[$key] = $ttl > 0 ? Time::now()->getTimestamp() + $ttl : null;
104105

105106
return true;
106107
}
@@ -221,7 +222,7 @@ public function getMetaData(string $key)
221222
}
222223

223224
// Count expired items as a miss
224-
if (is_int($this->expirations[$key]) && $this->expirations[$key] > time()) {
225+
if (is_int($this->expirations[$key]) && $this->expirations[$key] > Time::now()->getTimestamp()) {
225226
return null;
226227
}
227228

system/Test/Mock/MockSession.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Test\Mock;
1313

1414
use CodeIgniter\Cookie\Cookie;
15+
use CodeIgniter\I18n\Time;
1516
use CodeIgniter\Session\Session;
1617

1718
/**
@@ -56,7 +57,7 @@ protected function startSession()
5657
*/
5758
protected function setCookie()
5859
{
59-
$expiration = $this->sessionExpiration === 0 ? 0 : time() + $this->sessionExpiration;
60+
$expiration = $this->sessionExpiration === 0 ? 0 : Time::now()->getTimestamp() + $this->sessionExpiration;
6061
$this->cookie = $this->cookie->withValue(session_id())->withExpires($expiration);
6162

6263
$this->cookies[] = $this->cookie;
@@ -65,6 +66,6 @@ protected function setCookie()
6566
public function regenerate(bool $destroy = false)
6667
{
6768
$this->didRegenerate = true;
68-
$_SESSION['__ci_last_regenerate'] = time();
69+
$_SESSION['__ci_last_regenerate'] = Time::now()->getTimestamp();
6970
}
7071
}

system/Test/TestResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\HTTP\RedirectResponse;
1515
use CodeIgniter\HTTP\RequestInterface;
1616
use CodeIgniter\HTTP\ResponseInterface;
17+
use CodeIgniter\I18n\Time;
1718
use Config\Services;
1819
use Exception;
1920
use PHPUnit\Framework\Constraint\IsEqual;
@@ -339,7 +340,7 @@ public function assertCookieMissing(string $key)
339340
public function assertCookieExpired(string $key, string $prefix = '')
340341
{
341342
$this->assertTrue($this->response->hasCookie($key, null, $prefix));
342-
$this->assertGreaterThan(time(), $this->response->getCookie($key, $prefix)->getExpiresTimestamp());
343+
$this->assertGreaterThan(Time::now()->getTimestamp(), $this->response->getCookie($key, $prefix)->getExpiresTimestamp());
343344
}
344345

345346
// --------------------------------------------------------------------

0 commit comments

Comments
 (0)