php - 正确使用 Psalm 中一组类的模板注解
问题描述
如何在 psalm 中使用模板注释。我有抽象类
abstract class eventNotification
{
/**
* @param eventAbstract|aggregateChanged $e
* @return eventNotification
*/
abstract static public function occurred(aggregateChanged|eventAbstract $e): eventNotification;
}
例如实现
final class e1Notify extends eventNotification
{
/**
* @param aggregateChanged|eventAbstract $e
* @return eventNotification
*/
static public function occurred(aggregateChanged|eventAbstract $e): eventNotification
{
return new self(
['v1'=>$e->v1(), 'v2'=>$e->v2()],
['id'=>$e->aggregateId()]
);
}
}
当然每个类都位于自己的文件中。e1Notify::occurred() 只能接收类型为 e1 的对象,该类型具有方法 v1() 和 v2()。并非所有的 aggregateChanged|eventAbstract 对象都具有相同的方法。所以 psalm 向我展示了关于方法 v1()、v2() 的错误 UndefinedMethod。
我需要解释一下 e1Notify 只接收类型为 e1 的对象。我认为我必须使用模板注释,但我不明白如何。
解决方案
推荐阅读
- java - 导入模块后清单合并失败
- python - 简洁的python方法来改变一个值,如果它不是Falsey
- java - 从 EventHandler 目标访问任意节点
- python - 将 REST API 中的特定部分保存到列表中
- jquery - 除非 URL 栏发出请求,否则页面拒绝刷新
- python - 将 Pandas Multiindexed DataFrame 与 Singleindexed Pandas DataFrame 合并
- javascript - UnhandledPromiseRejectionWarning: ReferenceError: name is not defined
- typescript - const counts: { [c: string]: boolean } = {} 是什么意思?
- amazon-web-services - AWS DynamoDb:按需索引的定价
- python - Odoo 测试未运行