From 8aa9d94acf2c90956a190c31ad2bba8b3f0024c1 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Sat, 6 Feb 2021 20:08:37 +0100 Subject: [PATCH] Remove inline if clauses --- MD_STD.php | 56 ++++++++++++++++++++++++++++++++++++------------ MD_STD_CACHE.php | 4 +++- MD_STD_IN.php | 28 ++++++++++++++++++------ MD_STD_SEC.php | 4 +++- 4 files changed, 69 insertions(+), 23 deletions(-) diff --git a/MD_STD.php b/MD_STD.php index 9a2f2a0..32ec335 100644 --- a/MD_STD.php +++ b/MD_STD.php @@ -44,7 +44,9 @@ final class MD_STD { public static function realpath(string $path):string { $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; } @@ -124,7 +126,9 @@ final class MD_STD { public static function ob_get_clean():string { $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; } @@ -139,8 +143,12 @@ final class MD_STD { */ public static function startsWith(string $haystack, string $needle):bool { - if (substr($haystack, 0, \strlen($needle)) == $needle) return true; - else return false; + if (substr($haystack, 0, \strlen($needle)) === $needle) { + return true; + } + else { + return false; + } } @@ -157,7 +165,9 @@ final class MD_STD { $output = false; foreach ($needles as $needle) { $output = self::startsWith($haystack, $needle); - if ($output == true) return $output; + if ($output == true) { + 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 { $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; } @@ -212,7 +224,9 @@ final class MD_STD { public static function strtotime(string $datetime):int { $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; } @@ -268,7 +282,9 @@ final class MD_STD { // if ($errmsg = curl_error($curl)) echo $errmsg; \curl_close($curl); - if (\is_bool($result)) return ""; + if (\is_bool($result)) { + return ""; + } return $result; } @@ -333,7 +349,9 @@ final class MD_STD { // $_SERVER['HTTP_ACCEPT_LANGUAGE'] verwenden, wenn keine Sprachvariable mitgegeben wurde 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? @@ -344,7 +362,9 @@ final class MD_STD { // Den Header auftrennen $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 $current_lang = $default_language; @@ -453,7 +473,9 @@ final class MD_STD { public static function openssl_random_pseudo_bytes(int $length):string { $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; } @@ -469,7 +491,9 @@ final class MD_STD { $input = \explode(PHP_EOL, $input); $output = ""; - foreach ($input as $line) $output .= \trim($line) . PHP_EOL; + foreach ($input as $line) { + $output .= \trim($line) . PHP_EOL; + } return $output; } @@ -572,8 +596,12 @@ final class MD_STD { */ public static function levenshtein(string $str1, string $str2):int { - if (\strlen($str1) > 250) $str1 = \substr($str1, 0, 250); - if (\strlen($str2) > 250) $str2 = \substr($str2, 0, 250); + if (\strlen($str1) > 250) { + $str1 = \substr($str1, 0, 250); + } + if (\strlen($str2) > 250) { + $str2 = \substr($str2, 0, 250); + } return \levenshtein($str1, $str2); diff --git a/MD_STD_CACHE.php b/MD_STD_CACHE.php index 097c0e7..99049ac 100644 --- a/MD_STD_CACHE.php +++ b/MD_STD_CACHE.php @@ -48,7 +48,9 @@ final class MD_STD_CACHE { */ 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->connect(self::$redis_host, self::$redis_port, 1, null, 0, 0, ['auth' => [MD_CONF::$redis_pw]]); diff --git a/MD_STD_IN.php b/MD_STD_IN.php index fb0102a..6831f18 100644 --- a/MD_STD_IN.php +++ b/MD_STD_IN.php @@ -44,7 +44,9 @@ final class MD_STD_IN { */ public static function sanitize_id_or_zero($input):int { - if ($input === "") return 0; + if ($input === "") { + return 0; + } $input = \filter_var($input, \FILTER_VALIDATE_INT, [ 'options' => [ @@ -76,7 +78,9 @@ final class MD_STD_IN { FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES); - if ($output === false) return ""; + if ($output === false) { + return ""; + } while (strpos($output, " ") !== false) { $output = str_replace(" ", " ", $output); } @@ -126,7 +130,9 @@ final class MD_STD_IN { else if (isset($_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)) { 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])) { $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)) { 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 { - if ($input === "") return ""; + if ($input === "") { + return ""; + } $output = \filter_var($input, FILTER_SANITIZE_URL); 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 { - if ($input === "") return ""; + if ($input === "") { + return ""; + } $output = \filter_var($input, FILTER_SANITIZE_EMAIL); 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 { - if ($input === "") return ""; + if ($input === "") { + return ""; + } // Remove hyphens $input = trim(strtr($input, ["-" => "", "–" => ""])); diff --git a/MD_STD_SEC.php b/MD_STD_SEC.php index 0c3afee..b9f92f3 100644 --- a/MD_STD_SEC.php +++ b/MD_STD_SEC.php @@ -68,7 +68,9 @@ final class MD_STD_SEC { $logfile_common = \sys_get_temp_dir() . "/logins_{$tool_name}.json"; // 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 // entering the system.