-
Notifications
You must be signed in to change notification settings - Fork 439
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
Treat SplFileObject methods as impure/having side effects #3174
Treat SplFileObject methods as impure/having side effects #3174
Conversation
Do really all of these methods have side effects? I doubt methods like "valid" or "getBasename" have side effects. |
I haven't written any tests to demonstrate that these changes have the desired effect. Hopefully there will be a change of state of https://phpstan.org/r/f2ce4361-b9e5-4f51-a97e-d6828f618e0f from producing errors to producing none. If tests are required to be written, please point me to some existing tests for similar function signature updates (or some test docs), so I can take a look at doing that using the existing tests as a template for what's required. Thanks. |
Look at tests in this directory https://github.com/phpstan/phpstan-src/tree/1.11.x/tests/PHPStan/Analyser/nsrt and the docs: https://phpstan.org/developing-extensions/testing#type-inference |
What I'm actually trying to do is get certain methods as being treated as impure. Having side effects means something different, I think, so I may not be doing this the right way. I'm just copying what was done for The return value of As for properties of the file object being pointed to, like |
When So |
If I'm understanding correctly, you're saying that if a method marked as having side effects is called, all other methods on the object are treated as potentially providing a different return value? If that's the case, then maybe only the following methods need to be marked as having side effects:
|
I think the above can all potentially move the pointer to the current position in the file, and cause other methods to return different values. |
Watch me talking about this for about 10 minutes: https://youtu.be/AFjr3RlDOZQ?si=x5msNPD-mJPZz20T&t=1729 You can also read about it here: https://phpstan.org/blog/remembering-and-forgetting-returned-values |
Thank you. (I put the video on 1.5x speed, but then got hooked and watched much more than 10 mins anyway!) I believe that I understand |
eca25b2
to
72a762f
Compare
@ondrejmirtes I've written some tests that assert the expected behaviour of PHPStan and confirmed that they fail when running the pipeline. I've then introduced a change to mark the relevant methods has having side effects. The assertions in the new test should now be valid. One is still failing though. This relates to public function fgetss() : void
{
// fgetss has been removed as of PHP 8.0.0
if ( \version_compare(\PHP_VERSION,'8.0.0','<') )
{
$file = new \SplFileObject('php://memory', 'r');
assertType('bool', $file->eof());
if ( $file->eof() )
{
return;
}
assertType('false', $file->eof());
// call method that has side effects
$file->fgetss();
// the value of eof may have changed
assertType('bool', $file->eof());
}
} phpstan is not recognising the that code is not being executed for PHP >= 8.0.0 though, and so this particular test is failing for all PHP >= 8.0.0, but passing for PHP < 8.0.0. How should I handle this case? Is there anything I can do to instruct PHPStan that the code should be ignored for PHP >= 8.0.0? Thanks. |
@mind-bending-forks You can put this assertion in a separate file and mark it with |
07a1090
to
056035e
Compare
…ng calls to SplFileObject methods with side effects
… position in the file as impure/having side effects. This addresses phpstan/phpstan#11200 - functionMetadata_original has been updated to mark relevant SplFileObject methods as having side effects. - bin/generate-function-metadata.php has been run to recreate functionMetadata.php.
056035e
to
9ea27c4
Compare
@ondrejmirtes All tests are passing now. There appear to be some failing integration and extension checks, but I don't think they are anything to do with my changes. So, please consider this pull request finalised and ready for review/merging by you. Note of caution: The assertions in the tests I have written are based on how I anticipate the corresponding SplFileObject methods may behave and, in particular, the (types of) values the associated methods may output in certain circumstances. As I understand it though (and I may be wrong) the PHP test code I have written is not being executed - simply statically-analysed by PHP. To prove that the I'm hoping that you agree that what I have done looks sensible and is sufficient! |
Absolutely, this is more than sufficient to test this, thank you! |
This addresses phpstan/phpstan#11200
functionMetadata_original
has been updated to mark relevantSplFileObject
methods as having side effects.bin/generate-function-metadata.php
has been run to recreatefunctionMetadata.php
.