diff options
| author | 2023-03-31 08:23:39 +0200 | |
|---|---|---|
| committer | 2023-03-31 08:23:39 +0200 | |
| commit | 288ed04ccc30b58373576dc3be811aee43e67034 (patch) | |
| tree | 27f4c571e04d64c97737416dfa2b8d65f481dfd8 /app | |
| parent | c9d5fe2da12cbc3a071ebf9a518afe2789bb3d61 (diff) | |
PHPStan level 6 for all PDO and Exception classes (#5239)
* PHPStan level 6 for all PDO and Exception classes
Contributes to https://github.com/FreshRSS/FreshRSS/issues/4112
* Fix type
* Now also our remaining own librairies
* Motivation for a few more files
* A few more DAO classes
* Last interface
Diffstat (limited to 'app')
| -rw-r--r-- | app/Controllers/feedController.php | 5 | ||||
| -rw-r--r-- | app/Controllers/tagController.php | 3 | ||||
| -rw-r--r-- | app/Controllers/userController.php | 6 | ||||
| -rw-r--r-- | app/Exceptions/AlreadySubscribedException.php | 6 | ||||
| -rw-r--r-- | app/Exceptions/BadUrlException.php | 2 | ||||
| -rw-r--r-- | app/Exceptions/FeedNotAddedException.php | 6 | ||||
| -rw-r--r-- | app/Exceptions/ZipException.php | 6 | ||||
| -rw-r--r-- | app/Mailers/UserMailer.php | 2 | ||||
| -rw-r--r-- | app/Models/BooleanSearch.php | 11 | ||||
| -rw-r--r-- | app/Models/CategoryDAO.php | 3 | ||||
| -rw-r--r-- | app/Models/CategoryDAOSQLite.php | 3 | ||||
| -rw-r--r-- | app/Models/DatabaseDAO.php | 18 | ||||
| -rw-r--r-- | app/Models/DatabaseDAOPGSQL.php | 5 | ||||
| -rw-r--r-- | app/Models/DatabaseDAOSQLite.php | 5 | ||||
| -rw-r--r-- | app/Models/FeedDAO.php | 3 | ||||
| -rw-r--r-- | app/Models/FeedDAOSQLite.php | 3 | ||||
| -rw-r--r-- | app/Models/ReadingMode.php | 55 | ||||
| -rw-r--r-- | app/Models/TagDAO.php | 3 | ||||
| -rw-r--r-- | app/Models/TagDAOSQLite.php | 3 | ||||
| -rw-r--r-- | app/Models/UserDAO.php | 13 |
20 files changed, 85 insertions, 76 deletions
diff --git a/app/Controllers/feedController.php b/app/Controllers/feedController.php index 2fcc5eda6..0e98d1e16 100644 --- a/app/Controllers/feedController.php +++ b/app/Controllers/feedController.php @@ -47,10 +47,11 @@ class FreshRSS_feed_Controller extends FreshRSS_ActionController { $url = trim($url); /** @var string|null $url */ - $url = Minz_ExtensionManager::callHook('check_url_before_add', $url); - if (null === $url) { + $urlHooked = Minz_ExtensionManager::callHook('check_url_before_add', $url); + if ($urlHooked === $url) { throw new FreshRSS_FeedNotAdded_Exception($url); } + $url = $urlHooked; $cat = null; if ($cat_id > 0) { diff --git a/app/Controllers/tagController.php b/app/Controllers/tagController.php index b8db23f3e..69844f7bc 100644 --- a/app/Controllers/tagController.php +++ b/app/Controllers/tagController.php @@ -126,7 +126,8 @@ class FreshRSS_tag_Controller extends FreshRSS_ActionController { $sourceId = Minz_Request::param('id_tag'); if ($targetName == '' || $sourceId == '') { - return Minz_Error::error(400); + Minz_Error::error(400); + return; } $tagDAO = FreshRSS_Factory::createTagDao(); diff --git a/app/Controllers/userController.php b/app/Controllers/userController.php index 89489e590..f49406b13 100644 --- a/app/Controllers/userController.php +++ b/app/Controllers/userController.php @@ -431,11 +431,13 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController { } elseif (FreshRSS_Auth::hasAccess()) { $user_config = FreshRSS_Context::$user_conf; } else { - return Minz_Error::error(403); + Minz_Error::error(403); + return; } if (!FreshRSS_UserDAO::exists($username) || $user_config === null) { - return Minz_Error::error(404); + Minz_Error::error(404); + return; } if ($user_config->email_validation_token === '') { diff --git a/app/Exceptions/AlreadySubscribedException.php b/app/Exceptions/AlreadySubscribedException.php index 33b9f9555..c2b11d3a0 100644 --- a/app/Exceptions/AlreadySubscribedException.php +++ b/app/Exceptions/AlreadySubscribedException.php @@ -1,14 +1,16 @@ <?php class FreshRSS_AlreadySubscribed_Exception extends Exception { + + /** @var string */ private $feedName = ''; - public function __construct($url, $feedName) { + public function __construct(string $url, string $feedName) { parent::__construct('Already subscribed! ' . $url, 2135); $this->feedName = $feedName; } - public function feedName() { + public function feedName(): string { return $this->feedName; } } diff --git a/app/Exceptions/BadUrlException.php b/app/Exceptions/BadUrlException.php index d2509e4ba..748a619d6 100644 --- a/app/Exceptions/BadUrlException.php +++ b/app/Exceptions/BadUrlException.php @@ -2,7 +2,7 @@ class FreshRSS_BadUrl_Exception extends FreshRSS_Feed_Exception { - public function __construct($url) { + public function __construct(string $url) { parent::__construct('`' . $url . '` is not a valid URL'); } diff --git a/app/Exceptions/FeedNotAddedException.php b/app/Exceptions/FeedNotAddedException.php index 59fa74b16..b10e93f05 100644 --- a/app/Exceptions/FeedNotAddedException.php +++ b/app/Exceptions/FeedNotAddedException.php @@ -1,14 +1,16 @@ <?php class FreshRSS_FeedNotAdded_Exception extends Exception { + + /** @var string */ private $url = ''; - public function __construct($url) { + public function __construct(string $url) { parent::__construct('Feed not added! ' . $url, 2147); $this->url = $url; } - public function url() { + public function url(): string { return $this->url; } } diff --git a/app/Exceptions/ZipException.php b/app/Exceptions/ZipException.php index 9ed40c4cb..ecf546533 100644 --- a/app/Exceptions/ZipException.php +++ b/app/Exceptions/ZipException.php @@ -1,14 +1,16 @@ <?php class FreshRSS_Zip_Exception extends Exception { + + /** @var int */ private $zipErrorCode = 0; - public function __construct($zipErrorCode) { + public function __construct(int $zipErrorCode) { parent::__construct('ZIP error!', 2141); $this->zipErrorCode = $zipErrorCode; } - public function zipErrorCode() { + public function zipErrorCode(): int { return $this->zipErrorCode; } } diff --git a/app/Mailers/UserMailer.php b/app/Mailers/UserMailer.php index 4450bd96c..c722a5520 100644 --- a/app/Mailers/UserMailer.php +++ b/app/Mailers/UserMailer.php @@ -10,7 +10,7 @@ class FreshRSS_User_Mailer extends Minz_Mailer { */ protected $view; - public function send_email_need_validation($username, $user_config) { + public function send_email_need_validation(string $username, FreshRSS_UserConfiguration $user_config): bool { Minz_Translate::reset($user_config->language); $this->view->_path('user_mailer/email_need_validation.txt.php'); diff --git a/app/Models/BooleanSearch.php b/app/Models/BooleanSearch.php index 279040a5a..f2e16f972 100644 --- a/app/Models/BooleanSearch.php +++ b/app/Models/BooleanSearch.php @@ -10,10 +10,11 @@ class FreshRSS_BooleanSearch { /** @var array<FreshRSS_BooleanSearch|FreshRSS_Search> */ private $searches = array(); - /** @var string 'AND' or 'OR' or 'AND NOT' */ + /** @var 'AND'|'OR'|'AND NOT' */ private $operator; - public function __construct(string $input, int $level = 0, $operator = 'AND') { + /** @param 'AND'|'OR'|'AND NOT' $operator */ + public function __construct(string $input, int $level = 0, string $operator = 'AND') { $this->operator = $operator; $input = trim($input); if ($input == '') { @@ -221,7 +222,7 @@ class FreshRSS_BooleanSearch { return false; } - private function parseOrSegments(string $input) { + private function parseOrSegments(string $input): void { $input = trim($input); if ($input == '') { return; @@ -258,13 +259,13 @@ class FreshRSS_BooleanSearch { return $this->searches; } - /** @return string 'AND' or 'OR' depending on how this BooleanSearch should be combined */ + /** @return 'AND'|'OR'|'AND NOT' depending on how this BooleanSearch should be combined */ public function operator(): string { return $this->operator; } /** @param FreshRSS_BooleanSearch|FreshRSS_Search $search */ - public function add($search) { + public function add($search): void { $this->searches[] = $search; } diff --git a/app/Models/CategoryDAO.php b/app/Models/CategoryDAO.php index 02845ebe7..dd97bd6cc 100644 --- a/app/Models/CategoryDAO.php +++ b/app/Models/CategoryDAO.php @@ -75,7 +75,8 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo { return false; } - protected function autoUpdateDb(array $errorInfo) { + /** @param array<string> $errorInfo */ + protected function autoUpdateDb(array $errorInfo): bool { if (isset($errorInfo[0])) { if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_COLUMN) { $errorLines = explode("\n", $errorInfo[2], 2); // The relevant column name is on the first line, other lines are noise diff --git a/app/Models/CategoryDAOSQLite.php b/app/Models/CategoryDAOSQLite.php index 363ffb427..870106f20 100644 --- a/app/Models/CategoryDAOSQLite.php +++ b/app/Models/CategoryDAOSQLite.php @@ -2,7 +2,8 @@ class FreshRSS_CategoryDAOSQLite extends FreshRSS_CategoryDAO { - protected function autoUpdateDb(array $errorInfo) { + /** @param array<string> $errorInfo */ + protected function autoUpdateDb(array $errorInfo): bool { if ($tableInfo = $this->pdo->query("PRAGMA table_info('category')")) { $columns = $tableInfo->fetchAll(PDO::FETCH_COLUMN, 1); foreach (['kind', 'lastUpdate', 'error', 'attributes'] as $column) { diff --git a/app/Models/DatabaseDAO.php b/app/Models/DatabaseDAO.php index bf9cbb2d3..28dd36cd9 100644 --- a/app/Models/DatabaseDAO.php +++ b/app/Models/DatabaseDAO.php @@ -63,13 +63,15 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo { return count(array_keys($tables, true, true)) == count($tables); } + /** @return array<array<string,string|bool>> */ public function getSchema(string $table): array { $sql = 'DESC `_' . $table . '`'; $stm = $this->pdo->query($sql); return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC)); } - public function checkTable(string $table, $schema): bool { + /** @param array<string> $schema */ + public function checkTable(string $table, array $schema): bool { $columns = $this->getSchema($table); $ok = (count($columns) == count($schema)); @@ -120,6 +122,10 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo { )); } + /** + * @param array<string,string> $dao + * @return array<string,string|bool> + */ public function daoToSchema(array $dao): array { return array( 'name' => $dao['Field'], @@ -129,7 +135,11 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo { ); } - public function listDaoToSchema($listDAO): array { + /** + * @param array<array<string,string>> $listDAO + * @return array<array<string,string|bool>> + */ + public function listDaoToSchema(array $listDAO): array { $list = array(); foreach ($listDAO as $dao) { @@ -185,14 +195,14 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo { return $ok; } - public function minorDbMaintenance() { + public function minorDbMaintenance(): void { $catDAO = FreshRSS_Factory::createCategoryDao(); $catDAO->resetDefaultCategoryName(); $this->ensureCaseInsensitiveGuids(); } - private static function stdError($error): bool { + private static function stdError(string $error): bool { if (defined('STDERR')) { fwrite(STDERR, $error . "\n"); } diff --git a/app/Models/DatabaseDAOPGSQL.php b/app/Models/DatabaseDAOPGSQL.php index 6ff706250..b1db0f347 100644 --- a/app/Models/DatabaseDAOPGSQL.php +++ b/app/Models/DatabaseDAOPGSQL.php @@ -33,6 +33,7 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite { return count(array_keys($tables, true, true)) == count($tables); } + /** @return array<array<string,string|bool>> */ public function getSchema(string $table): array { $sql = 'select column_name as field, data_type as type, column_default as default, is_nullable as null from INFORMATION_SCHEMA.COLUMNS where table_name = ?'; $stm = $this->pdo->prepare($sql); @@ -40,6 +41,10 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite { return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC)); } + /** + * @param array<string,string> $dao + * @return array<string,string|bool> + */ public function daoToSchema(array $dao): array { return array( 'name' => $dao['field'], diff --git a/app/Models/DatabaseDAOSQLite.php b/app/Models/DatabaseDAOSQLite.php index e5a6f5a04..3fab1134d 100644 --- a/app/Models/DatabaseDAOSQLite.php +++ b/app/Models/DatabaseDAOSQLite.php @@ -25,6 +25,7 @@ class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO { return count(array_keys($tables, true, true)) == count($tables); } + /** @return array<array<string,string|bool>> */ public function getSchema(string $table): array { $sql = 'PRAGMA table_info(' . $table . ')'; $stm = $this->pdo->query($sql); @@ -45,6 +46,10 @@ class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO { )); } + /** + * @param array<string,string> $dao + * @return array<string,string|bool> + */ public function daoToSchema(array $dao): array { return [ 'name' => $dao['name'], diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.php index 1047a218b..2a123e0db 100644 --- a/app/Models/FeedDAO.php +++ b/app/Models/FeedDAO.php @@ -19,7 +19,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo { return false; } - protected function autoUpdateDb(array $errorInfo) { + /** @param array<string> $errorInfo */ + protected function autoUpdateDb(array $errorInfo): bool { if (isset($errorInfo[0])) { if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_COLUMN) { $errorLines = explode("\n", $errorInfo[2], 2); // The relevant column name is on the first line, other lines are noise diff --git a/app/Models/FeedDAOSQLite.php b/app/Models/FeedDAOSQLite.php index a4432ea62..08a352d5f 100644 --- a/app/Models/FeedDAOSQLite.php +++ b/app/Models/FeedDAOSQLite.php @@ -2,7 +2,8 @@ class FreshRSS_FeedDAOSQLite extends FreshRSS_FeedDAO { - protected function autoUpdateDb(array $errorInfo) { + /** @param array<string> $errorInfo */ + protected function autoUpdateDb(array $errorInfo): bool { if ($tableInfo = $this->pdo->query("PRAGMA table_info('feed')")) { $columns = $tableInfo->fetchAll(PDO::FETCH_COLUMN, 1); foreach (['attributes', 'kind'] as $column) { diff --git a/app/Models/ReadingMode.php b/app/Models/ReadingMode.php index ddb413315..6f2fc889c 100644 --- a/app/Models/ReadingMode.php +++ b/app/Models/ReadingMode.php @@ -28,12 +28,9 @@ class FreshRSS_ReadingMode { /** * ReadingMode constructor. - * @param string $id - * @param string $title - * @param string[] $urlParams - * @param bool $active + * @param array<string> $urlParams */ - public function __construct($id, $title, $urlParams, $active) { + public function __construct(string $id, string $title, array $urlParams, bool $active) { $this->id = $id; $this->name = _i($id); $this->title = $title; @@ -41,41 +38,24 @@ class FreshRSS_ReadingMode { $this->isActive = $active; } - /** - * @return string - */ - public function getId() { + public function getId(): string { return $this->id; } - /** - * @return string - */ - public function getName() { + public function getName(): string { return $this->name; } - /** - * @param string $name - * @return FreshRSS_ReadingMode - */ - public function setName($name) { + public function setName(string $name): FreshRSS_ReadingMode { $this->name = $name; return $this; } - /** - * @return string - */ - public function getTitle() { + public function getTitle(): string { return $this->title; } - /** - * @param string $title - * @return FreshRSS_ReadingMode - */ - public function setTitle($title) { + public function setTitle(string $title): FreshRSS_ReadingMode { $this->title = $title; return $this; } @@ -83,40 +63,31 @@ class FreshRSS_ReadingMode { /** * @return array<string> */ - public function getUrlParams() { + public function getUrlParams(): array { return $this->urlParams; } /** * @param array<string> $urlParams - * @return FreshRSS_ReadingMode */ - public function setUrlParams($urlParams) { + public function setUrlParams(array $urlParams): FreshRSS_ReadingMode { $this->urlParams = $urlParams; return $this; } - /** - * @return bool - */ - public function isActive() { + public function isActive(): bool { return $this->isActive; } - /** - * @param bool $isActive - * @return FreshRSS_ReadingMode - */ - public function setIsActive($isActive) { + public function setIsActive(bool $isActive): FreshRSS_ReadingMode { $this->isActive = $isActive; return $this; } /** - * Returns the built-in reading modes. - * return ReadingMode[] + * @return array<FreshRSS_ReadingMode> the built-in reading modes */ - public static function getReadingModes() { + public static function getReadingModes(): array { $actualView = Minz_Request::actionName(); $defaultCtrl = Minz_Request::defaultControllerName(); $isDefaultCtrl = Minz_Request::controllerName() === $defaultCtrl; diff --git a/app/Models/TagDAO.php b/app/Models/TagDAO.php index ca927ef38..8dc392160 100644 --- a/app/Models/TagDAO.php +++ b/app/Models/TagDAO.php @@ -30,7 +30,8 @@ class FreshRSS_TagDAO extends Minz_ModelPdo { return $ok; } - protected function autoUpdateDb(array $errorInfo) { + /** @param array<string> $errorInfo */ + protected function autoUpdateDb(array $errorInfo): bool { if (isset($errorInfo[0])) { if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_TABLE_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_TABLE) { if (stripos($errorInfo[2], 'tag') !== false) { diff --git a/app/Models/TagDAOSQLite.php b/app/Models/TagDAOSQLite.php index 530669699..910455546 100644 --- a/app/Models/TagDAOSQLite.php +++ b/app/Models/TagDAOSQLite.php @@ -6,7 +6,8 @@ class FreshRSS_TagDAOSQLite extends FreshRSS_TagDAO { return 'OR IGNORE'; } - protected function autoUpdateDb(array $errorInfo) { + /** @param array<string> $errorInfo */ + protected function autoUpdateDb(array $errorInfo): bool { if ($tableInfo = $this->pdo->query("SELECT sql FROM sqlite_master where name='tag'")) { $showCreate = $tableInfo->fetchColumn(); if (stripos($showCreate, 'tag') === false) { diff --git a/app/Models/UserDAO.php b/app/Models/UserDAO.php index 9f91df80e..8ba50cc29 100644 --- a/app/Models/UserDAO.php +++ b/app/Models/UserDAO.php @@ -1,7 +1,8 @@ <?php class FreshRSS_UserDAO extends Minz_ModelPdo { - public function createUser() { + + public function createUser(): bool { require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php'); try { @@ -21,7 +22,7 @@ class FreshRSS_UserDAO extends Minz_ModelPdo { } } - public function deleteUser() { + public function deleteUser(): bool { if (defined('STDERR')) { fwrite(STDERR, 'Deleting SQL data for user “' . $this->current_user . "”…\n"); } @@ -38,18 +39,18 @@ class FreshRSS_UserDAO extends Minz_ModelPdo { } } - public static function exists($username) { + public static function exists(string $username): bool { return is_dir(USERS_PATH . '/' . $username); } - public static function touch($username = '') { + public static function touch(string $username = ''): bool { if (!FreshRSS_user_Controller::checkUsername($username)) { $username = Minz_User::name() ?? Minz_User::INTERNAL_USER; } return touch(USERS_PATH . '/' . $username . '/config.php'); } - public static function mtime($username) { - return @filemtime(USERS_PATH . '/' . $username . '/config.php'); + public static function mtime(string $username): int { + return @(int)filemtime(USERS_PATH . '/' . $username . '/config.php'); } } |
