Laravel中的返回类型提示。好主意还是坏主意?

I analyzed my Laravel project using phpstan (https://github.com/phpstan/phpstan). One of the most frequent complaints are concerning methods without return typehints, which I think are valid complaints. So I thought I'd start adding typehints to my controller methods (ie public function index(): View ). Most of them returns either Illuminate\View\View or Illuminate\Http\RedirectResponse.

问题:

  1. 这是个好主意吗?我认为这通常会使事情变得更清楚,但是Laravel文档中的任何地方都没有使用返回类型提示。也许这不是一个好习惯?
  2. 我的大问题是一些返回或的方法。即,例如,如果用户已登录,则他们返回View,否则,他们返回RedirectResponse。如何为这些方法返回typehints?
评论