1
0
mirror of https://tt-rss.org/git/tt-rss.git synced 2024-06-27 12:05:06 +02:00

fix phpstan warnings in classes/feedparser.php

This commit is contained in:
Andrew Dolgov 2021-11-13 17:48:52 +03:00
parent 45431170b6
commit 77b8dc7386

View File

@ -1,19 +1,35 @@
<?php <?php
class FeedParser { class FeedParser {
/** @var DOMDocument */
private $doc; private $doc;
private $error;
private $libxml_errors = array(); /** @var string|null */
private $error = null;
/** @var array<string> */
private $libxml_errors = [];
/** @var array<FeedItem> */
private $items; private $items;
/** @var string */
private $link; private $link;
/** @var string */
private $title; private $title;
/** @var int */
private $type; private $type;
/** @var DOMXPath */
private $xpath; private $xpath;
const FEED_RDF = 0; const FEED_RDF = 0;
const FEED_RSS = 1; const FEED_RSS = 1;
const FEED_ATOM = 2; const FEED_ATOM = 2;
function __construct($data) { function __construct(string $data) {
libxml_use_internal_errors(true); libxml_use_internal_errors(true);
libxml_clear_errors(); libxml_clear_errors();
$this->doc = new DOMDocument(); $this->doc = new DOMDocument();
@ -26,7 +42,7 @@ class FeedParser {
if ($error) { if ($error) {
foreach (libxml_get_errors() as $error) { foreach (libxml_get_errors() as $error) {
if ($error->level == LIBXML_ERR_FATAL) { 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->error = $this->format_error($error);
$this->libxml_errors [] = $this->format_error($error); $this->libxml_errors [] = $this->format_error($error);
} }
@ -37,7 +53,7 @@ class FeedParser {
$this->items = array(); $this->items = array();
} }
function init() { function init() : void {
$root = $this->doc->firstChild; $root = $this->doc->firstChild;
$xpath = new DOMXPath($this->doc); $xpath = new DOMXPath($this->doc);
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom'); $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
@ -69,7 +85,7 @@ class FeedParser {
$this->type = $this::FEED_ATOM; $this->type = $this::FEED_ATOM;
break; break;
default: default:
if( !isset($this->error) ){ if (!isset($this->error) ){
$this->error = "Unknown/unsupported feed type"; $this->error = "Unknown/unsupported feed type";
} }
return; return;
@ -100,6 +116,7 @@ class FeedParser {
if (!$link) if (!$link)
$link = $xpath->query("//atom03:feed/atom03:link[@rel='alternate']")->item(0); $link = $xpath->query("//atom03:feed/atom03:link[@rel='alternate']")->item(0);
/** @var DOMElement|null $link */
if ($link && $link->hasAttributes()) { if ($link && $link->hasAttributes()) {
$this->link = $link->getAttribute("href"); $this->link = $link->getAttribute("href");
} }
@ -121,6 +138,7 @@ class FeedParser {
$this->title = $title->nodeValue; $this->title = $title->nodeValue;
} }
/** @var DOMElement|null $link */
$link = $xpath->query("//channel/link")->item(0); $link = $xpath->query("//channel/link")->item(0);
if ($link) { if ($link) {
@ -173,39 +191,37 @@ class FeedParser {
} }
} }
function format_error($error) { function format_error(LibXMLError $error) : string {
if ($error) { return sprintf("LibXML error %s at line %d (column %d): %s",
return sprintf("LibXML error %s at line %d (column %d): %s", $error->code, $error->line, $error->column,
$error->code, $error->line, $error->column, $error->message);
$error->message);
} else {
return "";
}
} }
// libxml may have invalid unicode data in error messages // libxml may have invalid unicode data in error messages
function error() { function error() : string {
return UConverter::transcode($this->error, 'UTF-8', 'UTF-8'); return UConverter::transcode($this->error, 'UTF-8', 'UTF-8');
} }
// WARNING: may return invalid unicode data /** @return array<string> - WARNING: may return invalid unicode data */
function errors() { function errors() : array {
return $this->libxml_errors; return $this->libxml_errors;
} }
function get_link() { function get_link() : string {
return clean($this->link); return clean($this->link);
} }
function get_title() { function get_title() : string {
return clean($this->title); return clean($this->title);
} }
function get_items() { /** @return array<FeedItem> */
function get_items() : array {
return $this->items; return $this->items;
} }
function get_links($rel) { /** @return array<string> */
function get_links(string $rel) : array {
$rv = array(); $rv = array();
switch ($this->type) { switch ($this->type) {