Description
I've been chatting with @veewee on Discord about adding a template to ResultInterface
to return what kind of error it could contain.
I'm not sure if this could be considered a breaking change, as it only affects static analysis and can be easily ignored, baselined, or fixed in a moment by going to each error and changing every @return ResultInterface<Foo>
to @return ResultInterface<Foo, Throwable>
if you don't feel like studying the specific throwables you want to return from there.
Another option apart from waiting for a new major version or just letting static analysis break is to wait for some default generic type syntax, see:
- (RFC) Implement template default syntax phpstan/phpdoc-parser#148
- Add support for template default types phpstan/phpstan#4801
- Implement template default types phpstan/phpstan-src#1835
This could allow us to have a syntax that would behave the same way but without having any new errors:
/**
* @template T
* @template E of Throwable = Throwable
*/
interface ResultInterface {}
If we are OK with baselining errors (regardless of whether the change is in a minor or major version), the following syntax would infer the template with Throwable
as the default value:
/**
* @template T
* @template E of Throwable
*/
interface ResultInterface {}
Psalm would only throw a MissingTemplateParalm
. At the same time, PHPStan would report two errors. Still, the return would be implicitly inferred from ResultInterface<int> to ResultInterface<Throwable>
in the not_documented()
case on both major static analysis tools.
Apart from this, there is another feature that would be nice to have in the static analysis, and that is to be able to infer the type of @throws
with a generic, so we could type this:
/**
* @template T
* @template E of Throwable = Throwable
*/
interface ResultInterface {
/**
* @return T
*
* @throws E
*/
public function getResult();
}