Sfoglia il codice sorgente

phpstan-9 for Share.php (#5431)

* phpstan 9 for Search.php
phpstan 9 for Share.php

* phpstan-9 for Search.php

* Better consistency for search results

---------

Co-authored-by: Luc <sanchezluc+freshrss@gmail.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Luc SANCHEZ 2 anni fa
parent
commit
1d2bb50f2e
4 ha cambiato i file con 73 aggiunte e 56 eliminazioni
  1. 38 26
      app/Models/Search.php
  2. 26 19
      app/Models/Share.php
  3. 9 9
      tests/app/Models/SearchTest.php
  4. 0 2
      tests/phpstan-next.txt

+ 38 - 26
app/Models/Search.php

@@ -71,13 +71,8 @@ class FreshRSS_Search {
 	/** @var array<string>|null */
 	private $not_search;
 
-	/**
-	 * @param string|null $input
-	 */
-	public function __construct($input) {
-		if ($input == '') {
-			return;
-		}
+	public function __construct(string $input) {
+		$input = self::cleanSearch($input);
 		$this->raw_input = $input;
 
 		$input = $this->parseNotEntryIds($input);
@@ -238,7 +233,9 @@ class FreshRSS_Search {
 	 * @return array<string>
 	 */
 	private static function removeEmptyValues(?array $anArray): array {
-		return empty($anArray) ? [] : array_filter($anArray, static function(string $value) { return $value !== ''; });
+		return empty($anArray) ? [] : array_filter($anArray, static function(string $value) {
+			return $value !== '';
+		});
 	}
 
 	/**
@@ -299,7 +296,7 @@ class FreshRSS_Search {
 			foreach ($ids_lists as $ids_list) {
 				$feed_ids = explode(',', $ids_list);
 				$feed_ids = self::removeEmptyValues($feed_ids);
-				/** @var array<int> */
+				/** @var array<int> $feed_ids */
 				$feed_ids = array_map('intval', $feed_ids);
 				if (!empty($feed_ids)) {
 					$this->feed_ids = array_merge($this->feed_ids, $feed_ids);
@@ -317,7 +314,7 @@ class FreshRSS_Search {
 			foreach ($ids_lists as $ids_list) {
 				$feed_ids = explode(',', $ids_list);
 				$feed_ids = self::removeEmptyValues($feed_ids);
-				/** @var array<int> */
+				/** @var array<int> $feed_ids */
 				$feed_ids = array_map('intval', $feed_ids);
 				if (!empty($feed_ids)) {
 					$this->not_feed_ids = array_merge($this->not_feed_ids, $feed_ids);
@@ -342,7 +339,7 @@ class FreshRSS_Search {
 				}
 				$label_ids = explode(',', $ids_list);
 				$label_ids = self::removeEmptyValues($label_ids);
-				/** @var array<int> */
+				/** @var array<int> $label_ids */
 				$label_ids = array_map('intval', $label_ids);
 				if (!empty($label_ids)) {
 					$this->label_ids = array_merge($this->label_ids, $label_ids);
@@ -364,7 +361,7 @@ class FreshRSS_Search {
 				}
 				$label_ids = explode(',', $ids_list);
 				$label_ids = self::removeEmptyValues($label_ids);
-				/** @var array<int> */
+				/** @var array<int> $label_ids */
 				$label_ids = array_map('intval', $label_ids);
 				if (!empty($label_ids)) {
 					$this->not_label_ids = array_merge($this->not_label_ids, $label_ids);
@@ -436,10 +433,13 @@ class FreshRSS_Search {
 			$input = str_replace($matches[0], '', $input);
 		}
 		if (preg_match_all('/\bintitle:(?P<search>[^\s"]*)/', $input, $matches)) {
-			$this->intitle = array_merge($this->intitle ? $this->intitle : array(), $matches['search']);
+			$this->intitle = array_merge($this->intitle ?: [], $matches['search']);
 			$input = str_replace($matches[0], '', $input);
 		}
 		$this->intitle = self::removeEmptyValues($this->intitle);
+		if (empty($this->intitle)) {
+			$this->intitle = null;
+		}
 		return $input;
 	}
 
@@ -449,10 +449,13 @@ class FreshRSS_Search {
 			$input = str_replace($matches[0], '', $input);
 		}
 		if (preg_match_all('/(?<=\s|^)[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {
-			$this->not_intitle = array_merge($this->not_intitle ? $this->not_intitle : array(), $matches['search']);
+			$this->not_intitle = array_merge($this->not_intitle ?: [], $matches['search']);
 			$input = str_replace($matches[0], '', $input);
 		}
 		$this->not_intitle = self::removeEmptyValues($this->not_intitle);
+		if (empty($this->not_intitle)) {
+			$this->not_intitle = null;
+		}
 		return $input;
 	}
 
@@ -467,10 +470,13 @@ class FreshRSS_Search {
 			$input = str_replace($matches[0], '', $input);
 		}
 		if (preg_match_all('/\bauthor:(?P<search>[^\s"]*)/', $input, $matches)) {
-			$this->author = array_merge($this->author ? $this->author : array(), $matches['search']);
+			$this->author = array_merge($this->author ?: [], $matches['search']);
 			$input = str_replace($matches[0], '', $input);
 		}
 		$this->author = self::removeEmptyValues($this->author);
+		if (empty($this->author)) {
+			$this->author = null;
+		}
 		return $input;
 	}
 
@@ -480,10 +486,13 @@ class FreshRSS_Search {
 			$input = str_replace($matches[0], '', $input);
 		}
 		if (preg_match_all('/(?<=\s|^)[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {
-			$this->not_author = array_merge($this->not_author ? $this->not_author : array(), $matches['search']);
+			$this->not_author = array_merge($this->not_author ?: [], $matches['search']);
 			$input = str_replace($matches[0], '', $input);
 		}
 		$this->not_author = self::removeEmptyValues($this->not_author);
+		if (empty($this->not_author)) {
+			$this->not_author = null;
+		}
 		return $input;
 	}
 
@@ -495,8 +504,8 @@ class FreshRSS_Search {
 		if (preg_match_all('/\binurl:(?P<search>[^\s]*)/', $input, $matches)) {
 			$this->inurl = $matches['search'];
 			$input = str_replace($matches[0], '', $input);
+			$this->inurl = self::removeEmptyValues($this->inurl);
 		}
-		$this->inurl = self::removeEmptyValues($this->inurl);
 		return $input;
 	}
 
@@ -504,8 +513,8 @@ class FreshRSS_Search {
 		if (preg_match_all('/(?<=\s|^)[!-]inurl:(?P<search>[^\s]*)/', $input, $matches)) {
 			$this->not_inurl = $matches['search'];
 			$input = str_replace($matches[0], '', $input);
+			$this->not_inurl = self::removeEmptyValues($this->not_inurl);
 		}
-		$this->not_inurl = self::removeEmptyValues($this->not_inurl);
 		return $input;
 	}
 
@@ -571,9 +580,9 @@ class FreshRSS_Search {
 		if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
 			$this->tags = $matches['search'];
 			$input = str_replace($matches[0], '', $input);
+			$this->tags = self::removeEmptyValues($this->tags);
+			$this->tags = self::decodeSpaces($this->tags);
 		}
-		$this->tags = self::removeEmptyValues($this->tags);
-		$this->tags = self::decodeSpaces($this->tags);
 		return $input;
 	}
 
@@ -581,9 +590,9 @@ class FreshRSS_Search {
 		if (preg_match_all('/(?<=\s|^)[!-]#(?P<search>[^\s]+)/', $input, $matches)) {
 			$this->not_tags = $matches['search'];
 			$input = str_replace($matches[0], '', $input);
+			$this->not_tags = self::removeEmptyValues($this->not_tags);
+			$this->not_tags = self::decodeSpaces($this->not_tags);
 		}
-		$this->not_tags = self::removeEmptyValues($this->not_tags);
-		$this->not_tags = self::decodeSpaces($this->not_tags);
 		return $input;
 	}
 
@@ -594,7 +603,7 @@ class FreshRSS_Search {
 	 */
 	private function parseQuotedSearch(string $input): string {
 		$input = self::cleanSearch($input);
-		if ($input == '') {
+		if ($input === '') {
 			return '';
 		}
 		if (preg_match_all('/(?<![!-])(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
@@ -611,7 +620,7 @@ class FreshRSS_Search {
 	 */
 	private function parseSearch(string $input): string {
 		$input = self::cleanSearch($input);
-		if ($input == '') {
+		if ($input === '') {
 			return '';
 		}
 		if (is_array($this->search)) {
@@ -624,7 +633,7 @@ class FreshRSS_Search {
 
 	private function parseNotSearch(string $input): string {
 		$input = self::cleanSearch($input);
-		if ($input == '') {
+		if ($input === '') {
 			return '';
 		}
 		if (preg_match_all('/(?<=\s|^)[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
@@ -632,7 +641,7 @@ class FreshRSS_Search {
 			$input = str_replace($matches[0], '', $input);
 		}
 		$input = self::cleanSearch($input);
-		if ($input == '') {
+		if ($input === '') {
 			return '';
 		}
 		if (preg_match_all('/(?<=\s|^)[!-](?P<search>[^\s]+)/', $input, $matches)) {
@@ -648,6 +657,9 @@ class FreshRSS_Search {
 	 */
 	private static function cleanSearch(string $input): string {
 		$input = preg_replace('/\s+/', ' ', $input);
+		if (!is_string($input)) {
+			return '';
+		}
 		return trim($input);
 	}
 }

+ 26 - 19
app/Models/Share.php

@@ -41,7 +41,7 @@ class FreshRSS_Share {
 	public static function load(string $filename): void {
 		$shares_from_file = @include($filename);
 		if (!is_array($shares_from_file)) {
-			$shares_from_file = array();
+			$shares_from_file = [];
 		}
 
 		foreach ($shares_from_file as $share_type => $share_options) {
@@ -50,7 +50,7 @@ class FreshRSS_Share {
 		}
 
 		uasort(self::$list_sharing, static function (FreshRSS_Share $a, FreshRSS_Share $b) {
-			return strcasecmp($a->name(), $b->name());
+			return strcasecmp($a->name() ?? '', $b->name() ?? '');
 		});
 	}
 
@@ -67,29 +67,25 @@ class FreshRSS_Share {
 	 * @return FreshRSS_Share|null object related to the given type.
 	 */
 	public static function get(string $type): ?FreshRSS_Share {
-		if (!isset(self::$list_sharing[$type])) {
-			return null;
-		}
-
-		return self::$list_sharing[$type];
+		return self::$list_sharing[$type] ?? null;
 	}
 
 
 	/** @var string */
-	private $type = '';
+	private $type;
 	/** @var string */
-	private $name = '';
+	private $name;
 	/** @var string */
-	private $url_transform = '';
+	private $url_transform;
 	/** @var array<callable>|array<string,array<callable>> */
-	private $transforms = [];
+	private $transforms;
 	/**
 	 * @phpstan-var 'simple'|'advanced'
 	 * @var string
 	 */
-	private $form_type = 'simple';
+	private $form_type;
 	/** @var string */
-	private $help_url = '';
+	private $help_url;
 	/** @var string|null */
 	private $custom_name = null;
 	/** @var string|null */
@@ -101,12 +97,12 @@ class FreshRSS_Share {
 	/** @var string|null */
 	private $link = null;
 	/** @var bool */
-	private $isDeprecated = false;
+	private $isDeprecated;
 	/**
 	 * @phpstan-var 'GET'|'POST'
 	 * @var string
 	 */
-	private $method = 'GET';
+	private $method;
 	/** @var string|null */
 	private $field;
 	/**
@@ -125,7 +121,9 @@ class FreshRSS_Share {
 	 *        decentralized ones.
 	 * @param string $help_url is an optional url to give help on this option.
 	 * @param 'GET'|'POST' $method defines the sharing method (GET or POST)
+	 * @param string|null $field
 	 * @param 'button'|null $HTMLtag
+	 * @param bool $isDeprecated
 	 */
 	private function __construct(string $type, string $url_transform, array $transforms, string $form_type,
 		string $help_url, string $method, ?string $field, ?string $HTMLtag, bool $isDeprecated = false) {
@@ -231,7 +229,7 @@ class FreshRSS_Share {
 	 * Return the current name of the share option.
 	 */
 	public function name(bool $real = false): ?string {
-		if ($real || is_null($this->custom_name) || empty($this->custom_name)) {
+		if ($real || empty($this->custom_name)) {
 			return $this->name;
 		} else {
 			return $this->custom_name;
@@ -242,7 +240,7 @@ class FreshRSS_Share {
 	 * Return the current base url of the share option.
 	 */
 	public function baseUrl(): string {
-		return $this->base_url;
+		return $this->base_url ?? '';
 	}
 
 	/**
@@ -280,6 +278,9 @@ class FreshRSS_Share {
 			return $this->id;
 		}
 
+		if ($this->id === null) {
+			return null;
+		}
 		return self::transform($this->id, $this->getTransform('id'));
 	}
 
@@ -289,9 +290,12 @@ class FreshRSS_Share {
 	 */
 	public function title(bool $raw = false): string {
 		if ($raw) {
-			return $this->title;
+			return $this->title ?? '';
 		}
 
+		if ($this->title === null) {
+			return '';
+		}
 		return self::transform($this->title, $this->getTransform('title'));
 	}
 
@@ -301,7 +305,10 @@ class FreshRSS_Share {
 	 */
 	public function link(bool $raw = false): string {
 		if ($raw) {
-			return $this->link;
+			return $this->link ?? '';
+		}
+		if ($this->link === null) {
+			return '';
 		}
 
 		return self::transform($this->link, $this->getTransform('link'));

+ 9 - 9
tests/app/Models/SearchTest.php

@@ -7,7 +7,7 @@ class SearchTest extends PHPUnit\Framework\TestCase {
 	/**
 	 * @dataProvider provideEmptyInput
 	 */
-	public function test__construct_whenInputIsEmpty_getsOnlyNullValues(?string $input): void {
+	public function test__construct_whenInputIsEmpty_getsOnlyNullValues(string $input): void {
 		$search = new FreshRSS_Search($input);
 		self::assertEquals('', $search->getRawInput());
 		self::assertNull($search->getIntitle());
@@ -23,13 +23,13 @@ class SearchTest extends PHPUnit\Framework\TestCase {
 	/**
 	 * Return an array of values for the search object.
 	 * Here is the description of the values
-	 * @return array{array{''},array{null}}
+	 * @return array{array{''},array{' '}}
 	 */
 	public function provideEmptyInput(): array {
-		return array(
-			array(''),
-			array(null),
-		);
+		return [
+			[''],
+			[' '],
+		];
 	}
 
 	/**
@@ -58,7 +58,7 @@ class SearchTest extends PHPUnit\Framework\TestCase {
 			array('word1 intitle:"word2 word3"', array('word2 word3'), array('word1')),
 			array("word1 intitle:'word2 word3'", array('word2 word3'), array('word1')),
 			array('intitle:word1 intitle:word2', array('word1', 'word2'), null),
-			array('intitle: word1 word2', array(), array('word1', 'word2')),
+			array('intitle: word1 word2', null, array('word1', 'word2')),
 			array('intitle:123', array('123'), null),
 			array('intitle:"word1 word2" word3"', array('word1 word2'), array('word3"')),
 			array("intitle:'word1 word2' word3'", array('word1 word2'), array("word3'")),
@@ -95,7 +95,7 @@ class SearchTest extends PHPUnit\Framework\TestCase {
 			array('word1 author:"word2 word3"', array('word2 word3'), array('word1')),
 			array("word1 author:'word2 word3'", array('word2 word3'), array('word1')),
 			array('author:word1 author:word2', array('word1', 'word2'), null),
-			array('author: word1 word2', array(), array('word1', 'word2')),
+			array('author: word1 word2', null, array('word1', 'word2')),
 			array('author:123', array('123'), null),
 			array('author:"word1 word2" word3"', array('word1 word2'), array('word3"')),
 			array("author:'word1 word2' word3'", array('word1 word2'), array("word3'")),
@@ -196,7 +196,7 @@ class SearchTest extends PHPUnit\Framework\TestCase {
 	public function provideTagsSearch(): array {
 		return array(
 			array('#word1', array('word1'), null),
-			array('# word1', array(), array('#', 'word1')),
+			array('# word1', null, array('#', 'word1')),
 			array('#123', array('123'), null),
 			array('#word1 word2', array('word1'), array('word2')),
 			array('#"word1 word2"', array('"word1'), array('word2"')),

+ 0 - 2
tests/phpstan-next.txt

@@ -32,8 +32,6 @@
 ./app/Models/Feed.php
 ./app/Models/FeedDAO.php
 ./app/Models/FormAuth.php
-./app/Models/Search.php
-./app/Models/Share.php
 ./app/Models/StatsDAO.php
 ./app/Services/ImportService.php
 ./app/views/auth/index.phtml