summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
authorwn_ <[email protected]>2021-11-10 21:38:25 +0000
committerwn_ <[email protected]>2021-11-10 21:38:25 +0000
commit4cc3374f9f6cdd6ea05d4d16942d0165d8b2ed90 (patch)
tree00f0ff6ba464d3b7741b9f0bb36c306d329d6836 /classes
parent87a30d88d36941ad817f0d1409e2ededbe576af6 (diff)
Initial go at PHPStan rule level 6.
Diffstat (limited to 'classes')
-rw-r--r--classes/config.php41
-rwxr-xr-xclasses/db.php9
-rw-r--r--classes/errors.php6
-rwxr-xr-xclasses/logger.php13
-rw-r--r--classes/logger/adapter.php4
-rwxr-xr-xclasses/logger/sql.php2
-rw-r--r--classes/logger/stdout.php3
-rw-r--r--classes/logger/syslog.php3
-rw-r--r--classes/urlhelper.php42
9 files changed, 84 insertions, 39 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<string, array<bool|int|string>> */
private $params = [];
+
+ /** @var array<string, mixed> */
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, mixed>|string
+ */
static function get_version(bool $as_string = true) {
return self::get_instance()->_get_version($as_string);
}
+ /**
+ * @return array<string, mixed>|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<string, int|string>
+ */
+ 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<int, array<string, string>> 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 "<div class=\"alert alert-danger\">$msg</div>";
}
- 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<string, string> $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 @@
<?php
class Logger {
+ /** @var Logger|null */
private static $instance;
+
+ /** @var Logger_Adapter|null */
private $adapter;
const LOG_DEST_SQL = "sql";
@@ -25,11 +28,11 @@ class Logger {
16384 => '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 @@
<?php
interface Logger_Adapter {
- function log_error(int $errno, string $errstr, string $file, int $line, $context);
-} \ No newline at end of file
+ function log_error(int $errno, string $errstr, string $file, int $line, string $context): bool;
+}
diff --git a/classes/logger/sql.php b/classes/logger/sql.php
index 0f6177a5b..5f3c67852 100755
--- a/classes/logger/sql.php
+++ b/classes/logger/sql.php
@@ -10,7 +10,7 @@ class Logger_SQL implements Logger_Adapter {
ORM::configure('return_result_sets', true, $conn);
}
- function log_error(int $errno, string $errstr, string $file, int $line, $context) {
+ function log_error(int $errno, string $errstr, string $file, int $line, string $context): bool {
if (Config::get_schema_version() > 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 @@
<?php
class Logger_Stdout implements Logger_Adapter {
- function log_error(int $errno, string $errstr, string $file, int $line, $context) {
+ function log_error(int $errno, string $errstr, string $file, int $line, string $context): bool {
switch ($errno) {
case E_ERROR:
@@ -25,6 +25,7 @@ class Logger_Stdout implements Logger_Adapter {
print "[EEE] $priority $errname ($file:$line) $errstr\n";
+ return true;
}
}
diff --git a/classes/logger/syslog.php b/classes/logger/syslog.php
index 3ad9858f3..568398ee0 100644
--- a/classes/logger/syslog.php
+++ b/classes/logger/syslog.php
@@ -1,7 +1,7 @@
<?php
class Logger_Syslog implements Logger_Adapter {
- function log_error(int $errno, string $errstr, string $file, int $line, $context) {
+ function log_error(int $errno, string $errstr, string $file, int $line, string $context): bool {
switch ($errno) {
case E_ERROR:
@@ -25,6 +25,7 @@ class Logger_Syslog implements Logger_Adapter {
syslog($priority, "[tt-rss] $errname ($file:$line) $errstr");
+ return true;
}
}
diff --git a/classes/urlhelper.php b/classes/urlhelper.php
index 4d11b5a4d..5f175af3c 100644
--- a/classes/urlhelper.php
+++ b/classes/urlhelper.php
@@ -6,16 +6,19 @@ class UrlHelper {
"tel"
];
- static $fetch_last_error;
- static $fetch_last_error_code;
- static $fetch_last_error_content;
- static $fetch_last_content_type;
- static $fetch_last_modified;
- static $fetch_effective_url;
- static $fetch_effective_ip_addr;
- static $fetch_curl_used;
-
- static function build_url($parts) {
+ static string $fetch_last_error;
+ static int $fetch_last_error_code;
+ static string $fetch_last_error_content;
+ static string $fetch_last_content_type;
+ static string $fetch_last_modified;
+ static string $fetch_effective_url;
+ static string $fetch_effective_ip_addr;
+ static bool $fetch_curl_used;
+
+ /**
+ * @param array<string, string> $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 = [