- renamed Item::visibleActivity() to Item::isVisibleActivity() as this returns
  a boolean value
- added some type-hints
- added some documentation
This commit is contained in:
Roland Häder 2022-06-30 14:16:30 +02:00
parent e33f5612ab
commit 83cbe586ac
Signed by: roland
GPG key ID: C82EDE5DDFA0BA77
9 changed files with 159 additions and 114 deletions

View file

@ -582,7 +582,7 @@ class Conversation
$uriids[] = $item['uri-id'];
if (!$this->item->visibleActivity($item)) {
if (!$this->item->isVisibleActivity($item)) {
continue;
}
@ -745,7 +745,7 @@ class Conversation
continue;
}
if (!$this->item->visibleActivity($item)) {
if (!$this->item->isVisibleActivity($item)) {
continue;
}

View file

@ -85,7 +85,7 @@ class Item
* ]
* ]
*/
public function determineCategoriesTerms(array $item, int $uid = 0)
public function determineCategoriesTerms(array $item, int $uid = 0): array
{
$categories = [];
$folders = [];
@ -141,16 +141,16 @@ class Item
* This function removes the tag $tag from the text $body and replaces it with
* the appropriate link.
*
* @param string $body the text to replace the tag in
* @param integer $profile_uid the user id to replace the tag for (0 = anyone)
* @param string $tag the tag to replace
* @param string $network The network of the post
* @param string $body the text to replace the tag in
* @param int $profile_uid the user id to replace the tag for (0 = anyone)
* @param string $tag the tag to replace
* @param string $network The network of the post
*
* @return array|bool ['replaced' => $replaced, 'contact' => $contact];
* @return array|bool ['replaced' => $replaced, 'contact' => $contact] or "false" on if already replaced
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function replaceTag(&$body, $profile_uid, $tag, $network = '')
public static function replaceTag(string &$body, int $profile_uid, string $tag, string $network = '')
{
$replaced = false;
@ -244,16 +244,17 @@ class Item
/**
* Render actions localized
*
* @param $item
* @param array $item
* @return void
* @throws ImagickException
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function localize(&$item)
public function localize(array &$item)
{
$this->profiler->startRecording('rendering');
/// @todo The following functionality needs to be cleaned up.
if (!empty($item['verb'])) {
$xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
$xmlhead = '<?xml version="1.0" encoding="UTF-8" ?>';
if (stristr($item['verb'], Activity::POKE)) {
$verb = urldecode(substr($item['verb'], strpos($item['verb'],'#') + 1));
@ -261,7 +262,7 @@ class Item
$this->profiler->stopRecording();
return;
}
if ($item['object-type'] == "" || $item['object-type'] !== Activity\ObjectType::PERSON) {
if ($item['object-type'] == '' || $item['object-type'] !== Activity\ObjectType::PERSON) {
$this->profiler->stopRecording();
return;
}
@ -270,18 +271,22 @@ class Item
$Bname = $obj->title;
$Blink = $obj->id;
$Bphoto = "";
$Bphoto = '';
foreach ($obj->link as $l) {
$atts = $l->attributes();
switch ($atts['rel']) {
case "alternate": $Blink = $atts['href'];
case "photo": $Bphoto = $atts['href'];
case 'alternate': $Blink = $atts['href'];
case 'photo': $Bphoto = $atts['href'];
}
}
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$author = [
'uid' => 0,
'id' => $item['author-id'],
'network' => $item['author-network'],
'url' => $item['author-link'],
];
$A = '[url=' . Contact::magicLinkByContact($author) . ']' . $item['author-name'] . '[/url]';
if (!empty($Blink)) {
@ -290,7 +295,7 @@ class Item
$B = '';
}
if ($Bphoto != "" && !empty($Blink)) {
if ($Bphoto != '' && !empty($Blink)) {
$Bphoto = '[url=' . Contact::magicLink($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
}
@ -305,9 +310,7 @@ class Item
$txt = str_replace($poked_t, $this->l10n->t($verb), $txt);
// then do the sprintf on the translation string
$item['body'] = sprintf($txt, $A, $B) . "\n\n\n" . $Bphoto;
}
if ($this->activity->match($item['verb'], Activity::TAG)) {
@ -319,12 +322,20 @@ class Item
return;
}
$author_arr = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$author_arr = [
'uid' => 0,
'id' => $item['author-id'],
'network' => $item['author-network'],
'url' => $item['author-link'],
];
$author = '[url=' . Contact::magicLinkByContact($author_arr) . ']' . $item['author-name'] . '[/url]';
$author_arr = ['uid' => 0, 'id' => $obj['author-id'],
'network' => $obj['author-network'], 'url' => $obj['author-link']];
$author_arr = [
'uid' => 0,
'id' => $obj['author-id'],
'network' => $obj['author-network'],
'url' => $obj['author-link'],
];
$objauthor = '[url=' . Contact::magicLinkByContact($author_arr) . ']' . $obj['author-name'] . '[/url]';
switch ($obj['verb']) {
@ -337,6 +348,7 @@ class Item
$post_type = $this->l10n->t('status');
}
break;
default:
if ($obj['resource-id']) {
$post_type = $this->l10n->t('photo');
@ -360,25 +372,29 @@ class Item
$this->profiler->stopRecording();
}
public function photoMenu($item, string $formSecurityToken)
/**
* Renders photo menu based on item
*
* @param array $item
* @param string $formSecurityToken
* @return string
*/
public function photoMenu(array $item, string $formSecurityToken): string
{
$this->profiler->startRecording('rendering');
$sub_link = '';
$poke_link = '';
$contact_url = '';
$pm_url = '';
$status_link = '';
$photos_link = '';
$posts_link = '';
$block_link = '';
$ignore_link = '';
$sub_link = $poke_link = $contact_url = $pm_url = $status_link = '';
$photos_link = $posts_link = $block_link = $ignore_link = '';
if (local_user() && local_user() == $item['uid'] && $item['gravity'] == GRAVITY_PARENT && !$item['self'] && !$item['mention']) {
$sub_link = 'javascript:doFollowThread(' . $item['id'] . '); return false;';
}
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$author = [
'uid' => 0,
'id' => $item['author-id'],
'network' => $item['author-network'],
'url' => $item['author-link'],
];
$profile_link = Contact::magicLinkByContact($author, $item['author-link']);
$sparkle = (strpos($profile_link, 'redir/') === 0);
@ -435,7 +451,7 @@ class Item
}
if ($network == Protocol::DFRN) {
$menu[$this->l10n->t("Poke")] = $poke_link;
$menu[$this->l10n->t('Poke')] = $poke_link;
}
if ((($cid == 0) || ($rel == Contact::FOLLOWER)) &&
@ -465,24 +481,28 @@ class Item
return $o;
}
public function visibleActivity($item) {
/**
* Checks if the activity is visible to current user
*
* @param array $item Activity item
* @return bool Whether the item is visible to the user
*/
public function isVisibleActivity(array $item): bool
{
// Empty verb or hidden?
if (empty($item['verb']) || $this->activity->isHidden($item['verb'])) {
return false;
}
// @TODO below if() block can be rewritten to a single line: $isVisible = allConditionsHere;
if ($this->activity->match($item['verb'], Activity::FOLLOW) &&
// Check conditions
return (!($this->activity->match($item['verb'], Activity::FOLLOW) &&
$item['object-type'] === Activity\ObjectType::NOTE &&
empty($item['self']) &&
$item['uid'] == local_user()) {
return false;
}
return true;
$item['uid'] == local_user())
);
}
public function expandTags(array $item, bool $setPermissions = false)
public function expandTags(array $item, bool $setPermissions = false): array
{
// Look for any tags and linkify them
$item['inform'] = '';

View file

@ -62,7 +62,7 @@ class Nav
*
* @param string $item
*/
public static function setSelected($item)
public static function setSelected(string $item)
{
self::$selected[$item] = 'selected';
}
@ -74,7 +74,7 @@ class Nav
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function build(App $a)
public static function build(App $a): string
{
// Placeholder div for popup panel
$nav = '<div id="panel" style="display: none;"></div>';
@ -106,7 +106,7 @@ class Nav
*
* @return array
*/
public static function getAppMenu()
public static function getAppMenu(): array
{
if (is_null(self::$app_menu)) {
self::populateAppMenu();
@ -117,6 +117,8 @@ class Nav
/**
* Fills the apps static variable with apps that require a menu
*
* @return void
*/
private static function populateAppMenu()
{

View file

@ -49,7 +49,13 @@ use Friendica\Util\Strings;
*/
class OEmbed
{
public static function replaceCallback($matches)
/**
* Callback for fetching URL, checking allowance and returning formatted HTML
*
* @param array $matches
* @return string Formatted HTML
*/
public static function replaceCallback(array $matches): string
{
$embedurl = $matches[1];
$j = self::fetchURL($embedurl, !self::isAllowedURL($embedurl));
@ -68,7 +74,7 @@ class OEmbed
* @return \Friendica\Object\OEmbed
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function fetchURL($embedurl, bool $no_rich_type = false, bool $use_parseurl = true)
public static function fetchURL(string $embedurl, bool $no_rich_type = false, bool $use_parseurl = true): \Friendica\Object\OEmbed
{
$embedurl = trim($embedurl, '\'"');
@ -209,12 +215,18 @@ class OEmbed
return $oembed;
}
private static function formatObject(\Friendica\Object\OEmbed $oembed)
/**
* Returns a formatted string from OEmbed object
*
* @param \Friendica\Object\OEmbed $oembed
* @return string
*/
private static function formatObject(\Friendica\Object\OEmbed $oembed): string
{
$ret = '<div class="oembed ' . $oembed->type . '">';
switch ($oembed->type) {
case "video":
case 'video':
if ($oembed->thumbnail_url) {
$tw = (isset($oembed->thumbnail_width) && intval($oembed->thumbnail_width)) ? $oembed->thumbnail_width : 200;
$th = (isset($oembed->thumbnail_height) && intval($oembed->thumbnail_height)) ? $oembed->thumbnail_height : 180;
@ -236,14 +248,14 @@ class OEmbed
}
break;
case "photo":
case 'photo':
$ret .= '<img width="' . $oembed->width . '" src="' . Proxy::proxifyUrl($oembed->url) . '">';
break;
case "link":
case 'link':
break;
case "rich":
case 'rich':
$ret .= Proxy::proxifyHtml($oembed->html);
break;
}
@ -292,9 +304,15 @@ class OEmbed
return str_replace("\n", "", $ret);
}
public static function BBCode2HTML($text)
/**
* Converts BBCode to HTML code
*
* @param string $text
* @return string
*/
public static function BBCode2HTML(string $text): string
{
$stopoembed = DI::config()->get("system", "no_oembed");
$stopoembed = DI::config()->get('system', 'no_oembed');
if ($stopoembed == true) {
return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>" . DI::l10n()->t('Embedding disabled') . " : $1</i><!-- /oembed $1 -->", $text);
}
@ -305,14 +323,13 @@ class OEmbed
* Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
* and replace it with [embed]url[/embed]
*
* @param $text
* @param string $text
* @return string
*/
public static function HTML2BBCode($text)
public static function HTML2BBCode(string $text): string
{
// start parser only if 'oembed' is in text
if (strpos($text, "oembed")) {
if (strpos($text, 'oembed')) {
// convert non ascii chars to html entities
$html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
@ -323,17 +340,17 @@ class OEmbed
}
$xpath = new DOMXPath($dom);
$xattr = self::buildXPath("class", "oembed");
$xattr = self::buildXPath('class', 'oembed');
$entries = $xpath->query("//div[$xattr]");
$xattr = "@rel='oembed'"; //oe_build_xpath("rel","oembed");
foreach ($entries as $e) {
$href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
if (!is_null($href)) {
$e->parentNode->replaceChild(new DOMText("[embed]" . $href . "[/embed]"), $e);
$e->parentNode->replaceChild(new DOMText('[embed]' . $href . '[/embed]'), $e);
}
}
return self::getInnerHTML($dom->getElementsByTagName("body")->item(0));
return self::getInnerHTML($dom->getElementsByTagName('body')->item(0));
} else {
return $text;
}
@ -346,7 +363,7 @@ class OEmbed
* @return boolean
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function isAllowedURL($url)
public static function isAllowedURL(string $url): bool
{
if (!DI::config()->get('system', 'no_oembed_rich_content')) {
return true;
@ -367,7 +384,14 @@ class OEmbed
return Network::isDomainAllowed($domain, $allowed);
}
public static function getHTML($url, $title = null)
/**
* Returns a formmated HTML code from given URL and sets optional title
*
* @param string $url URL to fetch
* @param string $title Optional title (default: what comes from OEmbed object)
* @return string Formatted HTML
*/
public static function getHTML(string $url, string $title = '')
{
$o = self::fetchURL($url, !self::isAllowedURL($url));
@ -401,12 +425,12 @@ class OEmbed
* @param string $src Original remote URL to embed
* @param string $width
* @param string $height
* @return string formatted HTML
* @return string Formatted HTML
*
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @see oembed_format_object()
*/
private static function iframe($src, $width, $height)
private static function iframe(string $src, string $width, string $height): string
{
if (!$height || strstr($height, '%')) {
$height = '200';
@ -427,7 +451,7 @@ class OEmbed
* @param string $value Value to search in a space-separated list
* @return string
*/
private static function buildXPath($attr, $value)
private static function buildXPath(string $attr, $value): string
{
// https://www.westhoffswelt.de/blog/2009/6/9/select-html-elements-with-more-than-one-css-class-using-xpath
return "contains(normalize-space(@$attr), ' $value ') or substring(normalize-space(@$attr), 1, string-length('$value') + 1) = '$value ' or substring(normalize-space(@$attr), string-length(@$attr) - string-length('$value')) = ' $value' or @$attr = '$value'";
@ -439,7 +463,7 @@ class OEmbed
* @param DOMNode $node
* @return string
*/
private static function getInnerHTML(DOMNode $node)
private static function getInnerHTML(DOMNode $node): string
{
$innerHTML = '';
$children = $node->childNodes;

View file

@ -64,7 +64,7 @@ class PageInfo
* @return string
* @throws HTTPException\InternalServerErrorException
*/
public static function appendDataToBody(string $body, array $data, bool $no_photos = false)
public static function appendDataToBody(string $body, array $data, bool $no_photos = false): string
{
// Only one [attachment] tag per body is allowed
$existingAttachmentPos = strpos($body, '[attachment');
@ -90,7 +90,7 @@ class PageInfo
* @return string
* @throws HTTPException\InternalServerErrorException
*/
public static function getFooterFromUrl(string $url, bool $no_photos = false, string $photo = '', bool $keywords = false, string $keyword_denylist = '')
public static function getFooterFromUrl(string $url, bool $no_photos = false, string $photo = '', bool $keywords = false, string $keyword_denylist = ''): string
{
$data = self::queryUrl($url, $photo, $keywords, $keyword_denylist);
@ -103,7 +103,7 @@ class PageInfo
* @return string
* @throws HTTPException\InternalServerErrorException
*/
public static function getFooterFromData(array $data, bool $no_photos = false)
public static function getFooterFromData(array $data, bool $no_photos = false): string
{
Hook::callAll('page_info_data', $data);
@ -220,7 +220,7 @@ class PageInfo
* @return array
* @throws HTTPException\InternalServerErrorException
*/
public static function getTagsFromUrl(string $url, string $photo = '', string $keyword_denylist = '')
public static function getTagsFromUrl(string $url, string $photo = '', string $keyword_denylist = ''): array
{
$data = self::queryUrl($url, $photo, true, $keyword_denylist);
@ -282,7 +282,7 @@ class PageInfo
* @param string $url
* @return string
*/
protected static function stripTrailingUrlFromBody(string $body, string $url)
protected static function stripTrailingUrlFromBody(string $body, string $url): string
{
$quotedUrl = preg_quote($url, '#');
$body = preg_replace_callback("#(?:

View file

@ -48,11 +48,11 @@ class Pager
*
* Guesses the page number from the GET parameter 'page'.
*
* @param L10n $l10n
* @param string $queryString The query string of the current page
* @param integer $itemsPerPage An optional number of items per page to override the default value
* @param L10n $l10n
* @param string $queryString The query string of the current page
* @param int $itemsPerPage An optional number of items per page to override the default value
*/
public function __construct(L10n $l10n, $queryString, $itemsPerPage = 50)
public function __construct(L10n $l10n, string $queryString, int $itemsPerPage = 50)
{
$this->l10n = $l10n;
@ -64,9 +64,9 @@ class Pager
/**
* Returns the start offset for a LIMIT clause. Starts at 0.
*
* @return integer
* @return int
*/
public function getStart()
public function getStart(): int
{
return max(0, ($this->page * $this->itemsPerPage) - $this->itemsPerPage);
}
@ -74,9 +74,9 @@ class Pager
/**
* Returns the number of items per page
*
* @return integer
* @return int
*/
public function getItemsPerPage()
public function getItemsPerPage(): int
{
return $this->itemsPerPage;
}
@ -86,7 +86,7 @@ class Pager
*
* @return int
*/
public function getPage()
public function getPage(): int
{
return $this->page;
}
@ -108,9 +108,9 @@ class Pager
/**
* Sets the number of items per page, 1 minimum.
*
* @param integer $itemsPerPage
* @param int $itemsPerPage
*/
public function setItemsPerPage($itemsPerPage)
public function setItemsPerPage(int $itemsPerPage)
{
$this->itemsPerPage = max(1, intval($itemsPerPage));
}
@ -118,11 +118,11 @@ class Pager
/**
* Sets the current page number. Starts at 1.
*
* @param integer $page
* @param int $page
*/
public function setPage($page)
public function setPage(int $page)
{
$this->page = max(1, intval($page));
$this->page = max(1, $page);
}
/**
@ -132,7 +132,7 @@ class Pager
*
* @param string $queryString
*/
public function setQueryString($queryString)
public function setQueryString(string $queryString)
{
$stripped = preg_replace('/([&?]page=[0-9]*)/', '', $queryString);

View file

@ -39,10 +39,9 @@ class Smilies
* @param array $b Array of emoticons
* @param string $smiley The text smilie
* @param string $representation The replacement
*
* @return void
*/
public static function add(&$b, $smiley, $representation)
public static function add(array &$b, string $smiley, string $representation)
{
$found = array_search($smiley, $b['texts']);
@ -66,7 +65,7 @@ class Smilies
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @hook smilie ('texts' => smilies texts array, 'icons' => smilies html array)
*/
public static function getList()
public static function getList(): array
{
$texts = [
'&lt;3',
@ -169,7 +168,7 @@ class Smilies
*
* @return string $subject with all substrings in the $search array replaced by the values in the $replace array
*/
private static function strOrigReplace($search, $replace, $subject)
private static function strOrigReplace(array $search, array $replace, string $subject): string
{
return strtr($subject, array_combine($search, $replace));
}
@ -191,7 +190,7 @@ class Smilies
* @return string HTML Output of the Smilie
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function replace($s, $no_images = false)
public static function replace(string $s, bool $no_images = false): string
{
$smilies = self::getList();
@ -211,7 +210,7 @@ class Smilies
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function replaceFromArray($text, array $smilies, $no_images = false)
public static function replaceFromArray(string $text, array $smilies, bool $no_images = false): string
{
if (intval(DI::config()->get('system', 'no_smilies'))
|| (local_user() && intval(DI::pConfig()->get(local_user(), 'system', 'no_smilies')))
@ -248,7 +247,7 @@ class Smilies
*
* @return string base64 encoded string
*/
private static function encode($m)
private static function encode(string $m): string
{
return '<' . $m[1] . '>' . Strings::base64UrlEncode($m[2]) . '</' . $m[1] . '>';
}
@ -259,7 +258,7 @@ class Smilies
* @return string base64 decoded string
* @throws \Exception
*/
private static function decode($m)
private static function decode(string $m): string
{
return '<' . $m[1] . '>' . Strings::base64UrlDecode($m[2]) . '</' . $m[1] . '>';
}
@ -274,7 +273,7 @@ class Smilies
*
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
private static function pregHeart($x)
private static function pregHeart(string $x): string
{
if (strlen($x[1]) == 1) {
return $x[0];

View file

@ -225,7 +225,7 @@ class Widget
* @return string
* @throws \Exception
*/
public static function contactRels($baseurl, $selected = '')
public static function contactRels(string $baseurl, string $selected = ''): string
{
if (!local_user()) {
return '';
@ -256,7 +256,7 @@ class Widget
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function networks($baseurl, $selected = '')
public static function networks(string $baseurl, string $selected = ''): string
{
if (!local_user()) {
return '';
@ -294,10 +294,10 @@ class Widget
*
* @param string $baseurl baseurl
* @param string $selected optional, default empty
* @return string|void
* @return string
* @throws \Exception
*/
public static function fileAs($baseurl, $selected = '')
public static function fileAs(string $baseurl, string $selected = ''): string
{
if (!local_user()) {
return '';
@ -325,10 +325,10 @@ class Widget
* @param int $uid Id of the user owning the categories
* @param string $baseurl Base page URL
* @param string $selected Selected category
* @return string|void
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function categories(int $uid, string $baseurl, string $selected = '')
public static function categories(int $uid, string $baseurl, string $selected = ''): string
{
if (!Feature::isEnabled($uid, 'categories')) {
return '';
@ -355,11 +355,11 @@ class Widget
*
* @param int $uid Viewed profile user ID
* @param string $nickname Viewed profile user nickname
* @return string|void
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function commonFriendsVisitor(int $uid, string $nickname)
public static function commonFriendsVisitor(int $uid, string $nickname): string
{
if (local_user() == $uid) {
return '';
@ -416,7 +416,7 @@ class Widget
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function tagCloud(int $uid, int $limit = 50)
public static function tagCloud(int $uid, int $limit = 50): string
{
if (empty($uid)) {
return '';
@ -441,7 +441,7 @@ class Widget
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function postedByYear(string $url, int $uid, bool $wall)
public static function postedByYear(string $url, int $uid, bool $wall): string
{
$o = '';
@ -515,7 +515,7 @@ class Widget
* @param int $accounttype Acount type
* @return string
*/
public static function accounttypes(string $base, $accounttype)
public static function accounttypes(string $base, int $accounttype): string
{
$accounts = [
['ref' => 'person', 'name' => DI::l10n()->t('Persons')],

View file

@ -106,7 +106,7 @@ class Post
// Only add will be displayed
if ($item['network'] === Protocol::MAIL && local_user() != $item['uid']) {
continue;
} elseif (!DI::contentItem()->visibleActivity($item)) {
} elseif (!DI::contentItem()->isVisibleActivity($item)) {
continue;
}