From f7b4a1e74220af1d0db310f17ba1294862a32393 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Sun, 23 Feb 2025 17:08:17 +0100 Subject: PHPStan more checkImplicitMixed (#7339) * PHPStan more checkImplicitMixed * Draft Entry.php * Finish Entry.php * Finish FeedDAO.php and Themes.php --- app/Models/Entry.php | 26 +++++++++++++++----------- app/Models/FeedDAO.php | 12 ++++++++---- app/Models/Themes.php | 4 ++-- app/views/helpers/feed/update.phtml | 17 +++++++++++++++-- phpstan-next.neon | 4 ---- 5 files changed, 40 insertions(+), 23 deletions(-) diff --git a/app/Models/Entry.php b/app/Models/Entry.php index fe0cf7429..2454e4831 100644 --- a/app/Models/Entry.php +++ b/app/Models/Entry.php @@ -65,7 +65,9 @@ class FreshRSS_Entry extends Minz_Model { } $dao['attributes'] = empty($dao['attributes']) ? [] : json_decode($dao['attributes'], true); - if (!is_array($dao['attributes'])) { + if (is_array($dao['attributes'])) { + $dao['attributes'] = array_filter($dao['attributes'], 'is_string', ARRAY_FILTER_USE_KEY); + } else { $dao['attributes'] = []; } @@ -168,7 +170,7 @@ class FreshRSS_Entry extends Minz_Model { return preg_match('/(?P[\'"])' . preg_quote($link, '/') . '(?P=delim)/', $html) == 1; } - /** @param array{'url'?:string,'length'?:int,'medium'?:string,'type'?:string} $enclosure */ + /** @param array{url?:string,length?:int,medium?:string,type?:string} $enclosure */ private static function enclosureIsImage(array $enclosure): bool { $elink = $enclosure['url'] ?? ''; $length = $enclosure['length'] ?? 0; @@ -230,15 +232,15 @@ HTML; continue; } $credits = $enclosure['credit'] ?? ''; - $description = nl2br($enclosure['description'] ?? '', true); - $length = $enclosure['length'] ?? 0; - $medium = $enclosure['medium'] ?? ''; - $mime = $enclosure['type'] ?? ''; + $description = is_string($enclosure['description'] ?? null) ? nl2br($enclosure['description'], true) : ''; + $length = is_numeric($enclosure['length'] ?? null) ? (int)$enclosure['length'] : 0; + $medium = is_string($enclosure['medium'] ?? null) ? $enclosure['medium'] : ''; + $mime = is_string($enclosure['type'] ?? null) ? $enclosure['type'] : ''; $thumbnails = $enclosure['thumbnails'] ?? null; if (!is_array($thumbnails)) { $thumbnails = []; } - $etitle = $enclosure['title'] ?? ''; + $etitle = is_string($enclosure['title'] ?? null) ? $enclosure['title'] : ''; $content .= "\n"; $content .= '
'; @@ -249,16 +251,16 @@ HTML; } } - if (self::enclosureIsImage($enclosure)) { + if (self::enclosureIsImage(['url' => $elink, 'length' => $length, 'medium' => $medium, 'type' => $mime])) { $content .= '

'; } elseif ($medium === 'audio' || str_starts_with($mime, 'audio')) { $content .= '

💾

'; } elseif ($medium === 'video' || str_starts_with($mime, 'video')) { $content .= '

💾

'; } else { //e.g. application, text, unknown @@ -273,7 +275,9 @@ HTML; $credits = [$credits]; } foreach ($credits as $credit) { - $content .= '

© ' . $credit . '

'; + if (is_string($credit)) { + $content .= '

© ' . $credit . '

'; + } } } if ($description != '') { diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.php index 8f2791485..6b5f7f96a 100644 --- a/app/Models/FeedDAO.php +++ b/app/Models/FeedDAO.php @@ -117,7 +117,9 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo { // Merge existing and import attributes $existingAttributes = $feed_search->attributes(); $importAttributes = $feed->attributes(); - $feed->_attributes(array_replace_recursive($existingAttributes, $importAttributes)); + $mergedAttributes = array_replace_recursive($existingAttributes, $importAttributes); + $mergedAttributes = array_filter($mergedAttributes, 'is_string', ARRAY_FILTER_USE_KEY); + $feed->_attributes($mergedAttributes); // Update some values of the existing feed using the import $values = [ @@ -382,8 +384,10 @@ SQL; . 'ORDER BY `lastUpdate` ' . ($limit < 1 ? '' : 'LIMIT ' . intval($limit)); $stm = $this->pdo->query($sql); - if ($stm !== false) { - return self::daoToFeeds($stm->fetchAll(PDO::FETCH_ASSOC)); + if ($stm !== false && ($res = $stm->fetchAll(PDO::FETCH_ASSOC)) !== false) { + /** @var list $res */ + return self::daoToFeeds($res); } else { $info = $this->pdo->errorInfo(); /** @var array{0:string,1:int,2:string} $info */ @@ -613,6 +617,6 @@ SQL; return -1; } $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0); - return (int)($res[0] ?? 0); + return is_numeric($res[0] ?? null) ? (int)$res[0] : 0; } } diff --git a/app/Models/Themes.php b/app/Models/Themes.php index 033bbe2a0..2b5bff2f7 100644 --- a/app/Models/Themes.php +++ b/app/Models/Themes.php @@ -92,14 +92,14 @@ class FreshRSS_Themes extends Minz_Model { } public static function title(string $name): string { - static $titles = [ + $titles = [ 'opml-dyn' => 'sub.category.dynamic_opml', ]; return $titles[$name] ?? ''; } public static function alt(string $name): string { - static $alts = [ + $alts = [ 'add' => '➕', //✚ 'all' => '☰', 'bookmark-add' => '➕', //✚ diff --git a/app/views/helpers/feed/update.phtml b/app/views/helpers/feed/update.phtml index 92fedd992..54d3e77a0 100644 --- a/app/views/helpers/feed/update.phtml +++ b/app/views/helpers/feed/update.phtml @@ -781,8 +781,14 @@ ?>
+ feed->attributeArray('curl_params')[CURLOPT_POSTFIELDS] ?? ''; + if (!is_string($postFields)) { + $postFields = ''; + } + ?>

@@ -810,8 +816,15 @@
+ feed->attributeArray('curl_params')[CURLOPT_HTTPHEADER] ?? []; + if (!is_array($httpHeaders)) { + $httpHeaders = []; + } + $httpHeaders = array_filter($httpHeaders, 'is_string'); + ?> diff --git a/phpstan-next.neon b/phpstan-next.neon index d8fefc422..19aaed28e 100644 --- a/phpstan-next.neon +++ b/phpstan-next.neon @@ -10,10 +10,6 @@ parameters: excludePaths: analyse: # TODO: Update files below and remove them from this list - - app/Models/Entry.php - app/Models/EntryDAO.php - - app/Models/FeedDAO.php - app/Models/TagDAO.php - - app/Models/Themes.php - app/Services/ImportService.php - - app/views/helpers/feed/update.phtml -- cgit v1.2.3