[CLEANUP] Rely on PHP to detect access to uninitialized properties#1620
Conversation
JakeQZ
left a comment
There was a problem hiding this comment.
Looks good. I've suggested a couple of assert()s which may be worthwhile additions if you agree.
coverage: 98.244% (+0.6%) from 97.672% — task/drop-throw/4 into main
Not looked into the reports - do they indicate which lines of code are not exercised in tests? (It's impossible to test code which can never be executed.)
Can we get to 100%? Back-of-an-envelope caclulation suggests there might just be three other impossible assert-throwing branches to eliminate to get us there...
| } | ||
|
|
||
| $instance = new static(); | ||
| $instance->setHtml($unprocessedHtml); |
There was a problem hiding this comment.
Perhaps we should add after this line:
\assert($instance->xPath instanceof \DOMXPath);This would maybe make it easier to debug any potential breakages in development, pinpointing the point of failure, rather than having to diagnose a less fathomable failure further down the line.
| public static function fromDomDocument(\DOMDocument $document): self | ||
| { | ||
| $instance = new static(); | ||
| $instance->setDomDocument($document); |
| @@ -50,9 +50,9 @@ | |||
| protected $domDocument = null; | |||
There was a problem hiding this comment.
We can similarly remove the nulls from this property - but as a separate PR (and perhaps likewise also add assertions for it being initialized).
We'll be able to come very near. I'll create a ticket for raising the code coverage with a list of not-yet-covered parts. |
f95dcbb to
d82b3bd
Compare
I just checked, and PHPStan would give an error if such |
I'd rather not. In the production code, I'd prefer to keep doing this:
Would you be okay with doing/continuing this? |
I'm fine with all this. But using or not using E.g. suppose I added this to public static function fromNothing(): self
{
$instance = new static();
return $instance;
}This would return an instance with |
|
I've given |
In our code, this exception that indicated that the property was access before initialization is never thrown. So we can simplify our code by removing the check (and the exception) and make use of PHP's own mechanism for throwing an error if the property is accessed before initialization.
d82b3bd to
0be5366
Compare
|
Rebased and added the assertions. |
ac6ce85 to
e9dd538
Compare
I wonder if there's a way to get the best of both worlds, running PHPStan twice:
From phpstan/phpstan#12719, it seems this would require creating a PHPStan extension, which seems like quite a bit of effort for something that isn't much of an issue - though we would like to ensure our DocBlock types are correct and consistent with the code (and vice versa) when used internally. |
It doesn't look too onerous to create an extension to handle |
This sounds intriguing to me. I wouldn't want to put time into writing the extension, but I'd be willing to review it and see its benefits. :-) |
I was easily able to create an extension to potentially disable some warnings, hook it in, but then unable to get context information (to disable the warning only within an |
Got some helpful pointers from some helpful people, so now have #1624... |
In our code, this exception that indicated that the property was access before initialization is never thrown.
So we can simplify our code by removing the check (and the exception) and make use of PHP's own mechanism for throwing an error if the property is accessed before initialization.