From 7560dccc088ff15980dcdf7f6db09b38980bd3d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Thu, 16 Jun 2022 20:17:04 +0200 Subject: [PATCH] Added again more type-hints --- src/Model/Contact.php | 61 ++++++++++++++++++++-------------------- src/Module/Directory.php | 2 +- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 3ed2785cfa..aea3faf3cd 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -119,7 +119,7 @@ class Contact * @return array * @throws \Exception */ - public static function selectToArray(array $fields = [], array $condition = [], array $params = []) + public static function selectToArray(array $fields = [], array $condition = [], array $params = []): array { return DBA::selectToArray('contact', $fields, $condition, $params); } @@ -131,7 +131,7 @@ class Contact * @return array * @throws \Exception */ - public static function selectFirst(array $fields = [], array $condition = [], array $params = []) + public static function selectFirst(array $fields = [], array $condition = [], array $params = []): array { $contact = DBA::selectFirst('contact', $fields, $condition, $params); @@ -148,7 +148,7 @@ class Contact * @return int id of the created contact * @throws \Exception */ - public static function insert(array $fields, int $duplicate_mode = Database::INSERT_DEFAULT) + public static function insert(array $fields, int $duplicate_mode = Database::INSERT_DEFAULT): int { if (!empty($fields['baseurl']) && empty($fields['gsid'])) { $fields['gsid'] = GServer::getID($fields['baseurl'], true); @@ -187,6 +187,7 @@ class Contact * * @return boolean was the update successfull? * @throws \Exception + * @todo Let's get rid of boolean type of $old_fields */ public static function update(array $fields, array $condition, $old_fields = []) { @@ -204,7 +205,7 @@ class Contact * @return array|boolean Contact record if it exists, false otherwise * @throws \Exception */ - public static function getById($id, $fields = []) + public static function getById(int $id, array $fields = []) { return DBA::selectFirst('contact', $fields, ['id' => $id]); } @@ -217,7 +218,7 @@ class Contact * @return array|boolean Contact record if it exists, false otherwise * @throws \Exception */ - public static function getByUriId($uri_id, $fields = []) + public static function getByUriId(int $uri_id, array $fields = []) { return DBA::selectFirst('contact', $fields, ['uri-id' => $uri_id], ['order' => ['uid']]); } @@ -231,7 +232,7 @@ class Contact * @param integer $uid User ID of the contact * @return array contact array */ - public static function getByURL(string $url, $update = null, array $fields = [], int $uid = 0) + public static function getByURL(string $url, $update = null, array $fields = [], int $uid = 0): array { if ($update || is_null($update)) { $cid = self::getIdForURL($url, $uid, $update); @@ -302,7 +303,7 @@ class Contact * @param array $fields Field list * @return array contact array */ - public static function getByURLForUser(string $url, int $uid = 0, $update = false, array $fields = []) + public static function getByURLForUser(string $url, int $uid = 0, $update = false, array $fields = []): array { if ($uid != 0) { $contact = self::getByURL($url, $update, $fields, $uid); @@ -333,7 +334,7 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function isFollower($cid, $uid) + public static function isFollower(int $cid, int $uid): bool { if (Contact\User::isBlocked($cid, $uid)) { return false; @@ -358,7 +359,7 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function isFollowerByURL($url, $uid) + public static function isFollowerByURL(string $url, uid $uid): bool { $cid = self::getIdForURL($url, $uid); @@ -370,16 +371,16 @@ class Contact } /** - * Tests if the given user follow the given contact + * Tests if the given user shares with the given contact * * @param int $cid Either public contact id or user's contact id * @param int $uid User ID * - * @return boolean is the contact url being followed? + * @return boolean is the contact sharing with given user? * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function isSharing($cid, $uid) + public static function isSharing(int $cid, int $uid): bool { if (Contact\User::isBlocked($cid, $uid)) { return false; @@ -404,7 +405,7 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function isSharingByURL($url, $uid) + public static function isSharingByURL(string $url, int $uid): bool { $cid = self::getIdForURL($url, $uid); @@ -425,7 +426,7 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function getBasepath($url, $dont_update = false) + public static function getBasepath(string $url, bool $dont_update = false): string { $contact = DBA::selectFirst('contact', ['id', 'baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($url)]); if (!DBA::isResult($contact)) { @@ -459,7 +460,7 @@ class Contact * * @return boolean Is it the same server? */ - public static function isLocal($url) + public static function isLocal(string $url): bool { if (!parse_url($url, PHP_URL_SCHEME)) { $addr_parts = explode('@', $url); @@ -476,7 +477,7 @@ class Contact * * @return boolean Is it the same server? */ - public static function isLocalById(int $cid) + public static function isLocalById(int $cid): bool { $contact = DBA::selectFirst('contact', ['url', 'baseurl'], ['id' => $cid]); if (!DBA::isResult($contact)) { @@ -500,7 +501,7 @@ class Contact * @return integer|boolean Public contact id for given user id * @throws \Exception */ - public static function getPublicIdByUserId($uid) + public static function getPublicIdByUserId(int $uid) { $self = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]); if (!DBA::isResult($self)) { @@ -519,7 +520,7 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function getPublicAndUserContactID($cid, $uid) + public static function getPublicAndUserContactID(int $cid, int $uid): array { // We have to use the legacy function as long as the post update hasn't finished if (DI::config()->get('system', 'post_update_version') < 1427) { @@ -560,7 +561,7 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - private static function legacyGetPublicAndUserContactID($cid, $uid) + private static function legacyGetPublicAndUserContactID(int $cid, int $uid): array { if (empty($uid) || empty($cid)) { return []; @@ -2614,7 +2615,7 @@ class Contact * @throws HTTPException\NotFoundException * @throws \ImagickException */ - public static function createFromProbeForUser(int $uid, $url, $network = '') + public static function createFromProbeForUser(int $uid, string $url, string $network = ''): array { $result = ['cid' => -1, 'success' => false, 'message' => '']; @@ -2803,7 +2804,7 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function addRelationship(array $importer, array $contact, array $datarray, $sharing = false, $note = '') + public static function addRelationship(array $importer, array $contact, array $datarray, bool $sharing = false, string $note = '') { // Should always be set if (empty($datarray['author-id'])) { @@ -3030,7 +3031,7 @@ class Contact * @return array * @throws \Exception */ - public static function pruneUnavailable(array $contact_ids) + public static function pruneUnavailable(array $contact_ids): array { if (empty($contact_ids)) { return []; @@ -3058,7 +3059,7 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function magicLink($contact_url, $url = '') + public static function magicLink(string $contact_url, string $url = ''): string { if (!Session::isAuthenticated()) { return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url; @@ -3085,7 +3086,7 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function magicLinkById($cid, $url = '') + public static function magicLinkById(int $cid, string $url = ''): string { $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], ['id' => $cid]); @@ -3102,7 +3103,7 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function magicLinkByContact($contact, $url = '') + public static function magicLinkByContact(array $contact, string $url = ''): string { $destination = $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url']; @@ -3143,7 +3144,7 @@ class Contact * * @return boolean "true" if it is a forum */ - public static function isForum($contactid) + public static function isForum(int $contactid): bool { $fields = ['contact-type']; $condition = ['id' => $contactid]; @@ -3162,7 +3163,7 @@ class Contact * @param array $contact * @return bool */ - public static function canReceivePrivateMessages(array $contact) + public static function canReceivePrivateMessages(array $contact): bool { $protocol = $contact['network'] ?? $contact['protocol'] ?? Protocol::PHANTOM; $self = $contact['self'] ?? false; @@ -3180,7 +3181,7 @@ class Contact * @return array with search results * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function searchByName(string $search, string $mode = '', int $uid = 0) + public static function searchByName(string $search, string $mode = '', int $uid = 0): array { if (empty($search)) { return []; @@ -3223,7 +3224,7 @@ class Contact * @param array $urls * @return array result "count", "added" and "updated" */ - public static function addByUrls(array $urls) + public static function addByUrls(array $urls): array { $added = 0; $updated = 0; @@ -3256,7 +3257,7 @@ class Contact * @return array The profile array * @throws Exception */ - public static function getRandomContact() + public static function getRandomContact(): array { $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], [ "`uid` = ? AND `network` = ? AND NOT `failed` AND `last-item` > ?", diff --git a/src/Module/Directory.php b/src/Module/Directory.php index b79e7b1495..d9cad8d623 100644 --- a/src/Module/Directory.php +++ b/src/Module/Directory.php @@ -112,7 +112,7 @@ class Directory extends BaseModule * * @throws \Exception */ - public static function formatEntry(array $contact, $photo_size = 'photo') + public static function formatEntry(array $contact, string $photo_size = 'photo'): array { $itemurl = (($contact['addr'] != "") ? $contact['addr'] : $contact['url']);