Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

PHPStan fixes #296

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Controller/AbstractActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function notFoundAction()
{
$event = $this->getEvent();
$routeMatch = $event->getRouteMatch();

if (null === $routeMatch) {
throw new Exception\RuntimeException('Event does not have a RouteMatch');
}

$routeMatch->setParam('action', 'not-found');

$helper = $this->plugin('createHttpNotFoundModel');
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class AbstractController implements
protected $response;

/**
* @var Event
* @var MvcEvent
*/
protected $event;

Expand Down
93 changes: 67 additions & 26 deletions src/Controller/AbstractRestfulController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
*/
namespace Zend\Mvc\Controller;

use Zend\Http\AbstractMessage;
use Zend\Http\Request as HttpRequest;
use Zend\Http\Response as HttpResponse;
use Zend\Json\Json;
use Zend\Mvc\Exception;
use Zend\Mvc\MvcEvent;
use Zend\Router\RouteMatch;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;

Expand Down Expand Up @@ -90,6 +93,20 @@ public function getIdentifierName()
return $this->identifierName;
}

/**
* @return HttpResponse
*/
private function getHttpResponse()
{
$response = $this->getResponse();

if (! $response instanceof HttpResponse) {
throw new Exception\RuntimeException('Response is not an instance of ' . HttpResponse::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the type of $response to this exception

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly an UnexpectedValueException btw - not a RuntimeException.

}

return $response;
}

/**
* Create a new resource
*
Expand All @@ -98,7 +115,7 @@ public function getIdentifierName()
*/
public function create($data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -113,7 +130,7 @@ public function create($data)
*/
public function delete($id)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -130,7 +147,7 @@ public function delete($id)
*/
public function deleteList($data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -145,7 +162,7 @@ public function deleteList($data)
*/
public function get($id)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -159,7 +176,7 @@ public function get($id)
*/
public function getList()
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -177,7 +194,7 @@ public function getList()
*/
public function head($id = null)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -197,7 +214,7 @@ public function head($id = null)
*/
public function options()
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -210,13 +227,13 @@ public function options()
* Not marked as abstract, as that would introduce a BC break
* (introduced in 2.1.0); instead, raises an exception if not implemented.
*
* @param $id
* @param $data
* @param mixed $id
* @param mixed $data
* @return array
*/
public function patch($id, $data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -234,7 +251,7 @@ public function patch($id, $data)
*/
public function replaceList($data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -252,7 +269,7 @@ public function replaceList($data)
*/
public function patchList($data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -268,7 +285,7 @@ public function patchList($data)
*/
public function update($id, $data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -282,7 +299,7 @@ public function update($id, $data)
*/
public function notFoundAction()
{
$this->response->setStatusCode(404);
$this->getHttpResponse()->setStatusCode(404);

return [
'content' => 'Page not found'
Expand Down Expand Up @@ -332,6 +349,20 @@ public function onDispatch(MvcEvent $e)

$request = $e->getRequest();

if (! $request instanceof HttpRequest) {
throw new Exception\RuntimeException(
'Request is not an instance of ' . HttpRequest::class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type of the found value is to be added to the exception

);
}

$response = $e->getResponse();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Group the two assignments together, then have the two check blocks (easier to read)


if (! $response instanceof HttpResponse) {
throw new Exception\RuntimeException(
'Request is not an instance of ' . HttpRequest::class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type of the found value is to be added to the exception

);
}

// Was an "action" requested?
$action = $routeMatch->getParam('action', false);
if ($action) {
Expand All @@ -352,7 +383,7 @@ public function onDispatch(MvcEvent $e)
case (isset($this->customHttpMethodsMap[$method])):
$callable = $this->customHttpMethodsMap[$method];
$action = $method;
$return = call_user_func($callable, $e);
$return = $callable($e);
break;
// DELETE
case 'delete':
Expand Down Expand Up @@ -388,15 +419,15 @@ public function onDispatch(MvcEvent $e)
}
$action = 'head';
$headResult = $this->head($id);
$response = ($headResult instanceof Response) ? clone $headResult : $e->getResponse();
$response->setContent('');
$return = $response;
$resultResponse = ($headResult instanceof Response) ? clone $headResult : $response;
$resultResponse->setContent('');
$return = $resultResponse;
break;
// OPTIONS
case 'options':
$action = 'options';
$this->options();
$return = $e->getResponse();
$return = $response;
break;
// PATCH
case 'patch':
Expand All @@ -416,7 +447,6 @@ public function onDispatch(MvcEvent $e)
$action = 'patchList';
$return = $this->patchList($data);
} catch (Exception\RuntimeException $ex) {
$response = $e->getResponse();
$response->setStatusCode(405);
return $response;
}
Expand All @@ -442,7 +472,6 @@ public function onDispatch(MvcEvent $e)
break;
// All others...
default:
$response = $e->getResponse();
$response->setStatusCode(405);
return $response;
}
Expand All @@ -462,6 +491,12 @@ public function onDispatch(MvcEvent $e)
*/
public function processPostData(Request $request)
{
if (! $request instanceof HttpRequest) {
throw new Exception\InvalidArgumentException(
'Request is not an instance of ' . HttpRequest::class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type of the found value is to be added to the exception

);
}

if ($this->requestHasContentType($request, self::CONTENT_TYPE_JSON)) {
return $this->create($this->jsonDecode($request->getContent()));
}
Expand All @@ -478,7 +513,13 @@ public function processPostData(Request $request)
*/
public function requestHasContentType(Request $request, $contentType = '')
{
/** @var $headerContentType \Zend\Http\Header\ContentType */
if (! $request instanceof HttpRequest) {
throw new Exception\InvalidArgumentException(
'Request is not an instance of ' . HttpRequest::class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type of the found value is to be added to the exception

);
}

/** @var \Zend\Http\Header\ContentType $headerContentType */
$headerContentType = $request->getHeaders()->get('content-type');
if (! $headerContentType) {
return false;
Expand Down Expand Up @@ -546,11 +587,11 @@ public function addHttpMethodHandler($method, /* Callable */ $handler)
* Attempts to see if an identifier was passed in either the URI or the
* query string, returning it if found. Otherwise, returns a boolean false.
*
* @param \Zend\Router\RouteMatch $routeMatch
* @param Request $request
* @param RouteMatch $routeMatch
* @param HttpRequest $request
* @return false|mixed
*/
protected function getIdentifier($routeMatch, $request)
protected function getIdentifier(RouteMatch $routeMatch, HttpRequest $request)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BC break that needs to be reversed

{
$identifier = $this->getIdentifierName();
$id = $routeMatch->getParam($identifier, false);
Expand Down Expand Up @@ -611,7 +652,7 @@ protected function processBodyContent($request)
*
* Marked protected to allow usage from extending classes.
*
* @param string
* @param string $string
* @return mixed
* @throws Exception\DomainException if no JSON decoding functionality is
* available.
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ControllerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ControllerManager extends AbstractPluginManager
* Injects an initializer for injecting controllers with an
* event manager and plugin manager.
*
* @param ConfigInterface|ContainerInterface $container
* @param ConfigInterface|ContainerInterface $configOrContainerInstance
* @param array $config
*/
public function __construct($configOrContainerInstance, array $config = [])
Expand Down
Loading