Fix psalm errors in DockerActionManager env handling code

Signed-off-by: Alan Savage <3028205+asavageiv@users.noreply.github.com>
This commit is contained in:
Alan Savage 2025-06-20 20:32:26 +00:00 committed by Simon L.
parent d374fd2c1c
commit 0f858dc3fe

View file

@ -475,7 +475,7 @@ readonly class DockerActionManager {
// Replaces placeholders in $envValue with their values. // Replaces placeholders in $envValue with their values.
// E.g. "%NC_DOMAIN%:%APACHE_PORT" becomes "my.nextcloud.com:11000" // E.g. "%NC_DOMAIN%:%APACHE_PORT" becomes "my.nextcloud.com:11000"
private function replaceEnvPlaceholders($envValue) { private function replaceEnvPlaceholders(string $envValue): string {
// $pattern breaks down as: // $pattern breaks down as:
// % - matches a literal percent sign // % - matches a literal percent sign
// ([^%]+) - capture group that matches one or more characters that are NOT percent signs // ([^%]+) - capture group that matches one or more characters that are NOT percent signs
@ -488,16 +488,17 @@ readonly class DockerActionManager {
if ($matchCount > 0) { if ($matchCount > 0) {
$placeholders = $matches[0]; // ["%PLACEHOLDER1%", "%PLACEHOLDER2%", ...] $placeholders = $matches[0]; // ["%PLACEHOLDER1%", "%PLACEHOLDER2%", ...]
$placeholderNames = $matches[1]; // ["PLACEHOLDER1", "PLACEHOLDER2", ...] $placeholderNames = $matches[1]; // ["PLACEHOLDER1", "PLACEHOLDER2", ...]
$placeholderToPattern = fn($placeholder) => '/' . $placeholder . '/'; $placeholderToPattern = fn(string $p): string => '/' . $p . '/';
$placeholderPatterns = array_map($placeholderToPattern, $placeholders); // ["/%PLACEHOLDER1%/", ...] $placeholderPatterns = array_map($placeholderToPattern, $placeholders); // ["/%PLACEHOLDER1%/", ...]
$placeholderValues = array_map([$this, 'getPlaceholderValue'], $placeholderNames); // ["val1", "val2"] $placeholderValues = array_map([$this, 'getPlaceholderValue'], $placeholderNames); // ["val1", "val2"]
$result = preg_replace($placeholderPatterns, $placeholderValues, $envValue); // Guaranteed to be non-null because we found the placeholders in the preg_match_all.
$result = (string) preg_replace($placeholderPatterns, $placeholderValues, $envValue);
return $result; return $result;
} }
return $envValue; return $envValue;
} }
private function getPlaceholderValue($placeholder) { private function getPlaceholderValue(string $placeholder) : string {
return match ($placeholder) { return match ($placeholder) {
'NC_DOMAIN' => $this->configurationManager->GetDomain(), 'NC_DOMAIN' => $this->configurationManager->GetDomain(),
'NC_BASE_DN' => $this->configurationManager->GetBaseDN(), 'NC_BASE_DN' => $this->configurationManager->GetBaseDN(),
@ -530,7 +531,7 @@ readonly class DockerActionManager {
'NEXTCLOUD_TRUSTED_CACERTS_DIR' => $this->configurationManager->GetTrustedCacertsDir(), 'NEXTCLOUD_TRUSTED_CACERTS_DIR' => $this->configurationManager->GetTrustedCacertsDir(),
'ADDITIONAL_DIRECTORIES_BACKUP' => $this->configurationManager->GetAdditionalBackupDirectoriesString() !== '' ? 'yes' : '', 'ADDITIONAL_DIRECTORIES_BACKUP' => $this->configurationManager->GetAdditionalBackupDirectoriesString() !== '' ? 'yes' : '',
'BORGBACKUP_HOST_LOCATION' => $this->configurationManager->GetBorgBackupHostLocation(), 'BORGBACKUP_HOST_LOCATION' => $this->configurationManager->GetBorgBackupHostLocation(),
'APACHE_MAX_SIZE' => $this->configurationManager->GetApacheMaxSize(), 'APACHE_MAX_SIZE' => (string)($this->configurationManager->GetApacheMaxSize()),
'COLLABORA_SECCOMP_POLICY' => $this->configurationManager->GetCollaboraSeccompPolicy(), 'COLLABORA_SECCOMP_POLICY' => $this->configurationManager->GetCollaboraSeccompPolicy(),
'NEXTCLOUD_STARTUP_APPS' => $this->configurationManager->GetNextcloudStartupApps(), 'NEXTCLOUD_STARTUP_APPS' => $this->configurationManager->GetNextcloudStartupApps(),
'NEXTCLOUD_ADDITIONAL_APKS' => $this->configurationManager->GetNextcloudAdditionalApks(), 'NEXTCLOUD_ADDITIONAL_APKS' => $this->configurationManager->GetNextcloudAdditionalApks(),
@ -546,7 +547,7 @@ readonly class DockerActionManager {
}; };
} }
private function getSecretOrThrow($secretName) { private function getSecretOrThrow(string $secretName): string {
$secret = $this->configurationManager->GetSecret($secretName); $secret = $this->configurationManager->GetSecret($secretName);
if ($secret === "") { if ($secret === "") {
throw new \Exception("The secret " . $secretName . " is empty. Cannot substitute its value. Please check if it is defined in secrets of containers.json."); throw new \Exception("The secret " . $secretName . " is empty. Cannot substitute its value. Please check if it is defined in secrets of containers.json.");