Skip to content

Commit 2c22bf2

Browse files
committed
Refactor getOSRelease method for improved readability and efficiency
1 parent cfb8cc9 commit 2c22bf2

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

src/StaticPHP/Util/System/LinuxUtil.php

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class LinuxUtil extends UnixUtil
1212
/**
1313
* Get current linux distro name and version.
1414
*
15-
* @noinspection PhpMissingBreakStatementInspection
1615
* @return array{dist: string, ver: string, family: string} Linux distro info (unknown if not found)
1716
*/
1817
public static function getOSRelease(): array
@@ -22,42 +21,39 @@ public static function getOSRelease(): array
2221
'ver' => 'unknown',
2322
'family' => 'unknown',
2423
];
25-
switch (true) {
26-
case file_exists('/etc/centos-release'):
27-
$lines = file('/etc/centos-release');
28-
$centos = true;
29-
goto rh;
30-
case file_exists('/etc/redhat-release'):
31-
$lines = file('/etc/redhat-release');
32-
$centos = false;
33-
rh:
34-
foreach ($lines as $line) {
35-
if (preg_match('/release\s+(\d*(\.\d+)*)/', $line, $matches)) {
36-
/* @phpstan-ignore-next-line */
37-
$ret['dist'] = $centos ? 'centos' : 'redhat';
38-
$ret['ver'] = $matches[1];
39-
}
24+
25+
if (file_exists('/etc/centos-release') || file_exists('/etc/redhat-release')) {
26+
$is_centos = file_exists('/etc/centos-release');
27+
$file = $is_centos ? '/etc/centos-release' : '/etc/redhat-release';
28+
$lines = file($file);
29+
30+
foreach ($lines as $line) {
31+
if (preg_match('/release\s+(\d*(\.\d+)*)/', $line, $matches)) {
32+
$ret['dist'] = $is_centos ? 'centos' : 'redhat';
33+
$ret['ver'] = $matches[1];
34+
}
35+
}
36+
} elseif (file_exists('/etc/os-release')) {
37+
$lines = file('/etc/os-release');
38+
39+
foreach ($lines as $line) {
40+
if (preg_match('/^ID=(.*)$/', $line, $matches)) {
41+
$ret['dist'] = $matches[1];
4042
}
41-
break;
42-
case file_exists('/etc/os-release'):
43-
$lines = file('/etc/os-release');
44-
foreach ($lines as $line) {
45-
if (preg_match('/^ID=(.*)$/', $line, $matches)) {
46-
$ret['dist'] = $matches[1];
47-
}
48-
if (preg_match('/^ID_LIKE=(.*)$/', $line, $matches)) {
49-
$ret['family'] = $matches[1];
50-
}
51-
if (preg_match('/^VERSION_ID=(.*)$/', $line, $matches)) {
52-
$ret['ver'] = $matches[1];
53-
}
43+
if (preg_match('/^ID_LIKE=(.*)$/', $line, $matches)) {
44+
$ret['family'] = $matches[1];
5445
}
55-
$ret['dist'] = trim($ret['dist'], '"\'');
56-
$ret['ver'] = trim($ret['ver'], '"\'');
57-
if (strcasecmp($ret['dist'], 'centos') === 0) {
58-
$ret['dist'] = 'redhat';
46+
if (preg_match('/^VERSION_ID=(.*)$/', $line, $matches)) {
47+
$ret['ver'] = $matches[1];
5948
}
60-
break;
49+
}
50+
51+
$ret['dist'] = trim($ret['dist'], '"\'');
52+
$ret['ver'] = trim($ret['ver'], '"\'');
53+
54+
if (strcasecmp($ret['dist'], 'centos') === 0) {
55+
$ret['dist'] = 'redhat';
56+
}
6157
}
6258
return $ret;
6359
}

0 commit comments

Comments
 (0)