diff options
| author | 2023-12-18 17:59:16 +0100 | |
|---|---|---|
| committer | 2023-12-18 17:59:16 +0100 | |
| commit | a80a5f48a16e7d232168a7aaa68e9a1804235ce1 (patch) | |
| tree | a515b88592629dea7e83b96e26e2452d3f98a98e /app/Models/Context.php | |
| parent | 6bb45a87268157aab961a6a4a728d9a9bbe043b0 (diff) | |
Pass PHPStan level 8 (#5946)
* Pass PHPStan level 8
And prepare for PHPStan level 9 https://phpstan.org/user-guide/rule-levels
* Revert wrong replace in comment
* Fix PHPStan level 8
* Update PHPStan and other dev dependencies
* Remove obsolete comment
* noVariableVariables and towards bleedingEdge
https://github.com/phpstan/phpstan-strict-rules
https://phpstan.org/blog/what-is-bleeding-edge
* More bleedingEdge
* A bit more PHPStan level 9
* More PHPStan level 9
* Prepare for booleansInConditions
Ignore int and null
* Revert wrong line
* More fixes
* Fix keep_max_n_unread
* Stricter attribute functions
* Stricter callHooks and more PHPStan level 9
* More typing
* A tiny more
Diffstat (limited to 'app/Models/Context.php')
| -rw-r--r-- | app/Models/Context.php | 82 |
1 files changed, 58 insertions, 24 deletions
diff --git a/app/Models/Context.php b/app/Models/Context.php index ac5547aa1..3ea5a29eb 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -7,8 +7,6 @@ declare(strict_types=1); */ final class FreshRSS_Context { - public static ?FreshRSS_UserConfiguration $user_conf = null; - public static ?FreshRSS_SystemConfiguration $system_conf = null; /** * @var array<int,FreshRSS_Category> */ @@ -57,21 +55,42 @@ final class FreshRSS_Context { public static bool $isCli = false; /** + * @deprecated Will be made `private`; use `FreshRSS_Context::systemConf()` instead. + * @internal + */ + public static ?FreshRSS_SystemConfiguration $system_conf = null; + /** + * @deprecated Will be made `private`; use `FreshRSS_Context::userConf()` instead. + * @internal + */ + public static ?FreshRSS_UserConfiguration $user_conf = null; + + /** * Initialize the context for the global system. */ - public static function initSystem(bool $reload = false): FreshRSS_SystemConfiguration { - if ($reload || FreshRSS_Context::$system_conf == null) { + public static function initSystem(bool $reload = false): void { + if ($reload || FreshRSS_Context::$system_conf === null) { //TODO: Keep in session what we need instead of always reloading from disk FreshRSS_Context::$system_conf = FreshRSS_SystemConfiguration::init(DATA_PATH . '/config.php', FRESHRSS_PATH . '/config.default.php'); } + } + + public static function &systemConf(): FreshRSS_SystemConfiguration { + if (FreshRSS_Context::$system_conf === null) { + throw new FreshRSS_Context_Exception('System configuration not initialised!'); + } return FreshRSS_Context::$system_conf; } + public static function hasSystemConf(): bool { + return FreshRSS_Context::$system_conf !== null; + } + /** * Initialize the context for the current user. * @throws Minz_ConfigurationParamException */ - public static function initUser(string $username = '', bool $userMustExist = true): ?FreshRSS_UserConfiguration { + public static function initUser(string $username = '', bool $userMustExist = true): void { FreshRSS_Context::$user_conf = null; if (!isset($_SESSION)) { Minz_Session::init('FreshRSS'); @@ -103,14 +122,16 @@ final class FreshRSS_Context { Minz_Session::unlock(); if (FreshRSS_Context::$user_conf == null) { - return null; + return; } FreshRSS_Context::$search = new FreshRSS_BooleanSearch(''); //Legacy - $oldEntries = (int)FreshRSS_Context::$user_conf->param('old_entries', 0); - $keepMin = (int)FreshRSS_Context::$user_conf->param('keep_history_default', -5); + $oldEntries = FreshRSS_Context::$user_conf->param('old_entries', 0); + $oldEntries = is_numeric($oldEntries) ? (int)$oldEntries : 0; + $keepMin = FreshRSS_Context::$user_conf->param('keep_history_default', -5); + $keepMin = is_numeric($keepMin) ? (int)$keepMin : -5; if ($oldEntries > 0 || $keepMin > -5) { //Freshrss < 1.15 $archiving = FreshRSS_Context::$user_conf->archiving; $archiving['keep_max'] = false; @@ -130,10 +151,23 @@ final class FreshRSS_Context { if (!in_array(FreshRSS_Context::$user_conf->display_categories, [ 'active', 'remember', 'all', 'none' ], true)) { FreshRSS_Context::$user_conf->display_categories = FreshRSS_Context::$user_conf->display_categories === true ? 'all' : 'active'; } + } + public static function &userConf(): FreshRSS_UserConfiguration { + if (FreshRSS_Context::$user_conf === null) { + throw new FreshRSS_Context_Exception('User configuration not initialised!'); + } return FreshRSS_Context::$user_conf; } + public static function hasUserConf(): bool { + return FreshRSS_Context::$user_conf !== null; + } + + public static function clearUserConf(): void { + FreshRSS_Context::$user_conf = null; + } + /** * This action updates the Context object by using request parameters. * @@ -162,28 +196,28 @@ final class FreshRSS_Context { self::_get(Minz_Request::paramString('get') ?: 'a'); - self::$state = Minz_Request::paramInt('state') ?: self::$user_conf->default_state; + self::$state = Minz_Request::paramInt('state') ?: FreshRSS_Context::userConf()->default_state; $state_forced_by_user = Minz_Request::paramString('state') !== ''; if (!$state_forced_by_user && !self::isStateEnabled(FreshRSS_Entry::STATE_READ)) { - if (self::$user_conf->default_view === 'all') { + if (FreshRSS_Context::userConf()->default_view === 'all') { self::$state |= FreshRSS_Entry::STATE_ALL; - } elseif (self::$user_conf->default_view === 'adaptive' && self::$get_unread <= 0) { + } elseif (FreshRSS_Context::userConf()->default_view === 'adaptive' && self::$get_unread <= 0) { self::$state |= FreshRSS_Entry::STATE_READ; } - if (self::$user_conf->show_fav_unread && + if (FreshRSS_Context::userConf()->show_fav_unread && (self::isCurrentGet('s') || self::isCurrentGet('T') || self::isTag())) { self::$state |= FreshRSS_Entry::STATE_READ; } } self::$search = new FreshRSS_BooleanSearch(Minz_Request::paramString('search')); - $order = Minz_Request::paramString('order') ?: self::$user_conf->sort_order; + $order = Minz_Request::paramString('order') ?: FreshRSS_Context::userConf()->sort_order; self::$order = in_array($order, ['ASC', 'DESC'], true) ? $order : 'DESC'; - self::$number = Minz_Request::paramInt('nb') ?: self::$user_conf->posts_per_page; - if (self::$number > self::$user_conf->max_posts_per_rss) { + self::$number = Minz_Request::paramInt('nb') ?: FreshRSS_Context::userConf()->posts_per_page; + if (self::$number > FreshRSS_Context::userConf()->max_posts_per_rss) { self::$number = max( - self::$user_conf->max_posts_per_rss, - self::$user_conf->posts_per_page); + FreshRSS_Context::userConf()->max_posts_per_rss, + FreshRSS_Context::userConf()->posts_per_page); } self::$first_id = Minz_Request::paramString('next'); self::$sinceHours = Minz_Request::paramInt('hours'); @@ -335,19 +369,19 @@ final class FreshRSS_Context { case 'a': self::$current_get['all'] = true; self::$name = _t('index.feed.title'); - self::$description = self::$system_conf->meta_description; + self::$description = FreshRSS_Context::systemConf()->meta_description; self::$get_unread = self::$total_unread; break; case 'i': self::$current_get['important'] = true; self::$name = _t('index.menu.important'); - self::$description = self::$system_conf->meta_description; + self::$description = FreshRSS_Context::systemConf()->meta_description; self::$get_unread = self::$total_unread; break; case 's': self::$current_get['starred'] = true; self::$name = _t('index.feed.title_fav'); - self::$description = self::$system_conf->meta_description; + self::$description = FreshRSS_Context::systemConf()->meta_description; self::$get_unread = self::$total_starred['unread']; // Update state if favorite is not yet enabled. @@ -355,7 +389,7 @@ final class FreshRSS_Context { break; case 'f': // We try to find the corresponding feed. When allowing robots, always retrieve the full feed including description - $feed = FreshRSS_Context::$system_conf->allow_robots ? null : FreshRSS_CategoryDAO::findFeed(self::$categories, $id); + $feed = FreshRSS_Context::systemConf()->allow_robots ? null : FreshRSS_CategoryDAO::findFeed(self::$categories, $id); if ($feed === null) { $feedDAO = FreshRSS_Factory::createFeedDao(); $feed = $feedDAO->searchById($id); @@ -427,7 +461,7 @@ final class FreshRSS_Context { self::$categories = $catDAO->listCategories(); } - if (self::$user_conf->onread_jump_next && strlen($get) > 2) { + if (FreshRSS_Context::userConf()->onread_jump_next && strlen($get) > 2) { $another_unread_id = ''; $found_current_get = false; switch ($get[0]) { @@ -491,7 +525,7 @@ final class FreshRSS_Context { * - the "unread" state is enable */ public static function isAutoRemoveAvailable(): bool { - if (!self::$user_conf->auto_remove_article) { + if (!FreshRSS_Context::userConf()->auto_remove_article) { return false; } if (self::isStateEnabled(FreshRSS_Entry::STATE_READ)) { @@ -510,7 +544,7 @@ final class FreshRSS_Context { * are read. */ public static function isStickyPostEnabled(): bool { - if (self::$user_conf->sticky_post) { + if (FreshRSS_Context::userConf()->sticky_post) { return true; } if (self::isAutoRemoveAvailable()) { |
