Skip to content

Commit

Permalink
Merge pull request #8091 from kenjis/refactor-fix-types
Browse files Browse the repository at this point in the history
refactor: fix types
  • Loading branch information
kenjis authored Oct 29, 2023
2 parents a4127ca + f8c8ca5 commit 40b31bc
Show file tree
Hide file tree
Showing 29 changed files with 61 additions and 49 deletions.
6 changes: 3 additions & 3 deletions system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ public static function table(array $tbody, array $thead = [])

foreach ($tableRows[$row] as $col) {
// Sets the size of this column in the current row
$allColsLengths[$row][$column] = static::strlen($col);
$allColsLengths[$row][$column] = static::strlen((string) $col);

// If the current column does not have a value among the larger ones
// or the value of this is greater than the existing one
Expand All @@ -1086,7 +1086,7 @@ public static function table(array $tbody, array $thead = [])
$column = 0;

foreach ($tableRows[$row] as $col) {
$diff = $maxColsLengths[$column] - static::strlen($col);
$diff = $maxColsLengths[$column] - static::strlen((string) $col);

if ($diff !== 0) {
$tableRows[$row][$column] .= str_repeat(' ', $diff);
Expand All @@ -1106,7 +1106,7 @@ public static function table(array $tbody, array $thead = [])
$cols = '+';

foreach ($tableRows[$row] as $col) {
$cols .= str_repeat('-', static::strlen($col) + 2) . '+';
$cols .= str_repeat('-', static::strlen((string) $col) + 2) . '+';
}
$table .= $cols . PHP_EOL;
}
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/ResponseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function generateCacheKey($request): string
? $uri->getQuery(is_array($this->cacheQueryString) ? ['only' => $this->cacheQueryString] : [])
: '';

return md5($uri->setFragment('')->setQuery($query));
return md5((string) $uri->setFragment('')->setQuery($query));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ protected function generateCacheName(Cache $config): string
? $uri->getQuery(is_array($config->cacheQueryString) ? ['only' => $config->cacheQueryString] : [])
: '';

return md5($uri->setFragment('')->setQuery($query));
return md5((string) $uri->setFragment('')->setQuery($query));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion system/Database/BasePreparedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function execute(...$data)
// Let others do something with this query
Events::trigger('DBQuery', $query);

if ($this->db->isWriteType($query)) {
if ($this->db->isWriteType((string) $query)) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion system/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ protected function matchSimpleBinds(string $sql, array $binds, int $bindCount, i
$escapedValue = '(' . implode(',', $escapedValue) . ')';
}

$sql = substr_replace($sql, $escapedValue, $matches[0][$c][1], $ml);
$sql = substr_replace($sql, (string) $escapedValue, $matches[0][$c][1], $ml);
} while ($c !== 0);

return $sql;
Expand Down
2 changes: 1 addition & 1 deletion system/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ protected function setDate()
{
$timezone = date('Z');
$operator = ($timezone[0] === '-') ? '-' : '+';
$timezone = abs($timezone);
$timezone = abs((int) $timezone);
$timezone = floor($timezone / 3600) * 100 + ($timezone % 3600) / 60;

return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Expand Down
2 changes: 1 addition & 1 deletion system/Entity/Cast/DatetimeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function get($value, array $params = [])
}

if (is_numeric($value)) {
return Time::createFromTimestamp($value);
return Time::createFromTimestamp((int) $value);
}

if (is_string($value)) {
Expand Down
2 changes: 2 additions & 0 deletions system/Format/XMLFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ protected function normalizeXMLTag($key)
'\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}';
$validName = $startChar . '\\.\\d\\x{B7}\\x{300}-\\x{36F}\\x{203F}-\\x{2040}';

$key = (string) $key;

$key = trim($key);
$key = preg_replace("/[^{$validName}-]+/u", '', $key);
$key = preg_replace("/^[^{$startChar}]+/u", 'item$0', $key);
Expand Down
2 changes: 1 addition & 1 deletion system/HTTP/Files/FileCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ protected function createFileObject(array $array)
$array['tmp_name'] ?? null,
$array['name'] ?? null,
$array['type'] ?? null,
$array['size'] ?? null,
($array['size'] ?? null) === null ? null : (int) $array['size'],
$array['error'] ?? null,
$array['full_path'] ?? null
);
Expand Down
18 changes: 9 additions & 9 deletions system/HTTP/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,15 @@ public function sendBody();
* Accepts an arbitrary number of binds (up to 7) or an associative
* array in the first parameter containing all the values.
*
* @param array|string $name Cookie name or array containing binds
* @param string $value Cookie value
* @param int $expire Cookie expiration time in seconds
* @param string $domain Cookie domain (e.g.: '.yourdomain.com')
* @param string $path Cookie path (default: '/')
* @param string $prefix Cookie name prefix
* @param bool $secure Whether to only transfer cookies via SSL
* @param bool $httponly Whether only make the cookie accessible via HTTP (no javascript)
* @param string|null $samesite
* @param array|Cookie|string $name Cookie name / array containing binds / Cookie object
* @param string $value Cookie value
* @param int $expire Cookie expiration time in seconds
* @param string $domain Cookie domain (e.g.: '.yourdomain.com')
* @param string $path Cookie path (default: '/')
* @param string $prefix Cookie name prefix
* @param bool $secure Whether to only transfer cookies via SSL
* @param bool $httponly Whether only make the cookie accessible via HTTP (no javascript)
* @param string|null $samesite
*
* @return $this
*/
Expand Down
2 changes: 1 addition & 1 deletion system/Helpers/html_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function doctype(string $type = 'html5'): string
$config = new DocTypes();
$doctypes = $config->list;

return $doctypes[$type] ?? false;
return $doctypes[$type] ?? '';
}
}

Expand Down
4 changes: 2 additions & 2 deletions system/Helpers/number_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function number_to_size($num, int $precision = 1, ?string $locale = null)
// Strip any formatting & ensure numeric input
try {
// @phpstan-ignore-next-line
$num = 0 + str_replace(',', '', $num);
$num = 0 + str_replace(',', '', (string) $num);
} catch (ErrorException $ee) {
// Catch "Warning: A non-numeric value encountered"
return false;
Expand Down Expand Up @@ -142,7 +142,7 @@ function format_number(float $num, int $precision = 1, ?string $locale = null, a

// Try to format it per the locale
if ($type === NumberFormatter::CURRENCY) {
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $options['fraction']);
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, (float) $options['fraction']);
$output = $formatter->formatCurrency($num, $options['currency']);
} else {
// In order to specify a precision, we'll have to modify
Expand Down
2 changes: 1 addition & 1 deletion system/Helpers/text_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function entities_to_ascii(string $str, bool $all = true): string
{
if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) {
for ($i = 0, $s = count($matches[0]); $i < $s; $i++) {
$digits = $matches[1][$i];
$digits = (int) $matches[1][$i];
$out = '';
if ($digits < 128) {
$out .= chr($digits);
Expand Down
16 changes: 8 additions & 8 deletions system/I18n/TimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ public function setYear($value)
public function setMonth($value)
{
if (is_numeric($value) && ($value < 1 || $value > 12)) {
throw I18nException::forInvalidMonth($value);
throw I18nException::forInvalidMonth((string) $value);
}

if (is_string($value) && ! is_numeric($value)) {
Expand All @@ -565,13 +565,13 @@ public function setMonth($value)
public function setDay($value)
{
if ($value < 1 || $value > 31) {
throw I18nException::forInvalidDay($value);
throw I18nException::forInvalidDay((string) $value);
}

$date = $this->getYear() . '-' . $this->getMonth();
$lastDay = date('t', strtotime($date));
if ($value > $lastDay) {
throw I18nException::forInvalidOverDay($lastDay, $value);
throw I18nException::forInvalidOverDay($lastDay, (string) $value);
}

return $this->setValue('day', $value);
Expand All @@ -589,7 +589,7 @@ public function setDay($value)
public function setHour($value)
{
if ($value < 0 || $value > 23) {
throw I18nException::forInvalidHour($value);
throw I18nException::forInvalidHour((string) $value);
}

return $this->setValue('hour', $value);
Expand All @@ -607,7 +607,7 @@ public function setHour($value)
public function setMinute($value)
{
if ($value < 0 || $value > 59) {
throw I18nException::forInvalidMinutes($value);
throw I18nException::forInvalidMinutes((string) $value);
}

return $this->setValue('minute', $value);
Expand All @@ -625,7 +625,7 @@ public function setMinute($value)
public function setSecond($value)
{
if ($value < 0 || $value > 59) {
throw I18nException::forInvalidSeconds($value);
throw I18nException::forInvalidSeconds((string) $value);
}

return $this->setValue('second', $value);
Expand Down Expand Up @@ -1008,7 +1008,7 @@ public function isAfter($testTime, ?string $timezone = null): bool
*/
public function humanize()
{
$now = IntlCalendar::fromDateTime(self::now($this->timezone));
$now = IntlCalendar::fromDateTime(self::now($this->timezone)->toDateTime());
$time = $this->getCalendar()->getTime();

$years = $now->fieldDifference($time, IntlCalendar::FIELD_YEAR);
Expand Down Expand Up @@ -1109,7 +1109,7 @@ public function getUTCObject($time, ?string $timezone = null)
*/
public function getCalendar()
{
return IntlCalendar::fromDateTime($this);
return IntlCalendar::fromDateTime($this->toDateTime());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions system/Images/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,12 @@ public function fit(int $width, ?int $height = null, string $position = 'center'
[$cropWidth, $cropHeight] = $this->calcAspectRatio($width, $height, $origWidth, $origHeight);

if ($height === null) {
$height = ceil(($width / $cropWidth) * $cropHeight);
$height = (int) ceil(($width / $cropWidth) * $cropHeight);
}

[$x, $y] = $this->calcCropCoords($cropWidth, $cropHeight, $origWidth, $origHeight, $position);

return $this->crop($cropWidth, $cropHeight, $x, $y)->resize($width, $height);
return $this->crop($cropWidth, $cropHeight, (int) $x, (int) $y)->resize($width, $height);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1367,14 +1367,14 @@ protected function buildReverseRoute(string $from, array $params): string
// or maybe $placeholder is not a placeholder, but a regex.
$pattern = $this->placeholders[$placeholderName] ?? $placeholder;

if (! preg_match('#^' . $pattern . '$#u', $params[$index])) {
if (! preg_match('#^' . $pattern . '$#u', (string) $params[$index])) {
throw RouterException::forInvalidParameterType();
}

// Ensure that the param we're inserting matches
// the expected param type.
$pos = strpos($from, $placeholder);
$from = substr_replace($from, $params[$index], $pos, strlen($placeholder));
$from = substr_replace($from, (string) $params[$index], $pos, strlen($placeholder));
}

$from = $this->replaceLocale($from, $locale);
Expand Down
4 changes: 3 additions & 1 deletion system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,9 @@ protected function checkRoutes(string $uri): bool
foreach ($routes as $routeKey => $handler) {
$routeKey = $routeKey === '/'
? $routeKey
: ltrim($routeKey, '/ ');
// $routeKey may be int, because it is an array key,
// and the URI `/1` is valid. The leading `/` is removed.
: ltrim((string) $routeKey, '/ ');

$matchedKey = $routeKey;

Expand Down
2 changes: 1 addition & 1 deletion system/View/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ final protected function renderCell(BaseCell $instance, string $method, array $p
$publicParams = array_intersect_key($params, $publicProperties);

foreach ($params as $key => $value) {
$getter = 'get' . ucfirst($key) . 'Property';
$getter = 'get' . ucfirst((string) $key) . 'Property';
if (in_array($key, $privateProperties, true) && method_exists($instance, $getter)) {
$publicParams[$key] = $value;
}
Expand Down
8 changes: 6 additions & 2 deletions system/View/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public static function date($value, string $format): string
$value = strtotime($value);
}

if ($value !== null) {
$value = (int) $value;
}

return date($format, $value);
}

Expand Down Expand Up @@ -158,7 +162,7 @@ public static function local_number($value, string $type = 'decimal', int $preci
'duration' => NumberFormatter::DURATION,
];

return format_number($value, $precision, $locale, ['type' => $types[$type]]);
return format_number((float) $value, $precision, $locale, ['type' => $types[$type]]);
}

/**
Expand All @@ -179,7 +183,7 @@ public static function local_currency($value, string $currency, ?string $locale
'fraction' => $fraction,
];

return format_number($value, 2, $locale, $options);
return format_number((float) $value, 2, $locale, $options);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion system/View/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ protected function applyFilters(string $replace, array $filters): string
$replace = $this->config->filters[$filter]($replace, ...$param);
}

return $replace;
return (string) $replace;
}

// Plugins
Expand Down
2 changes: 1 addition & 1 deletion tests/system/Commands/ClearDebugbarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function createDummyDebugbarJson(): void

// create 10 dummy debugbar json files
for ($i = 0; $i < 10; $i++) {
$path = str_replace($time, $time - $i, $path);
$path = str_replace((string) $time, (string) ($time - $i), $path);
file_put_contents($path, "{}\n");

$time -= $i;
Expand Down
2 changes: 1 addition & 1 deletion tests/system/CommonFunctionsSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testRedirectResponseCookiesSent(): void

$response = redirect()->route('login')
->setCookie('foo', 'onething', YEAR)
->setCookie('login_time', $loginTime, YEAR);
->setCookie('login_time', (string) $loginTime, YEAR);
$response->pretend(false);
$this->assertTrue($response->hasCookie('foo', 'onething'));
$this->assertTrue($response->hasCookie('login_time'));
Expand Down
2 changes: 1 addition & 1 deletion tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ public function testRedirectResponseCookies1(): void

$answer1 = redirect()->route('login')
->setCookie('foo', 'onething', YEAR)
->setCookie('login_time', $loginTime, YEAR);
->setCookie('login_time', (string) $loginTime, YEAR);

$this->assertTrue($answer1->hasCookie('foo', 'onething'));
$this->assertTrue($answer1->hasCookie('login_time'));
Expand Down
2 changes: 1 addition & 1 deletion tests/system/Debug/Toolbar/Collectors/HistoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function createDummyDebugbarJson(): void

// create 20 dummy debugbar json files
for ($i = 0; $i < 20; $i++) {
$path = str_replace($time, sprintf('%.6f', $time - self::STEP), $path);
$path = str_replace((string) $time, sprintf('%.6f', $time - self::STEP), $path);
file_put_contents($path, json_encode($dummyData));
$time = sprintf('%.6f', $time - self::STEP);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/system/HTTP/ResponseSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ public function testRedirectResponseCookies(): void

$answer1 = $response->redirect('/login')
->setCookie('foo', 'bar', YEAR)
->setCookie('login_time', $loginTime, YEAR);
->setCookie('login_time', (string) $loginTime, YEAR);

$this->assertTrue($answer1->hasCookie('foo', 'bar'));
$this->assertTrue($answer1->hasCookie('login_time'));

$response->setBody('Hello');

// send it
Expand Down
2 changes: 1 addition & 1 deletion tests/system/HTTP/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public function testRedirectResponseCookies(): void
$response = new Response(new App());
$answer1 = $response->redirect('/login')
->setCookie('foo', 'bar', YEAR)
->setCookie('login_time', $loginTime, YEAR);
->setCookie('login_time', (string) $loginTime, YEAR);

$this->assertTrue($answer1->hasCookie('foo'));
$this->assertTrue($answer1->hasCookie('login_time'));
Expand Down
4 changes: 3 additions & 1 deletion tests/system/Helpers/CookieHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,12 @@ public function testSameSiteParamArray(): void

public function testSameSiteParam(): void
{
set_cookie($this->name, $this->value, $this->expire, '', '', '', '', '', 'Strict');
set_cookie($this->name, $this->value, $this->expire, '', '', '', null, null, 'Strict');

$this->assertTrue($this->response->hasCookie($this->name));

$theCookie = $this->response->getCookie($this->name);

$this->assertSame('Strict', $theCookie->getSameSite());

delete_cookie($this->name);
Expand Down
Loading

0 comments on commit 40b31bc

Please sign in to comment.