Fix some PHPStan warnings in 'classes/db/migrations.php', 'classes/db/prefs.php', and 'classes/debug.php'.

This commit is contained in:
wn_ 2021-11-12 05:24:02 +00:00
parent b0eb347839
commit 011c941e7c
3 changed files with 32 additions and 17 deletions

View File

@ -1,29 +1,29 @@
<?php <?php
class Db_Migrations { class Db_Migrations {
private $base_filename = "schema.sql"; private string $base_filename = "schema.sql";
private $base_path; private string $base_path;
private $migrations_path; private string $migrations_path;
private $migrations_table; private string $migrations_table;
private $base_is_latest; private bool $base_is_latest;
private $pdo; private \PDO $pdo;
private $cached_version; private int $cached_version;
private $cached_max_version; private int $cached_max_version;
private $max_version_override; private int $max_version_override;
function __construct() { function __construct() {
$this->pdo = Db::pdo(); $this->pdo = Db::pdo();
} }
function initialize_for_plugin(Plugin $plugin, bool $base_is_latest = true, string $schema_suffix = "sql") { function initialize_for_plugin(Plugin $plugin, bool $base_is_latest = true, string $schema_suffix = "sql"): void {
$plugin_dir = PluginHost::getInstance()->get_plugin_dir($plugin); $plugin_dir = PluginHost::getInstance()->get_plugin_dir($plugin);
$this->initialize($plugin_dir . "/${schema_suffix}", $this->initialize($plugin_dir . "/${schema_suffix}",
strtolower("ttrss_migrations_plugin_" . get_class($plugin)), strtolower("ttrss_migrations_plugin_" . get_class($plugin)),
$base_is_latest); $base_is_latest);
} }
function initialize(string $root_path, string $migrations_table, bool $base_is_latest = true, int $max_version_override = 0) { function initialize(string $root_path, string $migrations_table, bool $base_is_latest = true, int $max_version_override = 0): void {
$this->base_path = "$root_path/" . Config::get(Config::DB_TYPE); $this->base_path = "$root_path/" . Config::get(Config::DB_TYPE);
$this->migrations_path = $this->base_path . "/migrations"; $this->migrations_path = $this->base_path . "/migrations";
$this->migrations_table = $migrations_table; $this->migrations_table = $migrations_table;
@ -31,7 +31,7 @@ class Db_Migrations {
$this->max_version_override = $max_version_override; $this->max_version_override = $max_version_override;
} }
private function set_version(int $version) { private function set_version(int $version): void {
Debug::log("Updating table {$this->migrations_table} with version ${version}...", Debug::LOG_EXTENDED); Debug::log("Updating table {$this->migrations_table} with version ${version}...", Debug::LOG_EXTENDED);
$sth = $this->pdo->query("SELECT * FROM {$this->migrations_table}"); $sth = $this->pdo->query("SELECT * FROM {$this->migrations_table}");
@ -66,11 +66,15 @@ class Db_Migrations {
} }
} }
private function create_migrations_table() { private function create_migrations_table(): void {
$this->pdo->query("CREATE TABLE IF NOT EXISTS {$this->migrations_table} (schema_version integer not null)"); $this->pdo->query("CREATE TABLE IF NOT EXISTS {$this->migrations_table} (schema_version integer not null)");
} }
private function migrate_to(int $version) { /**
* @throws PDOException
* @return bool false if the migration failed, otherwise true (or an exception)
*/
private function migrate_to(int $version): bool {
try { try {
if ($version <= $this->get_version()) { if ($version <= $this->get_version()) {
Debug::log("Refusing to apply version $version: current version is higher", Debug::LOG_VERBOSE); Debug::log("Refusing to apply version $version: current version is higher", Debug::LOG_VERBOSE);
@ -110,8 +114,10 @@ class Db_Migrations {
Debug::log("Migration finished, current version: " . $this->get_version(), Debug::LOG_VERBOSE); Debug::log("Migration finished, current version: " . $this->get_version(), Debug::LOG_VERBOSE);
Logger::log(E_USER_NOTICE, "Applied migration to version $version for {$this->migrations_table}"); Logger::log(E_USER_NOTICE, "Applied migration to version $version for {$this->migrations_table}");
return true;
} else { } else {
Debug::log("Migration failed: schema file is empty or missing.", Debug::LOG_VERBOSE); Debug::log("Migration failed: schema file is empty or missing.", Debug::LOG_VERBOSE);
return false;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
@ -174,6 +180,9 @@ class Db_Migrations {
return !$this->is_migration_needed(); return !$this->is_migration_needed();
} }
/**
* @return array<int, string>
*/
private function get_lines(int $version) : array { private function get_lines(int $version) : array {
if ($version > 0) if ($version > 0)
$filename = "{$this->migrations_path}/${version}.sql"; $filename = "{$this->migrations_path}/${version}.sql";

View File

@ -2,11 +2,17 @@
class Db_Prefs { class Db_Prefs {
// this class is a stub for the time being (to be removed) // this class is a stub for the time being (to be removed)
function read($pref_name, $user_id = false, $die_on_error = false) { /**
* @return bool|int|null|string
*/
function read(string $pref_name, ?int $user_id = null, bool $die_on_error = false) {
return get_pref($pref_name, $user_id); return get_pref($pref_name, $user_id);
} }
function write($pref_name, $value, $user_id = false, $strip_tags = true) { /**
* @param mixed $value
*/
function write(string $pref_name, $value, ?int $user_id = null, bool $strip_tags = true): bool {
return set_pref($pref_name, $value, $user_id, $strip_tags); return set_pref($pref_name, $value, $user_id, $strip_tags);
} }
} }

View File

@ -29,7 +29,7 @@ class Debug {
private static ?string $logfile = null; private static ?string $logfile = null;
/** /**
* @var Debug::LOG_* * @var int Debug::LOG_*
*/ */
private static int $loglevel = self::LOG_NORMAL; private static int $loglevel = self::LOG_NORMAL;