Skip to content

Commit 40b31bc

Browse files
authored
Merge pull request #8091 from kenjis/refactor-fix-types
refactor: fix types
2 parents a4127ca + f8c8ca5 commit 40b31bc

29 files changed

+61
-49
lines changed

system/CLI/CLI.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ public static function table(array $tbody, array $thead = [])
10661066

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

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

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

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

11081108
foreach ($tableRows[$row] as $col) {
1109-
$cols .= str_repeat('-', static::strlen($col) + 2) . '+';
1109+
$cols .= str_repeat('-', static::strlen((string) $col) + 2) . '+';
11101110
}
11111111
$table .= $cols . PHP_EOL;
11121112
}

system/Cache/ResponseCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function generateCacheKey($request): string
8383
? $uri->getQuery(is_array($this->cacheQueryString) ? ['only' => $this->cacheQueryString] : [])
8484
: '';
8585

86-
return md5($uri->setFragment('')->setQuery($query));
86+
return md5((string) $uri->setFragment('')->setQuery($query));
8787
}
8888

8989
/**

system/CodeIgniter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ protected function generateCacheName(Cache $config): string
744744
? $uri->getQuery(is_array($config->cacheQueryString) ? ['only' => $config->cacheQueryString] : [])
745745
: '';
746746

747-
return md5($uri->setFragment('')->setQuery($query));
747+
return md5((string) $uri->setFragment('')->setQuery($query));
748748
}
749749

750750
/**

system/Database/BasePreparedQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function execute(...$data)
175175
// Let others do something with this query
176176
Events::trigger('DBQuery', $query);
177177

178-
if ($this->db->isWriteType($query)) {
178+
if ($this->db->isWriteType((string) $query)) {
179179
return true;
180180
}
181181

system/Database/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ protected function matchSimpleBinds(string $sql, array $binds, int $bindCount, i
357357
$escapedValue = '(' . implode(',', $escapedValue) . ')';
358358
}
359359

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

363363
return $sql;

system/Email/Email.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ protected function setDate()
898898
{
899899
$timezone = date('Z');
900900
$operator = ($timezone[0] === '-') ? '-' : '+';
901-
$timezone = abs($timezone);
901+
$timezone = abs((int) $timezone);
902902
$timezone = floor($timezone / 3600) * 100 + ($timezone % 3600) / 60;
903903

904904
return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);

system/Entity/Cast/DatetimeCast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function get($value, array $params = [])
3838
}
3939

4040
if (is_numeric($value)) {
41-
return Time::createFromTimestamp($value);
41+
return Time::createFromTimestamp((int) $value);
4242
}
4343

4444
if (is_string($value)) {

system/Format/XMLFormatter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ protected function normalizeXMLTag($key)
9191
'\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}';
9292
$validName = $startChar . '\\.\\d\\x{B7}\\x{300}-\\x{36F}\\x{203F}-\\x{2040}';
9393

94+
$key = (string) $key;
95+
9496
$key = trim($key);
9597
$key = preg_replace("/[^{$validName}-]+/u", '', $key);
9698
$key = preg_replace("/^[^{$startChar}]+/u", 'item$0', $key);

system/HTTP/Files/FileCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ protected function createFileObject(array $array)
185185
$array['tmp_name'] ?? null,
186186
$array['name'] ?? null,
187187
$array['type'] ?? null,
188-
$array['size'] ?? null,
188+
($array['size'] ?? null) === null ? null : (int) $array['size'],
189189
$array['error'] ?? null,
190190
$array['full_path'] ?? null
191191
);

system/HTTP/ResponseInterface.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,15 @@ public function sendBody();
320320
* Accepts an arbitrary number of binds (up to 7) or an associative
321321
* array in the first parameter containing all the values.
322322
*
323-
* @param array|string $name Cookie name or array containing binds
324-
* @param string $value Cookie value
325-
* @param int $expire Cookie expiration time in seconds
326-
* @param string $domain Cookie domain (e.g.: '.yourdomain.com')
327-
* @param string $path Cookie path (default: '/')
328-
* @param string $prefix Cookie name prefix
329-
* @param bool $secure Whether to only transfer cookies via SSL
330-
* @param bool $httponly Whether only make the cookie accessible via HTTP (no javascript)
331-
* @param string|null $samesite
323+
* @param array|Cookie|string $name Cookie name / array containing binds / Cookie object
324+
* @param string $value Cookie value
325+
* @param int $expire Cookie expiration time in seconds
326+
* @param string $domain Cookie domain (e.g.: '.yourdomain.com')
327+
* @param string $path Cookie path (default: '/')
328+
* @param string $prefix Cookie name prefix
329+
* @param bool $secure Whether to only transfer cookies via SSL
330+
* @param bool $httponly Whether only make the cookie accessible via HTTP (no javascript)
331+
* @param string|null $samesite
332332
*
333333
* @return $this
334334
*/

system/Helpers/html_helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function doctype(string $type = 'html5'): string
179179
$config = new DocTypes();
180180
$doctypes = $config->list;
181181

182-
return $doctypes[$type] ?? false;
182+
return $doctypes[$type] ?? '';
183183
}
184184
}
185185

system/Helpers/number_helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function number_to_size($num, int $precision = 1, ?string $locale = null)
2424
// Strip any formatting & ensure numeric input
2525
try {
2626
// @phpstan-ignore-next-line
27-
$num = 0 + str_replace(',', '', $num);
27+
$num = 0 + str_replace(',', '', (string) $num);
2828
} catch (ErrorException $ee) {
2929
// Catch "Warning: A non-numeric value encountered"
3030
return false;
@@ -142,7 +142,7 @@ function format_number(float $num, int $precision = 1, ?string $locale = null, a
142142

143143
// Try to format it per the locale
144144
if ($type === NumberFormatter::CURRENCY) {
145-
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $options['fraction']);
145+
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, (float) $options['fraction']);
146146
$output = $formatter->formatCurrency($num, $options['currency']);
147147
} else {
148148
// In order to specify a precision, we'll have to modify

system/Helpers/text_helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function entities_to_ascii(string $str, bool $all = true): string
131131
{
132132
if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) {
133133
for ($i = 0, $s = count($matches[0]); $i < $s; $i++) {
134-
$digits = $matches[1][$i];
134+
$digits = (int) $matches[1][$i];
135135
$out = '';
136136
if ($digits < 128) {
137137
$out .= chr($digits);

system/I18n/TimeTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ public function setYear($value)
543543
public function setMonth($value)
544544
{
545545
if (is_numeric($value) && ($value < 1 || $value > 12)) {
546-
throw I18nException::forInvalidMonth($value);
546+
throw I18nException::forInvalidMonth((string) $value);
547547
}
548548

549549
if (is_string($value) && ! is_numeric($value)) {
@@ -565,13 +565,13 @@ public function setMonth($value)
565565
public function setDay($value)
566566
{
567567
if ($value < 1 || $value > 31) {
568-
throw I18nException::forInvalidDay($value);
568+
throw I18nException::forInvalidDay((string) $value);
569569
}
570570

571571
$date = $this->getYear() . '-' . $this->getMonth();
572572
$lastDay = date('t', strtotime($date));
573573
if ($value > $lastDay) {
574-
throw I18nException::forInvalidOverDay($lastDay, $value);
574+
throw I18nException::forInvalidOverDay($lastDay, (string) $value);
575575
}
576576

577577
return $this->setValue('day', $value);
@@ -589,7 +589,7 @@ public function setDay($value)
589589
public function setHour($value)
590590
{
591591
if ($value < 0 || $value > 23) {
592-
throw I18nException::forInvalidHour($value);
592+
throw I18nException::forInvalidHour((string) $value);
593593
}
594594

595595
return $this->setValue('hour', $value);
@@ -607,7 +607,7 @@ public function setHour($value)
607607
public function setMinute($value)
608608
{
609609
if ($value < 0 || $value > 59) {
610-
throw I18nException::forInvalidMinutes($value);
610+
throw I18nException::forInvalidMinutes((string) $value);
611611
}
612612

613613
return $this->setValue('minute', $value);
@@ -625,7 +625,7 @@ public function setMinute($value)
625625
public function setSecond($value)
626626
{
627627
if ($value < 0 || $value > 59) {
628-
throw I18nException::forInvalidSeconds($value);
628+
throw I18nException::forInvalidSeconds((string) $value);
629629
}
630630

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

10141014
$years = $now->fieldDifference($time, IntlCalendar::FIELD_YEAR);
@@ -1109,7 +1109,7 @@ public function getUTCObject($time, ?string $timezone = null)
11091109
*/
11101110
public function getCalendar()
11111111
{
1112-
return IntlCalendar::fromDateTime($this);
1112+
return IntlCalendar::fromDateTime($this->toDateTime());
11131113
}
11141114

11151115
/**

system/Images/Handlers/BaseHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,12 +559,12 @@ public function fit(int $width, ?int $height = null, string $position = 'center'
559559
[$cropWidth, $cropHeight] = $this->calcAspectRatio($width, $height, $origWidth, $origHeight);
560560

561561
if ($height === null) {
562-
$height = ceil(($width / $cropWidth) * $cropHeight);
562+
$height = (int) ceil(($width / $cropWidth) * $cropHeight);
563563
}
564564

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

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

570570
/**

system/Router/RouteCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,14 +1367,14 @@ protected function buildReverseRoute(string $from, array $params): string
13671367
// or maybe $placeholder is not a placeholder, but a regex.
13681368
$pattern = $this->placeholders[$placeholderName] ?? $placeholder;
13691369

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

13741374
// Ensure that the param we're inserting matches
13751375
// the expected param type.
13761376
$pos = strpos($from, $placeholder);
1377-
$from = substr_replace($from, $params[$index], $pos, strlen($placeholder));
1377+
$from = substr_replace($from, (string) $params[$index], $pos, strlen($placeholder));
13781378
}
13791379

13801380
$from = $this->replaceLocale($from, $locale);

system/Router/Router.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,9 @@ protected function checkRoutes(string $uri): bool
382382
foreach ($routes as $routeKey => $handler) {
383383
$routeKey = $routeKey === '/'
384384
? $routeKey
385-
: ltrim($routeKey, '/ ');
385+
// $routeKey may be int, because it is an array key,
386+
// and the URI `/1` is valid. The leading `/` is removed.
387+
: ltrim((string) $routeKey, '/ ');
386388

387389
$matchedKey = $routeKey;
388390

system/View/Cell.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ final protected function renderCell(BaseCell $instance, string $method, array $p
209209
$publicParams = array_intersect_key($params, $publicProperties);
210210

211211
foreach ($params as $key => $value) {
212-
$getter = 'get' . ucfirst($key) . 'Property';
212+
$getter = 'get' . ucfirst((string) $key) . 'Property';
213213
if (in_array($key, $privateProperties, true) && method_exists($instance, $getter)) {
214214
$publicParams[$key] = $value;
215215
}

system/View/Filters.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public static function date($value, string $format): string
3838
$value = strtotime($value);
3939
}
4040

41+
if ($value !== null) {
42+
$value = (int) $value;
43+
}
44+
4145
return date($format, $value);
4246
}
4347

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

161-
return format_number($value, $precision, $locale, ['type' => $types[$type]]);
165+
return format_number((float) $value, $precision, $locale, ['type' => $types[$type]]);
162166
}
163167

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

182-
return format_number($value, 2, $locale, $options);
186+
return format_number((float) $value, 2, $locale, $options);
183187
}
184188

185189
/**

system/View/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ protected function applyFilters(string $replace, array $filters): string
619619
$replace = $this->config->filters[$filter]($replace, ...$param);
620620
}
621621

622-
return $replace;
622+
return (string) $replace;
623623
}
624624

625625
// Plugins

tests/system/Commands/ClearDebugbarTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function createDummyDebugbarJson(): void
3939

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

4545
$time -= $i;

tests/system/CommonFunctionsSendTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testRedirectResponseCookiesSent(): void
4343

4444
$response = redirect()->route('login')
4545
->setCookie('foo', 'onething', YEAR)
46-
->setCookie('login_time', $loginTime, YEAR);
46+
->setCookie('login_time', (string) $loginTime, YEAR);
4747
$response->pretend(false);
4848
$this->assertTrue($response->hasCookie('foo', 'onething'));
4949
$this->assertTrue($response->hasCookie('login_time'));

tests/system/CommonFunctionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ public function testRedirectResponseCookies1(): void
576576

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

581581
$this->assertTrue($answer1->hasCookie('foo', 'onething'));
582582
$this->assertTrue($answer1->hasCookie('login_time'));

tests/system/Debug/Toolbar/Collectors/HistoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private function createDummyDebugbarJson(): void
6161

6262
// create 20 dummy debugbar json files
6363
for ($i = 0; $i < 20; $i++) {
64-
$path = str_replace($time, sprintf('%.6f', $time - self::STEP), $path);
64+
$path = str_replace((string) $time, sprintf('%.6f', $time - self::STEP), $path);
6565
file_put_contents($path, json_encode($dummyData));
6666
$time = sprintf('%.6f', $time - self::STEP);
6767
}

tests/system/HTTP/ResponseSendTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ public function testRedirectResponseCookies(): void
127127

128128
$answer1 = $response->redirect('/login')
129129
->setCookie('foo', 'bar', YEAR)
130-
->setCookie('login_time', $loginTime, YEAR);
130+
->setCookie('login_time', (string) $loginTime, YEAR);
131+
131132
$this->assertTrue($answer1->hasCookie('foo', 'bar'));
132133
$this->assertTrue($answer1->hasCookie('login_time'));
134+
133135
$response->setBody('Hello');
134136

135137
// send it

tests/system/HTTP/ResponseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ public function testRedirectResponseCookies(): void
551551
$response = new Response(new App());
552552
$answer1 = $response->redirect('/login')
553553
->setCookie('foo', 'bar', YEAR)
554-
->setCookie('login_time', $loginTime, YEAR);
554+
->setCookie('login_time', (string) $loginTime, YEAR);
555555

556556
$this->assertTrue($answer1->hasCookie('foo'));
557557
$this->assertTrue($answer1->hasCookie('login_time'));

tests/system/Helpers/CookieHelperTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,12 @@ public function testSameSiteParamArray(): void
250250

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

255255
$this->assertTrue($this->response->hasCookie($this->name));
256+
256257
$theCookie = $this->response->getCookie($this->name);
258+
257259
$this->assertSame('Strict', $theCookie->getSameSite());
258260

259261
delete_cookie($this->name);

0 commit comments

Comments
 (0)