Explorar o código

PHPStan Level 7 for Share userController logs_pagination (#5393)

Alexandre Alapetite %!s(int64=2) %!d(string=hai) anos
pai
achega
d8c535c25c

+ 1 - 1
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();

+ 8 - 5
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,
-		);
+		];
 	}
 }

+ 2 - 5
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;

+ 38 - 22
app/Models/Share.php

@@ -12,7 +12,7 @@ class FreshRSS_Share {
 
 	/**
 	 * Register a new sharing option.
-	 * @param array{'type':string,'url':string,'transform'?:array<string>|array<string,string>,'field'?:string,'help'?:string,'form'?:'simple'|'advanced',
+	 * @param array{'type':string,'url':string,'transform'?:array<callable>|array<string,array<callable>>,'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<string>|array<string,array<string>> */
+	/** @var array<callable>|array<string,array<callable>> */
 	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<string>|array<string,array<string>> $transforms is an array of transformations to apply on link and title.
+	 * @param array<callable>|array<string,array<callable>> $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<string> $transform an array containing a list of functions to apply.
+	 * @param array<callable> $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<string> containing a list of transformations to apply.
+	 * @return array<callable> 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;
 	}
 }

+ 2 - 2
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<array{'last_user_activity':int,'language':string,'enabled':bool,'is_admin':bool,'enabled':bool,'article_count':int,'database_size':int,'last_user_activity','mail_login':string,'feed_count':int,'is_default':bool}> */
+	/** @var array<array{'language':string,'enabled':bool,'is_admin':bool,'enabled':bool,'article_count':int,'database_size':int,'last_user_activity':string,'mail_login':string,'feed_count':int,'is_default':bool}> */
 	public $users;
 
 	// Updates

+ 1 - 3
app/views/helpers/logs_pagination.phtml

@@ -9,7 +9,7 @@
 <nav class="nav-pagination nav-list">
 	<ul class="pagination">
 		<?php
-			/** @var int $getteur */
+			/** @var string $getteur from Minz_Paginator::render() */
 			$params[$getteur] = 1;
 		?>
 		<li class="item pager-first">
@@ -18,14 +18,12 @@
 
 		<?php $params[$getteur] = $this->currentPage - 1; ?>
 
-
 		<li class="item pager-previous">
 			<?php if ($this->currentPage > 1) { ?>
 			<a href="<?= Minz_Url::display(array('c' => $c, 'a' => $a, 'params' => $params)) ?>">‹ <?= _t('conf.logs.pagination.previous') ?></a>
 			<?php } ?>
 		</li>
 
-
 		<?php if ($this->currentPage - 2 > 1) { ?>
 		<li class="item">…</a></li>
 		<?php } ?>

+ 2 - 2
lib/Minz/Paginator.php

@@ -47,9 +47,9 @@ class Minz_Paginator {
 	/**
 	 * Permet d'afficher la pagination
 	 * @param string $view nom du fichier de vue situé dans /app/views/helpers/
-	 * @param int $getteur variable de type $_GET[] permettant de retrouver la page
+	 * @param string $getteur variable de type $_GET[] permettant de retrouver la page
 	 */
-	public function render(string $view, int $getteur = 0): void {
+	public function render(string $view, string $getteur = '0'): void {
 		$view = APP_PATH . '/views/helpers/' . $view;
 
 		if (file_exists($view)) {

+ 0 - 3
tests/phpstan-next.txt

@@ -3,10 +3,7 @@
 # Can be regenerated with something like:
 # find . -type d -name 'vendor' -prune -o -name '*.php' -exec sh -c 'vendor/bin/phpstan analyse --level 7 --memory-limit 512M {} >/dev/null 2>/dev/null || echo {}' \;
 
-./app/Controllers/userController.php
 ./app/Models/Feed.php
-./app/Models/Share.php
-./app/views/helpers/logs_pagination.phtml
 ./lib/Minz/Error.php
 ./lib/Minz/Mailer.php
 ./lib/Minz/Migrator.php