From d8c535c25c2330f9c2b7ee19d74b6d2b4a3bf4b6 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Sat, 13 May 2023 22:47:51 +0200 Subject: PHPStan Level 7 for Share userController logs_pagination (#5393) --- app/Controllers/configureController.php | 2 +- app/Controllers/userController.php | 13 ++++--- app/Models/FeedDAO.php | 7 ++-- app/Models/Share.php | 60 +++++++++++++++++++++------------ app/Models/View.php | 4 +-- app/views/helpers/logs_pagination.phtml | 4 +-- lib/Minz/Paginator.php | 4 +-- tests/phpstan-next.txt | 3 -- 8 files changed, 54 insertions(+), 43 deletions(-) diff --git a/app/Controllers/configureController.php b/app/Controllers/configureController.php index 7611d93b2..2e0b14f4e 100644 --- a/app/Controllers/configureController.php +++ b/app/Controllers/configureController.php @@ -264,7 +264,7 @@ class FreshRSS_configure_Controller extends FreshRSS_ActionController { FreshRSS_Context::$user_conf->volatile = $volatile; $entryDAO = FreshRSS_Factory::createEntryDao(); - $this->view->nb_total = $entryDAO->count() ?: 0; + $this->view->nb_total = $entryDAO->count(); $databaseDAO = FreshRSS_Factory::createDatabaseDAO(); $this->view->size_user = $databaseDAO->size(); diff --git a/app/Controllers/userController.php b/app/Controllers/userController.php index 650f96fb2..f929fc779 100644 --- a/app/Controllers/userController.php +++ b/app/Controllers/userController.php @@ -392,7 +392,10 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController { $oldUserDAO = FreshRSS_Factory::createUserDao($username); $ok &= $oldUserDAO->deleteUser(); $ok &= recursive_unlink($user_data); - array_map('unlink', glob(PSHB_PATH . '/feeds/*/' . $username . '.txt')); + $filenames = glob(PSHB_PATH . '/feeds/*/' . $username . '.txt'); + if (!empty($filenames)) { + array_map('unlink', $filenames); + } } return (bool)$ok; } @@ -628,7 +631,7 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController { $this->view->details = $this->retrieveUserDetails($username); } - /** @return array{'feed_count':int|false,'article_count':int|false,'database_size':int,'language':string,'mail_login':string,'enabled':bool,'is_admin':bool,'last_user_activity':string,'is_default':bool} */ + /** @return array{'feed_count':int,'article_count':int,'database_size':int,'language':string,'mail_login':string,'enabled':bool,'is_admin':bool,'last_user_activity':string,'is_default':bool} */ private function retrieveUserDetails(string $username): array { $feedDAO = FreshRSS_Factory::createFeedDao($username); $entryDAO = FreshRSS_Factory::createEntryDao($username); @@ -636,7 +639,7 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController { $userConfiguration = get_user_configuration($username); - return array( + return [ 'feed_count' => $feedDAO->count(), 'article_count' => $entryDAO->count(), 'database_size' => $databaseDAO->size(), @@ -644,8 +647,8 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController { 'mail_login' => $userConfiguration->mail_login, 'enabled' => $userConfiguration->enabled, 'is_admin' => $userConfiguration->is_admin, - 'last_user_activity' => date('c', FreshRSS_UserDAO::mtime($username)), + 'last_user_activity' => date('c', FreshRSS_UserDAO::mtime($username)) ?: '', 'is_default' => FreshRSS_Context::$system_conf->default_user === $username, - ); + ]; } } diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.php index 67543b597..ec51486a6 100644 --- a/app/Models/FeedDAO.php +++ b/app/Models/FeedDAO.php @@ -640,14 +640,11 @@ SQL; } } - /** - * @return int|false - */ - public function count() { + public function count(): int { $sql = 'SELECT COUNT(e.id) AS count FROM `_feed` e'; $stm = $this->pdo->query($sql); if ($stm == false) { - return false; + return -1; } $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0); return isset($res[0]) ? $res[0] : 0; diff --git a/app/Models/Share.php b/app/Models/Share.php index a0bae22b3..8d8aed97b 100644 --- a/app/Models/Share.php +++ b/app/Models/Share.php @@ -12,7 +12,7 @@ class FreshRSS_Share { /** * Register a new sharing option. - * @param array{'type':string,'url':string,'transform'?:array|array,'field'?:string,'help'?:string,'form'?:'simple'|'advanced', + * @param array{'type':string,'url':string,'transform'?:array|array>,'field'?:string,'help'?:string,'form'?:'simple'|'advanced', * 'method'?:'GET'|'POST','HTMLtag'?:'button','deprecated'?:bool} $share_options is an array defining the share option. */ private static function register(array $share_options): void { @@ -81,7 +81,7 @@ class FreshRSS_Share { private $name = ''; /** @var string */ private $url_transform = ''; - /** @var array|array> */ + /** @var array|array> */ private $transforms = []; /** * @phpstan-var 'simple'|'advanced' @@ -119,7 +119,7 @@ class FreshRSS_Share { * Create a FreshRSS_Share object. * @param string $type is a unique string defining the kind of share option. * @param string $url_transform defines the url format to use in order to share. - * @param array|array> $transforms is an array of transformations to apply on link and title. + * @param array|array> $transforms is an array of transformations to apply on link and title. * @param 'simple'|'advanced' $form_type defines which form we have to use to complete. "simple" * is typically for a centralized service while "advanced" is for * decentralized ones. @@ -154,19 +154,29 @@ class FreshRSS_Share { * in this list: name, url, id, title, link. */ public function update(array $options): void { - $available_options = array( - 'name' => 'custom_name', - 'url' => 'base_url', - 'id' => 'id', - 'title' => 'title', - 'link' => 'link', - 'method' => 'method', - 'field' => 'field', - ); - foreach ($options as $key => $value) { - if (isset($available_options[$key])) { - $this->{$available_options[$key]} = $value; + switch ($key) { + case 'name': + $this->custom_name = $value; + break; + case 'url': + $this->base_url = $value; + break; + case 'id': + $this->id = $value; + break; + case 'title': + $this->title = $value; + break; + case 'link': + $this->link = $value; + break; + case 'method': + $this->method = strcasecmp($value, 'POST') === 0 ? 'POST' : 'GET'; + break; + case 'field'; + $this->field = $value; + break; } } } @@ -300,7 +310,7 @@ class FreshRSS_Share { /** * Transform a data with the given functions. * @param string $data the data to transform. - * @param array $transform an array containing a list of functions to apply. + * @param array $transform an array containing a list of functions to apply. * @return string the transformed data. */ private static function transform(string $data, array $transform): string { @@ -309,9 +319,7 @@ class FreshRSS_Share { } foreach ($transform as $action) { - if (is_string($action) && $action != '') { - $data = call_user_func($action, $data); - } + $data = call_user_func($action, $data); } return $data; @@ -320,13 +328,21 @@ class FreshRSS_Share { /** * Get the list of transformations for the given attribute. * @param string $attr the attribute of which we want the transformations. - * @return array containing a list of transformations to apply. + * @return array containing a list of transformations to apply. */ private function getTransform(string $attr): array { if (array_key_exists($attr, $this->transforms)) { - return $this->transforms[$attr]; + $candidates = is_array($this->transforms[$attr]) ? $this->transforms[$attr] : []; + } else { + $candidates = $this->transforms; } - return $this->transforms; + $transforms = []; + foreach ($candidates as $transform) { + if (is_callable($transform)) { + $transforms[] = $transform; + } + } + return $transforms; } } diff --git a/app/Models/View.php b/app/Models/View.php index cfa06da62..ca237971f 100644 --- a/app/Models/View.php +++ b/app/Models/View.php @@ -49,7 +49,7 @@ class FreshRSS_View extends Minz_View { public $signalError; // Manage users - /** @var array{'feed_count':int|false,'article_count':int|false,'database_size':int,'language':string,'mail_login':string,'enabled':bool,'is_admin':bool,'last_user_activity':string,'is_default':bool} */ + /** @var array{'feed_count':int,'article_count':int,'database_size':int,'language':string,'mail_login':string,'enabled':bool,'is_admin':bool,'last_user_activity':string,'is_default':bool} */ public $details; /** @var bool */ public $disable_aside; @@ -57,7 +57,7 @@ class FreshRSS_View extends Minz_View { public $show_email_field; /** @var string */ public $username; - /** @var array */ + /** @var array */ public $users; // Updates diff --git a/app/views/helpers/logs_pagination.phtml b/app/views/helpers/logs_pagination.phtml index c9f18dc07..349be0760 100755 --- a/app/views/helpers/logs_pagination.phtml +++ b/app/views/helpers/logs_pagination.phtml @@ -9,7 +9,7 @@