Skip to content

PHPStan: Raise to level 8 - #309

Draft
jtojnar wants to merge 48 commits into
microformats:mainfrom
jtojnar:phpstan-8
Draft

jtojnar wants to merge 48 commits into
microformats:mainfrom
jtojnar:phpstan-8

Conversation

This will allow the tests to work offline or if the servers have an outage,
and they will not break if the site changes.
Reported by PHPStan
Reported by PHPStan.
`parseImg` can return `array` in which case we would not resolve the URL properly.
This is already declared using native PHP keyword.
Performed automatically using Rector.
PHP would complain about passing `false` to `substr` with `strict_types` enabled.
To catch type errors rather than having PHP silently mangle the data and maybe do something incorrect.
To satisfy PHPStan level 2.
Performed automatically using rector.
Level 3 would complain:

    Parameter &$dtValue by-ref type of function Mf2\normalizeTimezoneOffset() expects string, string|null given.
Level 4 would compain:

    If condition is always true.

Because the method will throw an error on failure on PHP 8.1.0+ but will return false on older versions.
Level 4 would complain:

    Property DOMElement::$tagName (string) in isset() is not nullable.

But the property has always been non-null:
https://web.archive.org/web/20130818013638/http://www.php.net/manual/en/class.domelement.php#domelement.props.tagname
Level 5 complains:

    function curl_setopt expects bool, int given.
Level 6 would complain:

    Method … return type has no value type specified in iterable type array.
Level 6 would complain:

    Method … has parameter $… with no type specified.
Level 6 would complain about missing return type.

Let’s copy it from the parent function.
Fixes more PHPStan Level 6 complaints.
Fixes more PHPStan Level 6 complaints.
Fixes more PHPStan Level 6 complaints.
`parseRecursive(…, depth: 0)` always returns an array of non-empty arrays. This will be pointed out by PHPStan once we adjust PHPDoc.
Fixes more PHPStan Level 6 complaints.
Once we add PHPUnit extension, PHPStan will start to complain because the `parse` result always contains `rels` and `rel-urls` according to PHPDoc return annotation:

    Call to method PHPUnit\Framework\Assert::assertArrayHasKey() with '…' and … will always evaluate to true.

We can just drop those key checks. Even if the annotations were incorrect for some reason, the later assertions would fail anyway.
This will be required for PHPUnit assertion to narrow down types to pass PHPStan level 7.
This is expected by `CURLOPT_URL` according to PHPStan level 7.
If we enable PHPStan level 7, it will complain about `rels` and `rel-urls` possibly being `stdClass`.
Let’s assert it is an array.
These should not fail since the XPath query is static but PHPStan level 7 will complain about `foreach.nonIterable`:

    Argument of an invalid type DOMNodeList<DOMNameSpaceNode|DOMNode>|false supplied for foreach, only iterables are supported.
If encoding detection fails, `mb_detect_encoding` let’s switch it to use
internal encoding as the fallback. It defaults to UTF-8 which is as good
fallback as anything.

https://www.php.net/manual/en/function.mb-detect-encoding.php
https://www.php.net/manual/en/function.mb-encode-numericentity.php
https://www.php.net/manual/en/function.mb-internal-encoding.php
`array_unique` can return array with non-consecutive indices.
PHPStan level 7 correctly points out that if the method did not find any `dtValue`, `false` would be passed to a function like `preg_match`, which expects a `string`.

This was previously coerced to empty string but with `strict_types` it will raise an error at runtime.

Let’s terminate the function early if we have no value and adjust the return type to reflect that the function can return `false`.
Picked up by PHPStan level 7.
This is essentially equivalent but allows PHPStan level 7 to infer its type as `object{}&stdClass` rather than just `stdClass`.
As picked up by PHPStan level 8.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant