zend-mvc
zend-mvc copied to clipboard
Updated AbstractActionController::indexAction return type to reflect real usage
- [x] Is this related to quality assurance?
When running static analysis against a simple userland controller like:
class MyController extends AbstractActionController
{
public function indexAction()
{
return ['posts' => ['foo', 'bar']];
}
}
That do NOT provide the return type, neither with PHP 7.1 return type nor via DocBlock, tools like PHPstan use inherited docs and report:
Method MyController::indexAction() should return Zend\View\Model\ViewModel but returns array.
Of course users should add return types, but it would result in requiring a massive code update without real benefits, since the default behavior of Zend\Mvc framework is to allow:
-
null
from\Zend\Mvc\View\Http\CreateViewModelListener::createViewModelFromNull
listener -
array
from\Zend\Mvc\View\Http\CreateViewModelListener::createViewModelFromArray
listener -
ViewModel
from\Zend\Mvc\View\Http\DefaultRenderingStrategy::render
listener -
ResponseInterface
from\Zend\Mvc\SendResponseListener::sendResponse
listener
This change only applies to indexAction
since I guess its usage is widespread: notFoundAction
is untouched as users that override it are, IMHO, already advanced ones.
Ping @Ocramius you type lover :heart:
Reference: https://github.com/Slamdunk/phpstan-zend-framework/issues/5
As it seems you've worked on ZF with PHPStan quite a lot, what do you think about this @thomasvargiu?
@Slamdunk I have the same problem in my projects and right now I’m ignoring that kind of errors. But yes, even if I don’t like methods that can return 5 or 6 different types, we should fix the phodoc return type.
But probably it's better to use only ResponseInterface
and ModelInterface
, starting deprecating array
, null
, and void
.
This repository has been closed and moved to laminas/laminas-mvc; a new issue has been opened at https://github.com/laminas/laminas-mvc/issues/5.
This repository has been moved to laminas/laminas-mvc. If you feel that this patch is still relevant, please re-open against that repository, and reference this issue. To re-open, we suggest the following workflow:
- Squash all commits in your branch (
git rebase -i origin/{branch}
) - Make a note of all changed files (`git diff --name-only origin/{branch}...HEAD
- Run the laminas/laminas-migration tool on the code.
- Clone laminas/laminas-mvc to another directory.
- Copy the files from the second bullet point to the clone of laminas/laminas-mvc.
- In your clone of laminas/laminas-mvc, commit the files, push to your fork, and open the new PR. We will be providing tooling via laminas/laminas-migration soon to help automate the process.