Skip to content

Commit ac181e3

Browse files
Merge pull request #18931 from kamil-tekiela/Refactor-VersionInformation
Refactor VersionInformation
2 parents 641f0c0 + e6b72cc commit ac181e3

File tree

8 files changed

+93
-311
lines changed

8 files changed

+93
-311
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -6235,16 +6235,6 @@ parameters:
62356235
count: 1
62366236
path: src/Controllers/UserPasswordController.php
62376237

6238-
-
6239-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
6240-
count: 2
6241-
path: src/Controllers/VersionCheckController.php
6242-
6243-
-
6244-
message: "#^Parameter \\#1 \\$releases of method PhpMyAdmin\\\\VersionInformation\\:\\:getLatestCompatibleVersion\\(\\) expects array, mixed given\\.$#"
6245-
count: 1
6246-
path: src/Controllers/VersionCheckController.php
6247-
62486238
-
62496239
message: "#^Cannot access offset 'back' on mixed\\.$#"
62506240
count: 1
@@ -14575,31 +14565,6 @@ parameters:
1457514565
count: 4
1457614566
path: src/Setup/Index.php
1457714567

14578-
-
14579-
message: "#^Parameter \\#1 \\$releases of method PhpMyAdmin\\\\VersionInformation\\:\\:getLatestCompatibleVersion\\(\\) expects array, mixed given\\.$#"
14580-
count: 1
14581-
path: src/Setup/Index.php
14582-
14583-
-
14584-
message: "#^Parameter \\#1 \\$string of function htmlspecialchars expects string, mixed given\\.$#"
14585-
count: 2
14586-
path: src/Setup/Index.php
14587-
14588-
-
14589-
message: "#^Parameter \\#1 \\$version of method PhpMyAdmin\\\\VersionInformation\\:\\:versionToInt\\(\\) expects string, mixed given\\.$#"
14590-
count: 1
14591-
path: src/Setup/Index.php
14592-
14593-
-
14594-
message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
14595-
count: 1
14596-
path: src/Setup/Index.php
14597-
14598-
-
14599-
message: "#^Parameter \\#3 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
14600-
count: 1
14601-
path: src/Setup/Index.php
14602-
1460314568
-
1460414569
message: "#^Access to an undefined property PhpMyAdmin\\\\SqlParser\\\\Statement\\:\\:\\$expr\\.$#"
1460514570
count: 2
@@ -16100,21 +16065,6 @@ parameters:
1610016065
count: 8
1610116066
path: src/Utils/SessionCache.php
1610216067

16103-
-
16104-
message: "#^Access to an undefined property object\\:\\:\\$date\\.$#"
16105-
count: 1
16106-
path: src/VersionInformation.php
16107-
16108-
-
16109-
message: "#^Access to an undefined property object\\:\\:\\$releases\\.$#"
16110-
count: 1
16111-
path: src/VersionInformation.php
16112-
16113-
-
16114-
message: "#^Access to an undefined property object\\:\\:\\$version\\.$#"
16115-
count: 1
16116-
path: src/VersionInformation.php
16117-
1611816068
-
1611916069
message: "#^Cannot access offset 'response' on mixed\\.$#"
1612016070
count: 1
@@ -16130,41 +16080,11 @@ parameters:
1613016080
count: 2
1613116081
path: src/VersionInformation.php
1613216082

16133-
-
16134-
message: "#^Cannot access property \\$date on mixed\\.$#"
16135-
count: 1
16136-
path: src/VersionInformation.php
16137-
16138-
-
16139-
message: "#^Cannot access property \\$mysql_versions on mixed\\.$#"
16140-
count: 1
16141-
path: src/VersionInformation.php
16142-
16143-
-
16144-
message: "#^Cannot access property \\$php_versions on mixed\\.$#"
16145-
count: 1
16146-
path: src/VersionInformation.php
16147-
16148-
-
16149-
message: "#^Cannot access property \\$version on mixed\\.$#"
16150-
count: 2
16151-
path: src/VersionInformation.php
16152-
1615316083
-
1615416084
message: "#^Comparison operation \"\\>\\=\" between int\\<1, max\\> and 1 is always true\\.$#"
1615516085
count: 1
1615616086
path: src/VersionInformation.php
1615716087

16158-
-
16159-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
16160-
count: 3
16161-
path: src/VersionInformation.php
16162-
16163-
-
16164-
message: "#^Method PhpMyAdmin\\\\VersionInformation\\:\\:getLatestVersion\\(\\) should return stdClass\\|null but returns object\\.$#"
16165-
count: 1
16166-
path: src/VersionInformation.php
16167-
1616816088
-
1616916089
message: "#^Only booleans are allowed in an if condition, int\\|false given\\.$#"
1617016090
count: 1
@@ -16175,21 +16095,6 @@ parameters:
1617516095
count: 1
1617616096
path: src/VersionInformation.php
1617716097

16178-
-
16179-
message: "#^Parameter \\#1 \\$version1 of function version_compare expects string, mixed given\\.$#"
16180-
count: 1
16181-
path: src/VersionInformation.php
16182-
16183-
-
16184-
message: "#^Parameter \\#2 \\$string of function explode expects string, mixed given\\.$#"
16185-
count: 2
16186-
path: src/VersionInformation.php
16187-
16188-
-
16189-
message: "#^Parameter \\#2 \\$version2 of function version_compare expects string, mixed given\\.$#"
16190-
count: 1
16191-
path: src/VersionInformation.php
16192-
1619316098
-
1619416099
message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#"
1619516100
count: 1

psalm-baseline.xml

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4248,13 +4248,6 @@
42484248
</PossiblyUnusedMethod>
42494249
</file>
42504250
<file src="src/Controllers/VersionCheckController.php">
4251-
<MixedArgument>
4252-
<code><![CDATA[$versionDetails->releases]]></code>
4253-
</MixedArgument>
4254-
<MixedAssignment>
4255-
<code>$date</code>
4256-
<code>$version</code>
4257-
</MixedAssignment>
42584251
<PossiblyUnusedParam>
42594252
<code>$request</code>
42604253
</PossiblyUnusedParam>
@@ -11021,12 +11014,6 @@
1102111014
</PossiblyNullOperand>
1102211015
</file>
1102311016
<file src="src/Setup/Index.php">
11024-
<MixedArgument>
11025-
<code>$date</code>
11026-
<code>$date</code>
11027-
<code>$version</code>
11028-
<code><![CDATA[$versionData->releases]]></code>
11029-
</MixedArgument>
1103011017
<MixedArrayAccess>
1103111018
<code>$messages[$id]</code>
1103211019
<code><![CDATA[$msg['active']]]></code>
@@ -11044,7 +11031,6 @@
1104411031
<code>$messages[$id]</code>
1104511032
</MixedArrayOffset>
1104611033
<MixedAssignment>
11047-
<code>$date</code>
1104811034
<code>$id</code>
1104911035
<code>$id</code>
1105011036
<code>$id</code>
@@ -11056,7 +11042,6 @@
1105611042
<code>$msg</code>
1105711043
<code>$removeIds[]</code>
1105811044
<code>$type</code>
11059-
<code>$version</code>
1106011045
</MixedAssignment>
1106111046
</file>
1106211047
<file src="src/Sql.php">
@@ -12223,14 +12208,7 @@
1222312208
<code>Config::getInstance()</code>
1222412209
<code>DatabaseInterface::getInstance()</code>
1222512210
</DeprecatedMethod>
12226-
<LessSpecificReturnStatement>
12227-
<code>$data</code>
12228-
</LessSpecificReturnStatement>
1222912211
<MixedArgument>
12230-
<code><![CDATA[$latestRelease['version']]]></code>
12231-
<code>$mysqlVersions</code>
12232-
<code>$phpVersions</code>
12233-
<code><![CDATA[$release->version]]></code>
1223412212
<code>$response</code>
1223512213
</MixedArgument>
1223612214
<MixedArrayAccess>
@@ -12241,25 +12219,12 @@
1224112219
<code><![CDATA[$_SESSION['cache']['version_check']]]></code>
1224212220
</MixedArrayAssignment>
1224312221
<MixedAssignment>
12244-
<code>$mysqlVersions</code>
12245-
<code>$phpVersions</code>
12246-
<code>$release</code>
1224712222
<code>$response</code>
1224812223
<code>$response</code>
1224912224
</MixedAssignment>
1225012225
<MixedOperand>
1225112226
<code><![CDATA[$_SESSION['cache']['version_check']['timestamp']]]></code>
1225212227
</MixedOperand>
12253-
<MixedPropertyFetch>
12254-
<code><![CDATA[$release->date]]></code>
12255-
<code><![CDATA[$release->mysql_versions]]></code>
12256-
<code><![CDATA[$release->php_versions]]></code>
12257-
<code><![CDATA[$release->version]]></code>
12258-
<code><![CDATA[$release->version]]></code>
12259-
</MixedPropertyFetch>
12260-
<MoreSpecificReturnType>
12261-
<code>stdClass|null</code>
12262-
</MoreSpecificReturnType>
1226312228
<PossiblyInvalidArgument>
1226412229
<code>$response</code>
1226512230
</PossiblyInvalidArgument>

src/Controllers/VersionCheckController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ public function __invoke(ServerRequest $request): void
3939
header(sprintf('%s: %s', $name, $value));
4040
}
4141

42-
$versionDetails = $this->versionInformation->getLatestVersion();
42+
$versionDetails = $this->versionInformation->getLatestVersions();
4343

4444
if ($versionDetails === null) {
4545
echo json_encode([]);
4646

4747
return;
4848
}
4949

50-
$latestCompatible = $this->versionInformation->getLatestCompatibleVersion($versionDetails->releases);
50+
$latestCompatible = $this->versionInformation->getLatestCompatibleVersion($versionDetails);
5151
$version = '';
5252
$date = '';
5353
if ($latestCompatible != null) {
54-
$version = $latestCompatible['version'];
55-
$date = $latestCompatible['date'];
54+
$version = $latestCompatible->version;
55+
$date = $latestCompatible->date;
5656
}
5757

58-
echo json_encode(['version' => ! empty($version) ? $version : '', 'date' => ! empty($date) ? $date : '']);
58+
echo json_encode(['version' => $version, 'date' => $date]);
5959
}
6060
}

src/Release.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin;
6+
7+
final class Release
8+
{
9+
public function __construct(
10+
public readonly string $version,
11+
public readonly string $date,
12+
public readonly string $phpVersions,
13+
public readonly string $mysqlVersions,
14+
) {
15+
}
16+
}

src/Setup/Index.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public static function versionCheck(): void
116116

117117
// Fetch data
118118
$versionInformation = new VersionInformation();
119-
$versionData = $versionInformation->getLatestVersion();
119+
$versionData = $versionInformation->getLatestVersions();
120120

121121
if ($versionData === null) {
122122
self::messagesSet(
@@ -131,13 +131,13 @@ public static function versionCheck(): void
131131
return;
132132
}
133133

134-
$latestCompatible = $versionInformation->getLatestCompatibleVersion($versionData->releases);
134+
$latestCompatible = $versionInformation->getLatestCompatibleVersion($versionData);
135135
if ($latestCompatible == null) {
136136
return;
137137
}
138138

139-
$version = $latestCompatible['version'];
140-
$date = $latestCompatible['date'];
139+
$version = $latestCompatible->version;
140+
$date = $latestCompatible->date;
141141

142142
$versionUpstream = $versionInformation->versionToInt($version);
143143

src/VersionInformation.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
namespace PhpMyAdmin;
99

1010
use PhpMyAdmin\Utils\HttpRequest;
11-
use stdClass;
1211

1312
use function count;
1413
use function explode;
1514
use function intval;
15+
use function is_array;
1616
use function is_numeric;
17-
use function is_object;
1817
use function is_string;
1918
use function json_decode;
2019
use function preg_match;
@@ -34,9 +33,9 @@ class VersionInformation
3433
/**
3534
* Returns information with latest version from phpmyadmin.net
3635
*
37-
* @return stdClass|null JSON decoded object with the data
36+
* @return Release[]|null JSON decoded object with the data
3837
*/
39-
public function getLatestVersion(): stdClass|null
38+
public function getLatestVersions(): array|null
4039
{
4140
if (! Config::getInstance()->settings['VersionCheck']) {
4241
return null;
@@ -59,18 +58,29 @@ public function getLatestVersion(): stdClass|null
5958

6059
$response = $response ?: '{}';
6160
/* Parse response */
62-
$data = json_decode($response);
61+
$data = json_decode($response, true);
6362

6463
/* Basic sanity checking */
65-
if (! is_object($data) || empty($data->version) || empty($data->releases) || empty($data->date)) {
64+
if (! is_array($data) || ! isset($data['releases']) || ! is_array($data['releases'])) {
6665
return null;
6766
}
6867

6968
if ($save) {
7069
$_SESSION['cache']['version_check'] = ['response' => $response, 'timestamp' => time()];
7170
}
7271

73-
return $data;
72+
$releases = [];
73+
/** @var string[] $release */
74+
foreach ($data['releases'] as $release) {
75+
$releases[] = new Release(
76+
$release['version'],
77+
$release['date'],
78+
$release['php_versions'],
79+
$release['mysql_versions'],
80+
);
81+
}
82+
83+
return $releases;
7484
}
7585

7686
/**
@@ -137,17 +147,16 @@ public function versionToInt(string $version): int
137147
* Returns the version and date of the latest phpMyAdmin version compatible
138148
* with the available PHP and MySQL versions
139149
*
140-
* @param mixed[] $releases array of information related to each version
150+
* @param Release[] $releases array of information related to each version
141151
*
142-
* @return mixed[]|null containing the version and date of latest compatible version
152+
* @return Release|null containing the version and date of latest compatible version
143153
*/
144-
public function getLatestCompatibleVersion(array $releases): array|null
154+
public function getLatestCompatibleVersion(array $releases): Release|null
145155
{
146156
// Maintains the latest compatible version
147157
$latestRelease = null;
148158
foreach ($releases as $release) {
149-
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
150-
$phpVersions = $release->php_versions;
159+
$phpVersions = $release->phpVersions;
151160
$phpConditions = explode(',', $phpVersions);
152161
foreach ($phpConditions as $phpCondition) {
153162
if (! $this->evaluateVersionCondition('PHP', $phpCondition)) {
@@ -159,8 +168,7 @@ public function getLatestCompatibleVersion(array $releases): array|null
159168
// We evaluate MySQL version constraint if there are only
160169
// one server configured.
161170
if (count(Config::getInstance()->settings['Servers']) === 1) {
162-
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
163-
$mysqlVersions = $release->mysql_versions;
171+
$mysqlVersions = $release->mysqlVersions;
164172
$mysqlConditions = explode(',', $mysqlVersions);
165173
foreach ($mysqlConditions as $mysqlCondition) {
166174
if (! $this->evaluateVersionCondition('MySQL', $mysqlCondition)) {
@@ -170,11 +178,11 @@ public function getLatestCompatibleVersion(array $releases): array|null
170178
}
171179

172180
// To compare the current release with the previous latest release or no release is set
173-
if ($latestRelease !== null && ! version_compare($latestRelease['version'], $release->version, '<')) {
181+
if ($latestRelease !== null && ! version_compare($latestRelease->version, $release->version, '<')) {
174182
continue;
175183
}
176184

177-
$latestRelease = ['version' => $release->version, 'date' => $release->date];
185+
$latestRelease = $release;
178186
}
179187

180188
// no compatible version

0 commit comments

Comments
 (0)