From 87e42d591cfb4791ff6e2d1ea83259c88ccea169 Mon Sep 17 00:00:00 2001
From: Kamil Tekiela
Date: Tue, 14 Feb 2023 13:22:49 +0000
Subject: [PATCH] Use short closures where possible
Signed-off-by: Kamil Tekiela
---
libraries/classes/Advisory/Advisor.php | 77 ++++---------------
libraries/classes/Config.php | 4 +-
libraries/classes/DatabaseInterface.php | 8 +-
libraries/classes/Export.php | 4 +-
libraries/classes/LanguageManager.php | 6 +-
libraries/classes/Plugins.php | 7 +-
libraries/classes/Sanitize.php | 12 +--
libraries/classes/Server/Privileges.php | 6 +-
libraries/classes/StorageEngine.php | 8 +-
libraries/classes/Twig/MessageExtension.php | 12 +--
libraries/classes/Util.php | 4 +-
.../classes/WebAuthn/WebauthnLibServer.php | 6 +-
phpstan-baseline.neon | 5 --
psalm-baseline.xml | 15 ----
14 files changed, 42 insertions(+), 132 deletions(-)
diff --git a/libraries/classes/Advisory/Advisor.php b/libraries/classes/Advisory/Advisor.php
index 5f7e902608..ed6477d92e 100644
--- a/libraries/classes/Advisory/Advisor.php
+++ b/libraries/classes/Advisory/Advisor.php
@@ -71,89 +71,48 @@ class Advisor
'round',
static function (): void {
},
- /**
- * @param array $arguments
- * @param float $num
- */
- static function ($arguments, $num) {
- return round($num);
- }
+ static fn (array $arguments, float $num) => round($num)
);
$this->expression->register(
'substr',
static function (): void {
},
- /**
- * @param array $arguments
- * @param string $string
- * @param int $start
- * @param int $length
- */
- static function ($arguments, $string, $start, $length) {
- return substr($string, $start, $length);
- }
+ static fn (array $arguments, string $string, int $start, int $length) => substr($string, $start, $length)
);
$this->expression->register(
'preg_match',
static function (): void {
},
- /**
- * @param array $arguments
- * @param string $pattern
- * @param string $subject
- */
- static function ($arguments, $pattern, $subject) {
- return preg_match($pattern, $subject);
- }
+ static fn (array $arguments, string $pattern, string $subject) => preg_match($pattern, $subject)
);
$this->expression->register(
'ADVISOR_bytime',
static function (): void {
},
- /**
- * @param array $arguments
- * @param float $num
- * @param int $precision
- */
- static function ($arguments, $num, $precision) {
- return self::byTime($num, $precision);
- }
+ static fn (array $arguments, float $num, int $precision) => self::byTime($num, $precision)
);
$this->expression->register(
'ADVISOR_timespanFormat',
static function (): void {
},
- /**
- * @param array $arguments
- * @param string $seconds
- */
- static function ($arguments, $seconds) {
- return Util::timespanFormat((int) $seconds);
- }
+ static fn (array $arguments, string $seconds) => Util::timespanFormat((int) $seconds)
);
$this->expression->register(
'ADVISOR_formatByteDown',
static function (): void {
},
- /**
- * @param array $arguments
- * @param int $value
- * @param int $limes
- * @param int $comma
- */
- static function ($arguments, $value, $limes = 6, $comma = 0) {
- return implode(' ', (array) Util::formatByteDown($value, $limes, $comma));
- }
+ static fn (
+ array $arguments,
+ int $value,
+ int $limes = 6,
+ int $comma = 0
+ ) => implode(' ', (array) Util::formatByteDown($value, $limes, $comma))
);
$this->expression->register(
'fired',
static function (): void {
},
- /**
- * @param array $arguments
- * @param int $value
- */
- function ($arguments, $value) {
+ function (array $arguments, int $value) {
// Did matching rule fire?
foreach ($this->runResult['fired'] as $rule) {
if ($rule['id'] == $value) {
@@ -345,25 +304,19 @@ class Advisor
// linking to /server/variables
$rule['recommendation'] = preg_replace_callback(
'/\{([a-z_0-9]+)\}/Ui',
- function (array $matches) {
- return $this->replaceVariable($matches);
- },
+ fn (array $matches) => $this->replaceVariable($matches),
$rule['recommendation']
);
$rule['issue'] = preg_replace_callback(
'/\{([a-z_0-9]+)\}/Ui',
- function (array $matches) {
- return $this->replaceVariable($matches);
- },
+ fn (array $matches) => $this->replaceVariable($matches),
$rule['issue']
);
// Replaces external Links with Core::linkURL() generated links
$rule['recommendation'] = preg_replace_callback(
'#href=("|\')(https?://[^"\']+)\1#i',
- function (array $matches) {
- return $this->replaceLinkURL($matches);
- },
+ fn (array $matches) => $this->replaceLinkURL($matches),
$rule['recommendation']
);
diff --git a/libraries/classes/Config.php b/libraries/classes/Config.php
index 6f1f4b9edd..3d6c0aadeb 100644
--- a/libraries/classes/Config.php
+++ b/libraries/classes/Config.php
@@ -395,9 +395,7 @@ class Config
*/
$cfg = array_filter(
$cfg,
- static function (string $key): bool {
- return ! str_contains($key, '/');
- },
+ static fn (string $key): bool => ! str_contains($key, '/'),
ARRAY_FILTER_USE_KEY
);
diff --git a/libraries/classes/DatabaseInterface.php b/libraries/classes/DatabaseInterface.php
index 4964372e4a..6d650e9ddb 100644
--- a/libraries/classes/DatabaseInterface.php
+++ b/libraries/classes/DatabaseInterface.php
@@ -486,9 +486,7 @@ class DatabaseInterface implements DbalInterface
. implode(
', ',
array_map(
- function (string $string) use ($connectionType): string {
- return $this->quoteString($string, $connectionType);
- },
+ fn (string $string): string => $this->quoteString($string, $connectionType),
$table
)
) . ')';
@@ -753,9 +751,7 @@ class DatabaseInterface implements DbalInterface
if ($applyLimitAndOrderManual) {
usort(
$databases,
- static function ($a, $b) use ($sortBy, $sortOrder) {
- return Utilities::usortComparisonCallback($a, $b, $sortBy, $sortOrder);
- }
+ static fn ($a, $b) => Utilities::usortComparisonCallback($a, $b, $sortBy, $sortOrder)
);
/**
diff --git a/libraries/classes/Export.php b/libraries/classes/Export.php
index 3aa57f13f5..707f43f2f7 100644
--- a/libraries/classes/Export.php
+++ b/libraries/classes/Export.php
@@ -1310,9 +1310,7 @@ class Export
);
}
- $postParams = array_filter($this->getPostParams($exportType), static function ($value) {
- return ! is_array($value);
- });
+ $postParams = array_filter($this->getPostParams($exportType), static fn ($value) => ! is_array($value));
$backButton .= '&' . http_build_query($postParams);
$backButton .= '&repopulate=1">' . __('Back') . ' ]
';
diff --git a/libraries/classes/LanguageManager.php b/libraries/classes/LanguageManager.php
index c568eb5fb7..baff65dfb1 100644
--- a/libraries/classes/LanguageManager.php
+++ b/libraries/classes/LanguageManager.php
@@ -711,7 +711,7 @@ class LanguageManager
/** @var array */
private $availableLocales;
- /** @var array */
+ /** @var Language[] */
private $availableLanguages = [];
/** @var bool */
@@ -847,9 +847,7 @@ class LanguageManager
public function sortedLanguages()
{
$this->availableLanguages();
- uasort($this->availableLanguages, static function (Language $a, Language $b) {
- return $a->cmp($b);
- });
+ uasort($this->availableLanguages, static fn (Language $a, Language $b) => $a->cmp($b));
return $this->availableLanguages;
}
diff --git a/libraries/classes/Plugins.php b/libraries/classes/Plugins.php
index 025dce23e0..652107c1d9 100644
--- a/libraries/classes/Plugins.php
+++ b/libraries/classes/Plugins.php
@@ -168,9 +168,10 @@ class Plugins
}
}
- usort($plugins, static function (Plugin $plugin1, Plugin $plugin2): int {
- return strcasecmp($plugin1->getProperties()->getText(), $plugin2->getProperties()->getText());
- });
+ usort($plugins, static fn (Plugin $plugin1, Plugin $plugin2): int => strcasecmp(
+ $plugin1->getProperties()->getText(),
+ $plugin2->getProperties()->getText()
+ ));
return $plugins;
}
diff --git a/libraries/classes/Sanitize.php b/libraries/classes/Sanitize.php
index 7628123a75..225c36b8f8 100644
--- a/libraries/classes/Sanitize.php
+++ b/libraries/classes/Sanitize.php
@@ -208,17 +208,17 @@ class Sanitize
$pattern = '/\[a@([^]"@]*)(@([^]"]*))?\]/';
/* Find and replace all links */
- $message = (string) preg_replace_callback($pattern, static function (array $match) {
- return self::replaceBBLink($match);
- }, $message);
+ $message = (string) preg_replace_callback(
+ $pattern,
+ static fn (array $match) => self::replaceBBLink($match),
+ $message
+ );
/* Replace documentation links */
$message = (string) preg_replace_callback(
'/\[doc@([a-zA-Z0-9_-]+)(@([a-zA-Z0-9_-]*))?\]/',
/** @param string[] $match */
- static function (array $match): string {
- return self::replaceDocLink($match);
- },
+ static fn (array $match): string => self::replaceDocLink($match),
$message
);
diff --git a/libraries/classes/Server/Privileges.php b/libraries/classes/Server/Privileges.php
index a3a81daba7..b1dcab8b14 100644
--- a/libraries/classes/Server/Privileges.php
+++ b/libraries/classes/Server/Privileges.php
@@ -259,12 +259,8 @@ class Privileges
$grantCols = array_map(
/**
* @param string $val
- *
- * @return string
*/
- static function ($val) {
- return Util::backquote($val);
- },
+ static fn ($val): string => Util::backquote($val),
$GLOBALS[$currentGrant[0]]
);
diff --git a/libraries/classes/StorageEngine.php b/libraries/classes/StorageEngine.php
index de82e41baa..7456458a61 100644
--- a/libraries/classes/StorageEngine.php
+++ b/libraries/classes/StorageEngine.php
@@ -111,11 +111,9 @@ class StorageEngine
$disabled = (string) SessionCache::get(
'disabled_storage_engines',
/** @return mixed|false */
- static function () {
- return $GLOBALS['dbi']->fetchValue(
- 'SELECT @@disabled_storage_engines'
- );
- }
+ static fn () => $GLOBALS['dbi']->fetchValue(
+ 'SELECT @@disabled_storage_engines'
+ )
);
foreach (explode(',', $disabled) as $engine) {
if (! isset($storage_engines[$engine])) {
diff --git a/libraries/classes/Twig/MessageExtension.php b/libraries/classes/Twig/MessageExtension.php
index f7b145a418..079bb42794 100644
--- a/libraries/classes/Twig/MessageExtension.php
+++ b/libraries/classes/Twig/MessageExtension.php
@@ -20,23 +20,17 @@ class MessageExtension extends AbstractExtension
return [
new TwigFilter(
'notice',
- static function (string $string) {
- return Message::notice($string)->getDisplay();
- },
+ static fn (string $string) => Message::notice($string)->getDisplay(),
['is_safe' => ['html']]
),
new TwigFilter(
'error',
- static function (string $string) {
- return Message::error($string)->getDisplay();
- },
+ static fn (string $string) => Message::error($string)->getDisplay(),
['is_safe' => ['html']]
),
new TwigFilter(
'raw_success',
- static function (string $string) {
- return Message::rawSuccess($string)->getDisplay();
- },
+ static fn (string $string) => Message::rawSuccess($string)->getDisplay(),
['is_safe' => ['html']]
),
];
diff --git a/libraries/classes/Util.php b/libraries/classes/Util.php
index ff5b6413db..24794af713 100644
--- a/libraries/classes/Util.php
+++ b/libraries/classes/Util.php
@@ -2522,9 +2522,7 @@ class Util
$disabled = ini_get('disable_functions');
if (is_string($disabled)) {
$disabled = explode(',', $disabled);
- $disabled = array_map(static function (string $part) {
- return trim($part);
- }, $disabled);
+ $disabled = array_map(static fn (string $part) => trim($part), $disabled);
return ! in_array('error_reporting', $disabled);
}
diff --git a/libraries/classes/WebAuthn/WebauthnLibServer.php b/libraries/classes/WebAuthn/WebauthnLibServer.php
index 2fbe424499..9a9895e649 100644
--- a/libraries/classes/WebAuthn/WebauthnLibServer.php
+++ b/libraries/classes/WebAuthn/WebauthnLibServer.php
@@ -82,9 +82,9 @@ final class WebauthnLibServer implements Server
$server = new WebauthnServer($relyingPartyEntity, $publicKeyCredentialSourceRepository);
$credentialSources = $publicKeyCredentialSourceRepository->findAllForUserEntity($userEntity);
$allowedCredentials = array_map(
- static function (PublicKeyCredentialSource $credential): PublicKeyCredentialDescriptor {
- return $credential->getPublicKeyCredentialDescriptor();
- },
+ static fn (
+ PublicKeyCredentialSource $credential
+ ): PublicKeyCredentialDescriptor => $credential->getPublicKeyCredentialDescriptor(),
$credentialSources
);
$publicKeyCredentialRequestOptions = $server->generatePublicKeyCredentialRequestOptions(
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index 26381b8e06..680a94955a 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -5285,11 +5285,6 @@ parameters:
count: 1
path: libraries/classes/LanguageManager.php
- -
- message: "#^Property PhpMyAdmin\\\\LanguageManager\\:\\:\\$availableLanguages type has no value type specified in iterable type array\\.$#"
- count: 1
- path: libraries/classes/LanguageManager.php
-
-
message: "#^Property PhpMyAdmin\\\\LanguageManager\\:\\:\\$availableLocales \\(array\\) does not accept array\\|false\\.$#"
count: 1
diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index 95a1619312..c9df89764a 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -9549,9 +9549,6 @@
$this->listLocaleDir()
- uasort($this->availableLanguages, static function (Language $a, Language $b) {
- return $a->cmp($b);
- })
$langs[$GLOBALS['config']->get('DefaultLang')]
@@ -9564,21 +9561,9 @@
$lang
-
- Language
-
$GLOBALS['config']->get('FilterLanguages')
-
- $this->availableLanguages[strtolower($GLOBALS['lang'])]
-
-
- $this->availableLanguages
- $this->availableLanguages
- Language[]
- Language[]
-
$_GET['lang']
$_POST['lang']