From b8353a6eb796e1baea49fa47c825cd757936b9ed Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 23 Jun 2022 11:39:45 +0200 Subject: [PATCH] Changes: - reformatted some array - added missing documentation - added type-hints - changed double-quotes to single --- src/Model/Contact/Group.php | 2 +- src/Model/Contact/Relation.php | 61 +++++++++++++++++++---------- src/Model/Contact/User.php | 35 +++++++++-------- src/Model/Log/ParsedLogIterator.php | 16 ++++---- src/Module/Install.php | 3 +- src/Module/Maintenance.php | 2 +- src/Module/NoScrape.php | 2 +- src/Module/PermissionTooltip.php | 2 +- src/Module/Photo.php | 44 ++++++++++----------- src/Module/Proxy.php | 23 +++++------ 10 files changed, 106 insertions(+), 84 deletions(-) diff --git a/src/Model/Contact/Group.php b/src/Model/Contact/Group.php index 2b7096a9a8..0f0b4965cd 100644 --- a/src/Model/Contact/Group.php +++ b/src/Model/Contact/Group.php @@ -36,7 +36,7 @@ class Group * @return array * @throws \Exception */ - public static function getById(int $gid) + public static function getById(int $gid): array { $return = []; diff --git a/src/Model/Contact/Relation.php b/src/Model/Contact/Relation.php index d68555eb3e..4f41e25ba0 100644 --- a/src/Model/Contact/Relation.php +++ b/src/Model/Contact/Relation.php @@ -174,7 +174,7 @@ class Relation * @param array $rel * @return array contact list */ - private static function getContacts(int $uid, array $rel) + private static function getContacts(int $uid, array $rel): array { $list = []; $profile = Profile::getByUID($uid); @@ -182,8 +182,15 @@ class Relation return $list; } - $condition = ['rel' => $rel, 'uid' => $uid, 'self' => false, 'deleted' => false, - 'hidden' => false, 'archive' => false, 'pending' => false]; + $condition = [ + 'rel' => $rel, + 'uid' => $uid, + 'self' => false, + 'deleted' => false, + 'hidden' => false, + 'archive' => false, + 'pending' => false, + ]; $condition = DBA::mergeConditions($condition, ["`url` IN (SELECT `url` FROM `apcontact`)"]); $contacts = DBA::select('contact', ['url'], $condition); while ($contact = DBA::fetch($contacts)) { @@ -201,7 +208,7 @@ class Relation * @param array $contact Contact array * @return boolean True if contact is discoverable */ - public static function isDiscoverable(string $url, array $contact = []) + public static function isDiscoverable(string $url, array $contact = []): bool { $contact_discovery = DI::config()->get('system', 'contact_discovery'); @@ -254,12 +261,14 @@ class Relation } /** - * @param int $uid user + * Returns an array of sugguested contacts for given user id + * + * @param int $uid User id * @param int $start optional, default 0 * @param int $limit optional, default 80 * @return array */ - static public function getSuggestions(int $uid, int $start = 0, int $limit = 80) + static public function getSuggestions(int $uid, int $start = 0, int $limit = 80): array { $cid = Contact::getPublicIdByUserId($uid); $totallimit = $start + $limit; @@ -272,20 +281,30 @@ class Relation // The query returns contacts where contacts interacted with whom the given user follows. // Contacts who already are in the user's contact table are ignored. - $results = DBA::select('contact', [], - ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN + $results = DBA::select('contact', [], ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ?) AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?)))) AND NOT `hidden` AND `network` IN (?, ?, ?, ?)", - $cid, 0, $uid, Contact::FRIEND, Contact::SHARING, - Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus], - ['order' => ['last-item' => true], 'limit' => $totallimit] + $cid, + 0, + $uid, + Contact::FRIEND, + Contact::SHARING, + Protocol::ACTIVITYPUB, + Protocol::DFRN, + $diaspora, + $ostatus, + ], [ + 'order' => ['last-item' => true], + 'limit' => $totallimit, + ] ); while ($contact = DBA::fetch($results)) { $contacts[$contact['id']] = $contact; } + DBA::close($results); Logger::info('Contacts of contacts who are followed by the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]); @@ -365,12 +384,12 @@ class Relation * @return int * @throws Exception */ - public static function countFollows(int $cid, array $condition = []) + public static function countFollows(int $cid, array $condition = []): int { - $condition = DBA::mergeConditions($condition, - ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)', - $cid] - ); + $condition = DBA::mergeConditions($condition, [ + '`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)', + $cid, + ]); return DI::dba()->count('contact', $condition); } @@ -556,7 +575,7 @@ class Relation * @param int $count * @param int $offset * @param bool $shuffle - * @return array + * @return array|bool Array on success, false on failure * @throws Exception */ public static function listCommon(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false) @@ -581,7 +600,7 @@ class Relation * @return int * @throws Exception */ - public static function countCommonFollows(int $sourceId, int $targetId, array $condition = []) + public static function countCommonFollows(int $sourceId, int $targetId, array $condition = []): int { $condition = DBA::mergeConditions($condition, ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) @@ -601,7 +620,7 @@ class Relation * @param int $count * @param int $offset * @param bool $shuffle - * @return array + * @return array|bool Array on success, false on failure * @throws Exception */ public static function listCommonFollows(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false) @@ -626,7 +645,7 @@ class Relation * @return int * @throws Exception */ - public static function countCommonFollowers(int $sourceId, int $targetId, array $condition = []) + public static function countCommonFollowers(int $sourceId, int $targetId, array $condition = []): int { $condition = DBA::mergeConditions($condition, ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`) @@ -646,7 +665,7 @@ class Relation * @param int $count * @param int $offset * @param bool $shuffle - * @return array + * @return array|bool Array on success, false on failure * @throws Exception */ public static function listCommonFollowers(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false) diff --git a/src/Model/Contact/User.php b/src/Model/Contact/User.php index 665b95624d..ab409077b5 100644 --- a/src/Model/Contact/User.php +++ b/src/Model/Contact/User.php @@ -82,11 +82,11 @@ class User /** * Apply changes from contact update data to user-contact table * - * @param array $fields - * @param array $condition - * @return void - * @throws PDOException - * @throws Exception + * @param array $fields Fields + * @param array $condition Conditions + * @return void + * @throws PDOException + * @throws Exception */ public static function updateByContactUpdate(array $fields, array $condition) { @@ -138,9 +138,10 @@ class User * @param int $cid Either public contact id or user's contact id * @param int $uid User ID * @param boolean $blocked Is the contact blocked or unblocked? + * @return void * @throws \Exception */ - public static function setBlocked($cid, $uid, $blocked) + public static function setBlocked(int $cid, int $uid, bool $blocked) { $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { @@ -170,7 +171,7 @@ class User * @return boolean is the contact id blocked for the given user? * @throws \Exception */ - public static function isBlocked($cid, $uid) + public static function isBlocked(int $cid, int $uid): bool { $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { @@ -208,9 +209,10 @@ class User * @param int $cid Either public contact id or user's contact id * @param int $uid User ID * @param boolean $ignored Is the contact ignored or unignored? + * @return void * @throws \Exception */ - public static function setIgnored($cid, $uid, $ignored) + public static function setIgnored(int $cid, int $uid, bool $ignored) { $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { @@ -229,11 +231,10 @@ class User * * @param int $cid Either public contact id or user's contact id * @param int $uid User ID - * * @return boolean is the contact id ignored for the given user? * @throws \Exception */ - public static function isIgnored($cid, $uid) + public static function isIgnored(int $cid, int $uid): bool { $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { @@ -271,9 +272,10 @@ class User * @param int $cid Either public contact id or user's contact id * @param int $uid User ID * @param boolean $collapsed are the contact's posts collapsed or uncollapsed? + * @return void * @throws \Exception */ - public static function setCollapsed($cid, $uid, $collapsed) + public static function setCollapsed(int $cid, int $uid, bool $collapsed) { $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { @@ -288,16 +290,15 @@ class User * * @param int $cid Either public contact id or user's contact id * @param int $uid User ID - * * @return boolean is the contact id blocked for the given user? * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function isCollapsed($cid, $uid) + public static function isCollapsed(int $cid, int $uid): bool { $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { - return; + return false; } $collapsed = false; @@ -318,9 +319,10 @@ class User * @param int $cid Either public contact id or user's contact id * @param int $uid User ID * @param boolean $blocked Is the user blocked or unblocked by the contact? + * @return void * @throws \Exception */ - public static function setIsBlocked($cid, $uid, $blocked) + public static function setIsBlocked(int $cid, int $uid, bool $blocked) { $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { @@ -335,11 +337,10 @@ class User * * @param int $cid Either public contact id or user's contact id * @param int $uid User ID - * * @return boolean Is the user blocked or unblocked by the contact? * @throws \Exception */ - public static function isIsBlocked($cid, $uid) + public static function isIsBlocked(int $cid, int $uid): bool { $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (empty($cdata)) { diff --git a/src/Model/Log/ParsedLogIterator.php b/src/Model/Log/ParsedLogIterator.php index d3a5650397..4309e4cd00 100644 --- a/src/Model/Log/ParsedLogIterator.php +++ b/src/Model/Log/ParsedLogIterator.php @@ -45,7 +45,7 @@ class ParsedLogIterator implements \Iterator private $filters = []; /** @var string search term */ - private $search = ""; + private $search = ''; /** @@ -108,10 +108,11 @@ class ParsedLogIterator implements \Iterator $match = true; foreach ($this->filters as $filter => $filtervalue) { switch ($filter) { - case "level": + case 'level': $match = $match && ($parsedlogline->level == strtoupper($filtervalue)); break; - case "context": + + case 'context': $match = $match && ($parsedlogline->context == $filtervalue); break; } @@ -128,7 +129,7 @@ class ParsedLogIterator implements \Iterator */ private function search(ParsedLogLine $parsedlogline): bool { - if ($this->search != "") { + if ($this->search != '') { return strstr($parsedlogline->logline, $this->search) !== false; } return true; @@ -138,7 +139,6 @@ class ParsedLogIterator implements \Iterator * Read a line from reader and parse. * Returns null if limit is reached or the reader is invalid. * - * @param ParsedLogLine $parsedlogline * @return ?ParsedLogLine */ private function read() @@ -191,7 +191,7 @@ class ParsedLogIterator implements \Iterator * @see ReversedFileReader::key() * @return int */ - public function key() + public function key(): int { return $this->reader->key(); } @@ -213,8 +213,8 @@ class ParsedLogIterator implements \Iterator * @see Iterator::valid() * @return bool */ - public function valid() + public function valid(): bool { - return ! is_null($this->value); + return !is_null($this->value); } } diff --git a/src/Module/Install.php b/src/Module/Install.php index 09025ff493..59d6581056 100644 --- a/src/Module/Install.php +++ b/src/Module/Install.php @@ -363,7 +363,7 @@ class Install extends BaseModule * @return string The text for the next steps * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - private function whatNext() + private function whatNext(): string { $baseurl = $this->baseUrl->get(); return @@ -383,6 +383,7 @@ class Install extends BaseModule * @param string $cat The category of the setting * @param string $key The key of the setting * @param null|string $default The default value + * @return void */ private function checkSetting(Cache $configCache, array $post, string $cat, string $key, ?string $default = null) { diff --git a/src/Module/Maintenance.php b/src/Module/Maintenance.php index 05540a228b..4080869158 100644 --- a/src/Module/Maintenance.php +++ b/src/Module/Maintenance.php @@ -45,7 +45,7 @@ class Maintenance extends BaseModule $exception = new HTTPException\ServiceUnavailableException($reason); - header($_SERVER["SERVER_PROTOCOL"] . ' ' . $exception->getCode() . ' ' . DI::l10n()->t('System down for maintenance')); + header($_SERVER['SERVER_PROTOCOL'] . ' ' . $exception->getCode() . ' ' . DI::l10n()->t('System down for maintenance')); $tpl = Renderer::getMarkupTemplate('exception.tpl'); diff --git a/src/Module/NoScrape.php b/src/Module/NoScrape.php index fbc01c2d3c..e3a01ceaab 100644 --- a/src/Module/NoScrape.php +++ b/src/Module/NoScrape.php @@ -131,7 +131,7 @@ class NoScrape extends BaseModule $profile_fields = ['about', 'locality', 'region', 'postal-code', 'country-name', 'xmpp', 'matrix']; foreach ($profile_fields as $field) { if (!empty($owner[$field])) { - $json_info["$field"] = $owner[$field]; + $json_info[$field] = $owner[$field]; } } diff --git a/src/Module/PermissionTooltip.php b/src/Module/PermissionTooltip.php index 07faa72e3e..ec449aa15f 100644 --- a/src/Module/PermissionTooltip.php +++ b/src/Module/PermissionTooltip.php @@ -174,7 +174,7 @@ class PermissionTooltip extends \Friendica\BaseModule * @param int $uriId * @return string */ - private function fetchReceivers(int $uriId):string + private function fetchReceivers(int $uriId): string { $own_url = ''; $uid = local_user(); diff --git a/src/Module/Photo.php b/src/Module/Photo.php index bedc216fb2..341858259f 100644 --- a/src/Module/Photo.php +++ b/src/Module/Photo.php @@ -60,17 +60,17 @@ class Photo extends BaseModule { $totalstamp = microtime(true); - if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) { - header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT"); - if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) { - header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]); + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { + header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); + if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) { + header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']); } - header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT"); - header("Cache-Control: max-age=31536000"); - if (function_exists("header_remove")) { - header_remove("Last-Modified"); - header_remove("Expires"); - header_remove("Cache-Control"); + header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT'); + header('Cache-Control: max-age=31536000'); + if (function_exists('header_remove')) { + header_remove('Last-Modified'); + header_remove('Expires'); + header_remove('Cache-Control'); } throw new NotModifiedException(); } @@ -132,7 +132,7 @@ class Photo extends BaseModule } else { $photoid = pathinfo($this->parameters['name'], PATHINFO_FILENAME); $scale = 0; - if (substr($photoid, -2, 1) == "-") { + if (substr($photoid, -2, 1) == '-') { $scale = intval(substr($photoid, -1, 1)); $photoid = substr($photoid, 0, -2); } @@ -148,7 +148,7 @@ class Photo extends BaseModule throw new HTTPException\NotFoundException(); } - $cacheable = ($photo["allow_cid"] . $photo["allow_gid"] . $photo["deny_cid"] . $photo["deny_gid"] === "") && (isset($photo["cacheable"]) ? $photo["cacheable"] : true); + $cacheable = ($photo['allow_cid'] . $photo['allow_gid'] . $photo['deny_cid'] . $photo['deny_gid'] === '') && (isset($photo['cacheable']) ? $photo['cacheable'] : true); $stamp = microtime(true); @@ -179,35 +179,35 @@ class Photo extends BaseModule } // if customsize is set and image is not a gif, resize it - if ($photo['type'] !== "image/gif" && $customsize > 0 && $customsize <= Proxy::PIXEL_THUMB && $square_resize) { + if ($photo['type'] !== 'image/gif' && $customsize > 0 && $customsize <= Proxy::PIXEL_THUMB && $square_resize) { $img = new Image($imgdata, $photo['type']); $img->scaleToSquare($customsize); $imgdata = $img->asString(); - } elseif ($photo['type'] !== "image/gif" && $customsize > 0) { + } elseif ($photo['type'] !== 'image/gif' && $customsize > 0) { $img = new Image($imgdata, $photo['type']); $img->scaleDown($customsize); $imgdata = $img->asString(); } - if (function_exists("header_remove")) { - header_remove("Pragma"); - header_remove("pragma"); + if (function_exists('header_remove')) { + header_remove('Pragma'); + header_remove('pragma'); } - header("Content-type: " . $photo['type']); + header('Content-type: ' . $photo['type']); $stamp = microtime(true); if (!$cacheable) { // it is a private photo that they have no permission to view. // tell the browser not to cache it, in case they authenticate // and subsequently have permission to see it - header("Cache-Control: no-store, no-cache, must-revalidate"); + header('Cache-Control: no-store, no-cache, must-revalidate'); } else { $md5 = $photo['hash'] ?: md5($imgdata); - header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT"); + header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); header("Etag: \"{$md5}\""); - header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT"); - header("Cache-Control: max-age=31536000"); + header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT'); + header('Cache-Control: max-age=31536000'); } $checksum = microtime(true) - $stamp; diff --git a/src/Module/Proxy.php b/src/Module/Proxy.php index 90f1185800..cff5ed90b0 100644 --- a/src/Module/Proxy.php +++ b/src/Module/Proxy.php @@ -55,17 +55,17 @@ class Proxy extends BaseModule throw new \Friendica\Network\HTTPException\NotFoundException(); } - if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) { - header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT"); - if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) { - header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]); + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { + header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); + if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) { + header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']); } - header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT"); - header("Cache-Control: max-age=31536000"); - if (function_exists("header_remove")) { - header_remove("Last-Modified"); - header_remove("Expires"); - header_remove("Cache-Control"); + header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT'); + header('Cache-Control: max-age=31536000'); + if (function_exists('header_remove')) { + header_remove('Last-Modified'); + header_remove('Expires'); + header_remove('Cache-Control'); } throw new NotModifiedException(); } @@ -123,7 +123,7 @@ class Proxy extends BaseModule * ] * @throws \Exception */ - private function getRequestInfo() + private function getRequestInfo(): array { $size = ProxyUtils::PIXEL_LARGE; $sizetype = ''; @@ -187,6 +187,7 @@ class Proxy extends BaseModule * Output the image with cache headers * * @param Image $img + * @return void * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private static function responseImageHttpCache(Image $img) -- 2.39.5