From c6c936a80ff947ae77a90ad910c8dc03e10f6dbd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 23 Jun 2022 10:56:37 +0200 Subject: [PATCH] Added more type-hints --- src/Model/OpenWebAuthToken.php | 2 +- src/Model/Profile.php | 1 - src/Model/Register.php | 24 ++++++++++----------- src/Model/Search.php | 3 +-- src/Model/Subscription.php | 27 +++++++++++------------- src/Model/User.php | 38 +++++++++++++++++----------------- src/Model/Verb.php | 4 ++-- 7 files changed, 46 insertions(+), 53 deletions(-) diff --git a/src/Model/OpenWebAuthToken.php b/src/Model/OpenWebAuthToken.php index 5071b00d3a..b57356a986 100644 --- a/src/Model/OpenWebAuthToken.php +++ b/src/Model/OpenWebAuthToken.php @@ -36,7 +36,6 @@ class OpenWebAuthToken * @param int $uid The user ID. * @param string $token * @param string $meta - * * @return boolean * @throws \Exception */ @@ -80,6 +79,7 @@ class OpenWebAuthToken * * @param string $type Verify type. * @param string $interval SQL compatible time interval + * @return void * @throws \Exception */ public static function purge(string $type, string $interval) diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 8a9333734b..a3dcc60b1c 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -162,7 +162,6 @@ class Profile * Returns a formatted location string from the given profile array * * @param array $profile Profile array (Generated from the "profile" table) - * * @return string Location string */ public static function formatLocation(array $profile): string diff --git a/src/Model/Register.php b/src/Model/Register.php index b4e9f75ea7..c24e66d4d0 100644 --- a/src/Model/Register.php +++ b/src/Model/Register.php @@ -34,13 +34,12 @@ class Register /** * Return the list of pending registrations * - * @param int $start Start count (Default is 0) + * @param int $start Start count (Default is 0) * @param int $count Count of the items per page (Default is @see Pager::ITEMS_PER_PAGE) - * - * @return array + * @return array|bool Array on succes, false on failure * @throws \Exception */ - public static function getPending($start = 0, $count = Pager::ITEMS_PER_PAGE) + public static function getPending(int $start = 0, int $count = Pager::ITEMS_PER_PAGE) { return DBA::selectToArray('pending-view', [], [], ['limit' => [$start, $count]]); } @@ -50,8 +49,7 @@ class Register * * @param int $uid The user id * - * @return array The pending user information - * + * @return array|bool Array on succes, false on failure * @throws \Exception */ public static function getPendingForUser(int $uid) @@ -65,7 +63,7 @@ class Register * @return int * @throws \Exception */ - public static function getPendingCount() + public static function getPendingCount(): int { return DBA::count('pending-view', ['self' => true]); } @@ -74,10 +72,10 @@ class Register * Returns the register record associated with the provided hash * * @param string $hash - * @return array + * @return array|bool Array on succes, false on failure * @throws \Exception */ - public static function getByHash($hash) + public static function getByHash(string $hash) { return DBA::selectFirst('register', [], ['hash' => $hash]); } @@ -89,7 +87,7 @@ class Register * @return boolean * @throws \Exception */ - public static function existsByHash($hash) + public static function existsByHash(string $hash): bool { return DBA::exists('register', ['hash' => $hash]); } @@ -100,7 +98,7 @@ class Register * @return string * @throws \Exception */ - public static function createForInvitation() + public static function createForInvitation(): string { $code = Strings::getRandomName(8) . random_int(1000, 9999); @@ -124,7 +122,7 @@ class Register * @return boolean * @throws \Exception */ - public static function createForApproval($uid, $language, $note = '') + public static function createForApproval(int $uid, string $language, string $note = ''): bool { $hash = Strings::getRandomHex(); @@ -151,7 +149,7 @@ class Register * @return boolean * @throws \Exception */ - public static function deleteByHash($hash) + public static function deleteByHash(string $hash): bool { return DBA::delete('register', ['hash' => $hash]); } diff --git a/src/Model/Search.php b/src/Model/Search.php index 09e490e77b..cb518c0b17 100644 --- a/src/Model/Search.php +++ b/src/Model/Search.php @@ -32,10 +32,9 @@ class Search * Returns the list of user defined tags (e.g. #Friendica) * * @return array - * * @throws \Exception */ - public static function getUserTags() + public static function getUserTags(): array { $termsStmt = DBA::p("SELECT DISTINCT(`term`) FROM `search`"); diff --git a/src/Model/Subscription.php b/src/Model/Subscription.php index cb14ae2b5f..e5305378e3 100644 --- a/src/Model/Subscription.php +++ b/src/Model/Subscription.php @@ -37,8 +37,7 @@ class Subscription * @param int $applicationid * @param int $uid * @param array $fields - * - * @return bool Does it exist? + * @return array|bool Array on success, false on failure */ public static function select(int $applicationid, int $uid, array $fields = []) { @@ -53,7 +52,7 @@ class Subscription * * @return bool Does it exist? */ - public static function exists(int $applicationid, int $uid) + public static function exists(int $applicationid, int $uid): bool { return DBA::exists('subscription', ['application-id' => $applicationid, 'uid' => $uid]); } @@ -64,10 +63,9 @@ class Subscription * @param int $applicationid * @param int $uid * @param array $fields subscription fields - * * @return bool result of update */ - public static function update(int $applicationid, int $uid, array $fields) + public static function update(int $applicationid, int $uid, array $fields): bool { return DBA::update('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]); } @@ -76,10 +74,9 @@ class Subscription * Insert or replace a subscription record * * @param array $fields subscription fields - * * @return bool result of replace */ - public static function replace(array $fields) + public static function replace(array $fields): bool { return DBA::replace('subscription', $fields); } @@ -91,7 +88,7 @@ class Subscription * @param int $uid * @return bool */ - public static function delete(int $applicationid, int $uid) + public static function delete(int $applicationid, int $uid): bool { return DBA::delete('subscription', ['application-id' => $applicationid, 'uid' => $uid]); } @@ -136,25 +133,25 @@ class Subscription /** * Prepare push notification * - * @param int $nid + * @param Notification $Notification Notification instance * @return void */ - public static function pushByNotification(Entity\Notification $Notification) + public static function pushByNotification(Entity\Notification $notification) { - $type = \Friendica\Factory\Api\Mastodon\Notification::getType($Notification); + $type = \Friendica\Factory\Api\Mastodon\Notification::getType($notification); - if (DI::notify()->NotifyOnDesktop($Notification, $type)) { - DI::notify()->createFromNotification($Notification); + if (DI::notify()->NotifyOnDesktop($notification, $type)) { + DI::notify()->createFromNotification($notification); } if (empty($type)) { return; } - $subscriptions = DBA::select('subscription', [], ['uid' => $Notification->uid, $type => true]); + $subscriptions = DBA::select('subscription', [], ['uid' => $notification->uid, $type => true]); while ($subscription = DBA::fetch($subscriptions)) { Logger::info('Push notification', ['id' => $subscription['id'], 'uid' => $subscription['uid'], 'type' => $type]); - Worker::add(PRIORITY_HIGH, 'PushSubscription', $subscription['id'], $Notification->id); + Worker::add(PRIORITY_HIGH, 'PushSubscription', $subscription['id'], $notification->id); } DBA::close($subscriptions); } diff --git a/src/Model/User.php b/src/Model/User.php index 07f2dfe8de..d1306fcb98 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -117,16 +117,18 @@ class User switch ($accounttype) { case 'person': return User::ACCOUNT_TYPE_PERSON; + case 'organisation': return User::ACCOUNT_TYPE_ORGANISATION; + case 'news': return User::ACCOUNT_TYPE_NEWS; + case 'community': return User::ACCOUNT_TYPE_COMMUNITY; - default: - return null; - break; + } + return null; } /** @@ -362,14 +364,12 @@ class User /** * Get a user based on its email * - * @param string $email - * @param array $fields - * + * @param string $email + * @param array $fields * @return array|boolean User record if it exists, false otherwise - * * @throws Exception */ - public static function getByEmail($email, array $fields = []) + public static function getByEmail(string $email, array $fields = []) { return DBA::selectFirst('user', $fields, ['email' => $email]); } @@ -512,7 +512,7 @@ class User * @throws HTTPException\ForbiddenException * @throws HTTPException\NotFoundException */ - public static function getIdFromPasswordAuthentication($user_info, string $password, bool $third_party = false) + public static function getIdFromPasswordAuthentication($user_info, string $password, bool $third_party = false): int { // Addons registered with the "authenticate" hook may create the user on the // fly. `getAuthenticationInfo` will fail if the user doesn't exist yet. If @@ -1164,32 +1164,32 @@ class User $type = Images::getMimeTypeByData($img_str, $photo, $type); - $Image = new Image($img_str, $type); - if ($Image->isValid()) { - $Image->scaleToSquare(300); + $image = new Image($img_str, $type); + if ($image->isValid()) { + $image->scaleToSquare(300); $resource_id = Photo::newResource(); // Not using Photo::PROFILE_PHOTOS here, so that it is discovered as translateble string $profile_album = DI::l10n()->t('Profile Photos'); - $r = Photo::store($Image, $uid, 0, $resource_id, $filename, $profile_album, 4); + $r = Photo::store($image, $uid, 0, $resource_id, $filename, $profile_album, 4); if ($r === false) { $photo_failure = true; } - $Image->scaleDown(80); + $image->scaleDown(80); - $r = Photo::store($Image, $uid, 0, $resource_id, $filename, $profile_album, 5); + $r = Photo::store($image, $uid, 0, $resource_id, $filename, $profile_album, 5); if ($r === false) { $photo_failure = true; } - $Image->scaleDown(48); + $image->scaleDown(48); - $r = Photo::store($Image, $uid, 0, $resource_id, $filename, $profile_album, 6); + $r = Photo::store($image, $uid, 0, $resource_id, $filename, $profile_album, 6); if ($r === false) { $photo_failure = true; @@ -1342,7 +1342,6 @@ class User * @param string $email The user's email address * @param string $nick The user's nick name * @param string $lang The user's language (default is english) - * * @return bool True, if the user was created successfully * @throws HTTPException\InternalServerErrorException * @throws ErrorException @@ -1731,7 +1730,6 @@ class User * @param string $type The type of users, which should get (all, bocked, removed) * @param string $order Order of the user list (Default is 'contact.name') * @param bool $descending Order direction (Default is ascending) - * * @return array|bool The list of the users * @throws Exception */ @@ -1744,11 +1742,13 @@ class User $condition['account_removed'] = false; $condition['blocked'] = false; break; + case 'blocked': $condition['account_removed'] = false; $condition['blocked'] = true; $condition['verified'] = true; break; + case 'removed': $condition['account_removed'] = true; break; diff --git a/src/Model/Verb.php b/src/Model/Verb.php index 714cae4209..17145adfe7 100644 --- a/src/Model/Verb.php +++ b/src/Model/Verb.php @@ -34,7 +34,7 @@ class Verb * @return integer verb id * @throws \Exception */ - public static function getID(string $verb) + public static function getID(string $verb): int { if (empty($verb)) { return 0; @@ -56,7 +56,7 @@ class Verb * @param integer $id * @return string verb */ - public static function getByID(int $id) + public static function getByID(int $id): string { if (empty($id)) { return ''; -- 2.39.5