Skip to content

Commit

Permalink
Do not call pure Misc methods through registry
Browse files Browse the repository at this point in the history
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 f6e63e6
  • Loading branch information
jtojnar committed Mar 19, 2023
1 parent 0dadc1e commit f30ba38
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 50 deletions.
16 changes: 8 additions & 8 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -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])))) {
Expand Down Expand Up @@ -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])))) {
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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];
Expand Down
32 changes: 12 additions & 20 deletions src/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Sanitize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 11 additions & 11 deletions src/SimplePie.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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'] !== '') {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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')) {
Expand Down

0 comments on commit f30ba38

Please sign in to comment.