[TASK] Add PHPStan extension to allow assertions#1624
Conversation
Suppress warnings for `assert()`s that, according to the DocBlock, can never fail, but in reality could, due to programmer error, since the DocBlock types are not enforced by PHP. This allows us to retain `treatPhpDocTypesAsCertain:true` for stricter static analysis, whilst also using assertions for runtime checks in a development environment, without cluttering up the code with `@phpstan-ignore` comments. This change reverts and replaces #1622.
|
Got some helpful pointers from some kind folk in response to phpstan/phpstan#14616. Not sure about the location and namespace of the extension class (file). Also, I read some suggestions that PHPStan extensions should have a corresponding Looking for initial comments and feedback on this, rather than merging right now... |
|
Catering for if ($impossibleConditionAccordingToDocBlock) {
throw \Exception();
}looks quite challenging... |
|
Concerning the location: I'd say we put this in The class then also needs to be |
|
Also, we might consider moving this to a separate repo & package at some point in time so we can reuse it in PHP-CSS-Parser. |
|
As far as the code is concerned, this already looks quite good to me! |
configurations that don't have `IgnoreErrorExtension` from PHPStan
Done.
Done.
I took this challenge on as a break from my paid work. Then sort-of wished I hadn't. But I think it was a decent learning curve, with a result that is potentially reusable, at least as an example.
I dug into this and have added tests. |
|
@oliverklee request-review |
| */ | ||
| public static function fromHtml(string $unprocessedHtml): self | ||
| { | ||
| // @phpstan-ignore-next-line argument.type We're checking for a contract violation here. |
There was a problem hiding this comment.
This line ignores everything, not just the specified identifier
There was a problem hiding this comment.
Thanks for spotting that. It seems we have been using @phpstan-ignore-next-line in several places where we should have @phpstan-ignore, which also only ignores warnings from the next line, but allows (requires) a warning type parameter to narrow down which warnings should be suppressed.
|
I'd like to have this for our sister project as well. Should I port it, or would you be willing (or like) to do it, @JakeQZ ? |
Suppress warnings for
assert()s that, according to the DocBlock, can never fail, but in reality could, due to programmer error, since the DocBlock types are not enforced by PHP.This allows us to retain
treatPhpDocTypesAsCertain:truefor stricter static analysis, whilst also using assertions for runtime checks in a development environment, without cluttering up the code with@phpstan-ignorecomments.This change reverts and replaces #1622.