summaryrefslogtreecommitdiff
path: root/classes/feeditem/common.php
diff options
context:
space:
mode:
authorwn_ <[email protected]>2021-11-15 02:40:45 +0000
committerwn_ <[email protected]>2021-11-15 02:40:45 +0000
commit78acf18b70e3d6ba22e2c2db950e132cfb5d35be (patch)
tree199178700033e27ce505c783105a6f71d0ab1c0a /classes/feeditem/common.php
parent8943604aad6d01865e824520130733682f4088ab (diff)
Address PHPStan warnings in FeedItem classes.
Diffstat (limited to 'classes/feeditem/common.php')
-rwxr-xr-xclasses/feeditem/common.php50
1 files changed, 36 insertions, 14 deletions
diff --git a/classes/feeditem/common.php b/classes/feeditem/common.php
index 18afeaa94..6a9be8aca 100755
--- a/classes/feeditem/common.php
+++ b/classes/feeditem/common.php
@@ -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 = [];