Skip to content

Commit 2926ff0

Browse files
committed
dry
1 parent 8775492 commit 2926ff0

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

resources/lib/UnityUser.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -300,27 +300,23 @@ public function getSSHKeys(): array
300300
return $result;
301301
}
302302

303-
/* checks if key exists, ignoring the optional comment suffix */
304-
public function SSHKeyExists(string $key): bool
303+
private static function removeSSHKeyOptionalCommentSuffix(string $key): string
305304
{
306-
// if a key is 3 words, assume the 3rd word is the optional comment suffix
307-
$keyMatches = [];
308-
if (preg_match("/^\s*(\S+\s+\S+)\s+(\S+)\s*$/", $key, $keyMatches)) {
309-
$keyNoComment = $keyMatches[1];
305+
$matches = [];
306+
if (preg_match("/^\s*(\S+\s+\S+)\s+(\S+)\s*$/", $key, $matches)) {
307+
return $matches[1];
310308
} else {
311-
$keyNoComment = $key;
309+
return $key;
312310
}
311+
}
312+
313+
/* checks if key exists, ignoring the optional comment suffix */
314+
public function SSHKeyExists(string $key): bool
315+
{
316+
$keyNoSuffix = self::removeSSHKeyOptionalCommentSuffix($key);
313317
foreach ($this->getSSHKeys() as $foundKey) {
314-
if ($key === $foundKey) {
315-
return true;
316-
}
317-
$foundKeyMatches = [];
318-
if (preg_match("/^\s*(\S+\s+\S+)\s+(\S+)\s*$/", $foundKey, $foundKeyMatches)) {
319-
$foundKeyNoComment = $foundKeyMatches[1];
320-
} else {
321-
$foundKeyNoComment = $foundKey;
322-
}
323-
if ($keyNoComment === $foundKeyNoComment) {
318+
$foundKeyNoSuffix = self::removeSSHKeyOptionalCommentSuffix($foundKey);
319+
if ($key === $foundKey || $keyNoSuffix === $foundKeyNoSuffix) {
324320
return true;
325321
}
326322
}

0 commit comments

Comments
 (0)