From 011c941e7cdfce21d415eb6fa479c411776c79ce Mon Sep 17 00:00:00 2001 From: wn_ Date: Fri, 12 Nov 2021 05:24:02 +0000 Subject: Fix some PHPStan warnings in 'classes/db/migrations.php', 'classes/db/prefs.php', and 'classes/debug.php'. --- classes/db/migrations.php | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'classes/db/migrations.php') diff --git a/classes/db/migrations.php b/classes/db/migrations.php index 3008af535..cb74c247a 100644 --- a/classes/db/migrations.php +++ b/classes/db/migrations.php @@ -1,29 +1,29 @@ 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); $this->initialize($plugin_dir . "/${schema_suffix}", strtolower("ttrss_migrations_plugin_" . get_class($plugin)), $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->migrations_path = $this->base_path . "/migrations"; $this->migrations_table = $migrations_table; @@ -31,7 +31,7 @@ class Db_Migrations { $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); $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)"); } - 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 { if ($version <= $this->get_version()) { 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); Logger::log(E_USER_NOTICE, "Applied migration to version $version for {$this->migrations_table}"); + return true; } else { Debug::log("Migration failed: schema file is empty or missing.", Debug::LOG_VERBOSE); + return false; } } catch (PDOException $e) { @@ -174,6 +180,9 @@ class Db_Migrations { return !$this->is_migration_needed(); } + /** + * @return array + */ private function get_lines(int $version) : array { if ($version > 0) $filename = "{$this->migrations_path}/${version}.sql"; -- cgit v1.2.3 From d3a81f598b24d6ae4f98415fac9509df6749eaf8 Mon Sep 17 00:00:00 2001 From: wn_ Date: Fri, 12 Nov 2021 21:17:31 +0000 Subject: Switch class properties from PHP typing to PHPDoc for compatibility with PHP < 7.4.0 --- classes/db/migrations.php | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'classes/db/migrations.php') diff --git a/classes/db/migrations.php b/classes/db/migrations.php index cb74c247a..6e20ddf7f 100644 --- a/classes/db/migrations.php +++ b/classes/db/migrations.php @@ -1,16 +1,33 @@ pdo = Db::pdo(); -- cgit v1.2.3 From 45431170b629908b9fc39d19a87cf64d90bc9faf Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sat, 13 Nov 2021 17:31:13 +0300 Subject: fix phpstan warnings in classes/db/migrations.php --- classes/db/migrations.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'classes/db/migrations.php') diff --git a/classes/db/migrations.php b/classes/db/migrations.php index 6e20ddf7f..aecd9186c 100644 --- a/classes/db/migrations.php +++ b/classes/db/migrations.php @@ -21,10 +21,10 @@ class Db_Migrations { private $pdo; /** @var int */ - private $cached_version; + private $cached_version = 0; /** @var int */ - private $cached_max_version; + private $cached_max_version = 0; /** @var int */ private $max_version_override; @@ -65,7 +65,7 @@ class Db_Migrations { } function get_version() : int { - if (isset($this->cached_version)) + if ($this->cached_version) return $this->cached_version; try { @@ -152,7 +152,7 @@ class Db_Migrations { if ($this->max_version_override > 0) return $this->max_version_override; - if (isset($this->cached_max_version)) + if ($this->cached_max_version) return $this->cached_max_version; $migrations = glob("{$this->migrations_path}/*.sql"); -- cgit v1.2.3