扩展含模板的类时出现Psalm错误,寻求解决方案
解决Psalm MissingTemplateParam错误(继承ServiceDocumentRepository)
核心原因
ServiceDocumentRepository是带模板参数的泛型类,继承时必须明确指定它的模板参数(即仓库对应的实体类),否则Psalm会抛出缺失模板参数的错误。
正确的注解写法
直接给TripRepository添加@template-extends注解,指定父类的模板参数为你的实体类Trip:
<?php namespace App\Repository; use App\Document\Trip; use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepository; /** * @template-extends ServiceDocumentRepository<Trip> */ class TripRepository extends ServiceDocumentRepository { // 你的仓库逻辑代码 }
额外排查步骤
如果以上写法仍报错,按以下步骤排查:
- 检查
ServiceDocumentRepository的原生PHPDoc,确认它带有@template T of object的声明(Doctrine ODM Bundle v4.7版本的该类默认包含此注解) - 清除Psalm缓存:执行命令
psalm --clear-cache,避免缓存导致的注解识别问题 - 确保Psalm配置文件(
psalm.xml)中已正确引入Doctrine相关配置,比如启用doctrine插件:<plugins> <pluginClass class="Psalm\Plugin\Doctrine\Plugin" /> </plugins>
内容的提问来源于stack exchange,提问作者george007
相关产品推荐
相关产品推荐

