From 9069abbb308a6a4ca3125f5371f55d78dad2b59e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Thu, 23 Jun 2022 01:46:37 +0200 Subject: [PATCH 1/9] Changed doubled-quotes to single --- bin/daemon.php | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/bin/daemon.php b/bin/daemon.php index 1682d366ed..880115d0d0 100755 --- a/bin/daemon.php +++ b/bin/daemon.php @@ -26,7 +26,7 @@ * This script was taken from http://php.net/manual/en/function.pcntl-fork.php */ if (php_sapi_name() !== 'cli') { - header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden'); + header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden'); exit(); } @@ -45,13 +45,13 @@ $longopts = ['foreground']; $options = getopt($shortopts, $longopts); // Ensure that daemon.php is executed from the base path of the installation -if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) { - $directory = dirname($_SERVER["argv"][0]); +if (!file_exists('boot.php') && (sizeof($_SERVER['argv']) != 0)) { + $directory = dirname($_SERVER['argv'][0]); - if (substr($directory, 0, 1) != "/") { - $directory = $_SERVER["PWD"] . "/" . $directory; + if (substr($directory, 0, 1) != '/') { + $directory = $_SERVER['PWD'] . '/' . $directory; } - $directory = realpath($directory . "/.."); + $directory = realpath($directory . '/..'); chdir($directory); } @@ -86,16 +86,16 @@ TXT $pidfile = DI::config()->get('system', 'pidfile'); -if (in_array("start", $_SERVER["argv"])) { - $mode = "start"; +if (in_array('start', $_SERVER['argv'])) { + $mode = 'start'; } -if (in_array("stop", $_SERVER["argv"])) { - $mode = "stop"; +if (in_array('stop', $_SERVER['argv'])) { + $mode = 'stop'; } -if (in_array("status", $_SERVER["argv"])) { - $mode = "status"; +if (in_array('status', $_SERVER['argv'])) { + $mode = 'status'; } $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options); @@ -104,7 +104,7 @@ if (!isset($mode)) { die("Please use either 'start', 'stop' or 'status'.\n"); } -if (empty($_SERVER["argv"][0])) { +if (empty($_SERVER['argv'][0])) { die("Unexpected script behaviour. This message should never occur.\n"); } @@ -114,12 +114,12 @@ if (is_readable($pidfile)) { $pid = intval(file_get_contents($pidfile)); } -if (empty($pid) && in_array($mode, ["stop", "status"])) { +if (empty($pid) && in_array($mode, ['stop', 'status'])) { DI::config()->set('system', 'worker_daemon_mode', false); die("Pidfile wasn't found. Is the daemon running?\n"); } -if ($mode == "status") { +if ($mode == 'status') { if (posix_kill($pid, 0)) { die("Daemon process $pid is running.\n"); } @@ -130,12 +130,12 @@ if ($mode == "status") { die("Daemon process $pid isn't running.\n"); } -if ($mode == "stop") { +if ($mode == 'stop') { posix_kill($pid, SIGTERM); unlink($pidfile); - Logger::notice("Worker daemon process was killed", ["pid" => $pid]); + Logger::notice('Worker daemon process was killed', ['pid' => $pid]); DI::config()->set('system', 'worker_daemon_mode', false); die("Worker daemon process $pid was killed.\n"); @@ -145,7 +145,7 @@ if (!empty($pid) && posix_kill($pid, 0)) { die("Daemon process $pid is already running.\n"); } -Logger::notice('Starting worker daemon.', ["pid" => $pid]); +Logger::notice('Starting worker daemon.', ['pid' => $pid]); if (!$foreground) { echo "Starting worker daemon.\n"; @@ -194,7 +194,7 @@ $last_cron = 0; // Now running as a daemon. while (true) { if (!$do_cron && ($last_cron + $wait_interval) < time()) { - Logger::info('Forcing cron worker call.', ["pid" => $pid]); + Logger::info('Forcing cron worker call.', ['pid' => $pid]); $do_cron = true; } @@ -214,7 +214,7 @@ while (true) { } $start = time(); - Logger::info("Sleeping", ["pid" => $pid, 'until' => gmdate(DateTimeFormat::MYSQL, $start + $wait_interval)]); + Logger::info('Sleeping', ['pid' => $pid, 'until' => gmdate(DateTimeFormat::MYSQL, $start + $wait_interval)]); do { $seconds = (time() - $start); @@ -236,10 +236,10 @@ while (true) { if ($timeout) { $do_cron = true; - Logger::info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]); + Logger::info('Woke up after $wait_interval seconds.', ['pid' => $pid, 'sleep' => $wait_interval]); } else { $do_cron = false; - Logger::info("Worker jobs are calling to be forked.", ["pid" => $pid]); + Logger::info('Worker jobs are calling to be forked.', ['pid' => $pid]); } } From f12cc9648ecf2bdd0e468e29be4164a844b96a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Fri, 24 Jun 2022 03:01:13 +0200 Subject: [PATCH 2/9] Changes: - added some type-hints - changed some double-quotes to single --- src/App.php | 6 +++--- src/Model/Contact.php | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/App.php b/src/App.php index 2d635951be..400ea15b19 100644 --- a/src/App.php +++ b/src/App.php @@ -614,9 +614,9 @@ class App if (!empty($_GET['zrl']) && $this->mode->isNormal() && !$this->mode->isBackend() && !local_user()) { // Only continue when the given profile link seems valid // Valid profile links contain a path with "/profile/" and no query parameters - if ((parse_url($_GET['zrl'], PHP_URL_QUERY) == "") && - strstr(parse_url($_GET['zrl'], PHP_URL_PATH), "/profile/")) { - if (Core\Session::get('visitor_home') != $_GET["zrl"]) { + if ((parse_url($_GET['zrl'], PHP_URL_QUERY) == '') && + strstr(parse_url($_GET['zrl'], PHP_URL_PATH), '/profile/')) { + if (Core\Session::get('visitor_home') != $_GET['zrl']) { Core\Session::set('my_url', $_GET['zrl']); Core\Session::set('authenticated', 0); diff --git a/src/Model/Contact.php b/src/Model/Contact.php index d6b2780ef3..f8ff91ec55 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2557,7 +2557,7 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function updateFromProbeByURL($url) + public static function updateFromProbeByURL(string $url): int { $id = self::getIdForURL($url); @@ -2578,7 +2578,7 @@ class Contact * @param string $network Network of that contact * @return string with protocol */ - public static function getProtocol($url, $network) + public static function getProtocol(string $url, string $network): string { if ($network != Protocol::DFRN) { return $network; @@ -2945,6 +2945,7 @@ class Contact * Update the local relationship when a local user loses a follower * * @param array $contact User-specific contact (uid != 0) array + * @return void * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ From b0693c1ad7f0d83c0025f4a4d5d2451958d1ae07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Fri, 24 Jun 2022 03:24:51 +0200 Subject: [PATCH 3/9] Reformatted array --- src/Protocol/ActivityPub/Transmitter.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index b669a9f70d..d7995a8d1a 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -916,7 +916,13 @@ class Transmitter $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS]; } - $condition = ['uid' => $uid, 'archive' => false, 'pending' => false, 'blocked' => false, 'network' => Protocol::FEDERATED]; + $condition = [ + 'uid' => $uid, + 'archive' => false, + 'pending' => false, + 'blocked' => false, + 'network' => Protocol::FEDERATED, + ]; if (!empty($uid)) { $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND]; From 39675fe683f2aad71065a947e6652ee66bd59cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Fri, 24 Jun 2022 03:44:52 +0200 Subject: [PATCH 4/9] Space for the rescue. ;-) --- src/Content/Text/BBCode.php | 2 +- src/Content/Text/Plaintext.php | 2 +- src/Model/Contact.php | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index c3e255a4d9..bfc1dce188 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1086,7 +1086,7 @@ class BBCode * @param integer $uriid * @return string */ - private static function convertImages(string $text, int $simplehtml, int $uriid = 0):string + private static function convertImages(string $text, int $simplehtml, int $uriid = 0): string { DI::profiler()->startRecording('rendering'); $return = preg_replace_callback( diff --git a/src/Content/Text/Plaintext.php b/src/Content/Text/Plaintext.php index 2fcff72d59..ab8b712d89 100644 --- a/src/Content/Text/Plaintext.php +++ b/src/Content/Text/Plaintext.php @@ -36,7 +36,7 @@ class Plaintext * * @todo For Twitter URLs aren't shortened, but they have to be calculated as if. */ - public static function shorten(string $msg, int $limit, int $uid = 0):string + public static function shorten(string $msg, int $limit, int $uid = 0): string { $ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8'); diff --git a/src/Model/Contact.php b/src/Model/Contact.php index f8ff91ec55..731407fafc 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1958,7 +1958,7 @@ class Contact * @param string $updated Contact update date * @return string avatar link */ - public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''):string + public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''): string { // We have to fetch the "updated" variable when it wasn't provided // The parameter can be provided to improve performance @@ -1999,7 +1999,7 @@ class Contact * @param string $size One of the Proxy::SIZE_* constants * @return string avatar link */ - public static function getAvatarUrlForUrl(string $url, int $uid, string $size = ''):string + public static function getAvatarUrlForUrl(string $url, int $uid, string $size = ''): string { $condition = ["`nurl` = ? AND ((`uid` = ? AND `network` IN (?, ?)) OR `uid` = ?)", Strings::normaliseLink($url), $uid, Protocol::FEED, Protocol::MAIL, 0]; @@ -2015,7 +2015,7 @@ class Contact * @param string $updated Contact update date * @return string header link */ - public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''):string + public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''): string { // We have to fetch the "updated" variable when it wasn't provided // The parameter can be provided to improve performance From 88870d18be53fc38d70e842cdc35c7a1bc703b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Fri, 24 Jun 2022 04:42:35 +0200 Subject: [PATCH 5/9] Changes: - added some type-hints - added documentation --- src/Model/Contact.php | 8 ++++++- src/Model/FContact.php | 31 +++++++++++++++++--------- src/Module/Contact/Profile.php | 3 +++ src/Protocol/ActivityPub/Processor.php | 1 + src/Protocol/ActivityPub/Receiver.php | 2 ++ src/Worker/OnePoll.php | 5 +++-- 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 731407fafc..dd130a6416 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2312,6 +2312,8 @@ class Contact } /** + * Updates contact record by provided id and optional network + * * @param integer $id contact id * @param string $network Optional network we are probing for * @return boolean @@ -2335,13 +2337,15 @@ class Contact } /** + * Updates contact record by provided id and probed data + * * @param integer $id contact id * @param array $ret Probed data * @return boolean * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - private static function updateFromProbeArray(int $id, array $ret) + private static function updateFromProbeArray(int $id, array $ret): bool { /* Warning: Never ever fetch the public key via Probe::uri and write it into the contacts. @@ -2552,6 +2556,8 @@ class Contact } /** + * Updates contact record by provided URL + * * @param integer $url contact url * @return integer Contact id * @throws HTTPException\InternalServerErrorException diff --git a/src/Model/FContact.php b/src/Model/FContact.php index 97b1e98efb..cc6b2d2a7a 100644 --- a/src/Model/FContact.php +++ b/src/Model/FContact.php @@ -103,17 +103,27 @@ class FContact $posts = Post::countPosts(['author-id' => $contact['id'], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]]); } - $fields = ['name' => $arr["name"], 'photo' => $arr["photo"], - 'request' => $arr["request"], 'nick' => $arr["nick"], - 'addr' => strtolower($arr["addr"]), 'guid' => $arr["guid"], - 'batch' => $arr["batch"], 'notify' => $arr["notify"], - 'poll' => $arr["poll"], 'confirm' => $arr["confirm"], - 'alias' => $arr["alias"], 'pubkey' => $arr["pubkey"], - 'uri-id' => $uriid, 'interacting_count' => $interacting ?? 0, - 'interacted_count' => $interacted ?? 0, 'post_count' => $posts ?? 0, - 'updated' => DateTimeFormat::utcNow()]; + $fields = [ + 'name' => $arr['name'], + 'photo' => $arr['photo'], + 'request' => $arr['request'], + 'nick' => $arr['nick'], + 'addr' => strtolower($arr['addr']), + 'guid' => $arr['guid'], + 'batch' => $arr['batch'], + 'notify' => $arr['notify'], + 'poll' => $arr['poll'], + 'confirm' => $arr['confirm'], + 'alias' => $arr['alias'], + 'pubkey' => $arr['pubkey'], + 'uri-id' => $uriid, + 'interacting_count' => $interacting ?? 0, + 'interacted_count' => $interacted ?? 0, + 'post_count' => $posts ?? 0, + 'updated' => DateTimeFormat::utcNow(), + ]; - $condition = ['url' => $arr["url"], 'network' => $arr["network"]]; + $condition = ['url' => $arr['url'], 'network' => $arr['network']]; DBA::update('fcontact', $fields, $condition, true); } @@ -123,7 +133,6 @@ class FContact * fcontact guid * * @param string $fcontact_guid Hexadecimal string guid - * * @return string|null the contact url or null * @throws \Exception */ diff --git a/src/Module/Contact/Profile.php b/src/Module/Contact/Profile.php index 757ea64d87..793d0fa4a9 100644 --- a/src/Module/Contact/Profile.php +++ b/src/Module/Contact/Profile.php @@ -475,7 +475,10 @@ class Profile extends BaseModule } /** + * Updates contact from probing + * * @param int $contact_id Id of the contact with uid != 0 + * @return void * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index d9dd7ee5b3..c6b711ab23 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -1547,6 +1547,7 @@ class Processor * Switches a contact to AP if needed * * @param integer $cid Contact ID + * @return void * @throws \Exception */ private static function switchContact(int $cid) diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 38b6c4fc54..33c027933c 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -1087,6 +1087,7 @@ class Receiver * @param integer $cid Contact ID * @param integer $uid User ID * @param string $url Profile URL + * @return void * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ @@ -1113,6 +1114,7 @@ class Receiver * * @param $receivers * @param $actor + * @return void * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index 66b9851cf2..65f2277b73 100644 --- a/src/Worker/OnePoll.php +++ b/src/Worker/OnePoll.php @@ -41,11 +41,11 @@ use Friendica\Util\Strings; class OnePoll { - public static function execute($contact_id = 0, $command = '') + public static function execute(int $contact_id = 0, string $command = '') { Logger::notice('Start polling/probing contact', ['id' => $contact_id]); - $force = ($command == "force"); + $force = ($command == 'force'); if (empty($contact_id)) { Logger::notice('no contact provided'); @@ -118,6 +118,7 @@ class OnePoll * * @param array $contact The personal contact entry * @param array $fields The fields that are updated + * @return void * @throws \Exception */ private static function updateContact(array $contact, array $fields) From f689faca99cb9890b2b3fbe5817a4f1b7e58a5cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Fri, 24 Jun 2022 05:27:33 +0200 Subject: [PATCH 6/9] Changed double-quotes to single --- src/Database/Database.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 2231ca0ccb..2c59607e9d 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1719,29 +1719,29 @@ class Database */ public function processlist(): array { - $ret = $this->p("SHOW PROCESSLIST"); + $ret = $this->p('SHOW PROCESSLIST'); $data = $this->toArray($ret); $processes = 0; $states = []; foreach ($data as $process) { - $state = trim($process["State"]); + $state = trim($process['State']); // Filter out all non blocking processes - if (!in_array($state, ["", "init", "statistics", "updating"])) { + if (!in_array($state, ['', 'init', 'statistics', 'updating'])) { ++$states[$state]; ++$processes; } } - $statelist = ""; + $statelist = ''; foreach ($states as $state => $usage) { - if ($statelist != "") { - $statelist .= ", "; + if ($statelist != '') { + $statelist .= ', '; } - $statelist .= $state . ": " . $usage; + $statelist .= $state . ': ' . $usage; } - return (["list" => $statelist, "amount" => $processes]); + return (['list' => $statelist, 'amount' => $processes]); } /** From 804b759df1076bfec4d53a48b99cd3e81eff79f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Fri, 24 Jun 2022 05:47:53 +0200 Subject: [PATCH 7/9] Changes: - let's get rid of that `Database::exists()` is being misused for checking table existence - reformatted array --- mod/item.php | 9 +++++++-- src/Database/Database.php | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/mod/item.php b/mod/item.php index adfdc4cb03..2bfdf07a3b 100644 --- a/mod/item.php +++ b/mod/item.php @@ -439,8 +439,13 @@ function item_post(App $a) { // Ensure to only modify attachments that you own $srch = '<' . intval($contact_id) . '>'; - $condition = ['allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '', - 'id' => $attach]; + $condition = [ + 'allow_cid' => $srch, + 'allow_gid' => '', + 'deny_cid' => '', + 'deny_gid' => '', + 'id' => $attach, + ]; if (!Attach::exists($condition)) { continue; } diff --git a/src/Database/Database.php b/src/Database/Database.php index 2c59607e9d..867cda33e5 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -825,6 +825,7 @@ class Database * * @return boolean Are there rows for that condition? * @throws \Exception + * @todo Get rid of the DBStructure::existsTable() invocation */ public function exists(string $table, array $condition): bool { From 4164cdacf5743ce327828214006ded4f522dede4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Fri, 24 Jun 2022 06:27:38 +0200 Subject: [PATCH 8/9] Changes: - added type-hints - added documentation --- src/Util/ReversedFileReader.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Util/ReversedFileReader.php b/src/Util/ReversedFileReader.php index 0566002de2..cda78ad0ab 100644 --- a/src/Util/ReversedFileReader.php +++ b/src/Util/ReversedFileReader.php @@ -55,7 +55,7 @@ class ReversedFileReader implements \Iterator * @param string $filename File to open * @return $this */ - public function open(string $filename) + public function open(string $filename): ReversedFileReader { $this->fh = fopen($filename, 'r'); if (!$this->fh) { @@ -73,9 +73,10 @@ class ReversedFileReader implements \Iterator /** * Read $size bytes behind last position * + * @param int $size * @return string */ - private function _read($size) + private function _read(int $size): string { $this->pos -= $size; fseek($this->fh, $this->pos); @@ -86,7 +87,7 @@ class ReversedFileReader implements \Iterator * Read next line from end of file * Return null if no lines are left to read * - * @return ?string + * @return string|null Depending on data being buffered */ private function _readline() { @@ -140,7 +141,7 @@ class ReversedFileReader implements \Iterator * @see Iterator::key() * @return int */ - public function key() + public function key(): int { return $this->key; } @@ -151,7 +152,7 @@ class ReversedFileReader implements \Iterator * @see Iterator::current() * @return string */ - public function current() + public function current(): string { return $this->value; } @@ -162,7 +163,7 @@ class ReversedFileReader implements \Iterator * @see Iterator::valid() * @return bool */ - public function valid() + public function valid(): bool { return ! is_null($this->value); } From e10dd20036d80e918ad0a047a63a32c9fce947b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Sat, 25 Jun 2022 17:58:27 +0200 Subject: [PATCH 9/9] Made @TODO more clear? --- src/Database/Database.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 867cda33e5..6e5e147da9 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -825,7 +825,7 @@ class Database * * @return boolean Are there rows for that condition? * @throws \Exception - * @todo Get rid of the DBStructure::existsTable() invocation + * @todo Please unwrap the DBStructure::existsTable() call so this method has one behavior only: checking existence on records */ public function exists(string $table, array $condition): bool {