Browse Source

Fix phpstan level 6 error (#5272)

* Fix phpstan level 6 error

* Better typing

---------

Co-authored-by: Luc <sanchezluc+freshrss@gmail.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Luc SANCHEZ 3 years ago
parent
commit
2882f44179
3 changed files with 22 additions and 29 deletions
  1. 3 0
      app/Models/EntryDAO.php
  2. 19 28
      app/Services/ExportService.php
  3. 0 1
      tests/phpstan-next.txt

+ 3 - 0
app/Models/EntryDAO.php

@@ -1072,6 +1072,7 @@ SQL;
 	}
 
 	/**
+	 * @phpstan-param 'a'|'A'|'s'|'S'|'c'|'f'|'t'|'T'|'ST' $type
 	 * @param int $id category/feed/tag ID
 	 * @return array{0:array<int|string>,1:string}
 	 */
@@ -1135,6 +1136,7 @@ SQL;
 	}
 
 	/**
+	 * @phpstan-param 'a'|'A'|'s'|'S'|'c'|'f'|'t'|'T'|'ST' $type
 	 * @param int $id category/feed/tag ID
 	 * @return PDOStatement|false
 	 */
@@ -1214,6 +1216,7 @@ SQL;
 	}
 
 	/**
+	 * @phpstan-param 'a'|'A'|'s'|'S'|'c'|'f'|'t'|'T'|'ST' $type
 	 * @param int $id category/feed/tag ID
 	 * @return array<numeric-string>|false
 	 */

+ 19 - 28
app/Services/ExportService.php

@@ -19,17 +19,15 @@ class FreshRSS_Export_Service {
 	/** @var FreshRSS_TagDAO */
 	private $tag_dao;
 
-	const FRSS_NAMESPACE = 'https://freshrss.org/opml';
-	const TYPE_HTML_XPATH = 'HTML+XPath';
-	const TYPE_XML_XPATH = 'XML+XPath';
-	const TYPE_RSS_ATOM = 'rss';
+	public const FRSS_NAMESPACE = 'https://freshrss.org/opml';
+	public const TYPE_HTML_XPATH = 'HTML+XPath';
+	public const TYPE_XML_XPATH = 'XML+XPath';
+	public const TYPE_RSS_ATOM = 'rss';
 
 	/**
 	 * Initialize the service for the given user.
-	 *
-	 * @param string $username
 	 */
-	public function __construct($username) {
+	public function __construct(string $username) {
 		$this->username = $username;
 
 		$this->category_dao = FreshRSS_Factory::createCategoryDao($username);
@@ -40,10 +38,9 @@ class FreshRSS_Export_Service {
 
 	/**
 	 * Generate OPML file content.
-	 *
-	 * @return array First item is the filename, second item is the content
+	 * @return array{0:string,1:string} First item is the filename, second item is the content
 	 */
-	public function generateOpml() {
+	public function generateOpml(): array {
 		$view = new FreshRSS_View();
 		$day = date('Y-m-d');
 		$view->categories = $this->category_dao->listCategories(true, true);
@@ -61,14 +58,14 @@ class FreshRSS_Export_Service {
 	 * Both starred and labelled entries are put into a "starred" file, that’s
 	 * why there is only one method for both.
 	 *
+	 * @phpstan-param 'S'|'T'|'ST' $type
 	 * @param string $type must be one of:
 	 *     'S' (starred/favourite),
 	 *     'T' (taggued/labelled),
 	 *     'ST' (starred or labelled)
-	 *
-	 * @return array First item is the filename, second item is the content
+	 * @return array{0:string,1:string} First item is the filename, second item is the content
 	 */
-	public function generateStarredEntries($type) {
+	public function generateStarredEntries(string $type): array {
 		$view = new FreshRSS_View();
 		$view->categories = $this->category_dao->listCategories(true);
 		$day = date('Y-m-d');
@@ -90,14 +87,12 @@ class FreshRSS_Export_Service {
 
 	/**
 	 * Generate the entries file content for the given feed.
-	 *
 	 * @param integer $feed_id
 	 * @param integer $max_number_entries
-	 *
-	 * @return array|null First item is the filename, second item is the content.
+	 * @return array{0:string,1:string}|null First item is the filename, second item is the content.
 	 *                    It also can return null if the feed doesn’t exist.
 	 */
-	public function generateFeedEntries(int $feed_id, int $max_number_entries) {
+	public function generateFeedEntries(int $feed_id, int $max_number_entries): ?array {
 		$feed = $this->feed_dao->searchById($feed_id);
 		if (!$feed) {
 			return null;
@@ -128,12 +123,10 @@ class FreshRSS_Export_Service {
 
 	/**
 	 * Generate the entries file content for all the feeds.
-	 *
-	 * @param integer $max_number_entries
-	 *
-	 * @return array Keys are filenames and values are contents.
+	 * @param int $max_number_entries
+	 * @return array<string,string> Keys are filenames and values are contents.
 	 */
-	public function generateAllFeedEntries($max_number_entries) {
+	public function generateAllFeedEntries(int $max_number_entries): array {
 		$feed_ids = $this->feed_dao->listFeedsIds();
 
 		$exported_files = [];
@@ -143,7 +136,7 @@ class FreshRSS_Export_Service {
 				continue;
 			}
 
-			list($filename, $content) = $result;
+			[$filename, $content] = $result;
 			$exported_files[$filename] = $content;
 		}
 
@@ -152,12 +145,10 @@ class FreshRSS_Export_Service {
 
 	/**
 	 * Compress several files in a Zip file.
-	 *
-	 * @param array $files where first item is the filename, second item is the content
-	 *
-	 * @return array First item is the zip filename, second item is the zip content
+	 * @param array<string,string> $files where the key is the filename, the value is the content
+	 * @return array{0:string,1:string|false} First item is the zip filename, second item is the zip content
 	 */
-	public function zip($files) {
+	public function zip(array $files): array {
 		$day = date('Y-m-d');
 		$zip_filename = 'freshrss_' . $this->username . '_' . $day . '_export.zip';
 

+ 0 - 1
tests/phpstan-next.txt

@@ -17,7 +17,6 @@
 ./app/Models/Share.php
 ./app/Models/TagDAO.php
 ./app/Models/Themes.php
-./app/Services/ExportService.php
 ./app/Services/ImportService.php
 ./cli/i18n/I18nData.php
 ./cli/i18n/I18nFile.php