Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: changes time() to Time class in Throttle, ResponseTrait, Security and ImageMagickHandler #6872

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ parameters:
- Events
Entity:
- I18n
Files:
- I18n
Filters:
- HTTP
Honeypot:
Expand All @@ -178,10 +180,12 @@ parameters:
HTTP:
- Cookie
- Files
- I18n
- Security
- URI
Images:
- Files
- I18n
Model:
- Database
- I18n
Expand All @@ -200,6 +204,7 @@ parameters:
- HTTP
Security:
- Cookie
- I18n
- Session
- HTTP
Session:
Expand All @@ -209,6 +214,7 @@ parameters:
- I18n
Throttle:
- Cache
- I18n
Validation:
- HTTP
View:
Expand Down
3 changes: 2 additions & 1 deletion system/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CodeIgniter\Files\Exceptions\FileException;
use CodeIgniter\Files\Exceptions\FileNotFoundException;
use CodeIgniter\I18n\Time;
use Config\Mimes;
use ReturnTypeWillChange;
use SplFileInfo;
Expand Down Expand Up @@ -126,7 +127,7 @@ public function getRandomName(): string
$extension = $this->getExtension();
$extension = empty($extension) ? '' : '.' . $extension;

return time() . '_' . bin2hex(random_bytes(10)) . $extension;
return Time::now()->getTimestamp() . '_' . bin2hex(random_bytes(10)) . $extension;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions system/HTTP/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CodeIgniter\Cookie\CookieStore;
use CodeIgniter\Cookie\Exceptions\CookieException;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Pager\PagerInterface;
use CodeIgniter\Security\Exceptions\SecurityException;
use Config\Cookie as CookieConfig;
Expand Down Expand Up @@ -464,7 +465,7 @@ public function sendHeaders()
// Per spec, MUST be sent with each request, if possible.
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
if (! isset($this->headers['Date']) && PHP_SAPI !== 'cli-server') {
$this->setDate(DateTime::createFromFormat('U', (string) time()));
$this->setDate(DateTime::createFromFormat('U', (string) Time::now()->getTimestamp()));
}

// HTTP Status
Expand Down Expand Up @@ -587,7 +588,7 @@ public function setCookie(
}

if (is_numeric($expire)) {
$expire = $expire > 0 ? time() + $expire : 0;
$expire = $expire > 0 ? Time::now()->getTimestamp() + $expire : 0;
}

$cookie = new Cookie($name, $value, [
Expand Down
3 changes: 2 additions & 1 deletion system/Images/Handlers/ImageMagickHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Images\Handlers;

use CodeIgniter\I18n\Time;
use CodeIgniter\Images\Exceptions\ImageException;
use Config\Images;
use Exception;
Expand Down Expand Up @@ -276,7 +277,7 @@ protected function getResourcePath()
return $this->resource;
}

$this->resource = WRITEPATH . 'cache/' . time() . '_' . bin2hex(random_bytes(10)) . '.png';
$this->resource = WRITEPATH . 'cache/' . Time::now()->getTimestamp() . '_' . bin2hex(random_bytes(10)) . '.png';

$name = basename($this->resource);
$path = pathinfo($this->resource, PATHINFO_DIRNAME);
Expand Down
3 changes: 2 additions & 1 deletion system/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\Response;
use CodeIgniter\I18n\Time;
use CodeIgniter\Security\Exceptions\SecurityException;
use CodeIgniter\Session\Session;
use Config\App;
Expand Down Expand Up @@ -567,7 +568,7 @@ private function saveHashInCookie(): void
$this->rawCookieName,
$this->hash,
[
'expires' => $this->expires === 0 ? 0 : time() + $this->expires,
'expires' => $this->expires === 0 ? 0 : Time::now()->getTimestamp() + $this->expires,
]
);

Expand Down
3 changes: 2 additions & 1 deletion system/Throttle/Throttler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Throttle;

use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\I18n\Time;

/**
* Class Throttler
Expand Down Expand Up @@ -176,6 +177,6 @@ public function setTestTime(int $time)
*/
public function time(): int
{
return $this->testTime ?? time();
return $this->testTime ?? Time::now()->getTimestamp();
}
}