vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.55k stars 659 forks source link

Psalm doesn't seem to understand @phpstan-assert-if-true #8847

Open carlosas opened 1 year ago

carlosas commented 1 year ago

When using getClassReflection() from:

/**
 * @phpstan-assert-if-true !null $this->getClassReflection()
 */
public function isInClass(): bool;
public function getClassReflection(): ?ClassReflection;

https://github.com/phpstan/phpstan-src/blob/1.9.x/src/Reflection/ClassMemberAccessAnswerer.php#L14 and calling a method inside the returned type without checking null first, Psalm throws a PossiblyNullReference error because it doesn't take into account the @phpstan-assert-if-true annotation.

Tested with:

orklah commented 1 year ago

Note: IIRC, Psalm has a prerequisite that the class defining assertions on methods should be immutable (or at least the method itself). PHPStan does not support purity/immutability so this may not be a prerequisite on their part. However, if that's the case, Psalm should not silently fail but raise an error for wrong docblock or something