mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Merge pull request #8091 from kenjis/refactor-fix-types
refactor: fix types
This commit is contained in:
commit
40b31bc273
@ -1066,7 +1066,7 @@ class CLI
|
||||
|
||||
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
|
||||
@ -1086,7 +1086,7 @@ class CLI
|
||||
$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);
|
||||
@ -1106,7 +1106,7 @@ class CLI
|
||||
$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;
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ final class ResponseCache
|
||||
? $uri->getQuery(is_array($this->cacheQueryString) ? ['only' => $this->cacheQueryString] : [])
|
||||
: '';
|
||||
|
||||
return md5($uri->setFragment('')->setQuery($query));
|
||||
return md5((string) $uri->setFragment('')->setQuery($query));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -744,7 +744,7 @@ class CodeIgniter
|
||||
? $uri->getQuery(is_array($config->cacheQueryString) ? ['only' => $config->cacheQueryString] : [])
|
||||
: '';
|
||||
|
||||
return md5($uri->setFragment('')->setQuery($query));
|
||||
return md5((string) $uri->setFragment('')->setQuery($query));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -175,7 +175,7 @@ abstract class BasePreparedQuery implements PreparedQueryInterface
|
||||
// Let others do something with this query
|
||||
Events::trigger('DBQuery', $query);
|
||||
|
||||
if ($this->db->isWriteType($query)) {
|
||||
if ($this->db->isWriteType((string) $query)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -357,7 +357,7 @@ class Query implements QueryInterface
|
||||
$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;
|
||||
|
@ -898,7 +898,7 @@ class Email
|
||||
{
|
||||
$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);
|
||||
|
@ -38,7 +38,7 @@ class DatetimeCast extends BaseCast
|
||||
}
|
||||
|
||||
if (is_numeric($value)) {
|
||||
return Time::createFromTimestamp($value);
|
||||
return Time::createFromTimestamp((int) $value);
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
|
@ -91,6 +91,8 @@ class XMLFormatter implements FormatterInterface
|
||||
'\\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);
|
||||
|
@ -185,7 +185,7 @@ class FileCollection
|
||||
$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
|
||||
);
|
||||
|
@ -320,15 +320,15 @@ interface ResponseInterface extends MessageInterface
|
||||
* 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
|
||||
*/
|
||||
|
@ -179,7 +179,7 @@ if (! function_exists('doctype')) {
|
||||
$config = new DocTypes();
|
||||
$doctypes = $config->list;
|
||||
|
||||
return $doctypes[$type] ?? false;
|
||||
return $doctypes[$type] ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ if (! function_exists('number_to_size')) {
|
||||
// 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;
|
||||
@ -142,7 +142,7 @@ if (! function_exists('format_number')) {
|
||||
|
||||
// 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
|
||||
|
@ -131,7 +131,7 @@ if (! function_exists('entities_to_ascii')) {
|
||||
{
|
||||
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);
|
||||
|
@ -543,7 +543,7 @@ trait TimeTrait
|
||||
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)) {
|
||||
@ -565,13 +565,13 @@ trait TimeTrait
|
||||
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);
|
||||
@ -589,7 +589,7 @@ trait TimeTrait
|
||||
public function setHour($value)
|
||||
{
|
||||
if ($value < 0 || $value > 23) {
|
||||
throw I18nException::forInvalidHour($value);
|
||||
throw I18nException::forInvalidHour((string) $value);
|
||||
}
|
||||
|
||||
return $this->setValue('hour', $value);
|
||||
@ -607,7 +607,7 @@ trait TimeTrait
|
||||
public function setMinute($value)
|
||||
{
|
||||
if ($value < 0 || $value > 59) {
|
||||
throw I18nException::forInvalidMinutes($value);
|
||||
throw I18nException::forInvalidMinutes((string) $value);
|
||||
}
|
||||
|
||||
return $this->setValue('minute', $value);
|
||||
@ -625,7 +625,7 @@ trait TimeTrait
|
||||
public function setSecond($value)
|
||||
{
|
||||
if ($value < 0 || $value > 59) {
|
||||
throw I18nException::forInvalidSeconds($value);
|
||||
throw I18nException::forInvalidSeconds((string) $value);
|
||||
}
|
||||
|
||||
return $this->setValue('second', $value);
|
||||
@ -1008,7 +1008,7 @@ trait TimeTrait
|
||||
*/
|
||||
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);
|
||||
@ -1109,7 +1109,7 @@ trait TimeTrait
|
||||
*/
|
||||
public function getCalendar()
|
||||
{
|
||||
return IntlCalendar::fromDateTime($this);
|
||||
return IntlCalendar::fromDateTime($this->toDateTime());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -559,12 +559,12 @@ abstract class BaseHandler implements ImageHandlerInterface
|
||||
[$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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1367,14 +1367,14 @@ class RouteCollection implements RouteCollectionInterface
|
||||
// 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);
|
||||
|
@ -382,7 +382,9 @@ class Router implements RouterInterface
|
||||
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;
|
||||
|
||||
|
@ -209,7 +209,7 @@ class Cell
|
||||
$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;
|
||||
}
|
||||
|
@ -38,6 +38,10 @@ class Filters
|
||||
$value = strtotime($value);
|
||||
}
|
||||
|
||||
if ($value !== null) {
|
||||
$value = (int) $value;
|
||||
}
|
||||
|
||||
return date($format, $value);
|
||||
}
|
||||
|
||||
@ -158,7 +162,7 @@ class Filters
|
||||
'duration' => NumberFormatter::DURATION,
|
||||
];
|
||||
|
||||
return format_number($value, $precision, $locale, ['type' => $types[$type]]);
|
||||
return format_number((float) $value, $precision, $locale, ['type' => $types[$type]]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -179,7 +183,7 @@ class Filters
|
||||
'fraction' => $fraction,
|
||||
];
|
||||
|
||||
return format_number($value, 2, $locale, $options);
|
||||
return format_number((float) $value, 2, $locale, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -619,7 +619,7 @@ class Parser extends View
|
||||
$replace = $this->config->filters[$filter]($replace, ...$param);
|
||||
}
|
||||
|
||||
return $replace;
|
||||
return (string) $replace;
|
||||
}
|
||||
|
||||
// Plugins
|
||||
|
@ -39,7 +39,7 @@ final class ClearDebugbarTest extends CIUnitTestCase
|
||||
|
||||
// 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;
|
||||
|
@ -43,7 +43,7 @@ final class CommonFunctionsSendTest extends CIUnitTestCase
|
||||
|
||||
$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'));
|
||||
|
@ -576,7 +576,7 @@ final class CommonFunctionsTest extends CIUnitTestCase
|
||||
|
||||
$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'));
|
||||
|
@ -61,7 +61,7 @@ final class HistoryTest extends CIUnitTestCase
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
@ -127,9 +127,11 @@ final class ResponseSendTest extends CIUnitTestCase
|
||||
|
||||
$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
|
||||
|
@ -551,7 +551,7 @@ final class ResponseTest extends CIUnitTestCase
|
||||
$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'));
|
||||
|
@ -250,10 +250,12 @@ final class CookieHelperTest extends CIUnitTestCase
|
||||
|
||||
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);
|
||||
|
@ -92,7 +92,7 @@ final class FilesystemHelperTest extends CIUnitTestCase
|
||||
$root = vfsStream::setup('root', null, $this->structure);
|
||||
$this->assertTrue($root->hasChild('foo'));
|
||||
|
||||
$this->assertSame($expected, directory_map(vfsStream::url('root'), false, true));
|
||||
$this->assertSame($expected, directory_map(vfsStream::url('root'), 0, true));
|
||||
}
|
||||
|
||||
public function testDirectoryMapLimitsRecursion(): void
|
||||
|
@ -136,7 +136,7 @@ final class TextHelperTest extends CIUnitTestCase
|
||||
$this->assertSame('file_5', increment_string('file_4'));
|
||||
$this->assertSame('file-5', increment_string('file-4', '-'));
|
||||
$this->assertSame('file-5', increment_string('file-4', '-'));
|
||||
$this->assertSame('file-1', increment_string('file', '-', '1'));
|
||||
$this->assertSame('file-1', increment_string('file', '-', 1));
|
||||
$this->assertSame('124', increment_string('123', ''));
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user