From 17dc298dfa8d3abd47358f3719de76060a1db9b4 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sat, 25 Jul 2020 02:05:41 +0000 Subject: [PATCH] [UTIL][NICKNAME] Small refactor and remove the check between user nickname and group_alias, as these will have different semantics --- src/Util/Nickname.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Util/Nickname.php b/src/Util/Nickname.php index ff4aa4ec9d..0c25a2a137 100644 --- a/src/Util/Nickname.php +++ b/src/Util/Nickname.php @@ -19,6 +19,7 @@ namespace App\Util; +use App\Core\DB\DB; use Normalizer; /** @@ -109,8 +110,11 @@ class Nickname */ const BEFORE_MENTIONS = '(?:^|[\s\.\,\:\;\[\(]+)'; + const CHECK_USED = true; + const NO_CHECK_USED = false; + /** - * Validate an input $nickname, and normalize it to its canonical form. + * Normalize an input $nickname, and normalize it to its canonical form. * The canonical form will be returned, or an exception thrown if invalid. * * @throws NicknameException (base class) @@ -121,7 +125,7 @@ class Nickname * @throws NicknameTakenException * @throws NicknameTooLongException */ - public static function normalize(string $nickname, bool $check_already_used = false): string + public static function normalize(string $nickname, bool $check_already_used = NO_CHECK_USED): string { if (mb_strlen($nickname) > self::MAX_LEN) { // Display forms must also fit! @@ -157,7 +161,7 @@ class Nickname * * @return bool True if nickname is valid. False if invalid (or taken if $check_already_used == true). */ - public static function isValid(string $nickname, bool $check_already_used = false): bool + public static function isValid(string $nickname, bool $check_already_used = CHECK_USED): bool { try { self::normalize($nickname, $check_already_used); @@ -195,21 +199,16 @@ class Nickname */ public static function isTaken(string $nickname): ?Profile { - $found = DB::find('user', ['nickname' => $nickname]); - if ($found instanceof User) { + $found = DB::findBy('local_user', ['nickname' => $nickname]); + if ($found instanceof LocalUser) { return $found->getProfile(); } - $found = DB::find('local_group', ['nickname' => $nickname]); + $found = DB::findBy('local_group', ['nickname' => $nickname]); if ($found instanceof Local_group) { return $found->getProfile(); } - $found = DB::find('group_alias', ['nickname' => $nickname]); - if ($found instanceof Group_alias) { - return $found->getProfile(); - } - return null; } }