aboutsummaryrefslogtreecommitdiff
path: root/app/Utils
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2024-01-15 10:36:30 +0100
committerGravatar GitHub <noreply@github.com> 2024-01-15 10:36:30 +0100
commit314077a457f04cc2f0472e036af029e2676fbf02 (patch)
tree1f38bb78761a56b8ee2034caba0dbda3912ef7c1 /app/Utils
parent52f6c8399b41e0c8be49dd56c89f451843189791 (diff)
PHPStan prepare exceptions (#6037)
Take advantage of https://phpstan.org/blog/bring-your-exceptions-under-control Minimum changes to pass `tooWideThrowType` and `implicitThrows`. Revert some mistakes from: https://github.com/FreshRSS/FreshRSS/pull/5504 Preparation needed before new PRs of the same type: https://github.com/FreshRSS/FreshRSS/pull/5962 Fix several wrong PHPDocs and catches: > Method ... has ...Exception in PHPDoc @throws tag but it's not thrown. > Dead catch - ...Exception is never thrown in the try block.
Diffstat (limited to 'app/Utils')
-rw-r--r--app/Utils/dotpathUtil.php147
1 files changed, 71 insertions, 76 deletions
diff --git a/app/Utils/dotpathUtil.php b/app/Utils/dotpathUtil.php
index 299b1dcc4..b4da1506e 100644
--- a/app/Utils/dotpathUtil.php
+++ b/app/Utils/dotpathUtil.php
@@ -110,97 +110,92 @@ final class FreshRSS_dotpath_Util
$view->rss_url = $feedSourceUrl;
$view->entries = [];
- try {
- $view->rss_title = isset($dotPaths['feedTitle'])
- ? (htmlspecialchars(FreshRSS_dotpath_Util::getString($jf, $dotPaths['feedTitle']) ?? '', ENT_COMPAT, 'UTF-8') ?: $defaultRssTitle)
- : $defaultRssTitle;
-
- $jsonItems = FreshRSS_dotpath_Util::get($jf, $dotPaths['item']);
- if (!is_array($jsonItems) || count($jsonItems) === 0) {
- return null;
- }
+ $view->rss_title = isset($dotPaths['feedTitle'])
+ ? (htmlspecialchars(FreshRSS_dotpath_Util::getString($jf, $dotPaths['feedTitle']) ?? '', ENT_COMPAT, 'UTF-8') ?: $defaultRssTitle)
+ : $defaultRssTitle;
- foreach ($jsonItems as $jsonItem) {
- $rssItem = [];
- $rssItem['link'] = isset($dotPaths['itemUri']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemUri']) ?? '' : '';
- if (empty($rssItem['link'])) {
- continue;
- }
- $rssItem['title'] = isset($dotPaths['itemTitle']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemTitle']) ?? '' : '';
- $rssItem['author'] = isset($dotPaths['itemAuthor']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemAuthor']) ?? '' : '';
- $rssItem['timestamp'] = isset($dotPaths['itemTimestamp']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemTimestamp']) ?? '' : '';
-
- //get simple content, but if a path for HTML content has been provided, replace the simple content with HTML content
- $rssItem['content'] = isset($dotPaths['itemContent']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemContent']) ?? '' : '';
- $rssItem['content'] = isset($dotPaths['itemContentHTML'])
- ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemContentHTML']) ?? ''
- : $rssItem['content'];
-
- if (isset($dotPaths['itemTimeFormat']) && is_string($dotPaths['itemTimeFormat'])) {
- $dateTime = DateTime::createFromFormat($dotPaths['itemTimeFormat'], $rssItem['timestamp']);
- if ($dateTime != false) {
- $rssItem['timestamp'] = $dateTime->format(DateTime::ATOM);
- }
+ $jsonItems = FreshRSS_dotpath_Util::get($jf, $dotPaths['item']);
+ if (!is_array($jsonItems) || count($jsonItems) === 0) {
+ return null;
+ }
+
+ foreach ($jsonItems as $jsonItem) {
+ $rssItem = [];
+ $rssItem['link'] = isset($dotPaths['itemUri']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemUri']) ?? '' : '';
+ if (empty($rssItem['link'])) {
+ continue;
+ }
+ $rssItem['title'] = isset($dotPaths['itemTitle']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemTitle']) ?? '' : '';
+ $rssItem['author'] = isset($dotPaths['itemAuthor']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemAuthor']) ?? '' : '';
+ $rssItem['timestamp'] = isset($dotPaths['itemTimestamp']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemTimestamp']) ?? '' : '';
+
+ //get simple content, but if a path for HTML content has been provided, replace the simple content with HTML content
+ $rssItem['content'] = isset($dotPaths['itemContent']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemContent']) ?? '' : '';
+ $rssItem['content'] = isset($dotPaths['itemContentHTML'])
+ ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemContentHTML']) ?? ''
+ : $rssItem['content'];
+
+ if (isset($dotPaths['itemTimeFormat']) && is_string($dotPaths['itemTimeFormat'])) {
+ $dateTime = DateTime::createFromFormat($dotPaths['itemTimeFormat'], $rssItem['timestamp']);
+ if ($dateTime != false) {
+ $rssItem['timestamp'] = $dateTime->format(DateTime::ATOM);
}
+ }
- if (isset($dotPaths['itemCategories'])) {
- $jsonItemCategories = FreshRSS_dotpath_Util::get($jsonItem, $dotPaths['itemCategories']);
- if (is_string($jsonItemCategories) && $jsonItemCategories !== '') {
- $rssItem['tags'] = [$jsonItemCategories];
- } elseif (is_array($jsonItemCategories) && count($jsonItemCategories) > 0) {
- $rssItem['tags'] = [];
- foreach ($jsonItemCategories as $jsonItemCategory) {
- if (is_string($jsonItemCategory)) {
- $rssItem['tags'][] = $jsonItemCategory;
- }
+ if (isset($dotPaths['itemCategories'])) {
+ $jsonItemCategories = FreshRSS_dotpath_Util::get($jsonItem, $dotPaths['itemCategories']);
+ if (is_string($jsonItemCategories) && $jsonItemCategories !== '') {
+ $rssItem['tags'] = [$jsonItemCategories];
+ } elseif (is_array($jsonItemCategories) && count($jsonItemCategories) > 0) {
+ $rssItem['tags'] = [];
+ foreach ($jsonItemCategories as $jsonItemCategory) {
+ if (is_string($jsonItemCategory)) {
+ $rssItem['tags'][] = $jsonItemCategory;
}
}
}
+ }
- $rssItem['thumbnail'] = isset($dotPaths['itemThumbnail']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemThumbnail']) ?? '' : '';
-
- //Enclosures?
- if (isset($dotPaths['itemAttachment'])) {
- $jsonItemAttachments = FreshRSS_dotpath_Util::get($jsonItem, $dotPaths['itemAttachment']);
- if (is_array($jsonItemAttachments) && count($jsonItemAttachments) > 0) {
- $rssItem['attachments'] = [];
- foreach ($jsonItemAttachments as $attachment) {
- $rssAttachment = [];
- $rssAttachment['url'] = isset($dotPaths['itemAttachmentUrl'])
- ? FreshRSS_dotpath_Util::getString($attachment, $dotPaths['itemAttachmentUrl'])
- : '';
- $rssAttachment['type'] = isset($dotPaths['itemAttachmentType'])
- ? FreshRSS_dotpath_Util::getString($attachment, $dotPaths['itemAttachmentType'])
- : '';
- $rssAttachment['length'] = isset($dotPaths['itemAttachmentLength'])
- ? FreshRSS_dotpath_Util::get($attachment, $dotPaths['itemAttachmentLength'])
- : '';
- $rssItem['attachments'][] = $rssAttachment;
- }
+ $rssItem['thumbnail'] = isset($dotPaths['itemThumbnail']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemThumbnail']) ?? '' : '';
+
+ //Enclosures?
+ if (isset($dotPaths['itemAttachment'])) {
+ $jsonItemAttachments = FreshRSS_dotpath_Util::get($jsonItem, $dotPaths['itemAttachment']);
+ if (is_array($jsonItemAttachments) && count($jsonItemAttachments) > 0) {
+ $rssItem['attachments'] = [];
+ foreach ($jsonItemAttachments as $attachment) {
+ $rssAttachment = [];
+ $rssAttachment['url'] = isset($dotPaths['itemAttachmentUrl'])
+ ? FreshRSS_dotpath_Util::getString($attachment, $dotPaths['itemAttachmentUrl'])
+ : '';
+ $rssAttachment['type'] = isset($dotPaths['itemAttachmentType'])
+ ? FreshRSS_dotpath_Util::getString($attachment, $dotPaths['itemAttachmentType'])
+ : '';
+ $rssAttachment['length'] = isset($dotPaths['itemAttachmentLength'])
+ ? FreshRSS_dotpath_Util::get($attachment, $dotPaths['itemAttachmentLength'])
+ : '';
+ $rssItem['attachments'][] = $rssAttachment;
}
}
+ }
- if (isset($dotPaths['itemUid'])) {
- $rssItem['guid'] = FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemUid']);
- }
+ if (isset($dotPaths['itemUid'])) {
+ $rssItem['guid'] = FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemUid']);
+ }
- if (empty($rssItem['guid'])) {
- $rssItem['guid'] = 'urn:sha1:' . sha1($rssItem['title'] . $rssItem['content'] . $rssItem['link']);
- }
+ if (empty($rssItem['guid'])) {
+ $rssItem['guid'] = 'urn:sha1:' . sha1($rssItem['title'] . $rssItem['content'] . $rssItem['link']);
+ }
- if ($rssItem['title'] != '' || $rssItem['content'] != '' || $rssItem['link'] != '') {
- // HTML-encoding/escaping of the relevant fields (all except 'content')
- foreach (['author', 'guid', 'link', 'thumbnail', 'timestamp', 'tags', 'title'] as $key) {
- if (!empty($rssItem[$key]) && is_string($rssItem[$key])) {
- $rssItem[$key] = Minz_Helper::htmlspecialchars_utf8($rssItem[$key]);
- }
+ if ($rssItem['title'] != '' || $rssItem['content'] != '' || $rssItem['link'] != '') {
+ // HTML-encoding/escaping of the relevant fields (all except 'content')
+ foreach (['author', 'guid', 'link', 'thumbnail', 'timestamp', 'tags', 'title'] as $key) {
+ if (!empty($rssItem[$key]) && is_string($rssItem[$key])) {
+ $rssItem[$key] = Minz_Helper::htmlspecialchars_utf8($rssItem[$key]);
}
- $view->entries[] = FreshRSS_Entry::fromArray($rssItem);
}
+ $view->entries[] = FreshRSS_Entry::fromArray($rssItem);
}
- } catch (Exception $ex) {
- Minz_Log::warning($ex->getMessage());
- return null;
}
return $view->renderToString();