From 25ca884d61f60e2eaa0986cd07da7c4213cbbd6d Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 17 Dec 2024 00:29:49 +0000 Subject: [PATCH 1/7] Make Status\Data::$status readonly Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 32 ++++--------------- psalm-baseline.xml | 17 ++-------- src/Server/Status/Data.php | 7 ++-- .../Server/Status/QueriesControllerTest.php | 14 ++++++-- .../Server/Status/StatusControllerTest.php | 25 ++++++++++----- 5 files changed, 41 insertions(+), 54 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ea011733d8..4073ca8689 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -3948,18 +3948,6 @@ parameters: count: 1 path: src/Controllers/Server/Status/VariablesController.php - - - message: '#^Parameter \#1 \$haystack of function str_contains expects string, \(int\|string\) given\.$#' - identifier: argument.type - count: 1 - path: src/Controllers/Server/Status/VariablesController.php - - - - message: '#^Parameter \#1 \$name of static method PhpMyAdmin\\Html\\Generator\:\:linkToVarDocumentation\(\) expects string, \(int\|string\) given\.$#' - identifier: argument.type - count: 1 - path: src/Controllers/Server/Status/VariablesController.php - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' identifier: ternary.shortNotAllowed @@ -16296,6 +16284,12 @@ parameters: count: 1 path: src/Server/Status/Data.php + - + message: '#^Property PhpMyAdmin\\Server\\Status\\Data\:\:\$status \(array\\) does not accept array\\.$#' + identifier: assign.propertyType + count: 1 + path: src/Server/Status/Data.php + - message: '#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\.$#' identifier: foreach.nonIterable @@ -20490,12 +20484,6 @@ parameters: count: 1 path: tests/unit/Controllers/Server/Status/ProcessesControllerTest.php - - - message: '#^@readonly property PhpMyAdmin\\Server\\Status\\Data\:\:\$status is assigned outside of its declaring class\.$#' - identifier: property.readOnlyByPhpDocAssignOutOfClass - count: 1 - path: tests/unit/Controllers/Server/Status/QueriesControllerTest.php - - message: '#^@readonly property PhpMyAdmin\\Server\\Status\\Data\:\:\$usedQueries is assigned outside of its declaring class\.$#' identifier: property.readOnlyByPhpDocAssignOutOfClass @@ -20532,12 +20520,6 @@ parameters: count: 1 path: tests/unit/Controllers/Server/Status/QueriesControllerTest.php - - - message: '#^@readonly property PhpMyAdmin\\Server\\Status\\Data\:\:\$status is assigned outside of its declaring class\.$#' - identifier: property.readOnlyByPhpDocAssignOutOfClass - count: 6 - path: tests/unit/Controllers/Server/Status/StatusControllerTest.php - - message: ''' #^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Config\: @@ -20553,7 +20535,7 @@ parameters: Use dependency injection instead\.$# ''' identifier: staticMethod.deprecated - count: 3 + count: 2 path: tests/unit/Controllers/Server/Status/StatusControllerTest.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 85ee57d7d0..71be13cf82 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2410,10 +2410,6 @@ - - - - @@ -9530,6 +9526,9 @@ + + + @@ -11979,7 +11978,6 @@ - data->status]]> data->usedQueries]]> @@ -12001,16 +11999,7 @@ - - - status]]> - status]]> - status]]> - status]]> - status]]> - status]]> - diff --git a/src/Server/Status/Data.php b/src/Server/Status/Data.php index a6df004902..4b657d9ada 100644 --- a/src/Server/Status/Data.php +++ b/src/Server/Status/Data.php @@ -69,11 +69,8 @@ final class Data 'Opened_files' => 'files', ]; - /** - * @var mixed[] - * @readonly - * */ - public array $status; + /** @var array */ + public readonly array $status; /** @var array */ public readonly array $sections; diff --git a/tests/unit/Controllers/Server/Status/QueriesControllerTest.php b/tests/unit/Controllers/Server/Status/QueriesControllerTest.php index 96251ba4ed..2a6962fd28 100644 --- a/tests/unit/Controllers/Server/Status/QueriesControllerTest.php +++ b/tests/unit/Controllers/Server/Status/QueriesControllerTest.php @@ -46,8 +46,18 @@ class QueriesControllerTest extends AbstractTestCase $config->selectedServer['DisableIS'] = false; $config->selectedServer['host'] = 'localhost'; - $this->data = new Data($this->dbi, $config); - $this->data->status['Uptime'] = 36000; + $dummyDbi = $this->createDbiDummy(); + $dbi = $this->createDatabaseInterface($dummyDbi); + $dummyDbi->addResult('SHOW GLOBAL STATUS', [ + ['Uptime' , '36000'], + ['Aborted_connects' , '0'], + ['Aborted_clients', '0'], + ['Com_delete_multi', '0'], + ['Com_create_function', '0'], + ['Com_empty_query', '0'], + ], ['Variable_name', 'Value']); + + $this->data = new Data($dbi, $config); $this->data->usedQueries = [ 'Com_change_db' => '15', 'Com_select' => '12', diff --git a/tests/unit/Controllers/Server/Status/StatusControllerTest.php b/tests/unit/Controllers/Server/Status/StatusControllerTest.php index f05e9b7eca..79ba64ce54 100644 --- a/tests/unit/Controllers/Server/Status/StatusControllerTest.php +++ b/tests/unit/Controllers/Server/Status/StatusControllerTest.php @@ -44,19 +44,28 @@ class StatusControllerTest extends AbstractTestCase public function testIndex(): void { - $data = new Data(DatabaseInterface::getInstance(), Config::getInstance()); - $bytesReceived = 100; $bytesSent = 200; $maxUsedConnections = 500; $abortedConnections = 200; $connections = 1000; - $data->status['Uptime'] = 36000; - $data->status['Bytes_received'] = $bytesReceived; - $data->status['Bytes_sent'] = $bytesSent; - $data->status['Max_used_connections'] = $maxUsedConnections; - $data->status['Aborted_connects'] = $abortedConnections; - $data->status['Connections'] = $connections; + + $dummyDbi = $this->createDbiDummy(); + $dbi = $this->createDatabaseInterface($dummyDbi); + $dummyDbi->addResult('SHOW GLOBAL STATUS', [ + ['Uptime' , '36000'], + ['Bytes_received' , $bytesReceived], + ['Bytes_sent' , $bytesSent], + ['Max_used_connections' ,$maxUsedConnections], + ['Aborted_connects' , $abortedConnections], + ['Connections' , $connections], + ['Aborted_clients', '0'], + ['Com_delete_multi', '0'], + ['Com_create_function', '0'], + ['Com_empty_query', '0'], + ], ['Variable_name', 'Value']); + + $data = new Data($dbi, Config::getInstance()); $response = new ResponseRenderer(); $template = new Template(); From 755ec7c4507ac2ec5fd911ee6e0697f5ff40d418 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 17 Dec 2024 00:36:59 +0000 Subject: [PATCH 2/7] Narrow down the type Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 20 ++++++++++++++---- psalm-baseline.xml | 42 +++++++++++++++----------------------- src/Server/Status/Data.php | 12 ++++++----- 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4073ca8689..d19d8effea 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -3925,7 +3925,7 @@ parameters: path: src/Controllers/Server/Status/VariablesController.php - - message: '#^Binary operation "\*" between 0\.95 and mixed results in an error\.$#' + message: '#^Binary operation "\*" between 0\.95 and string results in an error\.$#' identifier: binaryOp.invalid count: 1 path: src/Controllers/Server/Status/VariablesController.php @@ -16225,13 +16225,19 @@ parameters: path: src/Server/Status/Data.php - - message: '#^Binary operation "\*" between mixed and 1024 results in an error\.$#' + message: '#^Binary operation "\*" between mixed and 100 results in an error\.$#' identifier: binaryOp.invalid count: 1 path: src/Server/Status/Data.php - - message: '#^Binary operation "\*" between mixed and mixed results in an error\.$#' + message: '#^Binary operation "\*" between string and 1024 results in an error\.$#' + identifier: binaryOp.invalid + count: 1 + path: src/Server/Status/Data.php + + - + message: '#^Binary operation "\*" between string and string results in an error\.$#' identifier: binaryOp.invalid count: 1 path: src/Server/Status/Data.php @@ -16239,7 +16245,7 @@ parameters: - message: '#^Binary operation "/" between \(float\|int\) and mixed results in an error\.$#' identifier: binaryOp.invalid - count: 4 + count: 2 path: src/Server/Status/Data.php - @@ -16248,6 +16254,12 @@ parameters: count: 1 path: src/Server/Status/Data.php + - + message: '#^Binary operation "/" between mixed and string results in an error\.$#' + identifier: binaryOp.invalid + count: 2 + path: src/Server/Status/Data.php + - message: '#^Cannot access offset ''doc'' on mixed\.$#' identifier: offsetAccess.nonOffsetAccessible diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 71be13cf82..d78b23e2cc 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2407,6 +2407,9 @@ + + data->variables['thread_cache_size']]]> + @@ -2425,7 +2428,6 @@ data->status['Key_read_requests']]]> data->status['Key_write_requests']]]> data->status['Qcache_total_blocks']]]> - data->variables['thread_cache_size']]]> @@ -9495,43 +9497,33 @@ + + + + + + - - - - - - - - - - - - - - - - - - - - + + + + + + + + diff --git a/src/Server/Status/Data.php b/src/Server/Status/Data.php index 4b657d9ada..49e907a213 100644 --- a/src/Server/Status/Data.php +++ b/src/Server/Status/Data.php @@ -75,7 +75,7 @@ final class Data /** @var array */ public readonly array $sections; - /** @var mixed[] */ + /** @var array */ public readonly array $variables; /** @@ -200,8 +200,8 @@ final class Data /** * Calculate some values * - * @param mixed[] $serverStatus contains results of SHOW GLOBAL STATUS - * @param mixed[] $serverVariables contains results of SHOW GLOBAL VARIABLES + * @param array $serverStatus contains results of SHOW GLOBAL STATUS + * @param array $serverVariables contains results of SHOW GLOBAL VARIABLES * * @return mixed[] */ @@ -311,11 +311,13 @@ final class Data $this->dataLoaded = false; } else { $this->dataLoaded = true; + /** @var array $serverStatus */ $serverStatus = $serverStatusResult->fetchAllKeyPair(); unset($serverStatusResult); } // for some calculations we require also some server settings + /** @var array $serverVariables */ $serverVariables = $this->dbi->fetchResult('SHOW GLOBAL VARIABLES', 0, 1); $serverStatus = self::cleanDeprecated($serverStatus); @@ -351,9 +353,9 @@ final class Data /** * cleanup of some deprecated values * - * @param (string|null)[] $serverStatus status array to process + * @param array $serverStatus status array to process * - * @return (string|null)[] + * @return array */ public static function cleanDeprecated(array $serverStatus): array { From 2e328e6f9f831b47bed664b338a98706b24eaf9d Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 17 Dec 2024 01:28:53 +0000 Subject: [PATCH 3/7] Narrow down the type Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 118 ++++++++++++++++--------------------- psalm-baseline.xml | 79 ++++--------------------- src/Server/Status/Data.php | 10 ++-- 3 files changed, 68 insertions(+), 139 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d19d8effea..ab94d77f11 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -3787,123 +3787,99 @@ parameters: path: src/Controllers/Server/Status/Processes/KillController.php - - message: '#^Binary operation "\*" between mixed and \(float\|int\) results in an error\.$#' + message: '#^Binary operation "\*" between float\|int and mixed results in an error\.$#' identifier: binaryOp.invalid count: 1 path: src/Controllers/Server/Status/QueriesController.php - - message: '#^Binary operation "\*" between mixed and 100 results in an error\.$#' + message: '#^Binary operation "\*" between float\|int\|string and 100 results in an error\.$#' identifier: binaryOp.invalid count: 1 path: src/Controllers/Server/Status/QueriesController.php - - message: '#^Binary operation "\+\=" between float\|int and mixed results in an error\.$#' + message: '#^Binary operation "\*" between float\|int\|string and mixed results in an error\.$#' + identifier: binaryOp.invalid + count: 1 + path: src/Controllers/Server/Status/QueriesController.php + + - + message: '#^Binary operation "\+\=" between mixed and float\|int\|string results in an error\.$#' identifier: assignOp.invalid count: 1 path: src/Controllers/Server/Status/QueriesController.php - - message: '#^Binary operation "/" between \(float\|int\) and mixed results in an error\.$#' + message: '#^Binary operation "/" between 3600 and float\|int\|string results in an error\.$#' + identifier: binaryOp.invalid + count: 1 + path: src/Controllers/Server/Status/QueriesController.php + + - + message: '#^Binary operation "/" between float\|int and float\|int\|string results in an error\.$#' identifier: binaryOp.invalid count: 2 path: src/Controllers/Server/Status/QueriesController.php - - message: '#^Binary operation "/" between 3600 and mixed results in an error\.$#' + message: '#^Binary operation "/" between mixed and float\|int results in an error\.$#' identifier: binaryOp.invalid count: 1 path: src/Controllers/Server/Status/QueriesController.php - - message: '#^Cannot cast mixed to int\.$#' - identifier: cast.int - count: 1 - path: src/Controllers/Server/Status/QueriesController.php - - - - message: '#^Parameter \#3 \$subject of function str_replace expects array\\|string, \(int\|string\) given\.$#' - identifier: argument.type - count: 1 - path: src/Controllers/Server/Status/QueriesController.php - - - - message: '#^Binary operation "\*" between \(array\|float\|int\) and \(float\|int\) results in an error\.$#' + message: '#^Binary operation "\*" between float\|int\|string and 100 results in an error\.$#' identifier: binaryOp.invalid - count: 1 + count: 2 path: src/Controllers/Server/Status/StatusController.php - - message: '#^Binary operation "\*" between mixed and \(float\|int\) results in an error\.$#' + message: '#^Binary operation "\*" between float\|int\|string and mixed results in an error\.$#' identifier: binaryOp.invalid count: 5 path: src/Controllers/Server/Status/StatusController.php - - message: '#^Binary operation "\*" between mixed and 100 results in an error\.$#' + message: '#^Binary operation "\*" between mixed and mixed results in an error\.$#' identifier: binaryOp.invalid - count: 2 + count: 1 path: src/Controllers/Server/Status/StatusController.php - - message: '#^Binary operation "\+" between mixed and mixed results in an error\.$#' + message: '#^Binary operation "\+" between float\|int\|string and float\|int\|string results in an error\.$#' identifier: binaryOp.invalid count: 3 path: src/Controllers/Server/Status/StatusController.php - - message: '#^Binary operation "\." between ''SELECT UNIX…'' and mixed results in an error\.$#' - identifier: binaryOp.invalid - count: 1 - path: src/Controllers/Server/Status/StatusController.php - - - - message: '#^Binary operation "/" between \(float\|int\) and mixed results in an error\.$#' + message: '#^Binary operation "/" between 3600 and float\|int\|string results in an error\.$#' identifier: binaryOp.invalid count: 2 path: src/Controllers/Server/Status/StatusController.php - - message: '#^Binary operation "/" between 3600 and mixed results in an error\.$#' + message: '#^Binary operation "/" between mixed and float\|int\<1, max\>\|string results in an error\.$#' identifier: binaryOp.invalid count: 2 path: src/Controllers/Server/Status/StatusController.php - - - message: '#^Cannot cast mixed to int\.$#' - identifier: cast.int - count: 1 - path: src/Controllers/Server/Status/StatusController.php - - message: '#^Only booleans are allowed in an if condition, mixed given\.$#' identifier: if.condNotBoolean count: 2 path: src/Controllers/Server/Status/StatusController.php - - - message: '#^Only numeric types are allowed in \*, \(array\|float\|int\) given on the left side\.$#' - identifier: mul.leftNonNumeric - count: 1 - path: src/Controllers/Server/Status/StatusController.php - - - - message: '#^Parameter \#1 \$value of static method PhpMyAdmin\\Util\:\:formatByteDown\(\) expects float\|int\|string\|null, \(array\|float\|int\) given\.$#' - identifier: argument.type - count: 2 - path: src/Controllers/Server/Status/StatusController.php - - message: '#^Parameter \#1 \$value of static method PhpMyAdmin\\Util\:\:formatByteDown\(\) expects float\|int\|string\|null, mixed given\.$#' identifier: argument.type - count: 2 + count: 4 path: src/Controllers/Server/Status/StatusController.php - message: '#^Parameter \#1 \$value of static method PhpMyAdmin\\Util\:\:formatNumber\(\) expects float\|int\|string, mixed given\.$#' identifier: argument.type - count: 4 + count: 5 path: src/Controllers/Server/Status/StatusController.php - @@ -3913,13 +3889,13 @@ parameters: path: src/Controllers/Server/Status/VariablesController.php - - message: '#^Binary operation "\*" between 0\.01 and mixed results in an error\.$#' + message: '#^Binary operation "\*" between 0\.01 and float\|int\|string results in an error\.$#' identifier: binaryOp.invalid count: 1 path: src/Controllers/Server/Status/VariablesController.php - - message: '#^Binary operation "\*" between 0\.9 and mixed results in an error\.$#' + message: '#^Binary operation "\*" between 0\.9 and float\|int\|string results in an error\.$#' identifier: binaryOp.invalid count: 1 path: src/Controllers/Server/Status/VariablesController.php @@ -3931,7 +3907,7 @@ parameters: path: src/Controllers/Server/Status/VariablesController.php - - message: '#^Binary operation "/" between mixed and 5 results in an error\.$#' + message: '#^Binary operation "/" between float\|int\|string and 5 results in an error\.$#' identifier: binaryOp.invalid count: 1 path: src/Controllers/Server/Status/VariablesController.php @@ -16278,6 +16254,12 @@ parameters: count: 2 path: src/Server/Status/Data.php + - + message: '#^Method PhpMyAdmin\\Server\\Status\\Data\:\:calculateValues\(\) should return array\ but returns array\\.$#' + identifier: return.type + count: 1 + path: src/Server/Status/Data.php + - message: '#^Only booleans are allowed in an if condition, mixed given\.$#' identifier: if.condNotBoolean @@ -16290,18 +16272,6 @@ parameters: count: 1 path: src/Server/Status/Data.php - - - message: '#^Parameter \#1 \$haystack of function str_contains expects string, \(int\|string\) given\.$#' - identifier: argument.type - count: 1 - path: src/Server/Status/Data.php - - - - message: '#^Property PhpMyAdmin\\Server\\Status\\Data\:\:\$status \(array\\) does not accept array\\.$#' - identifier: assign.propertyType - count: 1 - path: src/Server/Status/Data.php - - message: '#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\.$#' identifier: foreach.nonIterable @@ -20503,13 +20473,19 @@ parameters: path: tests/unit/Controllers/Server/Status/QueriesControllerTest.php - - message: '#^Binary operation "/" between \(float\|int\) and mixed results in an error\.$#' + message: '#^Binary operation "\*" between float\|int and mixed results in an error\.$#' identifier: binaryOp.invalid count: 1 path: tests/unit/Controllers/Server/Status/QueriesControllerTest.php - - message: '#^Binary operation "/" between 3600 and mixed results in an error\.$#' + message: '#^Binary operation "/" between 3600 and float\|int\|string results in an error\.$#' + identifier: binaryOp.invalid + count: 1 + path: tests/unit/Controllers/Server/Status/QueriesControllerTest.php + + - + message: '#^Binary operation "/" between float\|int and float\|int\|string results in an error\.$#' identifier: binaryOp.invalid count: 1 path: tests/unit/Controllers/Server/Status/QueriesControllerTest.php @@ -20532,6 +20508,12 @@ parameters: count: 1 path: tests/unit/Controllers/Server/Status/QueriesControllerTest.php + - + message: '#^Parameter \#1 \$value of static method PhpMyAdmin\\Util\:\:formatNumber\(\) expects float\|int\|string, mixed given\.$#' + identifier: argument.type + count: 1 + path: tests/unit/Controllers/Server/Status/QueriesControllerTest.php + - message: ''' #^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Config\: diff --git a/psalm-baseline.xml b/psalm-baseline.xml index d78b23e2cc..9d67b2ef96 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2336,58 +2336,20 @@ - - - - - - - - - - - + data->status['Uptime']]]> data->status['Uptime']]]> data->status['Uptime']]]> - - - - - - - + - - data->status['Aborted_clients']]]> - data->status['Aborted_clients'] * $hourFactor]]> - data->status['Aborted_clients'] * 100 / $this->data->status['Connections']]]> - data->status['Aborted_connects']]]> - data->status['Aborted_connects'] * $hourFactor]]> - data->status['Aborted_connects'] * 100 / $this->data->status['Connections']]]> - data->status['Bytes_received']]]> - data->status['Bytes_received'] * $hourFactor]]> - data->status['Bytes_received'] + $this->data->status['Bytes_sent']]]> - data->status['Bytes_received'] + $this->data->status['Bytes_sent']]]> - data->status['Bytes_sent']]]> - data->status['Bytes_sent'] * $hourFactor]]> - data->status['Connections']]]> - data->status['Connections'] * $hourFactor]]> - data->status['Max_used_connections']]]> - data->status['Bytes_received'] + $this->data->status['Bytes_sent']) * $hourFactor]]> - - - - - - + data->status['Aborted_clients']]]> data->status['Aborted_clients']]]> data->status['Aborted_connects']]]> @@ -2397,11 +2359,15 @@ data->status['Bytes_received']]]> data->status['Bytes_received']]]> data->status['Bytes_sent']]]> + data->status['Bytes_sent']]]> + data->status['Bytes_sent']]]> + data->status['Bytes_sent']]]> + data->status['Connections']]]> + data->status['Connections']]]> data->status['Connections']]]> data->status['Uptime']]]> data->status['Uptime']]]> - data->status['Uptime']]]> - + @@ -2422,13 +2388,12 @@ - - + data->status['Key_read_requests']]]> data->status['Key_write_requests']]]> data->status['Qcache_total_blocks']]]> - + @@ -9503,16 +9468,6 @@ - - - - - - - - - - @@ -11972,18 +11927,10 @@ data->usedQueries]]> - - - data->status['Uptime']]]> - - - - - - + data->status['Uptime']]]> data->status['Uptime']]]> - + diff --git a/src/Server/Status/Data.php b/src/Server/Status/Data.php index 49e907a213..65eff063ec 100644 --- a/src/Server/Status/Data.php +++ b/src/Server/Status/Data.php @@ -69,7 +69,7 @@ final class Data 'Opened_files' => 'files', ]; - /** @var array */ + /** @var array */ public readonly array $status; /** @var array */ @@ -79,7 +79,7 @@ final class Data public readonly array $variables; /** - * @var mixed[] + * @var array * @readonly */ public array $usedQueries; @@ -203,7 +203,7 @@ final class Data * @param array $serverStatus contains results of SHOW GLOBAL STATUS * @param array $serverVariables contains results of SHOW GLOBAL VARIABLES * - * @return mixed[] + * @return array */ private function calculateValues(array $serverStatus, array $serverVariables): array { @@ -263,9 +263,9 @@ final class Data /** * Sort variables into arrays * - * @param mixed[] $serverStatus contains results of SHOW GLOBAL STATUS + * @param array $serverStatus contains results of SHOW GLOBAL STATUS * - * @return array{string[], true[], mixed[]} + * @return array{string[], true[], array} */ private function sortVariables(array $serverStatus): array { From df36e752f8aeef45b86578bf869f4b8020d4f26f Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 17 Dec 2024 01:45:07 +0000 Subject: [PATCH 4/7] Narrow down the type Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 6 ------ psalm-baseline.xml | 3 --- src/Server/Status/Data.php | 8 +++----- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ab94d77f11..873088f8f9 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -3882,12 +3882,6 @@ parameters: count: 5 path: src/Controllers/Server/Status/StatusController.php - - - message: '#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\.$#' - identifier: foreach.nonIterable - count: 1 - path: src/Controllers/Server/Status/VariablesController.php - - message: '#^Binary operation "\*" between 0\.01 and float\|int\|string results in an error\.$#' identifier: binaryOp.invalid diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 9d67b2ef96..fff26680d5 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2385,9 +2385,6 @@ - - - data->status['Key_read_requests']]]> diff --git a/src/Server/Status/Data.php b/src/Server/Status/Data.php index 65eff063ec..6ccbaae9af 100644 --- a/src/Server/Status/Data.php +++ b/src/Server/Status/Data.php @@ -87,7 +87,7 @@ final class Data /** @var string[] */ public readonly array $allocationMap; - /** @var mixed[] */ + /** @var array> */ public readonly array $links; public readonly bool $dbIsLocal; @@ -138,7 +138,7 @@ final class Data /** * Gets the links for constructor * - * @return mixed[] + * @return array> */ private function getLinks(): array { @@ -324,8 +324,6 @@ final class Data $serverStatus = $this->calculateValues($serverStatus, $serverVariables); - $links = $this->getLinks(); - [ $allocationMap, $sectionUsed, @@ -346,7 +344,7 @@ final class Data $this->variables = $serverVariables; $this->usedQueries = $usedQueries; $this->allocationMap = $allocationMap; - $this->links = $links; + $this->links = $this->getLinks(); $this->sectionUsed = $sectionUsed; } From 54d42dc0c43a07d1b11a8bef07d7e1ec2ea87c86 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 17 Dec 2024 02:01:37 +0000 Subject: [PATCH 5/7] Make $usedQueries readonly Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 6 ------ psalm-baseline.xml | 3 --- src/Server/Status/Data.php | 7 ++----- .../Server/Status/QueriesControllerTest.php | 14 ++++++++++++-- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 873088f8f9..d14bd96f1e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -20460,12 +20460,6 @@ parameters: count: 1 path: tests/unit/Controllers/Server/Status/ProcessesControllerTest.php - - - message: '#^@readonly property PhpMyAdmin\\Server\\Status\\Data\:\:\$usedQueries is assigned outside of its declaring class\.$#' - identifier: property.readOnlyByPhpDocAssignOutOfClass - count: 1 - path: tests/unit/Controllers/Server/Status/QueriesControllerTest.php - - message: '#^Binary operation "\*" between float\|int and mixed results in an error\.$#' identifier: binaryOp.invalid diff --git a/psalm-baseline.xml b/psalm-baseline.xml index fff26680d5..c8ece131af 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -11921,9 +11921,6 @@ - - data->usedQueries]]> - data->status['Uptime']]]> data->status['Uptime']]]> diff --git a/src/Server/Status/Data.php b/src/Server/Status/Data.php index 6ccbaae9af..c6f43a9fe7 100644 --- a/src/Server/Status/Data.php +++ b/src/Server/Status/Data.php @@ -78,11 +78,8 @@ final class Data /** @var array */ public readonly array $variables; - /** - * @var array - * @readonly - */ - public array $usedQueries; + /** @var array */ + public readonly array $usedQueries; /** @var string[] */ public readonly array $allocationMap; diff --git a/tests/unit/Controllers/Server/Status/QueriesControllerTest.php b/tests/unit/Controllers/Server/Status/QueriesControllerTest.php index 2a6962fd28..1aceeb2375 100644 --- a/tests/unit/Controllers/Server/Status/QueriesControllerTest.php +++ b/tests/unit/Controllers/Server/Status/QueriesControllerTest.php @@ -55,17 +55,27 @@ class QueriesControllerTest extends AbstractTestCase ['Com_delete_multi', '0'], ['Com_create_function', '0'], ['Com_empty_query', '0'], + ['Com_change_db' , '15'], + ['Com_select' , '12'], + ['Com_set_option' , '54'], + ['Com_show_databases' , '16'], + ['Com_show_status' , '14'], + ['Com_show_tables' , '13'], ], ['Variable_name', 'Value']); $this->data = new Data($dbi, $config); - $this->data->usedQueries = [ + } + + public function testUsedQueries(): void + { + self::assertSame([ 'Com_change_db' => '15', 'Com_select' => '12', 'Com_set_option' => '54', 'Com_show_databases' => '16', 'Com_show_status' => '14', 'Com_show_tables' => '13', - ]; + ], $this->data->usedQueries); } public function testIndex(): void From e8b44fb629983cbb616a0a0562009f168e4223cc Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 17 Dec 2024 02:07:22 +0000 Subject: [PATCH 6/7] Make $dataLoaded readonly Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 8 +------- psalm-baseline.xml | 5 +---- src/Server/Status/Data.php | 3 +-- .../Controllers/Server/Status/AdvisorControllerTest.php | 9 +++++---- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d14bd96f1e..9cedd6f501 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -20265,19 +20265,13 @@ parameters: count: 1 path: tests/unit/Controllers/Server/ShowEngineControllerTest.php - - - message: '#^@readonly property PhpMyAdmin\\Server\\Status\\Data\:\:\$dataLoaded is assigned outside of its declaring class\.$#' - identifier: property.readOnlyByPhpDocAssignOutOfClass - count: 2 - path: tests/unit/Controllers/Server/Status/AdvisorControllerTest.php - - message: ''' #^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Config\: Use dependency injection instead\.$# ''' identifier: staticMethod.deprecated - count: 1 + count: 2 path: tests/unit/Controllers/Server/Status/AdvisorControllerTest.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index c8ece131af..dce7ebe83b 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -11849,14 +11849,11 @@ + - - data->dataLoaded]]> - data->dataLoaded]]> - diff --git a/src/Server/Status/Data.php b/src/Server/Status/Data.php index c6f43a9fe7..eb66946d48 100644 --- a/src/Server/Status/Data.php +++ b/src/Server/Status/Data.php @@ -92,8 +92,7 @@ final class Data /** @var true[] */ public readonly array $sectionUsed; - /** @readonly */ - public bool $dataLoaded; + public readonly bool $dataLoaded; private readonly ReplicationInfo $replicationInfo; diff --git a/tests/unit/Controllers/Server/Status/AdvisorControllerTest.php b/tests/unit/Controllers/Server/Status/AdvisorControllerTest.php index 5257ea291f..d56bcb6c01 100644 --- a/tests/unit/Controllers/Server/Status/AdvisorControllerTest.php +++ b/tests/unit/Controllers/Server/Status/AdvisorControllerTest.php @@ -44,12 +44,15 @@ class AdvisorControllerTest extends AbstractTestCase public function testIndexWithoutData(): void { - $this->data->dataLoaded = false; + $dummyDbi = $this->createDbiDummy(); + $dbi = $this->createDatabaseInterface($dummyDbi); + $dummyDbi->addResult('SHOW GLOBAL STATUS', false); + $data = new Data($dbi, Config::getInstance()); $controller = new AdvisorController( $this->response, $this->template, - $this->data, + $data, new Advisor(DatabaseInterface::getInstance(), new ExpressionLanguage()), ); @@ -91,8 +94,6 @@ class AdvisorControllerTest extends AbstractTestCase $advisor = self::createMock(Advisor::class); $advisor->method('run')->willReturn($advisorData); - $this->data->dataLoaded = true; - $controller = new AdvisorController($this->response, $this->template, $this->data, $advisor); $controller(self::createStub(ServerRequest::class)); From 574bc914ed139be89c8efe151731bb66fa8d2a80 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 17 Dec 2024 02:10:36 +0000 Subject: [PATCH 7/7] Remove temp variable Signed-off-by: Kamil Tekiela --- src/Server/Status/Data.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Server/Status/Data.php b/src/Server/Status/Data.php index eb66946d48..0ce8757e7c 100644 --- a/src/Server/Status/Data.php +++ b/src/Server/Status/Data.php @@ -330,8 +330,7 @@ final class Data // which is excluded from $server_status['Questions']) unset($usedQueries['Com_admin_commands']); - $serverHostToLower = mb_strtolower($config->selectedServer['host']); - $this->dbIsLocal = $serverHostToLower === 'localhost' + $this->dbIsLocal = mb_strtolower($config->selectedServer['host']) === 'localhost' || $config->selectedServer['host'] === '127.0.0.1' || $config->selectedServer['host'] === '::1';