From 94dfa1729020a62f95a6da94c3d615b7727d8bf8 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Tue, 11 Jun 2024 21:57:56 +0200 Subject: [PATCH] Fix code smells --- src/MD_JAIL.php | 6 +++--- src/MD_STD.php | 5 ++--- src/MD_STD_CACHE.php | 2 +- src/MD_STD_IN.php | 4 ++-- src/MD_STD_SEC.php | 14 +++++++------- tests/MD_STD_IN_Test.php | 5 +++-- tests/MD_STD_Test.php | 2 -- tests/bootstrap.php | 1 - 8 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/MD_JAIL.php b/src/MD_JAIL.php index 681c7d3..ec30ee8 100644 --- a/src/MD_JAIL.php +++ b/src/MD_JAIL.php @@ -13,9 +13,9 @@ */ final class MD_JAIL { - const STATUS_NONE = 0; - const STATUS_STARTED = 1; - const STATUS_SPECIFIED = 2; // Determines that everything is fine. + public const STATUS_NONE = 0; + public const STATUS_STARTED = 1; + public const STATUS_SPECIFIED = 2; // Determines that everything is fine. /** @var integer */ private int $_status = self::STATUS_NONE; diff --git a/src/MD_STD.php b/src/MD_STD.php index 00b0e05..1699e29 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -445,7 +445,7 @@ public static function runCurlMulti(array $urls, int $timeout = 1200, array $hea * * @param string $url URL to validate. * - * @return bool + * @return boolean */ public static function checkUrlIsReachable(string $url):bool { @@ -952,7 +952,7 @@ public static function date_to_int(string $date):int { * 0000-01-01 > 101; 2001-01-01 > 20010101. * Needed to retrieve negative dates (BC) stored in MySQL. * - * @param int $date_int Date represented as an integer. + * @param integer $date_int Date represented as an integer. * * @return string */ @@ -979,6 +979,5 @@ public static function int_to_date(int $date_int):string { false => '', } . $year . '-' . $month . '-' . $day; - } } diff --git a/src/MD_STD_CACHE.php b/src/MD_STD_CACHE.php index 08e76c9..8f133a2 100644 --- a/src/MD_STD_CACHE.php +++ b/src/MD_STD_CACHE.php @@ -34,9 +34,9 @@ public static function shutdown_cache_through_redis(Redis $redis, string $redisK * Caches and serves a page through redis. Should be called at the start * of the script generating a page. * + * @param Redis $redis Redis connection. * @param string $redisKey Key to cache by in redis. * @param integer $expiry Expiration time in seconds. - * @param Redis $redis Redis connection. * * @return string */ diff --git a/src/MD_STD_IN.php b/src/MD_STD_IN.php index a5bff94..355eaa8 100644 --- a/src/MD_STD_IN.php +++ b/src/MD_STD_IN.php @@ -216,7 +216,7 @@ public static function sanitize_url(mixed $input):string { } $rewritten .= $parsed['host']; if (!empty($parsed['port'])) $rewritten .= ':' . $parsed['port']; - $rewritten .= str_replace('%2F' , '/', urlencode($parsed['path'])); + $rewritten .= str_replace('%2F', '/', urlencode($parsed['path'])); if (!empty($parsed['query'])) { $rewritten .= '?' . str_replace('%3D', '=', urlencode($parsed['query'])); } @@ -267,7 +267,7 @@ public static function sanitize_email(mixed $input):string { } if (($output = \filter_var($input, FILTER_VALIDATE_EMAIL)) === false) { - throw new MDInvalidEmail("Invalid input email address" . ' '. $input); + throw new MDInvalidEmail("Invalid input email address" . ' ' . $input); } return $output; diff --git a/src/MD_STD_SEC.php b/src/MD_STD_SEC.php index 1e0c5e1..64e47b9 100644 --- a/src/MD_STD_SEC.php +++ b/src/MD_STD_SEC.php @@ -9,14 +9,14 @@ */ final class MD_STD_SEC { - const REFRESH_TIME_GENERAL = 60; // Time until the comp. with the whole service is cleared. - const REFRESH_TIME_USER = 600; // Time until the comp. with the same username service is cleared. - const REFRESH_TIME_IP = 180; // Time until the comp. with the same IP is cleared. This should be lower than the user-level one, as people working together may be using a common IP. + private const REFRESH_TIME_GENERAL = 60; // Time until the comp. with the whole service is cleared. + private const REFRESH_TIME_USER = 600; // Time until the comp. with the same username service is cleared. + private const REFRESH_TIME_IP = 180; // Time until the comp. with the same IP is cleared. This should be lower than the user-level one, as people working together may be using a common IP. - const BRUTE_FORCE_DELAY_DEFAULT = 2000; // 2000 microseconds = 2 milliseconds - const BRUTE_FORCE_DELAY_MULTIPLIER_COMMON = 1.04; - const BRUTE_FORCE_DELAY_MULTIPLIER_PER_USER = 2.0; - const BRUTE_FORCE_DELAY_MULTIPLIER_PER_IP = 1.6; + private const BRUTE_FORCE_DELAY_DEFAULT = 2000; // 2000 microseconds = 2 milliseconds + private const BRUTE_FORCE_DELAY_MULTIPLIER_COMMON = 1.04; + private const BRUTE_FORCE_DELAY_MULTIPLIER_PER_USER = 2.0; + private const BRUTE_FORCE_DELAY_MULTIPLIER_PER_IP = 1.6; /** * Function for retrieving the anti-csrf token or generating it if need be. diff --git a/tests/MD_STD_IN_Test.php b/tests/MD_STD_IN_Test.php index 92fec09..6b3e430 100644 --- a/tests/MD_STD_IN_Test.php +++ b/tests/MD_STD_IN_Test.php @@ -47,6 +47,7 @@ public static function valid_id_provider():array { */ public function test_sanitize_id_works(mixed $to_validate, int $expected):void { self::assertEquals($expected, MD_STD_IN::sanitize_id($to_validate)); + } /** @@ -79,6 +80,7 @@ public function test_sanitize_id_fails(mixed $to_validate):void { MD_STD_IN::sanitize_id($to_validate); } + /** * Data provider for valid IDs. * @@ -316,7 +318,6 @@ public function test_get_http_input_text():void { self::expectException(MDpageParameterNotFromListException::class); MD_STD_IN::get_http_input_text("a", "", ['a']); - } /** @@ -342,7 +343,6 @@ public function test_get_http_post_text():void { self::expectException(MDpageParameterNotFromListException::class); MD_STD_IN::get_http_post_text("a", "", ['a']); - } /** @@ -425,6 +425,7 @@ public function test_sanitize_email_empty():void { */ public function test_sanitize_email_works(string $to_validate, string $expected):void { self::assertEquals($expected, MD_STD_IN::sanitize_email($to_validate)); + } /** diff --git a/tests/MD_STD_Test.php b/tests/MD_STD_Test.php index 754236c..e8977f6 100644 --- a/tests/MD_STD_Test.php +++ b/tests/MD_STD_Test.php @@ -12,7 +12,6 @@ * Tests for MD_STD. */ final class MD_STD_Test extends TestCase { - /** * Returns sample dates and their equivalent integer values according * to MD_STD::date_to_int(). @@ -58,5 +57,4 @@ public function test_date_to_int(string $date, int $expectedInt):void { self::assertEquals($date, $toStr); } - } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b938e2e..3d4b66a 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -20,6 +20,5 @@ } } - });