1
0
mirror of https://git.tt-rss.org/git/tt-rss.git synced 2025-12-16 01:35:56 +00:00

Address PHPStan warnings in FeedItem classes.

This commit is contained in:
wn_
2021-11-15 02:40:45 +00:00
parent 8943604aad
commit 78acf18b70
6 changed files with 129 additions and 53 deletions

View File

@@ -1,16 +1,20 @@
<?php
abstract class FeedItem_Common extends FeedItem {
/** @var DOMElement */
protected $elem;
protected $xpath;
/** @var DOMDocument */
protected $doc;
function __construct($elem, $doc, $xpath) {
/** @var DOMXPath */
protected $xpath;
function __construct(DOMElement $elem, DOMDocument $doc, DOMXPath $xpath) {
$this->elem = $elem;
$this->xpath = $xpath;
$this->doc = $doc;
try {
$source = $elem->getElementsByTagName("source")->item(0);
// we don't need <source> element
@@ -21,11 +25,12 @@ abstract class FeedItem_Common extends FeedItem {
}
}
function get_element() {
function get_element(): DOMElement {
return $this->elem;
}
function get_author() {
function get_author(): string {
/** @var DOMElement|null */
$author = $this->elem->getElementsByTagName("author")->item(0);
if ($author) {
@@ -51,7 +56,7 @@ abstract class FeedItem_Common extends FeedItem {
return implode(", ", $authors);
}
function get_comments_url() {
function get_comments_url(): string {
//RSS only. Use a query here to avoid namespace clashes (e.g. with slash).
//might give a wrong result if a default namespace was declared (possible with XPath 2.0)
$com_url = $this->xpath->query("comments", $this->elem)->item(0);
@@ -65,20 +70,28 @@ abstract class FeedItem_Common extends FeedItem {
if ($com_url)
return clean($com_url->nodeValue);
return '';
}
function get_comments_count() {
function get_comments_count(): int {
//also query for ATE stuff here
$query = "slash:comments|thread:total|atom:link[@rel='replies']/@thread:count";
$comments = $this->xpath->query($query, $this->elem)->item(0);
if ($comments) {
return clean($comments->nodeValue);
if ($comments && is_numeric($comments->nodeValue)) {
return (int) clean($comments->nodeValue);
}
return 0;
}
// this is common for both Atom and RSS types and deals with various media: elements
function get_enclosures() {
/**
* this is common for both Atom and RSS types and deals with various 'media:' elements
*
* @return array<int, FeedEnclosure>
*/
function get_enclosures(): array {
$encs = [];
$enclosures = $this->xpath->query("media:content", $this->elem);
@@ -108,6 +121,7 @@ abstract class FeedItem_Common extends FeedItem {
foreach ($enclosures as $enclosure) {
$enc = new FeedEnclosure();
/** @var DOMElement|null */
$content = $this->xpath->query("media:content", $enclosure)->item(0);
if ($content) {
@@ -150,11 +164,14 @@ abstract class FeedItem_Common extends FeedItem {
return $encs;
}
function count_children($node) {
function count_children(DOMElement $node): int {
return $node->getElementsByTagName("*")->length;
}
function subtree_or_text($node) {
/**
* @return false|string false on failure, otherwise string contents
*/
function subtree_or_text(DOMElement $node) {
if ($this->count_children($node) == 0) {
return $node->nodeValue;
} else {
@@ -162,7 +179,12 @@ abstract class FeedItem_Common extends FeedItem {
}
}
static function normalize_categories($cats) {
/**
* @param array<int, string> $cats
*
* @return array<int, string>
*/
static function normalize_categories(array $cats): array {
$tmp = [];