Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPStan level 8 #857

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft

PHPStan level 8 #857

wants to merge 21 commits into from

Conversation

grimmdude
Copy link

@grimmdude grimmdude commented Jan 10, 2024

Work in progress, 4 errors remaining...

#850, #856

@Art4 Art4 mentioned this pull request Jan 10, 2024
9 tasks
@Art4
Copy link
Contributor

Art4 commented Jan 16, 2024

Please note that we have to support PHP 7.2, so mixed and Union types are not supported there...

You should keep a look at your GitHub Action here: https://github.com/grimmdude/simplepie/actions/runs/7536994157/job/20515243462

@@ -90,11 +90,11 @@ public function test_get_relationship(string $data, string $expected): void

if ($enclosure = $item->get_enclosure()) {
$this->assertInstanceOf(Enclosure::class, $enclosure);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, this should already ensure $enclosure is !== null.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the error that this change fixes:

 ------ ------------------------------------------------------------------- 
  Line   tests/Unit/RestrictionTest.php                                     
 ------ ------------------------------------------------------------------- 
  95     Cannot call method get_restriction() on SimplePie\Enclosure|null.  
 ------ ------------------------------------------------------------------- 

Problem was that $restriction = $enclosure->get_restriction(); was originally outside of this conditional.

@@ -199,7 +202,8 @@ public static function absolutizeDataProvider(): array
public function testAbsolutizeString(string $base, string $relative, string $expected): void
{
$base = new IRI($base);
$this->assertSame($expected, IRI::absolutize($base, $relative)->get_iri());
$absolutized = IRI::absolutize($base, $relative);
$this->assertSame($expected, ($absolutized ? $absolutized->get_iri() : null));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests all assume $absolutized !== null so it might be more explicit to turn the assumption to a separate asserting. But maybe it does not make much difference to bother.

src/Source.php Outdated Show resolved Hide resolved
}

$element->parentNode->replaceChild($fragment, $element);
if ($parentNode = $element->parentNode) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is generally preferred to avoid implicit conversions of truthy/falsy values and only allow bool expressions in conditionals:

Suggested change
if ($parentNode = $element->parentNode) {
if (($parentNode = $element->parentNode) !== null) {

Though currently we do not enforce this, it would require PHPStan strict rules.

src/Parser.php Outdated Show resolved Hide resolved
src/Parser.php Outdated
@@ -190,7 +190,7 @@ public function parse(string &$data, string $encoding, string $url = '')
} else {
$attrName = $xml->localName;
}
$attributes[$attrName] = $xml->value;
$attributes[(string) $attrName] = (string) $xml->value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What error causes this cast? Neither localName or value are supposed to be anything but string https://www.php.net/manual/en/class.xmlreader.php

Copy link
Author

@grimmdude grimmdude May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtojnar, below is the error that casting these to string resolves.

 ------ -------------------------------------------------------------------------------------------------------------------------------- 
  Line   src/Parser.php                                                                                                                  
 ------ -------------------------------------------------------------------------------------------------------------------------------- 
  195    Parameter #3 $attributes of method SimplePie\Parser::tag_open() expects array<string, string>, array<int|string, mixed> given.  
 ------ -------------------------------------------------------------------------------------------------------------------------------- 

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just pushed bb89c21 which moves this fix more upstream by only casting localName to string. This also seems to fix this error 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants