Remove inline if clauses

This commit is contained in:
Joshua Ramon Enslin 2021-02-06 20:08:37 +01:00
parent 217e1fc86b
commit 8aa9d94acf
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
4 changed files with 69 additions and 23 deletions

View File

@ -44,7 +44,9 @@ final class MD_STD {
public static function realpath(string $path):string { public static function realpath(string $path):string {
$output = \realpath($path); $output = \realpath($path);
if (!\is_string($output)) throw new MDFileDoesNotExist("The file {$path} does not exist or is not readable."); if (!\is_string($output)) {
throw new MDFileDoesNotExist("The file {$path} does not exist or is not readable.");
}
return $output; return $output;
} }
@ -124,7 +126,9 @@ final class MD_STD {
public static function ob_get_clean():string { public static function ob_get_clean():string {
$output = \ob_get_clean(); $output = \ob_get_clean();
if ($output === false) throw new MDOutputBufferNotStarted("Output buffer was not started"); if ($output === false) {
throw new MDOutputBufferNotStarted("Output buffer was not started");
}
return $output; return $output;
} }
@ -139,8 +143,12 @@ final class MD_STD {
*/ */
public static function startsWith(string $haystack, string $needle):bool { public static function startsWith(string $haystack, string $needle):bool {
if (substr($haystack, 0, \strlen($needle)) == $needle) return true; if (substr($haystack, 0, \strlen($needle)) === $needle) {
else return false; return true;
}
else {
return false;
}
} }
@ -157,7 +165,9 @@ final class MD_STD {
$output = false; $output = false;
foreach ($needles as $needle) { foreach ($needles as $needle) {
$output = self::startsWith($haystack, $needle); $output = self::startsWith($haystack, $needle);
if ($output == true) return $output; if ($output == true) {
return $output;
}
} }
return $output; return $output;
@ -197,7 +207,9 @@ final class MD_STD {
public static function json_encode(array $value, int $options = 0, int $depth = 512):string { public static function json_encode(array $value, int $options = 0, int $depth = 512):string {
$output = \json_encode($value, $options, $depth); $output = \json_encode($value, $options, $depth);
if ($output === false) throw new Exception("JSON output could not be generated"); if ($output === false) {
throw new Exception("JSON output could not be generated");
}
return $output; return $output;
} }
@ -212,7 +224,9 @@ final class MD_STD {
public static function strtotime(string $datetime):int { public static function strtotime(string $datetime):int {
$output = \strtotime($datetime); $output = \strtotime($datetime);
if ($output === false) throw new MDInvalidInputDate("Invalid input date {$datetime}."); if ($output === false) {
throw new MDInvalidInputDate("Invalid input date {$datetime}.");
}
return $output; return $output;
} }
@ -268,7 +282,9 @@ final class MD_STD {
// if ($errmsg = curl_error($curl)) echo $errmsg; // if ($errmsg = curl_error($curl)) echo $errmsg;
\curl_close($curl); \curl_close($curl);
if (\is_bool($result)) return ""; if (\is_bool($result)) {
return "";
}
return $result; return $result;
} }
@ -333,7 +349,9 @@ final class MD_STD {
// $_SERVER['HTTP_ACCEPT_LANGUAGE'] verwenden, wenn keine Sprachvariable mitgegeben wurde // $_SERVER['HTTP_ACCEPT_LANGUAGE'] verwenden, wenn keine Sprachvariable mitgegeben wurde
if ($lang_variable === "") { if ($lang_variable === "") {
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) $lang_variable = $_SERVER['HTTP_ACCEPT_LANGUAGE']; if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$lang_variable = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
}
} }
// wurde irgendwelche Information mitgeschickt? // wurde irgendwelche Information mitgeschickt?
@ -344,7 +362,9 @@ final class MD_STD {
// Den Header auftrennen // Den Header auftrennen
$accepted_languages = preg_split('/,\s*/', $lang_variable); $accepted_languages = preg_split('/,\s*/', $lang_variable);
if (!is_array($accepted_languages)) return $default_language; if (!is_array($accepted_languages)) {
return $default_language;
}
// Die Standardwerte einstellen // Die Standardwerte einstellen
$current_lang = $default_language; $current_lang = $default_language;
@ -453,7 +473,9 @@ final class MD_STD {
public static function openssl_random_pseudo_bytes(int $length):string { public static function openssl_random_pseudo_bytes(int $length):string {
$output = \openssl_random_pseudo_bytes($length); $output = \openssl_random_pseudo_bytes($length);
if ($output === false) throw new Exception("Failed generating random pseudo bytes using openssl_random_pseudo_bytes"); if ($output === false) {
throw new Exception("Failed generating random pseudo bytes using openssl_random_pseudo_bytes");
}
return $output; return $output;
} }
@ -469,7 +491,9 @@ final class MD_STD {
$input = \explode(PHP_EOL, $input); $input = \explode(PHP_EOL, $input);
$output = ""; $output = "";
foreach ($input as $line) $output .= \trim($line) . PHP_EOL; foreach ($input as $line) {
$output .= \trim($line) . PHP_EOL;
}
return $output; return $output;
} }
@ -572,8 +596,12 @@ final class MD_STD {
*/ */
public static function levenshtein(string $str1, string $str2):int { public static function levenshtein(string $str1, string $str2):int {
if (\strlen($str1) > 250) $str1 = \substr($str1, 0, 250); if (\strlen($str1) > 250) {
if (\strlen($str2) > 250) $str2 = \substr($str2, 0, 250); $str1 = \substr($str1, 0, 250);
}
if (\strlen($str2) > 250) {
$str2 = \substr($str2, 0, 250);
}
return \levenshtein($str1, $str2); return \levenshtein($str1, $str2);

View File

@ -48,7 +48,9 @@ final class MD_STD_CACHE {
*/ */
public static function serve_page_through_redis_cache(string $redisKey, int $expiry = 3600):string { public static function serve_page_through_redis_cache(string $redisKey, int $expiry = 3600):string {
if (PHP_SAPI === 'cli') return ''; if (PHP_SAPI === 'cli') {
return '';
}
$redis = new Redis(); $redis = new Redis();
$redis->connect(self::$redis_host, self::$redis_port, 1, null, 0, 0, ['auth' => [MD_CONF::$redis_pw]]); $redis->connect(self::$redis_host, self::$redis_port, 1, null, 0, 0, ['auth' => [MD_CONF::$redis_pw]]);

View File

@ -44,7 +44,9 @@ final class MD_STD_IN {
*/ */
public static function sanitize_id_or_zero($input):int { public static function sanitize_id_or_zero($input):int {
if ($input === "") return 0; if ($input === "") {
return 0;
}
$input = \filter_var($input, \FILTER_VALIDATE_INT, [ $input = \filter_var($input, \FILTER_VALIDATE_INT, [
'options' => [ 'options' => [
@ -76,7 +78,9 @@ final class MD_STD_IN {
FILTER_SANITIZE_STRING, FILTER_SANITIZE_STRING,
FILTER_FLAG_NO_ENCODE_QUOTES); FILTER_FLAG_NO_ENCODE_QUOTES);
if ($output === false) return ""; if ($output === false) {
return "";
}
while (strpos($output, " ") !== false) { while (strpos($output, " ") !== false) {
$output = str_replace(" ", " ", $output); $output = str_replace(" ", " ", $output);
} }
@ -126,7 +130,9 @@ final class MD_STD_IN {
else if (isset($_POST[$var_name])) { else if (isset($_POST[$var_name])) {
$output = self::sanitize_text($_POST[$var_name]); $output = self::sanitize_text($_POST[$var_name]);
} }
else $output = self::sanitize_text($default); else {
$output = self::sanitize_text($default);
}
if (!empty($allowed) and !\in_array($output, $allowed, true)) { if (!empty($allowed) and !\in_array($output, $allowed, true)) {
throw new MDpageParameterNotFromListException("Parameter `{$var_name}` must be any of the allowed values: '" . implode('\', \'', $allowed) . "'"); throw new MDpageParameterNotFromListException("Parameter `{$var_name}` must be any of the allowed values: '" . implode('\', \'', $allowed) . "'");
@ -151,7 +157,9 @@ final class MD_STD_IN {
if (isset($_POST[$var_name])) { if (isset($_POST[$var_name])) {
$output = self::sanitize_text($_POST[$var_name]); $output = self::sanitize_text($_POST[$var_name]);
} }
else $output = self::sanitize_text($default); else {
$output = self::sanitize_text($default);
}
if (!empty($allowed) and !\in_array($output, $allowed, true)) { if (!empty($allowed) and !\in_array($output, $allowed, true)) {
throw new MDpageParameterNotFromListException("Parameter `{$var_name}` must be any of the allowed values: '" . implode('\', \'', $allowed) . "'"); throw new MDpageParameterNotFromListException("Parameter `{$var_name}` must be any of the allowed values: '" . implode('\', \'', $allowed) . "'");
@ -170,7 +178,9 @@ final class MD_STD_IN {
*/ */
public static function sanitize_url($input):string { public static function sanitize_url($input):string {
if ($input === "") return ""; if ($input === "") {
return "";
}
$output = \filter_var($input, FILTER_SANITIZE_URL); $output = \filter_var($input, FILTER_SANITIZE_URL);
if (($output = \filter_var($output, FILTER_VALIDATE_URL)) === false) { if (($output = \filter_var($output, FILTER_VALIDATE_URL)) === false) {
@ -190,7 +200,9 @@ final class MD_STD_IN {
*/ */
public static function sanitize_email($input):string { public static function sanitize_email($input):string {
if ($input === "") return ""; if ($input === "") {
return "";
}
$output = \filter_var($input, FILTER_SANITIZE_EMAIL); $output = \filter_var($input, FILTER_SANITIZE_EMAIL);
if (($output = \filter_var($output, FILTER_VALIDATE_EMAIL)) === false) { if (($output = \filter_var($output, FILTER_VALIDATE_EMAIL)) === false) {
@ -227,7 +239,9 @@ final class MD_STD_IN {
*/ */
public static function validate_isbn(string $input):string { public static function validate_isbn(string $input):string {
if ($input === "") return ""; if ($input === "") {
return "";
}
// Remove hyphens // Remove hyphens
$input = trim(strtr($input, ["-" => "", "" => ""])); $input = trim(strtr($input, ["-" => "", "" => ""]));

View File

@ -68,7 +68,9 @@ final class MD_STD_SEC {
$logfile_common = \sys_get_temp_dir() . "/logins_{$tool_name}.json"; $logfile_common = \sys_get_temp_dir() . "/logins_{$tool_name}.json";
// Ensure the log files exist // Ensure the log files exist
if (!\file_exists($logfile_common)) \file_put_contents($logfile_common, "[]"); if (!\file_exists($logfile_common)) {
\file_put_contents($logfile_common, "[]");
}
// Hash entered username and IP to prevent malicious strings from // Hash entered username and IP to prevent malicious strings from
// entering the system. // entering the system.