From f30ba3881677923fa14510e27c69d1f64489ccc3 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 18 Feb 2023 20:46:06 +0100 Subject: [PATCH] Do not call pure Misc methods through registry These have a singular purpose and except for parse_uri do not interact with other classes. Overriding them makes little sense. This makes the code a bit cleaner and less opaque for static analysis. I am ignoring `Misc::error` since it will go away and `Misc::xml_encoding` because it accesses registry. Partly reverts https://github.com/simplepie/simplepie/commit/f6e63e6ba8e4a198283bb2323aa498fd0d6a216e --- src/Item.php | 16 ++++++++-------- src/Locator.php | 32 ++++++++++++-------------------- src/Parser.php | 2 +- src/Sanitize.php | 6 +++--- src/SimplePie.php | 22 +++++++++++----------- src/Source.php | 14 +++++++------- 6 files changed, 42 insertions(+), 50 deletions(-) diff --git a/src/Item.php b/src/Item.php index 1d357f540..89ed43953 100644 --- a/src/Item.php +++ b/src/Item.php @@ -214,9 +214,9 @@ public function get_title() { if (!isset($this->data['title'])) { if ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'title')) { - $this->data['title'] = $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + $this->data['title'] = $this->sanitize($return[0]['data'], Misc::atom_10_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'title')) { - $this->data['title'] = $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + $this->data['title'] = $this->sanitize($return[0]['data'], Misc::atom_03_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10, 'title')) { $this->data['title'] = $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_090, 'title')) { @@ -252,10 +252,10 @@ public function get_title() public function get_description(bool $description_only = false) { if (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'summary')) && - ($return = $this->sanitize($tags[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$tags[0]['attribs']]), $this->get_base($tags[0])))) { + ($return = $this->sanitize($tags[0]['data'], Misc::atom_10_construct_type($tags[0]['attribs']), $this->get_base($tags[0])))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'summary')) && - ($return = $this->sanitize($tags[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$tags[0]['attribs']]), $this->get_base($tags[0])))) { + ($return = $this->sanitize($tags[0]['data'], Misc::atom_03_construct_type($tags[0]['attribs']), $this->get_base($tags[0])))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10, 'description')) && ($return = $this->sanitize($tags[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($tags[0])))) { @@ -302,10 +302,10 @@ public function get_description(bool $description_only = false) public function get_content(bool $content_only = false) { if (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'content')) && - ($return = $this->sanitize($tags[0]['data'], $this->registry->call(Misc::class, 'atom_10_content_construct_type', [$tags[0]['attribs']]), $this->get_base($tags[0])))) { + ($return = $this->sanitize($tags[0]['data'], Misc::atom_10_content_construct_type($tags[0]['attribs']), $this->get_base($tags[0])))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'content')) && - ($return = $this->sanitize($tags[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$tags[0]['attribs']]), $this->get_base($tags[0])))) { + ($return = $this->sanitize($tags[0]['data'], Misc::atom_03_construct_type($tags[0]['attribs']), $this->get_base($tags[0])))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10_MODULES_CONTENT, 'encoded')) && ($return = $this->sanitize($tags[0]['data'], \SimplePie\SimplePie::CONSTRUCT_HTML, $this->get_base($tags[0])))) { @@ -582,7 +582,7 @@ public function get_authors() public function get_copyright() { if ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'rights')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_10_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'rights')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'rights')) { @@ -832,7 +832,7 @@ public function get_links(string $rel = 'alternate') $keys = array_keys($this->data['links']); foreach ($keys as $key) { - if ($this->registry->call(Misc::class, 'is_isegment_nz_nc', [$key])) { + if (Misc::is_isegment_nz_nc($key)) { if (isset($this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key])) { $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key]); $this->data['links'][$key] =& $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key]; diff --git a/src/Locator.php b/src/Locator.php index 42c8f2aa0..445b1d664 100644 --- a/src/Locator.php +++ b/src/Locator.php @@ -132,7 +132,7 @@ public function get_base() $elements = $this->dom->getElementsByTagName('base'); foreach ($elements as $element) { if ($element->hasAttribute('href')) { - $base = $this->registry->call(Misc::class, 'absolutize_url', [trim($element->getAttribute('href')), $this->http_base]); + $base = Misc::absolutize_url(trim($element->getAttribute('href')), $this->http_base); if ($base === false) { continue; } @@ -170,19 +170,19 @@ protected function search_elements_by_tag($name, &$done, $feeds) break; } if ($link->hasAttribute('href') && $link->hasAttribute('rel')) { - $rel = array_unique($this->registry->call(Misc::class, 'space_separated_tokens', [strtolower($link->getAttribute('rel'))])); + $rel = array_unique(Misc::space_separated_tokens(strtolower($link->getAttribute('rel')))); $line = method_exists($link, 'getLineNo') ? $link->getLineNo() : 1; if ($this->base_location < $line) { - $href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->base]); + $href = Misc::absolutize_url(trim($link->getAttribute('href')), $this->base); } else { - $href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->http_base]); + $href = Misc::absolutize_url(trim($link->getAttribute('href')), $this->http_base); } if ($href === false) { continue; } - if (!in_array($href, $done) && in_array('feed', $rel) || (in_array('alternate', $rel) && !in_array('stylesheet', $rel) && $link->hasAttribute('type') && in_array(strtolower($this->registry->call(Misc::class, 'parse_mime', [$link->getAttribute('type')])), ['text/html', 'application/rss+xml', 'application/atom+xml'])) && !isset($feeds[$href])) { + if (!in_array($href, $done) && in_array('feed', $rel) || (in_array('alternate', $rel) && !in_array('stylesheet', $rel) && $link->hasAttribute('type') && in_array(strtolower(Misc::parse_mime($link->getAttribute('type'))), ['text/html', 'application/rss+xml', 'application/atom+xml'])) && !isset($feeds[$href])) { $this->checked_feeds++; $headers = [ 'Accept' => SimplePie::DEFAULT_HTTP_ACCEPT_HEADER, @@ -209,18 +209,18 @@ public function get_links() foreach ($links as $link) { if ($link->hasAttribute('href')) { $href = trim($link->getAttribute('href')); - $parsed = $this->registry->call(Misc::class, 'parse_url', [$href]); + $parsed = Misc::parse_url($href); if ($parsed['scheme'] === '' || preg_match('/^(https?|feed)?$/i', $parsed['scheme'])) { if (method_exists($link, 'getLineNo') && $this->base_location < $link->getLineNo()) { - $href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->base]); + $href = Misc::absolutize_url(trim($link->getAttribute('href')), $this->base); } else { - $href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->http_base]); + $href = Misc::absolutize_url(trim($link->getAttribute('href')), $this->http_base); } if ($href === false) { continue; } - $current = $this->registry->call(Misc::class, 'parse_url', [$this->file->url]); + $current = Misc::parse_url($this->file->url); if ($parsed['authority'] === '' || $parsed['authority'] === $current['authority']) { $this->local[] = $href; @@ -254,22 +254,14 @@ public function get_rel_link($rel) foreach ($xpath->query($query) as $link) { /** @var \DOMElement $link */ $href = trim($link->getAttribute('href')); - $parsed = $this->registry->call(Misc::class, 'parse_url', [$href]); + $parsed = Misc::parse_url($href); if ($parsed['scheme'] === '' || preg_match('/^https?$/i', $parsed['scheme'])) { if (method_exists($link, 'getLineNo') && $this->base_location < $link->getLineNo()) { - $href = $this->registry->call( - Misc::class, - 'absolutize_url', - [trim($link->getAttribute('href')), $this->base] - ); + $href = Misc::absolutize_url(trim($link->getAttribute('href')), $this->base); } else { - $href = $this->registry->call( - Misc::class, - 'absolutize_url', - [trim($link->getAttribute('href')), $this->http_base] - ); + $href = Misc::absolutize_url(trim($link->getAttribute('href')), $this->http_base); } if ($href === false) { return null; diff --git a/src/Parser.php b/src/Parser.php index 3929029f5..d2e9827cd 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -234,7 +234,7 @@ public function tag_open($parser, $tag, $attributes) } if (isset($attribs[\SimplePie\SimplePie::NAMESPACE_XML]['base'])) { - $base = $this->registry->call(Misc::class, 'absolutize_url', [$attribs[\SimplePie\SimplePie::NAMESPACE_XML]['base'], end($this->xml_base)]); + $base = Misc::absolutize_url($attribs[\SimplePie\SimplePie::NAMESPACE_XML]['base'], end($this->xml_base)); if ($base !== false) { $this->xml_base[] = $base; $this->xml_base_explicit[] = true; diff --git a/src/Sanitize.php b/src/Sanitize.php index 2a79d2361..bf5fabafe 100644 --- a/src/Sanitize.php +++ b/src/Sanitize.php @@ -417,7 +417,7 @@ public function sanitize($data, $type, $base = '') } if ($type & \SimplePie\SimplePie::CONSTRUCT_IRI) { - $absolute = $this->registry->call(Misc::class, 'absolutize_url', [$data, $base]); + $absolute = Misc::absolutize_url($data, $base); if ($absolute !== false) { $data = $absolute; } @@ -428,7 +428,7 @@ public function sanitize($data, $type, $base = '') } if ($this->output_encoding !== 'UTF-8') { - $data = $this->registry->call(Misc::class, 'change_encoding', [$data, 'UTF-8', $this->output_encoding]); + $data = Misc::change_encoding($data, 'UTF-8', $this->output_encoding); } } return $data; @@ -466,7 +466,7 @@ public function replace_urls($document, $tag, $attributes) foreach ($elements as $element) { foreach ($attributes as $attribute) { if ($element->hasAttribute($attribute)) { - $value = $this->registry->call(Misc::class, 'absolutize_url', [$element->getAttribute($attribute), $this->base]); + $value = Misc::absolutize_url($element->getAttribute($attribute), $this->base); if ($value !== false) { $value = $this->https_url($value); $element->setAttribute($attribute, $value); diff --git a/src/SimplePie.php b/src/SimplePie.php index 225fb8c6e..497749410 100644 --- a/src/SimplePie.php +++ b/src/SimplePie.php @@ -750,10 +750,10 @@ public function set_feed_url($url) if (is_array($url)) { trigger_error('Fetching multiple feeds with single SimplePie instance is deprecated since SimplePie 1.9.0, create one SimplePie instance per feed and use SimplePie::merge_items to get a single list of items.', \E_USER_DEPRECATED); foreach ($url as $value) { - $this->multifeed_url[] = $this->registry->call(Misc::class, 'fix_protocol', [$value, 1]); + $this->multifeed_url[] = Misc::fix_protocol($value, 1); } } else { - $this->feed_url = $this->registry->call(Misc::class, 'fix_protocol', [$url, 1]); + $this->feed_url = Misc::fix_protocol($url, 1); $this->permanent_url = $this->feed_url; } } @@ -1548,7 +1548,7 @@ public function init() $cache = false; if ($this->feed_url !== null) { - $parsed_feed_url = $this->registry->call(Misc::class, 'parse_url', [$this->feed_url]); + $parsed_feed_url = Misc::parse_url($this->feed_url); // Decide whether to enable caching if ($this->enable_cache && $parsed_feed_url['scheme'] !== '') { @@ -1614,7 +1614,7 @@ public function init() // Loop through each possible encoding, till we return something, or run out of possibilities foreach ($encodings as $encoding) { // Change the encoding to UTF-8 (as we always use UTF-8 internally) - if ($utf8_data = $this->registry->call(Misc::class, 'change_encoding', [$this->raw_data, $encoding, 'UTF-8'])) { + if ($utf8_data = Misc::change_encoding($this->raw_data, $encoding, 'UTF-8')) { // Create new parser $parser = $this->registry->create(Parser::class); @@ -2295,9 +2295,9 @@ public function sanitize(string $data, int $type, ?string $base = '') public function get_title() { if ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'title')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_10_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_03, 'title')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_03_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_10, 'title')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_090, 'title')) { @@ -2606,7 +2606,7 @@ public function get_links(string $rel = 'alternate') $keys = array_keys($this->data['links']); foreach ($keys as $key) { - if ($this->registry->call(Misc::class, 'is_isegment_nz_nc', [$key])) { + if (Misc::is_isegment_nz_nc($key)) { if (isset($this->data['links'][self::IANA_LINK_RELATIONS_REGISTRY . $key])) { $this->data['links'][self::IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][self::IANA_LINK_RELATIONS_REGISTRY . $key]); $this->data['links'][$key] =& $this->data['links'][self::IANA_LINK_RELATIONS_REGISTRY . $key]; @@ -2656,9 +2656,9 @@ public function get_all_discovered_feeds() public function get_description() { if ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'subtitle')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_10_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_03, 'tagline')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_03_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_10, 'description')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_090, 'description')) { @@ -2689,9 +2689,9 @@ public function get_description() public function get_copyright() { if ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'rights')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_10_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_03, 'copyright')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_03_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_20, 'copyright')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_DC_11, 'rights')) { diff --git a/src/Source.php b/src/Source.php index 25b97dce1..61f51d007 100644 --- a/src/Source.php +++ b/src/Source.php @@ -66,9 +66,9 @@ public function get_item() public function get_title() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'title')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_10_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'title')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_03_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10, 'title')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_090, 'title')) { @@ -307,7 +307,7 @@ public function get_links($rel = 'alternate') $keys = array_keys($this->data['links']); foreach ($keys as $key) { - if ($this->registry->call(Misc::class, 'is_isegment_nz_nc', [$key])) { + if (Misc::is_isegment_nz_nc($key)) { if (isset($this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key])) { $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key]); $this->data['links'][$key] =& $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key]; @@ -331,9 +331,9 @@ public function get_links($rel = 'alternate') public function get_description() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'subtitle')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_10_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'tagline')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_03_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10, 'description')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_090, 'description')) { @@ -356,9 +356,9 @@ public function get_description() public function get_copyright() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'rights')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_10_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'copyright')) { - return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); + return $this->sanitize($return[0]['data'], Misc::atom_03_construct_type($return[0]['attribs']), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'copyright')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'rights')) {