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

Raise PHPStan to level 6 #837

Merged
merged 35 commits into from
Aug 25, 2023
Merged

Raise PHPStan to level 6 #837

merged 35 commits into from
Aug 25, 2023

Conversation

jtojnar
Copy link
Contributor

@jtojnar jtojnar commented May 30, 2023

No description provided.

@jtojnar jtojnar changed the title Raise PHPStan to level 3 Raise PHPStan to level 4 May 31, 2023
@jtojnar jtojnar force-pushed the phpstan-3 branch 2 times, most recently from 9b23542 to 46c813b Compare May 31, 2023 02:53
@jtojnar jtojnar changed the title Raise PHPStan to level 4 Raise PHPStan to level 5 Jun 8, 2023
@pull-request-size pull-request-size bot added size/XL and removed size/L labels Jun 9, 2023
@jtojnar jtojnar force-pushed the phpstan-3 branch 2 times, most recently from cb17a85 to 3e4edc6 Compare June 9, 2023 00:19
@jtojnar jtojnar mentioned this pull request Jun 9, 2023
48 tasks
@jtojnar jtojnar changed the title Raise PHPStan to level 5 Raise PHPStan to level 6 Jun 12, 2023
@mblaney
Copy link
Member

mblaney commented Aug 6, 2023

sorry I've been away, it this ok to merge or do we need to look at other pr's first?

@jtojnar
Copy link
Contributor Author

jtojnar commented Aug 6, 2023

@mblaney I wanted to give people a time to review this before merging but I guess there has been enough time now.

But it looks like new versions of dev dependencies broke the CI so #841 should be merged first.

Necessary for PHPStan level 3.
Mostly include false in return types when needed.

Necessary for PHPStan level 3.
Looks like it could potentially return true since it has been split out of init in ce9ac85
Not sure if it can happen in practice, possibly when cache expires?

Necessary for PHPStan level 3.
The headers returned from `curl_exec` are strings, whereas `File::$headers` expects an array.

Necessary for PHPStan level 3.
According to type annotations in PHPDoc.

Necessary for PHPStan level 3.
Necessary for PHPStan level 4.
It can be set to null by `Locator::find`.

Necessary for PHPStan level 4.
- `explode` with non-empty separator will return a non-empty array, remove the redundant `if`.
- Do not reuse `$duration_parent` variable name, it has different type.

Necessary for PHPStan level 4.
instanceof will return false on non-object types.
@jtojnar jtojnar force-pushed the phpstan-3 branch 2 times, most recently from 551f102 to e60fc48 Compare August 8, 2023 19:45
Explicitly unroll since PHPStan does not support recursive array types.
Four levels should be good enough for most domains in practice.

Necessary for PHPStan level 4.
We often have checks that throw `InvalidArgumentException`
when value of an argument does not match the type specified by PHPDoc
to ensure consumers do not break preconditions.
As a result, PHPStan level 4 would complain:

    Strict comparison using !== between 'GET' and 'GET' will always evaluate to false.
    Result of && is always false.

We could suppress it by setting `treatPhpDocTypesAsCertain: false` in `phpstan.neon`
but that would essentially disable static type checking for all internal
uses of the API as well, significantly decreasing our level of confidence.

Let’s ignore just the specific places where it causes issues.
It would return `false` on PHP < 8.0 when using out-of-range indices.

Necessary for PHPStan level 4.
Assignment of non-empty string will always be truthy.

Necessary for PHPStan level 4.
The check was here since the method was introduced 01eb2c5
As far as I can tell, `File` does not really parse headers any further,
so `$headers['link']` can only be a string, if it exists.

Necessary for PHPStan level 4.
It contained a reference to OldTests class, which was removed in 22b8eb0

Necessary for PHPStan level 5.
Necessary for PHPStan level 5.
As per PHP docs, the XML functions also accept just a method name as a string,
even though it is not actually a callable:

    Note: Instead of a function name, an array containing an object reference and a method name can also be supplied.

PHPStan level 5 will complain though:

    Parameter simplepie#2 $handler of function xml_set_character_data_handler expects callable(): mixed, 'cdata' given.

Let’s use proper callables.
In psr/simple-cache < 2.0.0, which is installed on PHP 7.4 or lower,
the `Psr\SimpleCache\InvalidArgumentException` interface does not extend `Throwable`.
But the current versions of PHPUnit require subtypes of `Throwable` to be given to `expectException()` method.
As a result, PHPStan will complain when we try to expect the non-`Throwable`-inheriting interface:

    Parameter simplepie#1 $exception of method PHPUnit\Framework\TestCase::expectException() expects class-string<Throwable>, string given.
This is in preparation for PHPStan level 6, which will complain when “method has no return type specified”.

Mostly done using Rector’s `AddVoidReturnTypeWhereNoReturnRector` and `SetList::TYPE_DECLARATION` with some minor nudges.
These became unnecessary in the previous commit.
There are not used by anything and prevent PHPStan from recognizing documentation comments,
resulting in complaints about missing type annotations.
So that we can keep property annotations simple.
It is expected to be a nested array so exploding does not make sense.
This has been broken since it was introduced in 8c375c2.

Let’s also remove the checks that can be addressed by a type hint.
The fourth argument (`$max_checked_feeds`) is supposed to be `int`.
These were missed in d894521
In preparation for PHPStan level 6
@jtojnar
Copy link
Contributor Author

jtojnar commented Aug 8, 2023

@mblaney I have fixed the issues.

@mblaney
Copy link
Member

mblaney commented Aug 25, 2023

thanks @jtojnar can someone confirm this won't affect #777? otherwise will wait to hear about which should be merged first

@Art4
Copy link
Contributor

Art4 commented Aug 25, 2023

can someone confirm this won't affect #777? otherwise will wait to hear about which should be merged first

At a first glance the changes should not collied with #777. But even if they are, the conflicts should be very easy to resolve.

@mblaney mblaney merged commit 65bc7d6 into simplepie:master Aug 25, 2023
9 checks passed
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