phpstan-src icon indicating copy to clipboard operation
phpstan-src copied to clipboard

Introduce all-methods-are-(im)pure

Open VincentLanglet opened this issue 2 months ago • 6 comments

Rework of https://github.com/phpstan/phpstan-src/pull/4409 and https://github.com/phpstan/phpstan-src/pull/4415

I added a non regression of https://github.com/phpstan/phpstan/issues/10215 as a proof it works.

I don't like the naming of my methods, suggestion are opened. Also I'm not fully sure getDefaultMethodPurity shouldn't be called somewhere else too...

VincentLanglet avatar Oct 08 '25 19:10 VincentLanglet

I dunno which strategy you will prefer @ondrejmirtes between

  • doing calls like isPure() ?? getDefaultMethodPurity() like https://github.com/phpstan/phpstan-src/pull/4422/commits/846d7bd2d2bc0e5db775f981e958a5c2873aa11a#diff-fb5399c56567ad1b8be9256fe9008a03a43b7686e02156b9f1b3118e76d95e2fR895

  • or using the fallback directly in the isPure method like https://github.com/phpstan/phpstan-src/pull/4422/files#diff-260042b10e8fde4d3e77fce0c1fc6fcf0851c7e442271dbefb5eded974d607e6R837-R843

The second one seems simpler to me because isPure was used multiple times like in NodeScopeResolver::getPhpDocs and it would have require more changes.

VincentLanglet avatar Oct 08 '25 20:10 VincentLanglet

This pull request has been marked as ready for review.

phpstan-bot avatar Oct 09 '25 18:10 phpstan-bot

I think it's ready for a new review :)

VincentLanglet avatar Oct 09 '25 18:10 VincentLanglet

Lot of test added and void behavior :)

VincentLanglet avatar Oct 10 '25 15:10 VincentLanglet

@staabm Do you see a case where

$scope->getFunctionType($node->getReturnType(), false, false)->isVoid()->maybe()

is possible or does the mutation testing reports a useless mutant ?

VincentLanglet avatar Dec 05 '25 11:12 VincentLanglet

I did not look into all details of this PR, but maybe you can do something like


class X {
  public function returnsVoid(): void{}
  public function returnsInt(): int{}
 
  public function doFoo() {
    $method = rand(0,1) ? 'returnsVoid' : 'returnsInt';
    $something = $this->$method();
  } 
}

staabm avatar Dec 05 '25 12:12 staabm