From 143e4c4a18a7d64e50bd43b5b6cf3a385d61431f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Wed, 8 Jun 2022 17:26:47 +0200 Subject: [PATCH 1/4] Added more known type-hints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- src/Content/Text/HTML.php | 35 +++++++++++++++++------------------ src/Model/Item.php | 2 +- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/Content/Text/HTML.php b/src/Content/Text/HTML.php index ae9452abb..9a5b90925 100644 --- a/src/Content/Text/HTML.php +++ b/src/Content/Text/HTML.php @@ -61,7 +61,7 @@ class HTML * inner value from an attribute value and disregard the tag children. * @return bool Whether a replacement was done */ - private static function tagToBBCodeSub(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false) + private static function tagToBBCodeSub(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false): bool { $savestart = str_replace('$', '\x01', $startbb); $replace = false; @@ -141,7 +141,7 @@ class HTML * @return string * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function toBBCode($message, $basepath = '') + public static function toBBCode(string $message, string $basepath = ''): string { DI::profiler()->startRecording('rendering'); $message = str_replace("\r", "", $message); @@ -409,7 +409,7 @@ class HTML * * @return string The expanded URL */ - private static function qualifyURLsSub($matches, $basepath) + private static function qualifyURLsSub(array $matches, string $basepath): string { $base = parse_url($basepath); unset($base['query']); @@ -436,7 +436,7 @@ class HTML * * @return string Body with expanded URLs */ - private static function qualifyURLs($body, $basepath) + private static function qualifyURLs(string $body, string $basepath): string { $URLSearchString = "^\[\]"; @@ -462,7 +462,7 @@ class HTML return $body; } - private static function breakLines($line, $level, $wraplength = 75) + private static function breakLines(string $line, int $level, int $wraplength = 75): string { if ($wraplength == 0) { $wraplength = 2000000; @@ -503,7 +503,7 @@ class HTML return implode("\n", $newlines); } - private static function quoteLevel($message, $wraplength = 75) + private static function quoteLevel(string $message, int $wraplength = 75): string { $lines = explode("\n", $message); @@ -539,7 +539,7 @@ class HTML return implode("\n", $newlines); } - private static function collectURLs($message) + private static function collectURLs(string $message): array { $pattern = '/(.*?)<\/a>/is'; preg_match_all($pattern, $message, $result, PREG_SET_ORDER); @@ -585,7 +585,7 @@ class HTML * @param bool $compact True: Completely strips image tags; False: Keeps image URLs * @return string */ - public static function toPlaintext(string $html, $wraplength = 75, $compact = false) + public static function toPlaintext(string $html, int $wraplength = 75, bool $compact = false): string { DI::profiler()->startRecording('rendering'); $message = str_replace("\r", "", $html); @@ -705,7 +705,7 @@ class HTML * @param string $html * @return string */ - public static function toMarkdown($html) + public static function toMarkdown(string $html): string { DI::profiler()->startRecording('rendering'); $converter = new HtmlConverter(['hard_break' => true]); @@ -721,7 +721,7 @@ class HTML * @param string $s * @return string */ - public static function toBBCodeVideo($s) + public static function toBBCodeVideo(string $s): string { $s = preg_replace( '#]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)#ism', @@ -751,7 +751,7 @@ class HTML * @param string $base base url * @return string */ - public static function relToAbs($text, $base) + public static function relToAbs(string $text, string $base): string { if (empty($base)) { return $text; @@ -790,7 +790,7 @@ class HTML * @return string html for loader * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function scrollLoader() + public static function scrollLoader(): string { $tpl = Renderer::getMarkupTemplate("scroll_loader.tpl"); return Renderer::replaceMacros($tpl, [ @@ -819,7 +819,7 @@ class HTML * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function micropro($contact, $redirect = false, $class = '', $textmode = false) + public static function micropro(array $contact, bool $redirect = false, string $class = '', bool $textmode = false): string { // Use the contact URL if no address is available if (empty($contact['addr'])) { @@ -859,13 +859,12 @@ class HTML * * @param string $s Search query. * @param string $id HTML id - * @param string $url Search url. * @param bool $aside Display the search widgit aside. * * @return string Formatted HTML. * @throws \Exception */ - public static function search($s, $id = 'search-box', $aside = true) + public static function search(string $s, string $id = 'search-box', bool $aside = true): string { $mode = 'text'; @@ -906,7 +905,7 @@ class HTML * @param string $s * @return string */ - public static function toLink($s) + public static function toLink(string $s): string { $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' $1', $s); $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism", '<$1$2=$3&$4>', $s); @@ -923,7 +922,7 @@ class HTML * @return string * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function applyContentFilter($html, array $reasons) + public static function applyContentFilter(string $html, array $reasons): string { if (count($reasons)) { $tpl = Renderer::getMarkupTemplate('wall/content_filter.tpl'); @@ -943,7 +942,7 @@ class HTML * @param string $s * @return string */ - public static function unamp($s) + public static function unamp(string $s): string { return str_replace('&', '&', $s); } diff --git a/src/Model/Item.php b/src/Model/Item.php index 4b42ad0e1..6009ea406 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2769,7 +2769,7 @@ class Item * @hook prepare_body ('item'=>item array, 'html'=>body string, 'is_preview'=>boolean, 'filter_reasons'=>string array) after first bbcode to html * @hook prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author) */ - public static function prepareBody(array &$item, $attach = false, $is_preview = false, $only_cache = false) + public static function prepareBody(array &$item, bool $attach = false, bool $is_preview = false, bool $only_cache = false): string { $a = DI::app(); Hook::callAll('prepare_body_init', $item); From 34aee64349de8311d028884b585b3cba93390eb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Tue, 14 Jun 2022 09:08:09 +0200 Subject: [PATCH 2/4] Added more type-hints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- src/Model/Item.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 6009ea406..387bd1118 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2891,7 +2891,7 @@ class Item * @param int $type * @return bool */ - public static function containsLink(string $body, string $url, int $type = 0) + public static function containsLink(string $body, string $url, int $type = 0): bool { // Make sure that for example site parameters aren't used when testing if the link is contained in the body $urlparts = parse_url($url); @@ -2924,7 +2924,7 @@ class Item * @param string $body * @return string modified body */ - private static function replaceVisualAttachments(array $attachments, string $body) + private static function replaceVisualAttachments(array $attachments, string $body): string { DI::profiler()->startRecording('rendering'); @@ -2955,7 +2955,7 @@ class Item * @param string $content * @return string modified content */ - private static function addVisualAttachments(array $attachments, array $item, string $content, bool $shared) + private static function addVisualAttachments(array $attachments, array $item, string $content, bool $shared): string { DI::profiler()->startRecording('rendering'); $leading = ''; @@ -3047,7 +3047,7 @@ class Item * @param array $ignore_links A list of URLs to ignore * @return string modified content */ - private static function addLinkAttachment(int $uriid, array $attachments, string $body, string $content, bool $shared, array $ignore_links) + private static function addLinkAttachment(int $uriid, array $attachments, string $body, string $content, bool $shared, array $ignore_links): string { DI::profiler()->startRecording('rendering'); // Don't show a preview when there is a visual attachment (audio or video) @@ -3161,7 +3161,7 @@ class Item * @param string $content * @return string modified content */ - private static function addNonVisualAttachments(array $attachments, array $item, string $content) + private static function addNonVisualAttachments(array $attachments, array $item, string $content): string { DI::profiler()->startRecording('rendering'); $trailing = ''; @@ -3244,7 +3244,7 @@ class Item * @return boolean|array False if item has not plink, otherwise array('href'=>plink url, 'title'=>translated title) * @throws \Exception */ - public static function getPlink($item) + public static function getPlink(array $item) { if (!empty($item['plink']) && Network::isValidHttpUrl($item['plink'])) { $plink = $item['plink']; @@ -3291,7 +3291,7 @@ class Item * * @return boolean "true" when it is a forum post */ - public static function isForumPost(int $uri_id) + public static function isForumPost(int $uri_id): bool { foreach (Tag::getByURIId($uri_id, [Tag::EXCLUSIVE_MENTION]) as $tag) { if (DBA::exists('contact', ['uid' => 0, 'nurl' => Strings::normaliseLink($tag['url']), 'contact-type' => Contact::TYPE_COMMUNITY])) { @@ -3309,7 +3309,7 @@ class Item * * @return integer item id */ - public static function searchByLink($uri, $uid = 0) + public static function searchByLink(string $uri, int $uid = 0): int { $ssl_uri = str_replace('http://', 'https://', $uri); $uris = [$uri, $ssl_uri, Strings::normaliseLink($uri)]; @@ -3360,7 +3360,7 @@ class Item * * @return integer item id */ - public static function fetchByLink(string $uri, int $uid = 0) + public static function fetchByLink(string $uri, int $uid = 0): int { Logger::info('Trying to fetch link', ['uid' => $uid, 'uri' => $uri]); $item_id = self::searchByLink($uri, $uid); @@ -3406,7 +3406,7 @@ class Item * * @return array with share information */ - public static function getShareArray($item) + public static function getShareArray(array $item): array { if (!preg_match("/(.*?)\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", $item['body'], $matches)) { return []; @@ -3429,7 +3429,7 @@ class Item * * @return array item array with data from the original item */ - public static function addShareDataFromOriginal(array $item) + public static function addShareDataFromOriginal(array $item): array { $shared = self::getShareArray($item); if (empty($shared)) { @@ -3490,7 +3490,7 @@ class Item * @return bool * @throws \Exception */ - protected static function isAllowedByUser(array $item, int $user_id) + protected static function isAllowedByUser(array $item, int $user_id): bool { if (!empty($item['author-id']) && Contact\User::isBlocked($item['author-id'], $user_id)) { Logger::notice('Author is blocked by user', ['author-link' => $item['author-link'], 'uid' => $user_id, 'item-uri' => $item['uri']]); From a3fa95e8e49c6d3b08212a883499b301a972db94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Wed, 15 Jun 2022 22:03:13 +0200 Subject: [PATCH 3/4] Changes: - added more type-hints - changed `return;` to `return '';` when `string` was requested as returned type-hint (thanks to @MrPetovan) --- src/Model/Item.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 387bd1118..eb92dcac7 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -255,7 +255,7 @@ class Item * @param integer $uid User who wants to delete this item * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function deleteForUser($condition, $uid) + public static function deleteForUser(array $condition, int $uid) { if ($uid == 0) { return; @@ -1323,7 +1323,7 @@ class Item * @param string $signed_text Original text (for Diaspora signatures), JSON encoded. * @throws \Exception */ - public static function distribute($itemid, $signed_text = '') + public static function distribute(int $itemid, string $signed_text = '') { $condition = ["`id` IN (SELECT `parent` FROM `post-user-view` WHERE `id` = ?)", $itemid]; $parent = Post::selectFirst(['owner-id'], $condition); @@ -1613,7 +1613,7 @@ class Item * @param integer $itemid Item ID that should be added * @throws \Exception */ - private static function addShadow($itemid) + private static function addShadow(int $itemid) { $fields = ['uid', 'private', 'visible', 'deleted', 'network', 'uri-id']; $condition = ['id' => $itemid, 'gravity' => GRAVITY_PARENT]; @@ -1676,7 +1676,7 @@ class Item * @param integer $itemid Item ID that should be added * @throws \Exception */ - private static function addShadowPost($itemid) + private static function addShadowPost(int $itemid) { $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $itemid]); if (!DBA::isResult($item)) { @@ -2066,7 +2066,7 @@ class Item self::performActivity($item['id'], 'announce', $item['uid']); } - public static function isRemoteSelf($contact, &$datarray) + public static function isRemoteSelf(array $contact, array &$datarray) { if (!$contact['remote_self']) { return false; @@ -2160,7 +2160,7 @@ class Item * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function fixPrivatePhotos($s, $uid, $item = null, $cid = 0) + public static function fixPrivatePhotos(string $s, int $uid, array $item = null, int $cid = 0) { if (DI::config()->get('system', 'disable_embedded')) { return $s; @@ -2254,7 +2254,7 @@ class Item return $new_body; } - private static function hasPermissions($obj) + private static function hasPermissions(array $obj) { return !empty($obj['allow_cid']) || !empty($obj['allow_gid']) || !empty($obj['deny_cid']) || !empty($obj['deny_gid']); @@ -2288,7 +2288,7 @@ class Item * @return array * @throws \Exception */ - public static function enumeratePermissions(array $obj, bool $check_dead = false) + public static function enumeratePermissions(array $obj, bool $check_dead = false): array { $aclFormater = DI::aclFormatter(); @@ -3193,7 +3193,7 @@ class Item return $content; } - private static function addQuestions(array $item, string $content) + private static function addQuestions(array $item, string $content): string { DI::profiler()->startRecording('rendering'); if (!empty($item['question-id'])) { @@ -3334,7 +3334,7 @@ class Item * * @return string URI */ - public static function getURIByLink(string $uri) + public static function getURIByLink(string $uri): string { $ssl_uri = str_replace('http://', 'https://', $uri); $uris = [$uri, $ssl_uri, Strings::normaliseLink($uri)]; @@ -3522,7 +3522,7 @@ class Item * @param array $item * @return string body */ - public static function improveSharedDataInBody(array $item) + public static function improveSharedDataInBody(array $item): string { $shared = BBCode::fetchShareAttributes($item['body']); if (empty($shared['link'])) { From 5fa954208a5a6652f08c6d7c94328dc8498d6f94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Wed, 15 Jun 2022 22:28:33 +0200 Subject: [PATCH 4/4] Changes: - ops, wasn't actually fixing `return;` to proper `return '';` - added more type-hints - added TODO ($uid is unused) --- src/Model/Item.php | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index eb92dcac7..51f879d4d 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -709,7 +709,7 @@ class Item * @param array $item * @return integer gravity */ - private static function getGravity(array $item) + private static function getGravity(array $item): int { $activity = DI::activity(); @@ -1417,7 +1417,7 @@ class Item * @param integer $source_uid User id of the source post * @return integer stored item id */ - public static function storeForUserByUriId(int $uri_id, int $uid, array $fields = [], int $source_uid = 0) + public static function storeForUserByUriId(int $uri_id, int $uid, array $fields = [], int $source_uid = 0): int { if ($uid == $source_uid) { Logger::warning('target UID must not be be equal to the source UID', ['uri-id' => $uri_id, 'uid' => $uid]); @@ -1525,7 +1525,7 @@ class Item * @return integer stored item id * @throws \Exception */ - private static function storeForUser(array $item, int $uid) + private static function storeForUser(array $item, int $uid): int { if (Post::exists(['uri-id' => $item['uri-id'], 'uid' => $uid])) { if (!empty($item['event-id'])) { @@ -1740,7 +1740,7 @@ class Item * @return string detected language * @throws \Text_LanguageDetect_Exception */ - private static function getLanguage(array $item) + private static function getLanguage(array $item): string { if (!empty($item['language'])) { return $item['language']; @@ -1784,7 +1784,7 @@ class Item return ''; } - public static function getLanguageMessage(array $item) + public static function getLanguageMessage(array $item): string { $iso639 = new \Matriphe\ISO639\ISO639; @@ -1806,7 +1806,7 @@ class Item * @param string $host hostname for the GUID prefix * @return string unique guid */ - public static function guidFromUri($uri, $host) + public static function guidFromUri(string $uri, string $host): string { // Our regular guid routine is using this kind of prefix as well // We have to avoid that different routines could accidentally create the same value @@ -1850,7 +1850,7 @@ class Item * @param array $arr Contains the just posted item record * @throws \Exception */ - private static function updateContact($arr) + private static function updateContact(array $arr) { // Unarchive the author $contact = DBA::selectFirst('contact', [], ['id' => $arr["author-id"]]); @@ -1897,7 +1897,7 @@ class Item } } - public static function setHashtags($body) + public static function setHashtags(string $body): string { $body = BBCode::performWithEscapedTags($body, ['noparse', 'pre', 'code', 'img'], function ($body) { $tags = BBCode::getTags($body); @@ -1971,7 +1971,7 @@ class Item * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - private static function tagDeliver($uid, $item_id) + private static function tagDeliver(int $uid, int $item_id): bool { $mention = false; @@ -2066,7 +2066,7 @@ class Item self::performActivity($item['id'], 'announce', $item['uid']); } - public static function isRemoteSelf(array $contact, array &$datarray) + public static function isRemoteSelf(array $contact, array &$datarray): bool { if (!$contact['remote_self']) { return false; @@ -2160,7 +2160,7 @@ class Item * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function fixPrivatePhotos(string $s, int $uid, array $item = null, int $cid = 0) + public static function fixPrivatePhotos(string $s, int $uid, array $item = null, int $cid = 0): string { if (DI::config()->get('system', 'disable_embedded')) { return $s; @@ -2260,7 +2260,8 @@ class Item !empty($obj['deny_cid']) || !empty($obj['deny_gid']); } - private static function samePermissions($uid, $obj1, $obj2) + // @TODO $uid is unused parameter + private static function samePermissions($uid, array $obj1, array $obj2): bool { // first part is easy. Check that these are exactly the same. if (($obj1['allow_cid'] == $obj2['allow_cid']) @@ -2376,7 +2377,7 @@ class Item Logger::notice('User ' . $uid . ": expired $expired items; expire items: $expire_items, expire notes: $expire_notes, expire starred: $expire_starred, expire photos: $expire_photos"); } - public static function firstPostDate($uid, $wall = false) + public static function firstPostDate(int $uid, bool $wall = false) { $user = User::getById($uid, ['register_date']); if (empty($user)) { @@ -2417,7 +2418,7 @@ class Item * array $arr * 'post_id' => ID of posted item */ - public static function performActivity(int $item_id, string $verb, int $uid, string $allow_cid = null, string $allow_gid = null, string $deny_cid = null, string $deny_gid = null) + public static function performActivity(int $item_id, string $verb, int $uid, string $allow_cid = null, string $allow_gid = null, string $deny_cid = null, string $deny_gid = null): bool { if (empty($uid)) { return false; @@ -2611,7 +2612,7 @@ class Item * @param integer $owner_id User ID for which the permissions should be fetched * @return array condition */ - public static function getPermissionsConditionArrayByUserId(int $owner_id) + public static function getPermissionsConditionArrayByUserId(int $owner_id): array { $local_user = local_user(); $remote_user = Session::getRemoteContactID($owner_id); @@ -2643,7 +2644,7 @@ class Item * @param string $table * @return string */ - public static function getPermissionsSQLByUserId(int $owner_id, string $table = '') + public static function getPermissionsSQLByUserId(int $owner_id, string $table = ''): string { $local_user = local_user(); $remote_user = Session::getRemoteContactID($owner_id); @@ -2691,7 +2692,7 @@ class Item * @param \Friendica\Core\L10n $l10n * @return string */ - public static function postType(array $item, \Friendica\Core\L10n $l10n) + public static function postType(array $item, \Friendica\Core\L10n $l10n): string { if (!empty($item['event-id'])) { return $l10n->t('event'); @@ -2757,10 +2758,10 @@ class Item * Given an item array, convert the body element from bbcode to html and add smilie icons. * If attach is true, also add icons for item attachments. * - * @param array $item - * @param boolean $attach - * @param boolean $is_preview - * @param boolean $only_cache + * @param array $item Record from item table + * @param boolean $attach If true, add icons for item attachments as well + * @param boolean $is_preview Whether this is a preview + * @param boolean $only_cache Whether only cached HTML should be updated * @return string item body html * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException @@ -2811,7 +2812,7 @@ class Item $s = $item["rendered-html"]; if ($only_cache) { - return; + return ''; } // Compile eventual content filter reasons