From c467bff79fcbed3d953eea051b89d4be20be506a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Fri, 17 Jun 2022 11:17:53 +0200 Subject: [PATCH] Some more type-hints added --- src/Model/Attach.php | 28 ++++++++++---------- src/Model/Conversation.php | 4 +-- src/Model/Event.php | 54 +++++++++++++++++++------------------- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/Model/Attach.php b/src/Model/Attach.php index 91970bfc62..e4d58c4b07 100644 --- a/src/Model/Attach.php +++ b/src/Model/Attach.php @@ -59,7 +59,7 @@ class Attach * @param array $conditions Array of fields for conditions * @param array $params Array of several parameters * - * @return array + * @return array|bool * * @throws \Exception * @see \Friendica\Database\DBA::selectToArray @@ -117,7 +117,7 @@ class Attach * @throws \Exception * @see \Friendica\Database\DBA::select */ - public static function getById($id) + public static function getById(int $id) { return self::selectFirst([], ['id' => $id]); } @@ -132,7 +132,7 @@ class Attach * @throws \Exception * @see \Friendica\Database\DBA::select */ - public static function getByIdWithPermission($id) + public static function getByIdWithPermission(int $id) { $r = self::selectFirst(['uid'], ['id' => $id]); if ($r === false) { @@ -156,10 +156,10 @@ class Attach * * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref' * - * @return string file data + * @return string|null file data or null on failure * @throws \Exception */ - public static function getData($item) + public static function getData(array $item) { if (!empty($item['data'])) { return $item['data']; @@ -195,10 +195,10 @@ class Attach * @param string $deny_cid Permissions, denied contacts.optional, default = '' * @param string $deny_gid Permissions, denied greoup.optional, default = '' * - * @return boolean/integer Row id on success, False on errors + * @return boolean|integer Row id on success, False on errors * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') + public static function store(string $data, int $uid, string $filename, string $filetype = '' , int $filesize = null, string $allow_cid = '', string $allow_gid = '', string $deny_cid = '', string $deny_gid = '') { if ($filetype === '') { $filetype = Mimetype::getContentType($filename); @@ -241,17 +241,17 @@ class Attach /** * Store new file metadata in db and binary in default backend from existing file * - * @param $src - * @param $uid - * @param string $filename + * @param string $src Source file name + * @param int $uid User id + * @param string $filename Optional file name * @param string $allow_cid * @param string $allow_gid * @param string $deny_cid * @param string $deny_gid - * @return boolean True on success + * @return boolean|int Insert id or false on failure * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') + public static function storeFile(string $src, int $uid, string $filename = '', string $allow_cid = '', string $allow_gid = '', string $deny_cid = '', string $deny_gid = '') { if ($filename === '') { $filename = basename($src); @@ -276,7 +276,7 @@ class Attach * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @see \Friendica\Database\DBA::update */ - public static function update($fields, $conditions, Image $img = null, array $old_fields = []) + public static function update(array $fields, array $conditions, Image $img = null, array $old_fields = []): bool { if (!is_null($img)) { // get items to update @@ -311,7 +311,7 @@ class Attach * @throws \Exception * @see \Friendica\Database\DBA::delete */ - public static function delete(array $conditions, array $options = []) + public static function delete(array $conditions, array $options = []): bool { // get items to delete data info $items = self::selectToArray(['backend-class','backend-ref'], $conditions); diff --git a/src/Model/Conversation.php b/src/Model/Conversation.php index 1403b30be2..7d8b8058f1 100644 --- a/src/Model/Conversation.php +++ b/src/Model/Conversation.php @@ -62,7 +62,7 @@ class Conversation */ const RELAY = 3; - public static function getByItemUri($item_uri) + public static function getByItemUri(string $item_uri) { return DBA::selectFirst('conversation', [], ['item-uri' => $item_uri]); } @@ -74,7 +74,7 @@ class Conversation * @return array Item array with removed conversation data * @throws \Exception */ - public static function insert(array $arr) + public static function insert(array $arr): array { if (in_array(($arr['network'] ?? '') ?: Protocol::PHANTOM, [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::TWITTER]) && !empty($arr['uri'])) { diff --git a/src/Model/Event.php b/src/Model/Event.php index dac00ceceb..f63ec3d559 100644 --- a/src/Model/Event.php +++ b/src/Model/Event.php @@ -41,7 +41,7 @@ use Friendica\Util\XML; class Event { - public static function getHTML(array $event, $simple = false, $uriid = 0) + public static function getHTML(array $event, bool $simple = false, int $uriid = 0): string { if (empty($event)) { return ''; @@ -127,7 +127,7 @@ class Event * @param array $event Array which contains the event data. * @return string The event as a bbcode formatted string. */ - private static function getBBCode(array $event) + private static function getBBCode(array $event): string { $o = ''; @@ -157,11 +157,10 @@ class Event /** * Extract bbcode formatted event data from a string. * - * @params: string $s The string which should be parsed for event data. - * @param $text + * @param string $text The string which should be parsed for event data. * @return array The array with the event information. */ - public static function fromBBCode($text) + public static function fromBBCode(string $text): array { $ev = []; @@ -195,13 +194,13 @@ class Event return $ev; } - public static function sortByDate($event_list) + public static function sortByDate(array $event_list): array { usort($event_list, ['self', 'compareDatesCallback']); return $event_list; } - private static function compareDatesCallback($event_a, $event_b) + private static function compareDatesCallback(array $event_a, array $event_b) { $date_a = DateTimeFormat::local($event_a['start']); $date_b = DateTimeFormat::local($event_b['start']); @@ -223,7 +222,7 @@ class Event * @return void * @throws \Exception */ - public static function delete($event_id) + public static function delete(int $event_id) { if ($event_id == 0) { return; @@ -242,7 +241,7 @@ class Event * @return int The new event id. * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function store($arr) + public static function store(array $arr): int { $event = []; $event['id'] = intval($arr['id'] ?? 0); @@ -317,7 +316,7 @@ class Event return $event['id']; } - public static function getItemArrayForId(int $event_id, array $item = []):array + public static function getItemArrayForId(int $event_id, array $item = []): array { if (empty($event_id)) { return $item; @@ -374,7 +373,7 @@ class Event return $item; } - public static function getItemArrayForImportedId(int $event_id, array $item = []):array + public static function getItemArrayForImportedId(int $event_id, array $item = []): array { if (empty($event_id)) { return $item; @@ -404,7 +403,7 @@ class Event * @return array Array with translations strings. * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function getStrings() + public static function getStrings(): array { // First day of the week (0 = Sunday). $firstDay = DI::pConfig()->get(local_user(), 'system', 'first_day_of_week', 0); @@ -477,7 +476,7 @@ class Event * * @todo We should replace this with a separate update function if there is some time left. */ - private static function removeDuplicates(array $dates) + private static function removeDuplicates(array $dates): array { $dates2 = []; @@ -500,7 +499,7 @@ class Event * @return array Query result * @throws \Exception */ - public static function getListById($owner_uid, $event_id, $sql_extra = '') + public static function getListById(int $owner_uid, int $event_id, string $sql_extra = ''): array { $return = []; @@ -536,7 +535,7 @@ class Event * @return array Query results. * @throws \Exception */ - public static function getListByDate($owner_uid, $event_params, $sql_extra = '') + public static function getListByDate(int $owner_uid, array $event_params, string $sql_extra = ''): array { $return = []; @@ -570,7 +569,7 @@ class Event * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function prepareListForTemplate(array $event_result) + public static function prepareListForTemplate(array $event_result): array { $event_list = []; @@ -651,12 +650,12 @@ class Event * @param array $events Query result for events. * @param string $format The output format (ical/csv). * - * @param $timezone + * @param string $timezone Timezone (missing parameter!) * @return string Content according to selected export format. * * @todo Implement timezone support */ - private static function formatListForExport(array $events, $format) + private static function formatListForExport(array $events, string $format): string { $o = ''; @@ -757,7 +756,7 @@ class Event * @return array Query results. * @throws \Exception */ - private static function getListByUserId($uid = 0) + private static function getListByUserId(int $uid = 0): array { $return = []; @@ -797,7 +796,7 @@ class Event * @throws \Exception * @todo Respect authenticated users with events_by_uid(). */ - public static function exportListByUserId($uid, $format = 'ical') + public static function exportListByUserId(int $uid, string $format = 'ical'): array { $process = false; @@ -845,7 +844,8 @@ class Event * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function getItemHTML(array $item) { + public static function getItemHTML(array $item): string + { $same_date = false; $finish = false; @@ -933,10 +933,11 @@ class Event * @return array The array with the location data. * 'name' => The name of the location,
* 'address' => The address of the location,
- * 'coordinates' => Latitude‎ and longitude‎ (e.g. '48.864716,2.349014').
+ * 'coordinates' => Latitude and longitude (e.g. '48.864716,2.349014').
* @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - private static function locationToArray($s = '') { + private static function locationToArray(string $s = ''): array + { if ($s == '') { return []; } @@ -981,7 +982,7 @@ class Event * @return bool * @throws \Exception */ - public static function createBirthday($contact, $birthday) + public static function createBirthday(array $contact, string $birthday): bool { // Check for duplicates $condition = [ @@ -1011,8 +1012,7 @@ class Event 'type' => 'birthday', ]; - self::store($values); - - return true; + // Check if self::store() was success + return (self::store($values) > 0); } }