Utils: added crutch assumeNotFalse()

this can be used to get PHPStan to shut up about stuff that will never return false in normal contexts.
It's more fine-grained than @phpstan-ignore-line and less hassle than ignoreErrors (and works in PhpStorm too).
In addition, it's easy to search for references.
This commit is contained in:
Dylan K. Taylor 2021-12-08 18:58:39 +00:00
parent 45b4fa0e96
commit c6466a6da9
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -592,4 +592,17 @@ final class Utils{
throw new \InvalidArgumentException("Text must be valid UTF-8");
}
}
/**
* @phpstan-template TValue
* @phpstan-param TValue|false $value
* @phpstan-param string|\Closure() : string $context
* @phpstan-return TValue
*/
public static function assumeNotFalse(mixed $value, \Closure|string $context = "This should never be false") : mixed{
if($value === false){
throw new AssumptionFailedError("Assumption failure: " . (is_string($context) ? $context : $context()) . " (THIS IS A BUG)");
}
return $value;
}
}