Skip to content

Commit 609c591

Browse files
Merge pull request #18620 from kamil-tekiela/Closure-return-type
Add return type to some closures
2 parents 839c078 + 8a47f88 commit 609c591

9 files changed

Lines changed: 33 additions & 28 deletions

File tree

libraries/classes/Advisory/Advisor.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,40 @@ public function __construct(private DatabaseInterface $dbi, private ExpressionLa
6565
'round',
6666
static function (): void {
6767
},
68-
static fn (array $arguments, float $num) => round($num)
68+
static fn (array $arguments, float $num): float => round($num)
6969
);
7070
$this->expression->register(
7171
'substr',
7272
static function (): void {
7373
},
74-
static fn (array $arguments, string $string, int $start, int $length) => substr($string, $start, $length)
74+
static fn (
75+
array $arguments,
76+
string $string,
77+
int $start,
78+
int $length,
79+
): string => substr($string, $start, $length)
7580
);
7681
$this->expression->register(
7782
'preg_match',
7883
static function (): void {
7984
},
80-
static fn (array $arguments, string $pattern, string $subject) => preg_match($pattern, $subject)
85+
static fn (
86+
array $arguments,
87+
string $pattern,
88+
string $subject,
89+
): int|bool => preg_match($pattern, $subject),
8190
);
8291
$this->expression->register(
8392
'ADVISOR_bytime',
8493
static function (): void {
8594
},
86-
static fn (array $arguments, float $num, int $precision) => self::byTime($num, $precision)
95+
static fn (array $arguments, float $num, int $precision): string => self::byTime($num, $precision)
8796
);
8897
$this->expression->register(
8998
'ADVISOR_timespanFormat',
9099
static function (): void {
91100
},
92-
static fn (array $arguments, string $seconds) => Util::timespanFormat((int) $seconds)
101+
static fn (array $arguments, string $seconds): string => Util::timespanFormat((int) $seconds)
93102
);
94103
$this->expression->register(
95104
'ADVISOR_formatByteDown',
@@ -100,13 +109,13 @@ static function (): void {
100109
int $value,
101110
int $limes = 6,
102111
int $comma = 0,
103-
) => implode(' ', Util::formatByteDown($value, $limes, $comma))
112+
): string => implode(' ', Util::formatByteDown($value, $limes, $comma))
104113
);
105114
$this->expression->register(
106115
'fired',
107116
static function (): void {
108117
},
109-
function (array $arguments, int|string $value) {
118+
function (array $arguments, int|string $value): string {
110119
// Did matching rule fire?
111120
foreach ($this->firedRules as $rule) {
112121
if ($rule['id'] == $value) {
@@ -303,19 +312,19 @@ public function addRule(string $type, array $rule): void
303312
// linking to /server/variables
304313
$rule['recommendation'] = preg_replace_callback(
305314
'/\{([a-z_0-9]+)\}/Ui',
306-
fn (array $matches) => $this->replaceVariable($matches),
315+
fn (array $matches): string => $this->replaceVariable($matches),
307316
$rule['recommendation'],
308317
);
309318
$rule['issue'] = preg_replace_callback(
310319
'/\{([a-z_0-9]+)\}/Ui',
311-
fn (array $matches) => $this->replaceVariable($matches),
320+
fn (array $matches): string => $this->replaceVariable($matches),
312321
$rule['issue'],
313322
);
314323

315324
// Replaces external Links with Core::linkURL() generated links
316325
$rule['recommendation'] = preg_replace_callback(
317326
'#href=("|\')(https?://[^"\']+)\1#i',
318-
fn (array $matches) => $this->replaceLinkURL($matches),
327+
fn (array $matches): string => $this->replaceLinkURL($matches),
319328
$rule['recommendation'],
320329
);
321330

libraries/classes/Config/Settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2630,7 +2630,7 @@ public function asArray(): array
26302630
'TranslationWarningThreshold' => $this->TranslationWarningThreshold,
26312631
'AllowThirdPartyFraming' => $this->AllowThirdPartyFraming,
26322632
'blowfish_secret' => $this->blowfish_secret,
2633-
'Servers' => array_map(static fn ($server) => $server->asArray(), $this->Servers),
2633+
'Servers' => array_map(static fn (Server $server): array => $server->asArray(), $this->Servers),
26342634
'ServerDefault' => $this->ServerDefault,
26352635
'VersionCheck' => $this->VersionCheck,
26362636
'ProxyUrl' => $this->ProxyUrl,

libraries/classes/DatabaseInterface.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,7 @@ public function getTablesFull(
453453
foreach ($tables as $oneDatabaseName => $oneDatabaseTables) {
454454
uasort(
455455
$oneDatabaseTables,
456-
/**
457-
* @param array $a
458-
* @param array $b
459-
*/
460-
static function ($a, $b) {
456+
static function (array $a, array $b): int {
461457
$aLength = $a['Data_length'] + $a['Index_length'];
462458
$bLength = $b['Data_length'] + $b['Index_length'];
463459

@@ -751,7 +747,7 @@ public function getDatabasesFull(
751747
if ($applyLimitAndOrderManual) {
752748
usort(
753749
$databases,
754-
static fn ($a, $b) => Utilities::usortComparisonCallback($a, $b, $sortBy, $sortOrder)
750+
static fn ($a, $b): int => Utilities::usortComparisonCallback($a, $b, $sortBy, $sortOrder)
755751
);
756752

757753
/**

libraries/classes/Export/Export.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ private function getHTMLForBackButton(string $exportType, string $db, string $ta
12901290
);
12911291
}
12921292

1293-
$postParams = array_filter($this->getPostParams($exportType), static fn ($value) => ! is_array($value));
1293+
$postParams = array_filter($this->getPostParams($exportType), static fn ($value): bool => ! is_array($value));
12941294
$backButton .= '&' . http_build_query($postParams);
12951295

12961296
$backButton .= '&amp;repopulate=1">' . __('Back') . '</a> ]</p>';

libraries/classes/LanguageManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ public function availableLanguages(): array
842842
public function sortedLanguages(): array
843843
{
844844
$this->availableLanguages();
845-
uasort($this->availableLanguages, static fn (Language $a, Language $b) => $a->cmp($b));
845+
uasort($this->availableLanguages, static fn (Language $a, Language $b): int => $a->cmp($b));
846846

847847
return $this->availableLanguages;
848848
}

libraries/classes/Sanitize.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public static function sanitizeMessage(string $message, bool $escape = false, bo
203203
/* Find and replace all links */
204204
$message = (string) preg_replace_callback(
205205
$pattern,
206-
static fn (array $match) => self::replaceBBLink($match),
206+
static fn (array $match): string => self::replaceBBLink($match),
207207
$message,
208208
);
209209

libraries/classes/Twig/MessageExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ public function getFilters(): array
2020
return [
2121
new TwigFilter(
2222
'notice',
23-
static fn (string $string) => Message::notice($string)->getDisplay(),
23+
static fn (string $string): string => Message::notice($string)->getDisplay(),
2424
['is_safe' => ['html']],
2525
),
2626
new TwigFilter(
2727
'error',
28-
static fn (string $string) => Message::error($string)->getDisplay(),
28+
static fn (string $string): string => Message::error($string)->getDisplay(),
2929
['is_safe' => ['html']],
3030
),
3131
new TwigFilter(
3232
'raw_success',
33-
static fn (string $string) => Message::rawSuccess($string)->getDisplay(),
33+
static fn (string $string): string => Message::rawSuccess($string)->getDisplay(),
3434
['is_safe' => ['html']],
3535
),
3636
];

test/classes/Config/SettingsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ public function testServers(mixed $actual, array $expected): void
11591159
$settingsArray = $settings->asArray();
11601160
$this->assertEquals($expected, $settings->Servers);
11611161
$this->assertArrayHasKey('Servers', $settingsArray);
1162-
$expectedArray = array_map(static fn ($server) => $server->asArray(), $expected);
1162+
$expectedArray = array_map(static fn (Server $server): array => $server->asArray(), $expected);
11631163
$this->assertSame($expectedArray, $settingsArray['Servers']);
11641164
}
11651165

test/classes/Server/Privileges/AccountLockingTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testLockWithValidAccount(): void
2020
$dbi->expects($this->once())->method('isMariaDB')->willReturn(true);
2121
$dbi->expects($this->once())->method('getVersion')->willReturn(100402);
2222
$dbi->expects($this->exactly(2))->method('quoteString')
23-
->willReturnCallback(static fn (string $string) => "'" . $string . "'");
23+
->willReturnCallback(static fn (string $string): string => "'" . $string . "'");
2424
$dbi->expects($this->once())
2525
->method('tryQuery')
2626
->with($this->equalTo('ALTER USER \'test.user\'@\'test.host\' ACCOUNT LOCK;'))
@@ -37,7 +37,7 @@ public function testLockWithInvalidAccount(): void
3737
$dbi->expects($this->once())->method('isMariaDB')->willReturn(true);
3838
$dbi->expects($this->once())->method('getVersion')->willReturn(100402);
3939
$dbi->expects($this->exactly(2))->method('quoteString')
40-
->willReturnCallback(static fn (string $string) => "'" . $string . "'");
40+
->willReturnCallback(static fn (string $string): string => "'" . $string . "'");
4141
$dbi->expects($this->once())
4242
->method('tryQuery')
4343
->with($this->equalTo('ALTER USER \'test.user\'@\'test.host\' ACCOUNT LOCK;'))
@@ -75,7 +75,7 @@ public function testUnlockWithValidAccount(): void
7575
$dbi->expects($this->once())->method('isMariaDB')->willReturn(true);
7676
$dbi->expects($this->once())->method('getVersion')->willReturn(100402);
7777
$dbi->expects($this->exactly(2))->method('quoteString')
78-
->willReturnCallback(static fn (string $string) => "'" . $string . "'");
78+
->willReturnCallback(static fn (string $string): string => "'" . $string . "'");
7979
$dbi->expects($this->once())
8080
->method('tryQuery')
8181
->with($this->equalTo('ALTER USER \'test.user\'@\'test.host\' ACCOUNT UNLOCK;'))
@@ -92,7 +92,7 @@ public function testUnlockWithInvalidAccount(): void
9292
$dbi->expects($this->once())->method('isMariaDB')->willReturn(true);
9393
$dbi->expects($this->once())->method('getVersion')->willReturn(100402);
9494
$dbi->expects($this->exactly(2))->method('quoteString')
95-
->willReturnCallback(static fn (string $string) => "'" . $string . "'");
95+
->willReturnCallback(static fn (string $string): string => "'" . $string . "'");
9696
$dbi->expects($this->once())
9797
->method('tryQuery')
9898
->with($this->equalTo('ALTER USER \'test.user\'@\'test.host\' ACCOUNT UNLOCK;'))

0 commit comments

Comments
 (0)