deal with phpstan warnings in plugins/af_redditimgur.php

This commit is contained in:
Andrew Dolgov 2021-11-14 20:51:22 +03:00
parent cfc31fc692
commit 80291ffe0c
1 changed files with 57 additions and 21 deletions

View File

@ -3,10 +3,20 @@ class Af_RedditImgur extends Plugin {
/** @var PluginHost $host */ /** @var PluginHost $host */
private $host; private $host;
/** @var array<string> */
private $domain_blacklist = [ "github.com" ]; private $domain_blacklist = [ "github.com" ];
/** @var bool */
private $dump_json_data = false; private $dump_json_data = false;
/** @var array<string> */
private $fallback_preview_urls = []; private $fallback_preview_urls = [];
/** @var int */
private $default_max_score = 100; private $default_max_score = 100;
/** @var array<int, array<int, string|null>> */
private $generated_enclosures = []; private $generated_enclosures = [];
function about() { function about() {
@ -118,7 +128,7 @@ class Af_RedditImgur extends Plugin {
<?php <?php
} }
function save() { function save() : void {
$enable_readability = checkbox_to_sql_bool($_POST["enable_readability"] ?? ""); $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"] ?? "");
$enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"] ?? ""); $enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"] ?? "");
$reddit_to_teddit = checkbox_to_sql_bool($_POST["reddit_to_teddit"] ?? ""); $reddit_to_teddit = checkbox_to_sql_bool($_POST["reddit_to_teddit"] ?? "");
@ -138,7 +148,14 @@ class Af_RedditImgur extends Plugin {
echo __("Configuration saved"); echo __("Configuration saved");
} }
private function process_post_media($data, $doc, $xpath, $anchor) { /**
* @param array<string,mixed> $data (this is a huge blob of random crap returned by reddit API)
* @param DOMDocument $doc
* @param DOMXPath $xpath
* @param DOMElement $anchor
* @return bool
*/
private function process_post_media(array $data, DOMDocument $doc, DOMXPath $xpath, DOMElement $anchor) : bool {
$found = 0; $found = 0;
if (isset($data["media_metadata"])) { if (isset($data["media_metadata"])) {
@ -242,14 +259,21 @@ class Af_RedditImgur extends Plugin {
} }
} }
return $found; return $found > 0;
} }
/* function score_convert(int $value, int $from1, int $from2, int $to1, int $to2) { /* function score_convert(int $value, int $from1, int $from2, int $to1, int $to2) {
return ($value - $from1) / ($from2 - $from1) * ($to2 - $to1) + $to1; return ($value - $from1) / ($from2 - $from1) * ($to2 - $to1) + $to1;
} */ } */
private function inline_stuff(&$article, &$doc, $xpath) { /**
* @param array<string, mixed> $article
* @param DOMDocument $doc
* @param DOMXPath $xpath
* @return bool
* @throws PDOException
*/
private function inline_stuff(array &$article, DOMDocument &$doc, DOMXpath $xpath) : bool {
$max_score = (int) $this->host->get($this, "max_score", $this->default_max_score); $max_score = (int) $this->host->get($this, "max_score", $this->default_max_score);
$import_score = (bool) $this->host->get($this, "import_score", $this->default_max_score); $import_score = (bool) $this->host->get($this, "import_score", $this->default_max_score);
@ -263,7 +287,7 @@ class Af_RedditImgur extends Plugin {
$this->generated_enclosures = []; $this->generated_enclosures = [];
// embed anchor element, before reddit <table> post layout /** @var DOMElement|false $anchor -- embed anchor element, before reddit <table> post layout */
$anchor = $xpath->query('//body/*')->item(0); $anchor = $xpath->query('//body/*')->item(0);
// deal with json-provided media content first // deal with json-provided media content first
@ -583,7 +607,7 @@ class Af_RedditImgur extends Plugin {
if ($found) if ($found)
$this->remove_post_thumbnail($doc, $xpath); $this->remove_post_thumbnail($doc, $xpath);
return $found; return $found > 0;
} }
function hook_article_filter($article) { function hook_article_filter($article) {
@ -651,14 +675,14 @@ class Af_RedditImgur extends Plugin {
return 2; return 2;
} }
private function remove_post_thumbnail($doc, $xpath) { private function remove_post_thumbnail(DOMDocument $doc, DOMXpath $xpath) : void {
$thumb = $xpath->query("//td/a/img[@src]")->item(0); $thumb = $xpath->query("//td/a/img[@src]")->item(0);
if ($thumb) if ($thumb)
$thumb->parentNode->parentNode->removeChild($thumb->parentNode); $thumb->parentNode->parentNode->removeChild($thumb->parentNode);
} }
private function handle_as_image($doc, $entry, $image_url, $link_url = false) { private function handle_as_image(DOMDocument $doc, DOMElement $entry, string $image_url, string $link_url = "") : void {
$img = $doc->createElement("img"); $img = $doc->createElement("img");
$img->setAttribute("src", $image_url); $img->setAttribute("src", $image_url);
@ -677,7 +701,7 @@ class Af_RedditImgur extends Plugin {
$entry->parentNode->insertBefore($p, $entry); $entry->parentNode->insertBefore($p, $entry);
} }
private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) { private function handle_as_video(DOMDocument $doc, DOMElement $entry, string $source_stream, string $poster_url = "") : void {
Debug::log("handle_as_video: $source_stream", Debug::LOG_VERBOSE); Debug::log("handle_as_video: $source_stream", Debug::LOG_VERBOSE);
@ -709,7 +733,7 @@ class Af_RedditImgur extends Plugin {
return $method === "testurl"; return $method === "testurl";
} }
function testurl() { function testurl() : void {
$url = clean($_POST["url"] ?? ""); $url = clean($_POST["url"] ?? "");
$article_url = clean($_POST["article_url"] ?? ""); $article_url = clean($_POST["article_url"] ?? "");
@ -804,8 +828,8 @@ class Af_RedditImgur extends Plugin {
} }
/** $useragent defaults to Config::get_user_agent() */ /** $useragent defaults to Config::get_user_agent() */
private function get_header($url, $header, $useragent = false) { private function get_header(string $url, int $header, string $useragent = "") : string {
$ret = false; $ret = "";
if (function_exists("curl_init")) { if (function_exists("curl_init")) {
$ch = curl_init($url); $ch = curl_init($url);
@ -823,16 +847,24 @@ class Af_RedditImgur extends Plugin {
return $ret; return $ret;
} }
private function get_content_type($url, $useragent = false) { private function get_content_type(string $url, string $useragent = "") : string {
return $this->get_header($url, CURLINFO_CONTENT_TYPE, $useragent); return $this->get_header($url, CURLINFO_CONTENT_TYPE, $useragent);
} }
// @phpstan-ignore-next-line /*private function get_location(string $url, string $useragent = "") : string {
private function get_location($url, $useragent = false) {
return $this->get_header($url, CURLINFO_EFFECTIVE_URL, $useragent); return $this->get_header($url, CURLINFO_EFFECTIVE_URL, $useragent);
} }*/
private function readability($article, $url, $doc, $xpath, $debug = false) { /**
* @param array<string,mixed> $article
* @param string $url
* @param DOMDocument $doc
* @param DOMXPath $xpath
* @param bool $debug
* @return array<string,mixed>
* @throws PDOException
*/
private function readability(array $article, string $url, DOMDocument $doc, DOMXpath $xpath, bool $debug = false) : array {
if (function_exists("curl_init") && $this->host->get($this, "enable_readability") && if (function_exists("curl_init") && $this->host->get($this, "enable_readability") &&
mb_strlen(strip_tags($article["content"])) <= 150) { mb_strlen(strip_tags($article["content"])) <= 150) {
@ -864,7 +896,12 @@ class Af_RedditImgur extends Plugin {
return $article; return $article;
} }
private function is_blacklisted($src, $also_blacklist = []) { /**
* @param string $src
* @param array<string> $also_blacklist
* @return bool
*/
private function is_blacklisted(string $src, array $also_blacklist = []) : bool {
$src_domain = parse_url($src, PHP_URL_HOST); $src_domain = parse_url($src, PHP_URL_HOST);
foreach (array_merge($this->domain_blacklist, $also_blacklist) as $domain) { foreach (array_merge($this->domain_blacklist, $also_blacklist) as $domain) {
@ -880,7 +917,7 @@ class Af_RedditImgur extends Plugin {
return $this->hook_render_article_cdm($article); return $this->hook_render_article_cdm($article);
} }
private function rewrite_to_teddit($str) { private function rewrite_to_teddit(string $str) : string {
if (strpos($str, "reddit.com") !== false) { if (strpos($str, "reddit.com") !== false) {
return preg_replace("/https?:\/\/([a-z]+\.)?reddit\.com/", "https://teddit.net", $str); return preg_replace("/https?:\/\/([a-z]+\.)?reddit\.com/", "https://teddit.net", $str);
} }
@ -888,7 +925,7 @@ class Af_RedditImgur extends Plugin {
return $str; return $str;
} }
private function rewrite_to_reddit($str) { private function rewrite_to_reddit(string $str) : string {
if (strpos($str, "teddit.net") !== false) { if (strpos($str, "teddit.net") !== false) {
$str = preg_replace("/https?:\/\/teddit.net/", "https://reddit.com", $str); $str = preg_replace("/https?:\/\/teddit.net/", "https://reddit.com", $str);
@ -899,7 +936,6 @@ class Af_RedditImgur extends Plugin {
return $str; return $str;
} }
function hook_render_article_cdm($article) { function hook_render_article_cdm($article) {
if ($this->host->get($this, "reddit_to_teddit")) { if ($this->host->get($this, "reddit_to_teddit")) {
$need_saving = false; $need_saving = false;