Przeglądaj źródła

Add some missing PHP native types (#7191)

* Add some missing PHP native types
Replaces https://github.com/FreshRSS/FreshRSS/pull/7184

* Clean some types
Alexandre Alapetite 1 rok temu
rodzic
commit
50adb55982

+ 4 - 1
app/Controllers/importExportController.php

@@ -457,10 +457,13 @@ class FreshRSS_importExport_Controller extends FreshRSS_ActionController {
 				$published = '0';
 			}
 			if (!ctype_digit($published)) {
-				$published = (string)strtotime($published);
+				$published = (string)(strtotime($published) ?: 0);
 			}
 			if (strlen($published) > 10) {	// Milliseconds, e.g. Feedly
 				$published = substr($published, 0, -3);
+				if (!is_numeric($published)) {
+					$published = '0';	// For PHPStan
+				}
 			}
 
 			$entry = new FreshRSS_Entry(

+ 1 - 1
app/Models/AttributesTrait.php

@@ -48,7 +48,7 @@ trait FreshRSS_AttributesTrait {
 	}
 
 	/** @param string|array<string,mixed> $values Values, not HTML-encoded */
-	public function _attributes($values): void {
+	public function _attributes(string|array $values): void {
 		if (is_string($values)) {
 			$values = json_decode($values, true);
 		}

+ 1 - 1
app/Models/BooleanSearch.php

@@ -412,7 +412,7 @@ class FreshRSS_BooleanSearch implements \Stringable {
 	}
 
 	/** @param FreshRSS_BooleanSearch|FreshRSS_Search $search */
-	public function add($search): void {
+	public function add(FreshRSS_BooleanSearch|FreshRSS_Search $search): void {
 		$this->searches[] = $search;
 	}
 

+ 1 - 2
app/Models/Category.php

@@ -65,8 +65,7 @@ class FreshRSS_Category extends Minz_Model {
 		return $this->error;
 	}
 
-	/** @param bool|int $value */
-	public function _error($value): void {
+	public function _error(bool|int $value): void {
 		$this->error = (bool)$value;
 	}
 	public function isDefault(): bool {

+ 19 - 15
app/Models/Entry.php

@@ -37,6 +37,7 @@ class FreshRSS_Entry extends Minz_Model {
 
 	/**
 	 * @param string|array<string> $tags
+	 * @param int|numeric-string $pubdate
 	 */
 	public function __construct(int $feedId = 0, string $guid = '', string $title = '', string $authors = '', string $content = '',
 			string $link = '', int|string $pubdate = 0, bool|int|null $is_read = false, bool|int|null $is_favorite = false, $tags = '') {
@@ -59,6 +60,10 @@ class FreshRSS_Entry extends Minz_Model {
 			$dao['content'] = '';
 		}
 
+		if (!is_numeric($dao['date'] ?? null)) {
+			$dao['date'] = 0;
+		}
+
 		$dao['attributes'] = empty($dao['attributes']) ? [] : json_decode($dao['attributes'], true);
 		if (!is_array($dao['attributes'])) {
 			$dao['attributes'] = [];
@@ -487,7 +492,7 @@ HTML;
 	}
 
 	/** @param int|numeric-string $value String is for compatibility with 32-bit platforms */
-	public function _id($value): void {
+	public function _id(int|string $value): void {
 		if (is_int($value)) {
 			$value = (string)$value;
 		}
@@ -510,7 +515,7 @@ HTML;
 		$this->_authors($value);
 	}
 	/** @param array<string>|string $value */
-	public function _authors($value): void {
+	public function _authors(array|string $value): void {
 		$this->hash = '';
 		if (!is_array($value)) {
 			if (str_contains($value, ';')) {
@@ -531,8 +536,8 @@ HTML;
 		$this->hash = '';
 		$this->link = trim($value);
 	}
-	/** @param int|string $value */
-	public function _date($value): void {
+	/** @param int|numeric-string $value */
+	public function _date(int|string $value): void {
 		$value = (int)$value;
 		$this->date = $value > 1 ? $value : time();
 	}
@@ -541,20 +546,20 @@ HTML;
 		$this->lastSeen = $value > 0 ? $value : 0;
 	}
 
-	/** @param int|string $value */
-	public function _dateAdded($value, bool $microsecond = false): void {
+	/** @param int|numeric-string $value */
+	public function _dateAdded(int|string $value, bool $microsecond = false): void {
 		if ($microsecond) {
 			$this->date_added = (string)($value);
 		} else {
 			$this->date_added = $value . '000000';
 		}
 	}
-	/** @param bool|int|null $value */
-	public function _isRead($value): void {
+
+	public function _isRead(bool|int|null $value): void {
 		$this->is_read = $value === null ? null : (bool)$value;
 	}
-	/** @param bool|int|null $value */
-	public function _isFavorite($value): void {
+
+	public function _isFavorite(bool|int|null $value): void {
 		$this->is_favorite = $value === null ? null : (bool)$value;
 	}
 
@@ -563,14 +568,13 @@ HTML;
 		$this->feedId = $this->feed == null ? 0 : $this->feed->id();
 	}
 
-	/** @param int|string $id */
-	private function _feedId($id): void {
+	private function _feedId(int $id): void {
 		$this->feed = null;
-		$this->feedId = (int)$id;
+		$this->feedId = $id;
 	}
 
 	/** @param array<string>|string $value */
-	public function _tags($value): void {
+	public function _tags(array|string $value): void {
 		$this->hash = '';
 		if (!is_array($value)) {
 			$value = preg_split('/\s*[#,]\s*/', $value, -1, PREG_SPLIT_NO_EMPTY) ?: [];
@@ -1052,7 +1056,7 @@ HTML;
 	 * @param numeric-string|int $dec Decimal number
 	 * @return string 64-bit hexa http://code.google.com/p/google-reader-api/wiki/ItemId
 	 */
-	private static function dec2hex($dec): string {
+	private static function dec2hex(string|int $dec): string {
 		return PHP_INT_SIZE < 8 ? // 32-bit ?
 			str_pad(gmp_strval(gmp_init($dec, 10), 16), 16, '0', STR_PAD_LEFT) :
 			str_pad(dechex((int)($dec)), 16, '0', STR_PAD_LEFT);

+ 1 - 1
app/Models/EntryDAO.php

@@ -344,7 +344,7 @@ SQL;
 	 *
 	 * @param numeric-string|list<numeric-string> $ids
 	 */
-	public function markFavorite($ids, bool $is_favorite = true): int|false {
+	public function markFavorite(string|array $ids, bool $is_favorite = true): int|false {
 		if (!is_array($ids)) {
 			$ids = [$ids];
 		}

+ 1 - 1
app/Models/EntryDAOSQLite.php

@@ -153,7 +153,7 @@ SQL;
 	 * @return int|false affected rows
 	 */
 	#[\Override]
-	public function markReadTag($id = 0, string $idMax = '0', ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true): int|false {
+	public function markReadTag(int $id = 0, string $idMax = '0', ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true): int|false {
 		FreshRSS_UserDAO::touch();
 		if ($idMax == 0) {
 			$idMax = time() . '000000';

+ 8 - 5
app/Models/Feed.php

@@ -287,8 +287,8 @@ class FreshRSS_Feed extends Minz_Model {
 		$this->categoryId = $this->category == null ? 0 : $this->category->id();
 	}
 
-	/** @param int|string $id */
-	public function _categoryId($id): void {
+	/** @param int|numeric-string $id */
+	public function _categoryId(int|string $id): void {
 		$this->category = null;
 		$this->categoryId = (int)$id;
 	}
@@ -321,8 +321,8 @@ class FreshRSS_Feed extends Minz_Model {
 	public function _httpAuth(string $value): void {
 		$this->httpAuth = $value;
 	}
-	/** @param bool|int $value */
-	public function _error($value): void {
+
+	public function _error(bool|int $value): void {
 		$this->error = (bool)$value;
 	}
 	public function _mute(bool $value): void {
@@ -350,7 +350,7 @@ class FreshRSS_Feed extends Minz_Model {
 			/**
 			 * @throws Minz_FileNotExistException
 			 */
-			if (CACHE_PATH == '') {
+			if (trim(CACHE_PATH) === '') {
 				throw new Minz_FileNotExistException(
 					'CACHE_PATH',
 					Minz_Exception::ERROR
@@ -549,6 +549,9 @@ class FreshRSS_Feed extends Minz_Model {
 			$authors = $item->get_authors();
 			$link = $item->get_permalink();
 			$date = $item->get_date('U');
+			if (!is_numeric($date)) {
+				$date = 0;
+			}
 
 			//Tag processing (tag == category)
 			$categories = $item->get_categories();

+ 6 - 6
app/Models/Tag.php

@@ -17,8 +17,8 @@ class FreshRSS_Tag extends Minz_Model {
 		return $this->id;
 	}
 
-	public function _id(int|string $value): void {
-		$this->id = (int)$value;
+	public function _id(int $value): void {
+		$this->id = $value;
 	}
 
 	public function name(): string {
@@ -37,8 +37,8 @@ class FreshRSS_Tag extends Minz_Model {
 		return $this->nbEntries;
 	}
 
-	public function _nbEntries(int|string $value): void {
-		$this->nbEntries = (int)$value;
+	public function _nbEntries(int $value): void {
+		$this->nbEntries = $value;
 	}
 
 	public function nbUnread(): int {
@@ -49,7 +49,7 @@ class FreshRSS_Tag extends Minz_Model {
 		return $this->nbUnread;
 	}
 
-	public function _nbUnread(int|string $value): void {
-		$this->nbUnread = (int)$value;
+	public function _nbUnread(int $value): void {
+		$this->nbUnread = $value;
 	}
 }

+ 1 - 1
app/Models/TagDAO.php

@@ -407,7 +407,7 @@ SQL;
 	}
 
 	/**
-	 * @param iterable<array{id:int,name:string,attributes?:string}> $listDAO
+	 * @param iterable<array{id:int,name:string,attributes?:string,unreads?:int}> $listDAO
 	 * @return array<int,FreshRSS_Tag> where the key is the label ID
 	 */
 	private static function daoToTags(iterable $listDAO): array {

+ 1 - 1
cli/i18n/I18nValue.php

@@ -15,7 +15,7 @@ class I18nValue implements \Stringable {
 	private ?string $state = null;
 
 	/** @param I18nValue|string $data */
-	public function __construct($data) {
+	public function __construct(I18nValue|string $data) {
 		if ($data instanceof I18nValue) {
 			$data = $data->__toString();
 		}

+ 1 - 1
lib/Minz/Error.php

@@ -52,7 +52,7 @@ class Minz_Error {
 	 * @param string|array<'error'|'warning'|'notice',list<string>> $logs logs sorted by category (error, warning, notice)
 	 * @return list<string> list of matching logs, without the category, according to environment preferences (production / development)
 	 */
-	private static function processLogs($logs): array {
+	private static function processLogs(string|array $logs): array {
 		if (is_string($logs)) {
 			return [$logs];
 		}

+ 4 - 4
lib/Minz/Paginator.php

@@ -64,11 +64,11 @@ class Minz_Paginator {
 	 * @param Minz_Model $item l'élément à retrouver
 	 * @return int|false la page à laquelle se trouve l’élément, false si non trouvé
 	 */
-	public function pageByItem($item): int|false {
+	public function pageByItem(Minz_Model $item): int|false {
 		$i = 0;
 
 		do {
-			if ($item == $this->items[$i]) {
+			if ($item === $this->items[$i]) {
 				return (int)(ceil(($i + 1) / $this->nbItemsPerPage));
 			}
 			$i++;
@@ -82,11 +82,11 @@ class Minz_Paginator {
 	 * @param Minz_Model $item the element to search
 	 * @return int|false the position of the element, or false if not found
 	 */
-	public function positionByItem($item): int|false {
+	public function positionByItem(Minz_Model $item): int|false {
 		$i = 0;
 
 		do {
-			if ($item == $this->items[$i]) {
+			if ($item === $this->items[$i]) {
 				return $i;
 			}
 			$i++;

+ 1 - 1
lib/Minz/Request.php

@@ -432,7 +432,7 @@ class Minz_Request {
 	 * @param bool $redirect If true, uses an HTTP redirection, and if false (default), performs an internal dispatcher redirection.
 	 * @throws Minz_ConfigurationException
 	 */
-	public static function forward($url = [], bool $redirect = false): void {
+	public static function forward(array $url = [], bool $redirect = false): void {
 		if (empty(Minz_Request::originalRequest())) {
 			self::$originalRequest = $url;
 		}

+ 1 - 1
lib/Minz/View.php

@@ -247,7 +247,7 @@ class Minz_View {
 	/**
 	 * @param string|array{dark?:string,light?:string,default?:string} $themeColors
 	 */
-	public static function appendThemeColors($themeColors): void {
+	public static function appendThemeColors(string|array $themeColors): void {
 		self::$themeColors = $themeColors;
 	}
 

+ 2 - 3
lib/lib_rss.php

@@ -217,8 +217,7 @@ function escapeToUnicodeAlternative(string $text, bool $extended = true): string
 	return trim(str_replace($problem, $replace, $text));
 }
 
-/** @param int|float $n */
-function format_number($n, int $precision = 0): string {
+function format_number(int|float $n, int $precision = 0): string {
 	// number_format does not seem to be Unicode-compatible
 	return str_replace(' ', ' ',	// Thin non-breaking space
 		number_format((float)$n, $precision, '.', ' ')
@@ -274,7 +273,7 @@ function html_only_entity_decode(?string $text): string {
  * @param array<string,mixed>|string $log
  * @return array<string,mixed>|string
  */
-function sensitive_log($log): array|string {
+function sensitive_log(array|string $log): array|string {
 	if (is_array($log)) {
 		foreach ($log as $k => $v) {
 			if (in_array($k, ['api_key', 'Passwd', 'T'], true)) {