diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff6f9fdd3..754510de8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,6 +67,10 @@ jobs: coverage: none tools: none + - name: Remove PHPStan for incompatible PHP versions + if: ${{ matrix.php < '8.0' }} + run: composer remove --dev phpstan/phpstan phpstan/phpstan-phpunit + # Install dependencies and handle caching in one go. # @link https://github.com/marketplace/actions/install-composer-dependencies - name: "Install Composer dependencies" @@ -81,7 +85,9 @@ jobs: - name: Run unit tests run: composer test + # It would be sufficient to run PHPStan with one supported PHP version - name: Run static analysis + if: ${{ matrix.php >= '8.0' }} run: composer phpstan -- --error-format=github test-compiled: diff --git a/composer.json b/composer.json index 2869c4969..33818d621 100644 --- a/composer.json +++ b/composer.json @@ -33,8 +33,9 @@ "donatj/mock-webserver": "^2.7", "friendsofphp/php-cs-fixer": "^2.19 || ^3.8", "mf2/mf2": "^0.5.0", - "phpstan/phpstan": "~1.12.2", - "phpunit/phpunit": "^8 || ^9 || ^10", + "phpstan/phpstan": "~2.3.5", + "phpstan/phpstan-phpunit": "~2.1.2", + "phpunit/phpunit": "^8 || ^9", "psr/http-client": "^1.0", "psr/http-factory": "^1.0", "psr/simple-cache": "^1 || ^2 || ^3" @@ -69,7 +70,7 @@ "coverage": "phpunit --coverage-html=.phpunit.cache/code-coverage", "cs": "php-cs-fixer fix --verbose --dry-run --diff", "fix": "php-cs-fixer fix --verbose --diff", - "phpstan": "phpstan analyze --memory-limit 512M", + "phpstan": "phpstan analyse --memory-limit 512M", "test": "phpunit" } } diff --git a/phpstan.dist.neon b/phpstan.dist.neon index 6562144b2..d3b949821 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -1,4 +1,8 @@ parameters: + phpVersion: + min: 70400 # PHP 7.4 + max: 80599 # PHP 8.5 + level: 8 paths: @@ -7,54 +11,15 @@ parameters: - tests/ - utils/ - ignoreErrors: - # Ignore that only one const exists atm - - - message: "#^Strict comparison using \\!\\=\\= between 'GET' and 'GET' will always evaluate to false\\.$#" - count: 1 - path: src/HTTP/Psr18Client.php - - # Not used since https://github.com/simplepie/simplepie/commit/b2eb0134d53921e75f0fa70b1cf901ed82b988b1 but cannot be removed due to BC. - - '(Constructor of class SimplePie\\Enclosure has an unused parameter \$javascript\.)' - - # Testing legacy dynamic property usage. - - '(Access to an undefined property SimplePie.IRI::\$nonexistent_prop\.)' - - - - message: '(^Strict comparison using === between string and false will always evaluate to false\.$)' - count: 1 - path: src/HTTP/Parser.php - # Only occurs on PHP ≥ 8.0 - reportUnmatched: false - - - - message: '(^Strict comparison using === between string and false will always evaluate to false\.$)' - count: 1 - path: src/IRI.php - # Only occurs on PHP ≥ 8.0 - reportUnmatched: false - - - - message: '(^Parameter #1 \$exception of method PHPUnit\\Framework\\TestCase::expectException\(\) expects class-string, string given\.$)' - - count: 3 - path: tests/Unit/Cache/Psr16Test.php - # Only occurs on PHP ≤ 7.4 - reportUnmatched: false - - - - message: '(^Parameter \$parser of method SimplePie\\Parser::(tag_open|cdata|tag_close)\(\) has invalid type XMLParser\.$)' - - count: 3 - path: src/Parser.php - # Only occurs on PHP ≤ 7.4 - reportUnmatched: false + excludePaths: + analyseAndScan: + - .git/*? + - build/*? + - vendor/* - # PHPStan stubs bug https://github.com/phpstan/phpstan/issues/8629 - - - message: '(^Access to an undefined property XMLReader::\$\w+\.$)' - # Only occurs on PHP ≥ 8.2 - reportUnmatched: false + treatPhpDocTypesAsCertain: false # TODO: Fix includes: - utils/PHPStan/extension.neon + - vendor/phpstan/phpstan-phpunit/extension.neon + - vendor/phpstan/phpstan-phpunit/rules.neon diff --git a/src/Cache/Psr16.php b/src/Cache/Psr16.php index 4dc8161d2..c353db8e5 100644 --- a/src/Cache/Psr16.php +++ b/src/Cache/Psr16.php @@ -83,6 +83,7 @@ public function get_data(string $key, $default = null) */ public function set_data(string $key, array $value, ?int $ttl = null): bool { + // @phpstan-ignore argument.type (mixed is not supported by PHP < 8.0, used by psr/simple-cache >= 2) return $this->cache->set($key, $value, $ttl); } diff --git a/src/Enclosure.php b/src/Enclosure.php index 89231c7f9..e2c72ed85 100644 --- a/src/Enclosure.php +++ b/src/Enclosure.php @@ -195,6 +195,7 @@ class Enclosure * @param Rating[]|null $ratings * @param Restriction[]|null $restrictions * @param string[]|null $thumbnails + * @phpstan-ignore constructor.unusedParameter */ public function __construct( ?string $link = null, diff --git a/src/File.php b/src/File.php index 27dd5e339..5f0f25990 100644 --- a/src/File.php +++ b/src/File.php @@ -645,7 +645,7 @@ final public static function fromResponse(Response $response): self /** * @param array $headers * @param array $curl_options - * @return \CurlHandle + * @return resource */ private static function curlInit( string $url, @@ -656,6 +656,9 @@ private static function curlInit( bool $setAcceptEncoding = true ) { $fp = curl_init(); + if ($fp === false) { + throw new \SimplePie\Exception('Could not initialize cURL'); + } $headers2 = []; foreach ($headers as $key => $value) { @@ -683,8 +686,13 @@ private static function curlInit( 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 - curl_setopt($fp, CURLOPT_USERAGENT, $useragent); + // $referer = \SimplePie\Misc::url_remove_credentials($url); + // if ($referer !== '') { + // curl_setopt($fp, CURLOPT_REFERER, $referer); // FreshRSS removed + // } + if ($useragent !== '') { + curl_setopt($fp, CURLOPT_USERAGENT, $useragent); + } curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2); foreach ($curl_options as $curl_param => $curl_value) { curl_setopt($fp, $curl_param, $curl_value); diff --git a/src/HTTP/FileClient.php b/src/HTTP/FileClient.php index 90fefebb0..6b35dcc8d 100644 --- a/src/HTTP/FileClient.php +++ b/src/HTTP/FileClient.php @@ -45,7 +45,6 @@ public function __construct(Registry $registry, array $options = []) */ public function request(string $method, string $url, array $headers = []): Response { - // @phpstan-ignore-next-line Enforce PHPDoc type. if ($method !== self::METHOD_GET) { throw new InvalidArgumentException(sprintf( '%s(): Argument #1 ($method) only supports method "%s".', diff --git a/src/Parser.php b/src/Parser.php index 103baa453..8f1fb1ca5 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -94,22 +94,27 @@ public function parse(string &$data, string $encoding, string $url = '') // Strip BOM: // UTF-32 Big Endian BOM if (substr($data, 0, 4) === "\x00\x00\xFE\xFF") { + /** @phpstan-ignore parameterByRef.type (for PHP < 8.0) */ $data = substr($data, 4); } // UTF-32 Little Endian BOM elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00") { + /** @phpstan-ignore parameterByRef.type (for PHP < 8.0) */ $data = substr($data, 4); } // UTF-16 Big Endian BOM elseif (substr($data, 0, 2) === "\xFE\xFF") { + /** @phpstan-ignore parameterByRef.type (for PHP < 8.0) */ $data = substr($data, 2); } // UTF-16 Little Endian BOM elseif (substr($data, 0, 2) === "\xFF\xFE") { + /** @phpstan-ignore parameterByRef.type (for PHP < 8.0) */ $data = substr($data, 2); } // UTF-8 BOM elseif (substr($data, 0, 3) === "\xEF\xBB\xBF") { + /** @phpstan-ignore parameterByRef.type (for PHP < 8.0) */ $data = substr($data, 3); } @@ -121,6 +126,7 @@ public function parse(string &$data, string $encoding, string $url = '') if (substr($data, 0, 5) === '')) !== false) { $declaration = $this->registry->create(DeclarationParser::class, [substr($data, 5, $pos - 5)]); if ($declaration->parse()) { + /** @phpstan-ignore parameterByRef.type (for PHP < 8.0) */ $data = substr($data, $pos + 2); $data = 'version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . "\n" . self::set_doctype($data); @@ -289,6 +295,7 @@ public function get_data() * @param XMLParser|resource|null $parser * @param array $attributes * @return void + * @phpstan-ignore class.notFound (XMLParser requires PHP >= 8.0) */ public function tag_open($parser, string $tag, array $attributes) { @@ -345,6 +352,7 @@ public function tag_open($parser, string $tag, array $attributes) /** * @param XMLParser|resource|null $parser * @return void + * @phpstan-ignore class.notFound (XMLParser requires PHP >= 8.0) */ public function cdata($parser, string $cdata) { @@ -358,6 +366,7 @@ public function cdata($parser, string $cdata) /** * @param XMLParser|resource|null $parser * @return void + * @phpstan-ignore class.notFound (XMLParser requires PHP >= 8.0) */ public function tag_close($parser, string $tag) { diff --git a/src/Registry.php b/src/Registry.php index c02652bb8..274e4b42c 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -206,6 +206,7 @@ public function &create($type, array $parameters = []) $instance->set_registry($this); } elseif (method_exists($instance, 'set_registry')) { trigger_error(sprintf('Using the method "set_registry()" without implementing "%s" is deprecated since SimplePie 1.8.0, implement "%s" in "%s".', RegistryAware::class, RegistryAware::class, $class), \E_USER_DEPRECATED); + // @phpstan-ignore method.nonObject $instance->set_registry($this); } diff --git a/src/Sanitize.php b/src/Sanitize.php index 9a5aca0cd..cd27c17b8 100644 --- a/src/Sanitize.php +++ b/src/Sanitize.php @@ -166,7 +166,6 @@ public function pass_cache_data(bool $enable_cache = true, string $cache_locatio $this->cache_location = $cache_location; } - // @phpstan-ignore-next-line Enforce PHPDoc type. if (!is_string($cache_name_function) && !$cache_name_function instanceof NameFilter) { throw new InvalidArgumentException(sprintf( '%s(): Argument #3 ($cache_name_function) must be of type %s', diff --git a/src/SimplePie.php b/src/SimplePie.php index 2bbe1dbea..949862f00 100644 --- a/src/SimplePie.php +++ b/src/SimplePie.php @@ -2029,7 +2029,6 @@ protected function fetch_data(&$cache) $cache = new BaseDataCache($cache); } - // @phpstan-ignore-next-line Enforce PHPDoc type. if ($cache !== false && !$cache instanceof DataCache) { throw new InvalidArgumentException(sprintf( '%s(): Argument #1 ($cache) must be of type %s|false', @@ -2112,7 +2111,7 @@ protected function fetch_data(&$cache) // Set raw_data to false here too, to signify that the cache // is still valid. $this->raw_data = false; - if (isset($file)) { // FreshRSS + if (isset($file)) { // FreshRSS // @phpstan-ignore isset.variable $old_cache_control = $this->data['headers']['cache-control'] ?? ''; $old_max_age = \SimplePie\HTTP\Utils::get_http_max_age($this->data['headers']); @@ -3622,8 +3621,6 @@ public static function merge_items(array $urls, int $start = 0, int $end = 0, in foreach ($urls as $arg) { if ($arg instanceof SimplePie) { $items = array_merge($items, $arg->get_items(0, $limit)); - - // @phpstan-ignore-next-line Enforce PHPDoc type. } else { trigger_error('Arguments must be SimplePie objects', E_USER_WARNING); } diff --git a/tests/Integration/SimplePieTest.php b/tests/Integration/SimplePieTest.php index eac342115..95a5de65c 100644 --- a/tests/Integration/SimplePieTest.php +++ b/tests/Integration/SimplePieTest.php @@ -347,6 +347,7 @@ public function testMicroformatItems(string $path, string $feedTitle, array $tit self::assertSame($feedTitle, $feed->get_title()); $items = $feed->get_items(); + // @phpstan-ignore phpunit.assertCount self::assertSame(count($titles), count($items), 'Number of items does not match'); foreach (array_map(null, $titles, $items) as $i => [$expectedTitle, $item]) { assert($item !== null); // For PHPStan diff --git a/tests/Unit/FileTest.php b/tests/Unit/FileTest.php index 1432292e8..ea0972aea 100644 --- a/tests/Unit/FileTest.php +++ b/tests/Unit/FileTest.php @@ -26,6 +26,7 @@ public function testClassExists(): void public function testFileExtendsResponse(): void { + // @phpstan-ignore staticMethod.alreadyNarrowedType self::assertInstanceOf(Response::class, new FileMock('')); } diff --git a/tests/Unit/HTTP/Psr18ClientTest.php b/tests/Unit/HTTP/Psr18ClientTest.php index 808c8bf01..e8bc4974c 100644 --- a/tests/Unit/HTTP/Psr18ClientTest.php +++ b/tests/Unit/HTTP/Psr18ClientTest.php @@ -27,6 +27,7 @@ public function testRequestReturnsResponse(): void $this->createMock(UriFactoryInterface::class) ); + // @phpstan-ignore staticMethod.alreadyNarrowedType self::assertInstanceOf(Response::class, $client->request(Client::METHOD_GET, 'https://example.com/feed.xml')); } diff --git a/tests/Unit/HTTP/Psr7ResponseTest.php b/tests/Unit/HTTP/Psr7ResponseTest.php index a32040fcb..60e895aa7 100644 --- a/tests/Unit/HTTP/Psr7ResponseTest.php +++ b/tests/Unit/HTTP/Psr7ResponseTest.php @@ -17,6 +17,7 @@ class Psr7ResponseTest extends TestCase { public function testPsr7ResponseExtendsResponse(): void { + // @phpstan-ignore staticMethod.alreadyNarrowedType self::assertInstanceOf(Response::class, new Psr7Response($this->createMock(ResponseInterface::class), '', '')); } diff --git a/tests/Unit/IRITest.php b/tests/Unit/IRITest.php index a4064ae3d..7a9390f80 100644 --- a/tests/Unit/IRITest.php +++ b/tests/Unit/IRITest.php @@ -483,6 +483,7 @@ function ($errno, $errstr): bool { E_USER_NOTICE ); + // @phpstan-ignore property.notFound (we want to test that it fails) $should_fail = $iri->nonexistent_prop; }