IcingaConfigHelper: improve readability

This commit is contained in:
Thomas Gelf 2021-05-20 10:28:07 +02:00
parent da1363d236
commit 10ada10527

View File

@ -77,15 +77,16 @@ class IcingaConfigHelper
{ {
if ($value === 'y' || $value === true) { if ($value === 'y' || $value === true) {
return 'true'; return 'true';
} elseif ($value === 'n' || $value === false) { }
if ($value === 'n' || $value === false) {
return 'false'; return 'false';
} else { }
throw new InvalidArgumentException(sprintf( throw new InvalidArgumentException(sprintf(
'%s is not a valid boolean', '%s is not a valid boolean',
$value $value
)); ));
} }
}
protected static function renderInteger($value) protected static function renderInteger($value)
{ {
@ -98,9 +99,9 @@ class IcingaConfigHelper
// implementations: // implementations:
if ((string) (int) $value === (string) $value) { if ((string) (int) $value === (string) $value) {
return static::renderInteger((int) $value); return static::renderInteger((int) $value);
} else {
return sprintf('%F', $value);
} }
return sprintf('%F', $value);
} }
protected static function renderNull() protected static function renderNull()
@ -143,35 +144,40 @@ class IcingaConfigHelper
{ {
if (is_null($value)) { if (is_null($value)) {
return static::renderNull(); return static::renderNull();
} elseif (is_bool($value)) { }
if (is_bool($value)) {
return static::renderBoolean($value); return static::renderBoolean($value);
} elseif (is_integer($value)) { }
if (is_int($value)) {
return static::renderInteger($value); return static::renderInteger($value);
} elseif (is_float($value)) { }
if (is_float($value)) {
return static::renderFloat($value); return static::renderFloat($value);
}
// TODO: // TODO:
// } elseif (is_object($value) || static::isAssocArray($value)) { // if (is_object($value) || static::isAssocArray($value)) {
// return static::renderHash($value, $prefix) // return static::renderHash($value, $prefix)
// TODO: also check array // TODO: also check array
} elseif (is_array($value)) { if (is_array($value)) {
return static::renderArray($value); return static::renderArray($value);
} elseif (is_string($value)) { }
if (is_string($value)) {
return static::renderString($value); return static::renderString($value);
} else { }
throw new InvalidArgumentException(sprintf( throw new InvalidArgumentException(sprintf(
'Unexpected type %s', 'Unexpected type %s',
var_export($value, 1) var_export($value, 1)
)); ));
} }
}
public static function renderDictionaryKey($key) public static function renderDictionaryKey($key)
{ {
if (preg_match('/^[a-z_]+[a-z0-9_]*$/i', $key)) { if (preg_match('/^[a-z_]+[a-z0-9_]*$/i', $key)) {
return static::escapeIfReserved($key); return static::escapeIfReserved($key);
} else {
return static::renderString($key);
} }
return static::renderString($key);
} }
// Requires an array // Requires an array
@ -241,9 +247,9 @@ class IcingaConfigHelper
{ {
if (self::isReserved($string)) { if (self::isReserved($string)) {
return '@' . $string; return '@' . $string;
} else {
return $string;
} }
return $string;
} }
public static function isValidInterval($interval) public static function isValidInterval($interval)
@ -339,17 +345,16 @@ class IcingaConfigHelper
} else { } else {
if ($macroName === null) { if ($macroName === null) {
return true; return true;
} else { }
if ($macroName === substr($string, $start + 1, $i - $start - 1)) { if ($macroName === substr($string, $start + 1, $i - $start - 1)) {
return true; return true;
} else { }
$start = false; $start = false;
} }
} }
} }
} }
}
}
return false; return false;
} }
@ -362,7 +367,7 @@ class IcingaConfigHelper
*/ */
public static function isValidMacroName($name) public static function isValidMacroName($name)
{ {
return preg_match('/^[A-z_][A-z_\.\d]+$/', $name) return preg_match('/^[A-z_][A-z_.\d]+$/', $name)
&& ! preg_match('/\.$/', $name); && ! preg_match('/\.$/', $name);
} }
@ -408,8 +413,8 @@ class IcingaConfigHelper
if (! empty($parts)) { if (! empty($parts)) {
return implode(' + ', $parts); return implode(' + ', $parts);
} else { }
return '""'; return '""';
} }
} }
}