From 4c6940583af429d251192abef1792831a57637c5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 18 Aug 2022 22:05:00 +0200 Subject: [PATCH] Changes: - added missing type-hints - first access level, then static - reformatted code following code-style --- src/Util/Mimetype.php | 1 + src/Util/Network.php | 13 ++++++++- src/Util/ParseUrl.php | 13 ++++++++- src/Util/PidFile.php | 13 +++++---- src/Util/Profiler.php | 66 +++++++++++++++++++++++++++---------------- src/Util/Proxy.php | 4 +++ src/Util/Strings.php | 17 +++++++---- src/Util/Temporal.php | 44 ++++++++++++++++------------- 8 files changed, 113 insertions(+), 58 deletions(-) diff --git a/src/Util/Mimetype.php b/src/Util/Mimetype.php index ca57648771..df6cf33f7b 100644 --- a/src/Util/Mimetype.php +++ b/src/Util/Mimetype.php @@ -27,6 +27,7 @@ class Mimetype * Return mimetype based on file extension * * @param string $filename filename + * * @return mixed array or string */ public static function getContentType(string $filename) diff --git a/src/Util/Network.php b/src/Util/Network.php index 63cc67f4c9..508934db2c 100644 --- a/src/Util/Network.php +++ b/src/Util/Network.php @@ -50,6 +50,7 @@ class Network * and check DNS to see if it's real (or check if is a valid IP address) * * @param string $url The URL to be validated + * * @return string|boolean The actual working URL, false else * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ @@ -259,6 +260,7 @@ class Network * * @param string $domain * @param array $domain_list + * * @return boolean */ public static function isDomainAllowed(string $domain, array $domain_list): bool @@ -297,6 +299,7 @@ class Network * Remove Google Analytics and other tracking platforms params from URL * * @param string $url Any user-submitted URL that may contain tracking params + * * @return string The same URL stripped of tracking parameters */ public static function stripTrackingQueryParams(string $url): string @@ -350,9 +353,10 @@ class Network * * @param string $url * @param string $basepath + * * @return string url */ - public static function addBasePath(string $url, string $basepath) + public static function addBasePath(string $url, string $basepath): string { if (!empty(parse_url($url, PHP_URL_SCHEME)) || empty(parse_url($basepath, PHP_URL_SCHEME)) || empty($url) || empty(parse_url($url))) { return $url; @@ -372,6 +376,7 @@ class Network * * @param string $url1 * @param string $url2 + * * @return string The matching part or empty string on error */ public static function getUrlMatch(string $url1, string $url2): string @@ -459,6 +464,7 @@ class Network * Glue url parts together * * @param array $parsed URL parts + * * @return string|null The glued URL or null on error * @deprecated since version 2021.12, use GuzzleHttp\Psr7\Uri::fromParts($parts) instead */ @@ -490,6 +496,7 @@ class Network * Convert an URI to an IDN compatible URI * * @param string $uri + * * @return string */ public static function convertToIdn(string $uri): string @@ -514,6 +521,7 @@ class Network * Switch the scheme of an url between http and https * * @param string $url + * * @return string Switched URL */ public static function switchScheme(string $url): string @@ -537,6 +545,7 @@ class Network * * @param string $path * @param array $additionalParams Associative array of parameters + * * @return string */ public static function appendQueryParam(string $path, array $additionalParams): string @@ -563,6 +572,7 @@ class Network * * @param string $etag The page etag * @param string $last_modified The page last modification UTC date + * * @return void * @throws \Exception */ @@ -601,6 +611,7 @@ class Network * Check if the given URL is a local link * * @param string $url + * * @return bool */ public static function isLocalLink(string $url): bool diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index 1fba29ec3b..a0ff0f1773 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -187,7 +187,7 @@ class ParseUrl * * @endverbatim */ - public static function getSiteinfo(string $url, bool $do_oembed = true, int $count = 1) + public static function getSiteinfo(string $url, bool $do_oembed = true, int $count = 1): array { if (empty($url)) { return [ @@ -645,6 +645,7 @@ class ParseUrl * Convert tags from CSV to an array * * @param string $string Tags + * * @return array with formatted Hashtags */ public static function convertTagsToArray(string $string): array @@ -666,6 +667,7 @@ class ParseUrl * * @param string $tag The pure tag name * @param int $k Counter for internal use + * * @return void */ private static function arrAddHashes(string &$tag, int $k) @@ -729,6 +731,7 @@ class ParseUrl * * @param array $siteinfo * @param array $jsonld + * * @return array siteinfo */ private static function parseParts(array $siteinfo, array $jsonld): array @@ -774,6 +777,7 @@ class ParseUrl * * @param array $siteinfo * @param array $jsonld + * * @return array siteinfo */ private static function parseJsonLd(array $siteinfo, array $jsonld): array @@ -867,6 +871,7 @@ class ParseUrl * * @param array $siteinfo * @param array $jsonld + * * @return array siteinfo */ private static function parseJsonLdAuthor(array $siteinfo, array $jsonld): array @@ -951,6 +956,7 @@ class ParseUrl * * @param array $siteinfo * @param array $jsonld + * * @return array siteinfo */ private static function parseJsonLdArticle(array $siteinfo, array $jsonld): array @@ -1021,6 +1027,7 @@ class ParseUrl * * @param array $siteinfo * @param array $jsonld + * * @return array siteinfo */ private static function parseJsonLdWebPage(array $siteinfo, array $jsonld): array @@ -1060,6 +1067,7 @@ class ParseUrl * * @param array $siteinfo * @param array $jsonld + * * @return array siteinfo */ private static function parseJsonLdWebSite(array $siteinfo, array $jsonld): array @@ -1098,6 +1106,7 @@ class ParseUrl * * @param array $siteinfo * @param array $jsonld + * * @return array siteinfo */ private static function parseJsonLdWebOrganization(array $siteinfo, array $jsonld): array @@ -1144,6 +1153,7 @@ class ParseUrl * * @param array $siteinfo * @param array $jsonld + * * @return array siteinfo */ private static function parseJsonLdWebPerson(array $siteinfo, array $jsonld): array @@ -1189,6 +1199,7 @@ class ParseUrl * * @param array $siteinfo * @param array $jsonld + * * @return array siteinfo */ private static function parseJsonLdMediaObject(array $siteinfo, array $jsonld, string $name): array diff --git a/src/Util/PidFile.php b/src/Util/PidFile.php index 38408bcf4b..2706ce925d 100644 --- a/src/Util/PidFile.php +++ b/src/Util/PidFile.php @@ -33,7 +33,8 @@ class PidFile * * @return boolean|string PID or "false" if not existent */ - static private function pidFromFile($file) { + private static function pidFromFile(string $file) + { if (!file_exists($file)) { return false; } @@ -48,7 +49,8 @@ class PidFile * * @return boolean Is it running? */ - static public function isRunningProcess($file) { + public static function isRunningProcess(string $file): bool + { $pid = self::pidFromFile($file); if (!$pid) { @@ -72,7 +74,8 @@ class PidFile * * @return boolean Was it killed successfully? */ - static public function killProcess($file) { + public static function killProcess(string $file): bool + { $pid = self::pidFromFile($file); // We don't have a process id? then we quit @@ -97,7 +100,7 @@ class PidFile * * @return boolean|string PID or "false" if not created */ - static public function create(string $file) + public static function create(string $file) { $pid = self::pidFromFile($file); @@ -120,7 +123,7 @@ class PidFile * * @return boolean Is it running? */ - static public function delete(string $file): bool + public static function delete(string $file): bool { return @unlink($file); } diff --git a/src/Util/Profiler.php b/src/Util/Profiler.php index 24289678c2..23eb1d8c33 100644 --- a/src/Util/Profiler.php +++ b/src/Util/Profiler.php @@ -61,7 +61,7 @@ class Profiler implements ContainerInterface * * @return bool */ - public function isRendertime() + public function isRendertime(): bool { return $this->rendertime; } @@ -91,6 +91,7 @@ class Profiler implements ContainerInterface * Start a profiler recording * * @param string $value + * * @return void */ public function startRecording(string $value) @@ -106,6 +107,7 @@ class Profiler implements ContainerInterface * Stop a profiler recording * * @param string $callstack + * * @return void */ public function stopRecording(string $callstack = '') @@ -147,8 +149,10 @@ class Profiler implements ContainerInterface * @param int $timestamp the Timestamp * @param string $value A value to profile * @param string $callstack A callstack string, generated if absent + * + * @return void */ - public function saveTimestamp($timestamp, $value, $callstack = '') + public function saveTimestamp(int $timestamp, string $value, string $callstack = '') { if (!$this->enabled) { return; @@ -176,6 +180,8 @@ class Profiler implements ContainerInterface /** * Resets the performance and callstack profiling + * + * @return void */ public function reset() { @@ -185,6 +191,8 @@ class Profiler implements ContainerInterface /** * Resets the performance profiling data + * + * @return void */ public function resetPerformance() { @@ -209,6 +217,8 @@ class Profiler implements ContainerInterface /** * Resets the callstack profiling data + * + * @return void */ public function resetCallstack() { @@ -229,7 +239,7 @@ class Profiler implements ContainerInterface * * @return string the rendertime */ - public function getRendertimeString(float $limit = 0) + public function getRendertimeString(float $limit = 0): string { $output = ''; @@ -237,58 +247,62 @@ class Profiler implements ContainerInterface return $output; } - if (isset($this->callstack["database"])) { + if (isset($this->callstack['database'])) { $output .= "\nDatabase Read:\n"; - foreach ($this->callstack["database"] as $func => $time) { + foreach ($this->callstack['database'] as $func => $time) { $time = round($time, 3); if ($time > $limit) { - $output .= $func . ": " . $time . "\n"; + $output .= $func . ': ' . $time . "\n"; } } } - if (isset($this->callstack["database_write"])) { + + if (isset($this->callstack['database_write'])) { $output .= "\nDatabase Write:\n"; - foreach ($this->callstack["database_write"] as $func => $time) { + foreach ($this->callstack['database_write'] as $func => $time) { $time = round($time, 3); if ($time > $limit) { - $output .= $func . ": " . $time . "\n"; + $output .= $func . ': ' . $time . "\n"; } } } - if (isset($this->callstack["cache"])) { + + if (isset($this->callstack['cache'])) { $output .= "\nCache Read:\n"; - foreach ($this->callstack["cache"] as $func => $time) { + foreach ($this->callstack['cache'] as $func => $time) { $time = round($time, 3); if ($time > $limit) { - $output .= $func . ": " . $time . "\n"; + $output .= $func . ': ' . $time . "\n"; } } } - if (isset($this->callstack["cache_write"])) { + + if (isset($this->callstack['cache_write'])) { $output .= "\nCache Write:\n"; - foreach ($this->callstack["cache_write"] as $func => $time) { + foreach ($this->callstack['cache_write'] as $func => $time) { $time = round($time, 3); if ($time > $limit) { - $output .= $func . ": " . $time . "\n"; + $output .= $func . ': ' . $time . "\n"; } } } - if (isset($this->callstack["network"])) { + + if (isset($this->callstack['network'])) { $output .= "\nNetwork:\n"; - foreach ($this->callstack["network"] as $func => $time) { + foreach ($this->callstack['network'] as $func => $time) { $time = round($time, 3); if ($time > $limit) { - $output .= $func . ": " . $time . "\n"; + $output .= $func . ': ' . $time . "\n"; } } } - if (isset($this->callstack["rendering"])) { + if (isset($this->callstack['rendering'])) { $output .= "\nRendering:\n"; - foreach ($this->callstack["rendering"] as $func => $time) { + foreach ($this->callstack['rendering'] as $func => $time) { $time = round($time, 3); if ($time > $limit) { - $output .= $func . ": " . $time . "\n"; + $output .= $func . ': ' . $time . "\n"; } } } @@ -301,8 +315,10 @@ class Profiler implements ContainerInterface * * @param LoggerInterface $logger The logger to save the current log * @param string $message Additional message for the log + * + * @return void */ - public function saveLog(LoggerInterface $logger, $message = '') + public function saveLog(LoggerInterface $logger, string $message = '') { $duration = microtime(true) - $this->get('start'); $logger->info( @@ -338,7 +354,7 @@ class Profiler implements ContainerInterface * * @return int Entry. */ - public function get($id) + public function get(string $id): int { if (!$this->has($id)) { return 0; @@ -347,7 +363,7 @@ class Profiler implements ContainerInterface } } - public function set($timestamp, $id) + public function set($timestamp, string $id) { $this->performance[$id] = $timestamp; } @@ -363,7 +379,7 @@ class Profiler implements ContainerInterface * * @return bool */ - public function has($id) + public function has(string $id): bool { return isset($this->performance[$id]); } diff --git a/src/Util/Proxy.php b/src/Util/Proxy.php index ed0ae8affc..fc7d369ad5 100644 --- a/src/Util/Proxy.php +++ b/src/Util/Proxy.php @@ -132,6 +132,7 @@ class Proxy * proxy storage directory. * * @param string $html Un-proxified HTML code + * * @return string Proxified HTML code * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ @@ -146,6 +147,7 @@ class Proxy * Checks if the URL is a local URL. * * @param string $url + * * @return boolean * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ @@ -166,6 +168,7 @@ class Proxy * Return the array of query string parameters from a URL * * @param string $url URL to parse + * * @return array Associative array of query string parameters */ private static function parseQuery(string $url): array @@ -182,6 +185,7 @@ class Proxy * Call-back method to replace the UR * * @param array $matches Matches from preg_replace_callback() + * * @return string Proxified HTML image tag * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ diff --git a/src/Util/Strings.php b/src/Util/Strings.php index 44ddc73259..84a8b6727a 100644 --- a/src/Util/Strings.php +++ b/src/Util/Strings.php @@ -33,6 +33,7 @@ class Strings * Generates a pseudo-random string of hexadecimal characters * * @param int $size Size of string (default: 64) + * * @return string Pseudo-random string * @throws \Exception */ @@ -159,6 +160,7 @@ class Strings * * @param string $network Network name of the contact (e.g. dfrn, rss and so on) * @param string $url The contact url + * * @return string Formatted network name * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ @@ -341,7 +343,8 @@ class Strings * Check if the trimmed provided string is starting with one of the provided characters * * @param string $string - * @param array $chars + * @param array $chars + * * @return bool */ public static function startsWithChars(string $string, array $chars): bool @@ -372,13 +375,12 @@ class Strings * @see http://maettig.com/code/php/php-performance-benchmarks.php#endswith * @param string $string * @param string $end + * * @return bool */ - public static function endsWith(string $string, string $end): string + public static function endsWith(string $string, string $end): bool { - $return = substr_compare($string, $end, -strlen($end)) === 0; - - return $return; + return (substr_compare($string, $end, -strlen($end)) === 0); } /** @@ -413,8 +415,9 @@ class Strings /** * Ensures a single path item doesn't contain any path-traversing characters * - * @see https://stackoverflow.com/a/46097713 * @param string $pathItem + * + * @see https://stackoverflow.com/a/46097713 * @return string */ public static function sanitizeFilePathItem(string $pathItem): string @@ -436,6 +439,7 @@ class Strings * @param string $replacement * @param int $start * @param int|null $length + * * @return string * @see substr_replace() */ @@ -474,6 +478,7 @@ class Strings * @param string $text * @param string $regex * @param callable $callback + * * @return string */ public static function performWithEscapedBlocks(string $text, string $regex, callable $callback): string diff --git a/src/Util/Temporal.php b/src/Util/Temporal.php index e6d0f207df..0186b59dd6 100644 --- a/src/Util/Temporal.php +++ b/src/Util/Temporal.php @@ -37,9 +37,10 @@ class Temporal * * @param string $a * @param string $b + * * @return int */ - private static function timezoneCompareCallback($a, $b) + private static function timezoneCompareCallback(string $a, string $b): int { if (strstr($a, '/') && strstr($b, '/')) { if (DI::l10n()->t($a) == DI::l10n()->t($b)) { @@ -63,9 +64,10 @@ class Temporal * Emit a timezone selector grouped (primarily) by continent * * @param string $current Timezone + * * @return string Parsed HTML output */ - public static function getTimezoneSelect($current = 'America/Los_Angeles') + public static function getTimezoneSelect(string $current = 'America/Los_Angeles'): string { $timezone_identifiers = DateTimeZone::listIdentifiers(); @@ -120,7 +122,7 @@ class Temporal * @return string Parsed HTML * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function getTimezoneField($name = 'timezone', $label = '', $current = 'America/Los_Angeles', $help = '') + public static function getTimezoneField(string $name = 'timezone', string $label = '', string $current = 'America/Los_Angeles', string $help = ''): string { $options = self::getTimezoneSelect($current); $options = str_replace('