Skip to content

Commit

Permalink
Improve Git class by removing switch cases on request responses
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed Dec 7, 2020
1 parent 15968b2 commit 0ae5216
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
66 changes: 32 additions & 34 deletions libraries/classes/Git.php
Expand Up @@ -39,6 +39,7 @@
use function substr;
use function trim;
use function unpack;
use function is_bool;

/**
* Git class to manipulate Git data
Expand Down Expand Up @@ -402,27 +403,30 @@ private function isRemoteCommit(&$commit, bool &$isRemoteCommit, string $hash):
&& isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])
) {
$isRemoteCommit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
} else {
$link = 'https://www.phpmyadmin.net/api/commit/' . $hash . '/';
$is_found = $httpRequest->create($link, 'GET');
switch ($is_found) {
case false:
$isRemoteCommit = false;
$_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
break;
case null:
// no remote link for now, but don't cache this as Github is down
$isRemoteCommit = false;
break;
default:
$isRemoteCommit = true;
$_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = true;
if ($commit === false) {
// if no local commit data, try loading from Github
return json_decode((string) $is_found);
}
break;
}

return null;
}

$link = 'https://www.phpmyadmin.net/api/commit/' . $hash . '/';
$is_found = $httpRequest->create($link, 'GET');
if ($is_found === false) {
$isRemoteCommit = false;
$_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;

return null;
}
if ($is_found === null) {
// no remote link for now, but don't cache this as GitHub is down
$isRemoteCommit = false;

return null;
}

$isRemoteCommit = true;
$_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = true;
if ($commit === false) {
// if no local commit data, try loading from Github
return json_decode((string) $is_found);
}

return null;
Expand Down Expand Up @@ -553,19 +557,13 @@ public function checkGitRevision(): ?array
$httpRequest = new HttpRequest();
$link = 'https://www.phpmyadmin.net/api/tree/' . $branch . '/';
$is_found = $httpRequest->create($link, 'GET', true);
switch ($is_found) {
case true:
$is_remote_branch = true;
$_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = true;
break;
case false:
$is_remote_branch = false;
$_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = false;
break;
case null:
// no remote link for now, but don't cache this as Github is down
$is_remote_branch = false;
break;
if (is_bool($is_found)) {
$is_remote_branch = $is_found;
$_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = $is_found;
}
if ($is_found === null) {
// no remote link for now, but don't cache this as Github is down
$is_remote_branch = false;
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions psalm-baseline.xml
Expand Up @@ -1106,9 +1106,6 @@
</PossiblyNullPropertyAssignmentValue>
</file>
<file src="libraries/classes/Git.php">
<ParadoxicalCondition occurrences="1">
<code>null</code>
</ParadoxicalCondition>
<PossiblyNullArgument occurrences="1">
<code>$gitFolder</code>
</PossiblyNullArgument>
Expand Down

0 comments on commit 0ae5216

Please sign in to comment.