From 4cc3374f9f6cdd6ea05d4d16942d0165d8b2ed90 Mon Sep 17 00:00:00 2001 From: wn_ Date: Wed, 10 Nov 2021 21:38:25 +0000 Subject: [PATCH] Initial go at PHPStan rule level 6. --- classes/config.php | 41 ++++++++++++++++++++++++++++++-------- classes/db.php | 9 ++++----- classes/errors.php | 6 +++++- classes/logger.php | 13 +++++++----- classes/logger/adapter.php | 4 ++-- classes/logger/sql.php | 2 +- classes/logger/stdout.php | 3 ++- classes/logger/syslog.php | 3 ++- classes/urlhelper.php | 40 ++++++++++++++++++++++++------------- phpstan.neon | 2 +- update.php | 4 ++-- update_daemon2.php | 14 ++++++------- 12 files changed, 93 insertions(+), 48 deletions(-) diff --git a/classes/config.php b/classes/config.php index 53a31b61c..9096a4953 100644 --- a/classes/config.php +++ b/classes/config.php @@ -231,9 +231,13 @@ class Config { Config::T_STRING ], ]; + /** @var Config|null */ private static $instance; + /** @var array> */ private $params = []; + + /** @var array */ private $version = []; /** @var Db_Migrations|null $migrations */ @@ -268,10 +272,16 @@ class Config { directory, its contents are displayed instead of git commit-based version, this could be generated based on source git tree commit used when creating the package */ + /** + * @return array|string + */ static function get_version(bool $as_string = true) { return self::get_instance()->_get_version($as_string); } + /** + * @return array|string + */ private function _get_version(bool $as_string = true) { $root_dir = dirname(__DIR__); @@ -298,7 +308,10 @@ class Config { return $as_string ? $this->version["version"] : $this->version; } - static function get_version_from_git(string $dir) { + /** + * @return array + */ + static function get_version_from_git(string $dir): array { $descriptorspec = [ 1 => ["pipe", "w"], // STDOUT 2 => ["pipe", "w"], // STDERR @@ -364,6 +377,9 @@ class Config { return self::get_migrations()->get_version(); } + /** + * @return bool|int|string + */ static function cast_to(string $value, int $type_hint) { switch ($type_hint) { case self::T_BOOL: @@ -375,24 +391,30 @@ class Config { } } + /** + * @return bool|int|string + */ private function _get(string $param) { list ($value, $type_hint) = $this->params[$param]; return $this->cast_to($value, $type_hint); } - private function _add(string $param, string $default, int $type_hint) { + private function _add(string $param, string $default, int $type_hint): void { $override = getenv(self::_ENVVAR_PREFIX . $param); $this->params[$param] = [ self::cast_to($override !== false ? $override : $default, $type_hint), $type_hint ]; } - static function add(string $param, string $default, int $type_hint = Config::T_STRING) { + static function add(string $param, string $default, int $type_hint = Config::T_STRING): void { $instance = self::get_instance(); - return $instance->_add($param, $default, $type_hint); + $instance->_add($param, $default, $type_hint); } + /** + * @return bool|int|string + */ static function get(string $param) { $instance = self::get_instance(); @@ -431,6 +453,9 @@ class Config { /* sanity check stuff */ + /** + * @return array> A list of entries identifying tt-rss tables with bad config + */ private static function check_mysql_tables() { $pdo = Db::pdo(); @@ -447,7 +472,7 @@ class Config { return $bad_tables; } - static function sanity_check() { + static function sanity_check(): void { /* we don't actually need the DB object right now but some checks below might use ORM which won't be initialized @@ -621,11 +646,11 @@ class Config { } } - private static function format_error($msg) { + private static function format_error(string $msg): string { return "
$msg
"; } - static function get_override_links() { + static function get_override_links(): string { $rv = ""; $local_css = get_theme_path(self::get(self::LOCAL_OVERRIDE_STYLESHEET)); @@ -637,7 +662,7 @@ class Config { return $rv; } - static function get_user_agent() { + static function get_user_agent(): string { return sprintf(self::get(self::HTTP_USER_AGENT), self::get_version()); } } diff --git a/classes/db.php b/classes/db.php index 7b669cf32..2cc89f5ba 100755 --- a/classes/db.php +++ b/classes/db.php @@ -17,7 +17,7 @@ class Db } } - static function NOW() { + static function NOW(): string { return date("Y-m-d H:i:s", time()); } @@ -25,7 +25,7 @@ class Db // } - public static function get_dsn() { + public static function get_dsn(): string { $db_port = Config::get(Config::DB_PORT) ? ';port=' . Config::get(Config::DB_PORT) : ''; $db_host = Config::get(Config::DB_HOST) ? ';host=' . Config::get(Config::DB_HOST) : ''; if (Config::get(Config::DB_TYPE) == "mysql" && Config::get(Config::MYSQL_CHARSET)) { @@ -88,12 +88,11 @@ class Db return self::$instance->pdo; } - public static function sql_random_function() { + public static function sql_random_function(): string { if (Config::get(Config::DB_TYPE) == "mysql") { return "RAND()"; - } else { - return "RANDOM()"; } + return "RANDOM()"; } } diff --git a/classes/errors.php b/classes/errors.php index 3599c2639..31be558cf 100644 --- a/classes/errors.php +++ b/classes/errors.php @@ -7,7 +7,11 @@ class Errors { const E_SCHEMA_MISMATCH = "E_SCHEMA_MISMATCH"; const E_URL_SCHEME_MISMATCH = "E_URL_SCHEME_MISMATCH"; - static function to_json(string $code, array $params = []) { + /** + * @param Errors::E_* $code + * @param array $params + */ + static function to_json(string $code, array $params = []): string { return json_encode(["error" => ["code" => $code, "params" => $params]]); } } diff --git a/classes/logger.php b/classes/logger.php index 42ab4452c..ef6173a42 100755 --- a/classes/logger.php +++ b/classes/logger.php @@ -1,6 +1,9 @@ 'E_USER_DEPRECATED', 32767 => 'E_ALL']; - static function log_error(int $errno, string $errstr, string $file, int $line, $context) { + static function log_error(int $errno, string $errstr, string $file, int $line, string $context): bool { return self::get_instance()->_log_error($errno, $errstr, $file, $line, $context); } - private function _log_error($errno, $errstr, $file, $line, $context) { + private function _log_error(int $errno, string $errstr, string $file, int $line, string $context): bool { //if ($errno == E_NOTICE) return false; if ($this->adapter) @@ -38,11 +41,11 @@ class Logger { return false; } - static function log(int $errno, string $errstr, $context = "") { + static function log(int $errno, string $errstr, string $context = ""): bool { return self::get_instance()->_log($errno, $errstr, $context); } - private function _log(int $errno, string $errstr, $context = "") { + private function _log(int $errno, string $errstr, string $context = ""): bool { if ($this->adapter) return $this->adapter->log_error($errno, $errstr, '', 0, $context); else @@ -65,7 +68,7 @@ class Logger { $this->adapter = new Logger_Stdout(); break; default: - $this->adapter = false; + $this->adapter = null; } if ($this->adapter && !implements_interface($this->adapter, "Logger_Adapter")) diff --git a/classes/logger/adapter.php b/classes/logger/adapter.php index 79f641441..b0287b5fa 100644 --- a/classes/logger/adapter.php +++ b/classes/logger/adapter.php @@ -1,4 +1,4 @@ 117) { diff --git a/classes/logger/stdout.php b/classes/logger/stdout.php index e906853ce..b15649028 100644 --- a/classes/logger/stdout.php +++ b/classes/logger/stdout.php @@ -1,7 +1,7 @@ $parts + */ + static function build_url(array $parts): string { $tmp = $parts['scheme'] . "://" . $parts['host']; if (isset($parts['path'])) $tmp .= $parts['path']; @@ -81,7 +84,10 @@ class UrlHelper { } // extended filtering involves validation for safe ports and loopback - static function validate($url, $extended_filtering = false) { + /** + * @return bool|string false if something went wrong, otherwise the URL string + */ + static function validate(string $url, bool $extended_filtering = false) { $url = clean($url); @@ -138,7 +144,10 @@ class UrlHelper { return $url; } - static function resolve_redirects($url, $timeout, $nest = 0) { + /** + * @return bool|string + */ + static function resolve_redirects(string $url, int $timeout, int $nest = 0) { // too many redirects if ($nest > 10) @@ -189,12 +198,15 @@ class UrlHelper { return false; } - // TODO: max_size currently only works for CURL transfers + /** + * @return bool|string false if something went wrong, otherwise string contents + */ + // TODO: max_size currently only works for CURL transfers // TODO: multiple-argument way is deprecated, first parameter is a hash now public static function fetch($options /* previously: 0: $url , 1: $type = false, 2: $login = false, 3: $pass = false, 4: $post_query = false, 5: $timeout = false, 6: $timestamp = 0, 7: $useragent = false*/) { - self::$fetch_last_error = false; + self::$fetch_last_error = ""; self::$fetch_last_error_code = -1; self::$fetch_last_error_content = ""; self::$fetch_last_content_type = ""; @@ -510,7 +522,7 @@ class UrlHelper { } } - public static function url_to_youtube_vid($url) { + public static function url_to_youtube_vid(string $url) { # : bool|string $url = str_replace("youtube.com", "youtube-nocookie.com", $url); $regexps = [ diff --git a/phpstan.neon b/phpstan.neon index 7fe02b07e..8f0447e80 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 5 + level: 6 ignoreErrors: - '#Constant.*\b(SUBSTRING_FOR_DATE|SCHEMA_VERSION|SELF_USER_AGENT|LABEL_BASE_INDEX|PLUGIN_FEED_BASE_INDEX)\b.*not found#' - '#Comparison operation ">" between int<1, max> and 0 is always true.#' diff --git a/update.php b/update.php index b10dde400..36c66b06c 100755 --- a/update.php +++ b/update.php @@ -12,7 +12,7 @@ Config::sanity_check(); - function make_stampfile($filename) { + function make_stampfile(string $filename): bool { $fp = fopen(Config::get(Config::LOCK_DIRECTORY) . "/$filename", "w"); if (flock($fp, LOCK_EX | LOCK_NB)) { @@ -25,7 +25,7 @@ } } - function cleanup_tags($days = 14, $limit = 1000) { + function cleanup_tags(int $days = 14, int $limit = 1000): int { $days = (int) $days; diff --git a/update_daemon2.php b/update_daemon2.php index 8931813ff..46f7450a6 100755 --- a/update_daemon2.php +++ b/update_daemon2.php @@ -37,7 +37,7 @@ /** * @SuppressWarnings(unused) */ - function reap_children() { + function reap_children(): int { global $children; global $ctimes; @@ -64,7 +64,7 @@ return count($tmp); } - function check_ctimes() { + function check_ctimes(): void { global $ctimes; foreach (array_keys($ctimes) as $pid) { @@ -80,7 +80,7 @@ /** * @SuppressWarnings(unused) */ - function sigchld_handler($signal) { + function sigchld_handler(int $signo, mixed $siginfo): void { $running_jobs = reap_children(); Debug::log("Received SIGCHLD, $running_jobs active tasks left."); @@ -88,7 +88,7 @@ pcntl_waitpid(-1, $status, WNOHANG); } - function shutdown($caller_pid) { + function shutdown(int $caller_pid): void { if ($caller_pid == posix_getpid()) { if (file_exists(Config::get(Config::LOCK_DIRECTORY) . "/update_daemon.lock")) { Debug::log("Removing lockfile (master)..."); @@ -97,7 +97,7 @@ } } - function task_shutdown() { + function task_shutdown(): void { $pid = posix_getpid(); if (file_exists(Config::get(Config::LOCK_DIRECTORY) . "/update_daemon-$pid.lock")) { @@ -106,13 +106,13 @@ } } - function sigint_handler() { + function sigint_handler(): void { Debug::log("[MASTER] SIG_INT received, shutting down master process."); shutdown(posix_getpid()); die; } - function task_sigint_handler() { + function task_sigint_handler(): void { Debug::log("[TASK] SIG_INT received, shutting down task."); task_shutdown(); die;