Skip to content

Commit

Permalink
Improve Util::formatByteDown() return type hint
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Jun 29, 2023
1 parent 1f935e5 commit 3abe8ed
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 117 deletions.
2 changes: 1 addition & 1 deletion libraries/classes/Advisory/Advisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static function (): void {
int $value,
int $limes = 6,
int $comma = 0,
) => implode(' ', (array) Util::formatByteDown($value, $limes, $comma))
) => implode(' ', Util::formatByteDown($value, $limes, $comma))
);
$this->expression->register(
'fired',
Expand Down
4 changes: 2 additions & 2 deletions libraries/classes/Controllers/Table/StructureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ protected function getTableStats(bool $isSystemSchema): string
'table_collation' => $tableCollation,
'is_innodb' => $isInnoDB,
'mergetable' => $mergetable,
'avg_size' => $avgSize ?? null,
'avg_unit' => $avgUnit ?? null,
'avg_size' => $avgSize,
'avg_unit' => $avgUnit,
'data_size' => $dataSize,
'data_unit' => $dataUnit,
'index_size' => $indexSize ?? null,
Expand Down
4 changes: 1 addition & 3 deletions libraries/classes/Display/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -3950,9 +3950,7 @@ private function handleNonPrintableContents(
if ($content !== null) {
$size = strlen($content);
$displaySize = Util::formatByteDown($size, 3, 1);
if ($displaySize !== null) {
$result .= ' - ' . $displaySize[0] . ' ' . $displaySize[1];
}
$result .= ' - ' . $displaySize[0] . ' ' . $displaySize[1];
} else {
$result .= ' - NULL';
$size = 0;
Expand Down
5 changes: 3 additions & 2 deletions libraries/classes/Engines/Pbxt.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ public function getVariables(): array
*
* @param int|string $formattedSize the size expression (for example 8MB)
*
* @return mixed[]|null the formatted value and its unit
* @return string[] the formatted value and its unit
* @psalm-return array{string, string}
*/
public function resolveTypeSize(int|string $formattedSize): array|null
public function resolveTypeSize(int|string $formattedSize): array
{
if (is_string($formattedSize) && preg_match('/^[0-9]+[a-zA-Z]+$/', $formattedSize)) {
$value = Util::extractValueFromFormattedSize($formattedSize);
Expand Down
5 changes: 1 addition & 4 deletions libraries/classes/InsertEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -1845,10 +1845,7 @@ private function getHtmlForInsertEditFormColumn(
|| ($GLOBALS['cfg']['ProtectBinary'] === 'all')
|| ($GLOBALS['cfg']['ProtectBinary'] === 'noblob' && ! $column->isBlob);
if ($isColumnProtectedBlob) {
$blobSize = Util::formatByteDown(mb_strlen(stripslashes($data)), 3, 1);
if ($blobSize !== null) {
[$blobValue, $blobValueUnit] = $blobSize;
}
[$blobValue, $blobValueUnit] = Util::formatByteDown(mb_strlen(stripslashes($data)), 3, 1);
}

if ($isUpload && $column->isBlob) {
Expand Down
4 changes: 2 additions & 2 deletions libraries/classes/Server/Status/Monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public function getJsonForLogDataTypeSlow(int $start, int $end): array|null
if (mb_strlen($row['sql_text']) > 220) {
$implodeSqlText = implode(
' ',
(array) Util::formatByteDown(
Util::formatByteDown(
mb_strlen($row['sql_text']),
2,
2,
Expand Down Expand Up @@ -408,7 +408,7 @@ public function getJsonForLogDataTypeGeneral(
. '... ['
. implode(
' ',
(array) Util::formatByteDown(
Util::formatByteDown(
mb_strlen($row['argument']),
2,
2,
Expand Down
9 changes: 4 additions & 5 deletions libraries/classes/StorageEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,7 @@ public function getHtmlVariables(): string
switch ($details['type']) {
case self::DETAILS_TYPE_SIZE:
$parsedSize = $this->resolveTypeSize($details['value']);
if ($parsedSize !== null) {
$ret .= $parsedSize[0] . '&nbsp;' . $parsedSize[1];
}
$ret .= $parsedSize[0] . '&nbsp;' . $parsedSize[1];

break;
case self::DETAILS_TYPE_NUMERIC:
Expand Down Expand Up @@ -338,9 +336,10 @@ public function getHtmlVariables(): string
*
* @param int|string $value Value to format
*
* @return mixed[]|null the formatted value and its unit
* @return string[] the formatted value and its unit
* @psalm-return array{string, string}
*/
public function resolveTypeSize(int|string $value): array|null
public function resolveTypeSize(int|string $value): array
{
return Util::formatByteDown($value);
}
Expand Down
3 changes: 2 additions & 1 deletion libraries/classes/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ public static function backquoteCompat(
* @param int $limes the sensitiveness
* @param int $comma the number of decimals to retain
*
* @return mixed[]|null the formatted value and its unit
* @return string[]|null the formatted value and its unit
* @psalm-return ($value is null ? null : array{string, string})
*/
public static function formatByteDown(float|int|string|null $value, int $limes = 6, int $comma = 0): array|null
{
Expand Down
29 changes: 2 additions & 27 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1235,11 +1235,6 @@ parameters:
count: 1
path: libraries/classes/Controllers/Database/StructureController.php

-
message: "#^Cannot use array destructuring on array\\|null\\.$#"
count: 4
path: libraries/classes/Controllers/Database/StructureController.php

-
message: "#^Parameter \\#1 \\$connection of method PhpMyAdmin\\\\Replication\\\\ReplicationInfo\\:\\:load\\(\\) expects string\\|null, mixed given\\.$#"
count: 1
Expand Down Expand Up @@ -2531,8 +2526,8 @@ parameters:
path: libraries/classes/Controllers/Table/StructureController.php

-
message: "#^Cannot use array destructuring on array\\|null\\.$#"
count: 7
message: "#^Cannot use array destructuring on array\\<int, string\\>\\|null\\.$#"
count: 1
path: libraries/classes/Controllers/Table/StructureController.php

-
Expand Down Expand Up @@ -8495,11 +8490,6 @@ parameters:
count: 2
path: libraries/classes/Util.php

-
message: "#^Cannot use array destructuring on array\\|null\\.$#"
count: 1
path: libraries/classes/Util.php

-
message: "#^For loop initial assignment overwrites variable \\$i\\.$#"
count: 1
Expand Down Expand Up @@ -8545,16 +8535,6 @@ parameters:
count: 1
path: libraries/classes/Util.php

-
message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
count: 1
path: libraries/classes/Util.php

-
message: "#^Parameter \\#3 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
count: 1
path: libraries/classes/Util.php

-
message: "#^Variable static method call on PhpMyAdmin\\\\Util\\.$#"
count: 1
Expand Down Expand Up @@ -9450,11 +9430,6 @@ parameters:
count: 1
path: test/classes/UtilTest.php

-
message: "#^Parameter \\#1 \\$string of function trim expects string, mixed given\\.$#"
count: 1
path: test/classes/UtilTest.php

-
message: "#^Parameter \\#1 \\$needle of method PHPUnit\\\\Framework\\\\Assert\\:\\:assertStringContainsString\\(\\) expects string, mixed given\\.$#"
count: 1
Expand Down
57 changes: 1 addition & 56 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
<code><![CDATA[$rule['recommendation']]]></code>
<code><![CDATA[$this->variables['version']]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code>(array) Util::formatByteDown($value, $limes, $comma)</code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess>
<code><![CDATA[$rule['id']]]></code>
</MixedArrayAccess>
Expand Down Expand Up @@ -1614,16 +1611,6 @@
<code><![CDATA[$currentTable['TABLE_NAME']]]></code>
<code><![CDATA[$currentTable['TABLE_ROWS']]]></code>
</MixedOperand>
<PossiblyNullArrayAccess>
<code>$formattedOverhead</code>
<code>$formattedSize</code>
<code>$formattedSize</code>
<code>$formattedSize</code>
<code>$overheadUnit</code>
<code>$unit</code>
<code>$unit</code>
<code>$unit</code>
</PossiblyNullArrayAccess>
</file>
<file src="libraries/classes/Controllers/Database/TrackingController.php">
<InvalidArrayOffset>
Expand Down Expand Up @@ -4068,22 +4055,6 @@
<PossiblyNullArgument>
<code><![CDATA[$GLOBALS['tbl_collation']]]></code>
</PossiblyNullArgument>
<PossiblyNullArrayAccess>
<code>$avgSize</code>
<code>$avgUnit</code>
<code>$dataSize</code>
<code>$dataUnit</code>
<code>$effectSize</code>
<code>$effectSize</code>
<code>$effectUnit</code>
<code>$effectUnit</code>
<code>$freeSize</code>
<code>$freeUnit</code>
<code>$indexSize</code>
<code>$indexUnit</code>
<code>$totSize</code>
<code>$totUnit</code>
</PossiblyNullArrayAccess>
<PossiblyNullOperand>
<code><![CDATA[$GLOBALS['showtable']['Data_length']]]></code>
<code><![CDATA[$GLOBALS['showtable']['Data_length']]]></code>
Expand Down Expand Up @@ -5812,8 +5783,6 @@
<code><![CDATA[$_SESSION['tmpval']['pos']]]></code>
<code><![CDATA[$_SESSION['tmpval']['repeat_cells']]]></code>
<code><![CDATA[$displayParams['rowdata'][$i][$rowNumber]]]></code>
<code>$displaySize[0]</code>
<code>$displaySize[1]</code>
<code>$file</code>
<code>$sortExpressionNoDirection[$indexInExpression]</code>
</MixedOperand>
Expand Down Expand Up @@ -11477,18 +11446,6 @@
<code>$statusVars</code>
<code>$temp[strlen($temp) - 1]</code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code><![CDATA[(array) Util::formatByteDown(
mb_strlen($row['argument']),
2,
2,
)]]></code>
<code><![CDATA[(array) Util::formatByteDown(
mb_strlen($row['sql_text']),
2,
2,
)]]></code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess>
<code><![CDATA[$cpuload['busy']]]></code>
<code><![CDATA[$cpuload['idle']]]></code>
Expand Down Expand Up @@ -11844,8 +11801,6 @@
<MixedOperand>
<code><![CDATA[$decodedData['disk_usage']]]></code>
<code><![CDATA[$decodedData['disk_usage']]]></code>
<code>$parsedSize[0]</code>
<code>$parsedSize[1]</code>
</MixedOperand>
<MixedReturnStatement>
<code>$storageEngines</code>
Expand Down Expand Up @@ -12752,8 +12707,6 @@
<code>$units[$d]</code>
</InvalidArrayOffset>
<MixedArgument>
<code>$maxSize</code>
<code>$maxUnit</code>
<code>$row[$i] ?? null</code>
</MixedArgument>
<MixedArgumentTypeCoercion>
Expand Down Expand Up @@ -12824,6 +12777,7 @@
<code><![CDATA[$row['Column_name']]]></code>
<code>$unit</code>
<code>$unit</code>
<code>$value</code>
</MixedOperand>
<MixedReturnStatement>
<code><![CDATA[$_SESSION['tmpval']['table_limit_offset']]]></code>
Expand All @@ -12839,14 +12793,6 @@
<code><![CDATA[$GLOBALS['cfg']['NavigationTreeTableSeparator']]]></code>
<code><![CDATA[$GLOBALS['cfg']['NavigationTreeTableSeparator']]]></code>
</PossiblyInvalidOperand>
<PossiblyNullArgument>
<code>$maxSize</code>
<code>$maxUnit</code>
</PossiblyNullArgument>
<PossiblyNullArrayAccess>
<code>$maxSize</code>
<code>$maxUnit</code>
</PossiblyNullArrayAccess>
<PossiblyNullArrayOffset>
<code>$sotCache</code>
<code>$tables</code>
Expand Down Expand Up @@ -14929,7 +14875,6 @@
<MixedArgument>
<code><![CDATA[$_SESSION['cache']['server_server']]]></code>
<code><![CDATA[$_SESSION['cache']['server_server']]]></code>
<code>$result[0]</code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$_SESSION['cache']['server_server']]]></code>
Expand Down
21 changes: 7 additions & 14 deletions test/classes/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use function str_repeat;
use function str_replace;
use function strlen;
use function trim;

use const LC_ALL;
use const MYSQLI_NUM_FLAG;
Expand Down Expand Up @@ -710,36 +709,30 @@ public static function providerExtractValueFromFormattedSize(): array
return [[100, -1], ['10GB', 10737418240], ['15MB', 15728640], ['256K', 262144]];
}

public function testFormatByteDownWithNullValue(): void
{
$this->assertNull(Util::formatByteDown(null));
}

/**
* format byte test, globals are defined
*
* @param float|int|string $a Value to format
* @param int $b Sensitiveness
* @param int $c Number of decimals to retain
* @param mixed[] $e Expected value
* @param float|int|string|null $a Value to format
* @param int $b Sensitiveness
* @param int $c Number of decimals to retain
* @param string[]|null $e Expected value
*/
#[DataProvider('providerFormatByteDown')]
public function testFormatByteDown(float|int|string $a, int $b, int $c, array $e): void
public function testFormatByteDown(float|int|string|null $a, int $b, int $c, array|null $e): void
{
$result = Util::formatByteDown($a, $b, $c);
$this->assertIsArray($result);
$result[0] = trim($result[0]);
$this->assertSame($e, $result);
}

/**
* format byte down data provider
*
* @return mixed[]
* @psalm-return list<array{float|int|string|null, int, int, string[]|null}>
*/
public static function providerFormatByteDown(): array
{
return [
[null, 0, 0, null],
['0', 6, 0, ['0', __('B')]],
['A4', 6, 0, ['0', __('B')]],
[10, 2, 2, ['10', __('B')]],
Expand Down

0 comments on commit 3abe8ed

Please sign in to comment.