Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ indent_style = space
max_line_length = off
trim_trailing_whitespace = false

[*.neon]
indent_size = 4
indent_style = space

[*.php]
indent_size = 4
indent_style = space
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ phpstan.neon
phpunit.xml
.php-cs-fixer.cache
.phpunit.cache/
.phpunit.result.cache
3 changes: 2 additions & 1 deletion autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
*/
class SimplePie_Autoloader
{
/** @var string */
protected $path;

/**
Expand All @@ -72,7 +73,7 @@ public function __construct()
*
* @param string $class The name of the class to attempt to load.
*/
public function autoload($class)
public function autoload(string $class): void
{
// Only load the class if it starts with "SimplePie"
if (strpos($class, 'SimplePie') !== 0)
Expand Down
2 changes: 1 addition & 1 deletion demo/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}

// Output buffer
function callable_htmlspecialchars($string)
function callable_htmlspecialchars(string $string): string
{
return htmlspecialchars($string);
}
Expand Down
2 changes: 2 additions & 0 deletions library/SimplePie/Decode/HTML/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function parse()
*
* @access private
* @return string|false The next byte, or false, if there is no more data
* @phpstan-impure
*/
public function consume()
{
Expand Down Expand Up @@ -547,6 +548,7 @@ public function entity()
'zwnj;' => "\xE2\x80\x8C"
];

$consumed = '';
for ($i = 0, $match = null; $i < 9 && $this->consume() !== false; $i++) {
// Cast for PHPStan on PHP < 8.0: We consumed as per the loop condition,
// so `$this->consumed` is non-empty and the substr offset is valid.
Expand Down
1 change: 1 addition & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ parameters:
- library/
- src/
- tests/
- utils/

ignoreErrors:
# Ignore that only one const exists atm
Expand Down
5 changes: 3 additions & 2 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ public function __construct(string $url, int $timeout = 10, int $redirects = 5,
} else {
curl_setopt($fp, CURLOPT_ENCODING, '');
}
/** @var non-empty-string $url */
curl_setopt($fp, CURLOPT_URL, $url);
curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($fp, CURLOPT_FAILONERROR, 1);
curl_setopt($fp, CURLOPT_RETURNTRANSFER, true);
curl_setopt($fp, CURLOPT_FAILONERROR, true);
curl_setopt($fp, CURLOPT_TIMEOUT, $timeout);
curl_setopt($fp, CURLOPT_CONNECTTIMEOUT, $timeout);
// curl_setopt($fp, CURLOPT_REFERER, \SimplePie\Misc::url_remove_credentials($url)); // FreshRSS removed
Expand Down
4 changes: 2 additions & 2 deletions src/HTTP/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private function add_header(string $name, string $value): void
$headers[$name][] = $value;
} else {
// For PHPStan: should be enforced by template parameter but PHPStan is not smart enough.
/** @var array<string, string>) */
/** @var array<string,string> */
$headers = &$this->headers;
$headers[$name] .= ', ' . $value;
}
Expand All @@ -258,7 +258,7 @@ private function replace_header(string $name, string $value): void
$headers[$name] = [$value];
} else {
// For PHPStan: should be enforced by template parameter but PHPStan is not smart enough.
/** @var array<string, string>) */
/** @var array<string,string> */
$headers = &$this->headers;
$headers[$name] = $value;
}
Expand Down
1 change: 0 additions & 1 deletion src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public function register(string $type, $class, bool $legacy = false)
return false;
}

/** @var string */
$base_class = $this->default[$type];

if (!is_subclass_of($class, $base_class)) {
Expand Down
6 changes: 6 additions & 0 deletions src/Sanitize.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,9 @@ protected function strip_tag(string $tag, DOMDocument $document, DOMXPath $xpath

if ($this->encode_instead_of_strip) {
foreach ($elements as $element) {
if (!($element instanceof \DOMNode)) {
continue;
}
$fragment = $document->createDocumentFragment();

// For elements which aren't script or style, include the tag itself
Expand Down Expand Up @@ -815,6 +818,9 @@ protected function strip_tag(string $tag, DOMDocument $document, DOMXPath $xpath
return;
} else {
foreach ($elements as $element) {
if (!($element instanceof \DOMNode)) {
continue;
}
$fragment = $document->createDocumentFragment();
$number = $element->childNodes->length;
for ($i = $number; $i > 0; $i--) {
Expand Down
7 changes: 4 additions & 3 deletions src/SimplePie.php
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,8 @@ public function init()
$single_success = $this->multifeed_objects[$i]->init();
$success |= $single_success;
if (!$single_success) {
$this->error[$i] = $this->multifeed_objects[$i]->error();
$error = $this->multifeed_objects[$i]->error() ?? '';
$this->error[$i] = is_string($error) ? $error : implode('; ', $error);
}
$i++;
}
Expand Down Expand Up @@ -1954,7 +1955,7 @@ public function init()
// Cache the file if caching is enabled
$this->data['cache_expiration_time'] = \SimplePie\HTTP\Utils::negociate_cache_expiration_time($this->data['headers'] ?? [], $this->cache_duration, $this->cache_duration_min, $this->cache_duration_max);

if ($cache && !$cache->set_data($this->get_cache_filename($this->feed_url), $this->data, $this->cache_duration)) {
if ($cache instanceof DataCache && !$cache->set_data($this->get_cache_filename($this->feed_url), $this->data, $this->cache_duration)) {
trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
}
return true;
Expand Down Expand Up @@ -3066,7 +3067,7 @@ public function get_links(string $rel = 'alternate')
}
// https://datatracker.ietf.org/doc/html/rfc8288
if (is_string($link_headers) &&
preg_match_all('/<(?P<uri>[^>]+)>\s*;\s*rel\s*=\s*(?P<quote>"?)' . preg_quote($rel) . '(?P=quote)\s*(?=,|$)/i', $link_headers, $matches)) {
preg_match_all('/<(?P<uri>[^>]+)>\s*;\s*rel\s*=\s*(?P<quote>"?)' . preg_quote($rel, '/') . '(?P=quote)\s*(?=,|$)/i', $link_headers, $matches)) {
return $matches['uri'];
}
}
Expand Down
5 changes: 3 additions & 2 deletions tests/IRITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ function ($errno, $errstr): bool {
E_USER_NOTICE
);

// @phpstan-ignore property.notFound (we want to test that it fails)
$should_fail = $iri->nonexistent_prop;
}

Expand All @@ -481,7 +482,7 @@ public function testBlankHost(): void
$iri = new SimplePie_IRI('http://example.com/a/?b=c#d');
$iri->host = null;

self::assertSame(null, $iri->host);
self::assertNull($iri->host);
self::assertSame('http:/a/?b=c#d', (string) $iri);
}

Expand All @@ -490,6 +491,6 @@ public function testBadPort(): void
$iri = new SimplePie_IRI();
$iri->port = 'example';

self::assertSame(null, $iri->port);
self::assertNull($iri->port);
}
}
4 changes: 2 additions & 2 deletions tests/LocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testInvalidMIMEType(): void
$locator->set_registry($registry);

$feed = $locator->find(SIMPLEPIE_LOCATOR_ALL, $all);
self::assertSame(null, $feed);
self::assertNull($feed);
}

public function testDirectNoDOM(): void
Expand Down Expand Up @@ -140,7 +140,7 @@ public function test_from_file(File $data): void
self::assertInstanceOf(FileMock::class, $feed);
$success = array_filter($expected, [get_class($this), 'filter_success']);

$found = array_map([get_class($this), 'map_url_file'], $all);
$found = is_array($all) ? array_map([get_class($this), 'map_url_file'], $all) : [];
self::assertSame($success, $found);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/IRITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public function testBlankHost(): void
$iri = new IRI('http://example.com/a/?b=c#d');
$iri->host = null;

self::assertSame(null, $iri->host);
self::assertNull($iri->host);
self::assertSame('http:/a/?b=c#d', (string) $iri);
}

Expand All @@ -500,6 +500,6 @@ public function testBadPort(): void
$iri = new IRI();
$iri->port = 'example';

self::assertSame(null, $iri->port);
self::assertNull($iri->port);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/LocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testInvalidMIMEType(): void
$locator->set_registry($registry);

$feed = $locator->find(SimplePie::LOCATOR_ALL, $all);
self::assertSame(null, $feed);
self::assertNull($feed);
}

public function testDirectNoDOM(): void
Expand Down Expand Up @@ -153,7 +153,7 @@ public function test_from_file(File $data): void
self::assertInstanceOf(FileMock::class, $feed);
$success = array_filter($expected, [get_class($this), 'filter_success']);

$found = array_map([get_class($this), 'map_url_file'], $all);
$found = is_array($all) ? array_map([get_class($this), 'map_url_file'], $all) : [];
self::assertSame($success, $found);
}

Expand Down
Binary file modified tests/data/feed-utf16be.xml
Binary file not shown.
Binary file modified tests/data/feed-utf16le.xml
Binary file not shown.
1 change: 0 additions & 1 deletion tests/data/feed_rss-2.0.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<description>Test feed description 1</description>
<guid>http://example.net/tests/#1.1</guid>
<link>http://example.net/tests/#1.1</link>
<media:thumbnail url="/link?a=&quot;b&quot;&amp;c=&lt;d&gt;" />
</item>
</channel>
</rss>