zend-mvc icon indicating copy to clipboard operation
zend-mvc copied to clipboard

Updated AbstractActionController::indexAction return type to reflect real usage

Open Slamdunk opened this issue 5 years ago • 5 comments

  • [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:

  1. null from \Zend\Mvc\View\Http\CreateViewModelListener::createViewModelFromNull listener
  2. array from \Zend\Mvc\View\Http\CreateViewModelListener::createViewModelFromArray listener
  3. ViewModel from \Zend\Mvc\View\Http\DefaultRenderingStrategy::render listener
  4. 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

Slamdunk avatar Jun 07 '19 09:06 Slamdunk

As it seems you've worked on ZF with PHPStan quite a lot, what do you think about this @thomasvargiu?

Slamdunk avatar Jun 10 '19 12:06 Slamdunk

@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.

thomasvargiu avatar Jun 11 '19 08:06 thomasvargiu

But probably it's better to use only ResponseInterface and ModelInterface, starting deprecating array, null, and void.

thomasvargiu avatar Jun 11 '19 08:06 thomasvargiu

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.

weierophinney avatar Dec 31 '19 21:12 weierophinney

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.

weierophinney avatar Dec 31 '19 21:12 weierophinney