From 77b8dc738616723bd891b09731478f7a4a77672e Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sat, 13 Nov 2021 17:48:52 +0300 Subject: fix phpstan warnings in classes/feedparser.php --- classes/feedparser.php | 58 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'classes/feedparser.php') diff --git a/classes/feedparser.php b/classes/feedparser.php index daba271fb..05412ef1e 100644 --- a/classes/feedparser.php +++ b/classes/feedparser.php @@ -1,19 +1,35 @@ */ + private $libxml_errors = []; + + /** @var array */ private $items; + + /** @var string */ private $link; + + /** @var string */ private $title; + + /** @var int */ private $type; + + /** @var DOMXPath */ private $xpath; const FEED_RDF = 0; const FEED_RSS = 1; const FEED_ATOM = 2; - function __construct($data) { + function __construct(string $data) { libxml_use_internal_errors(true); libxml_clear_errors(); $this->doc = new DOMDocument(); @@ -26,7 +42,7 @@ class FeedParser { if ($error) { foreach (libxml_get_errors() as $error) { if ($error->level == LIBXML_ERR_FATAL) { - if(!isset($this->error)) //currently only the first error is reported + if ($this->error) //currently only the first error is reported $this->error = $this->format_error($error); $this->libxml_errors [] = $this->format_error($error); } @@ -37,7 +53,7 @@ class FeedParser { $this->items = array(); } - function init() { + function init() : void { $root = $this->doc->firstChild; $xpath = new DOMXPath($this->doc); $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom'); @@ -69,7 +85,7 @@ class FeedParser { $this->type = $this::FEED_ATOM; break; default: - if( !isset($this->error) ){ + if (!isset($this->error) ){ $this->error = "Unknown/unsupported feed type"; } return; @@ -100,6 +116,7 @@ class FeedParser { if (!$link) $link = $xpath->query("//atom03:feed/atom03:link[@rel='alternate']")->item(0); + /** @var DOMElement|null $link */ if ($link && $link->hasAttributes()) { $this->link = $link->getAttribute("href"); } @@ -121,6 +138,7 @@ class FeedParser { $this->title = $title->nodeValue; } + /** @var DOMElement|null $link */ $link = $xpath->query("//channel/link")->item(0); if ($link) { @@ -173,39 +191,37 @@ class FeedParser { } } - function format_error($error) { - if ($error) { - return sprintf("LibXML error %s at line %d (column %d): %s", - $error->code, $error->line, $error->column, - $error->message); - } else { - return ""; - } + function format_error(LibXMLError $error) : string { + return sprintf("LibXML error %s at line %d (column %d): %s", + $error->code, $error->line, $error->column, + $error->message); } // libxml may have invalid unicode data in error messages - function error() { + function error() : string { return UConverter::transcode($this->error, 'UTF-8', 'UTF-8'); } - // WARNING: may return invalid unicode data - function errors() { + /** @return array - WARNING: may return invalid unicode data */ + function errors() : array { return $this->libxml_errors; } - function get_link() { + function get_link() : string { return clean($this->link); } - function get_title() { + function get_title() : string { return clean($this->title); } - function get_items() { + /** @return array */ + function get_items() : array { return $this->items; } - function get_links($rel) { + /** @return array */ + function get_links(string $rel) : array { $rv = array(); switch ($this->type) { -- cgit v1.2.3 From f5c881586bd6eb41036e36625f426c00aa993c4f Mon Sep 17 00:00:00 2001 From: wn_ Date: Sun, 14 Nov 2021 16:59:21 +0000 Subject: Handle potentially null link, title, etc. in FeedParser. --- classes/feedparser.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'classes/feedparser.php') diff --git a/classes/feedparser.php b/classes/feedparser.php index 05412ef1e..abf1545f8 100644 --- a/classes/feedparser.php +++ b/classes/feedparser.php @@ -11,18 +11,18 @@ class FeedParser { private $libxml_errors = []; /** @var array */ - private $items; + private $items = []; - /** @var string */ + /** @var string|null */ private $link; - /** @var string */ + /** @var string|null */ private $title; - /** @var int */ + /** @var FeedParser::FEED_*|null */ private $type; - /** @var DOMXPath */ + /** @var DOMXPath|null */ private $xpath; const FEED_RDF = 0; @@ -49,8 +49,6 @@ class FeedParser { } } libxml_clear_errors(); - - $this->items = array(); } function init() : void { @@ -208,11 +206,11 @@ class FeedParser { } function get_link() : string { - return clean($this->link); + return clean($this->link ?? ''); } function get_title() : string { - return clean($this->title); + return clean($this->title ?? ''); } /** @return array */ -- cgit v1.2.3 From aa924d9ee7674cfe649d498ce3cbcdb093bca956 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 15 Nov 2021 08:26:02 +0300 Subject: deal with several DOMElement-related errors --- classes/feedparser.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'classes/feedparser.php') diff --git a/classes/feedparser.php b/classes/feedparser.php index abf1545f8..dd58df941 100644 --- a/classes/feedparser.php +++ b/classes/feedparser.php @@ -65,10 +65,12 @@ class FeedParser { $this->xpath = $xpath; - $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)"); + $root_list = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)"); - if (!empty($root) && $root->length > 0) { - $root = $root->item(0); + if (!empty($root_list) && $root_list->length > 0) { + + /** @var DOMElement|false $root */ + $root = $root_list->item(0); if ($root) { switch (mb_strtolower($root->tagName)) { -- cgit v1.2.3 From 109b702ed0cd31a0dc8466b8127882d263705d8d Mon Sep 17 00:00:00 2001 From: wn_ Date: Mon, 15 Nov 2021 12:24:38 +0000 Subject: Minor fix to DOMNodeList#item() potential type (null vs false) --- classes/feedparser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'classes/feedparser.php') diff --git a/classes/feedparser.php b/classes/feedparser.php index dd58df941..f5649d2f8 100644 --- a/classes/feedparser.php +++ b/classes/feedparser.php @@ -69,7 +69,7 @@ class FeedParser { if (!empty($root_list) && $root_list->length > 0) { - /** @var DOMElement|false $root */ + /** @var DOMElement|null $root */ $root = $root_list->item(0); if ($root) { -- cgit v1.2.3 From fb1e85baaf2f3870297c03984f3122d4ed6ad767 Mon Sep 17 00:00:00 2001 From: wn_ Date: Wed, 17 Nov 2021 19:29:42 +0000 Subject: Switch FeedParser back to described behavior for setting 'error'. Also some formatting. --- classes/feedparser.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'classes/feedparser.php') diff --git a/classes/feedparser.php b/classes/feedparser.php index f5649d2f8..6ce69cc89 100644 --- a/classes/feedparser.php +++ b/classes/feedparser.php @@ -42,9 +42,11 @@ class FeedParser { if ($error) { foreach (libxml_get_errors() as $error) { if ($error->level == LIBXML_ERR_FATAL) { - if ($this->error) //currently only the first error is reported + // currently only the first error is reported + if (!isset($this->error)) { $this->error = $this->format_error($error); - $this->libxml_errors [] = $this->format_error($error); + } + $this->libxml_errors[] = $this->format_error($error); } } } @@ -85,7 +87,7 @@ class FeedParser { $this->type = $this::FEED_ATOM; break; default: - if (!isset($this->error) ){ + if (!isset($this->error)) { $this->error = "Unknown/unsupported feed type"; } return; @@ -184,7 +186,7 @@ class FeedParser { if ($this->link) $this->link = trim($this->link); } else { - if( !isset($this->error) ){ + if (!isset($this->error)) { $this->error = "Unknown/unsupported feed type"; } return; -- cgit v1.2.3