-
Notifications
You must be signed in to change notification settings - Fork 91
PHPStan fixes #296
base: master
Are you sure you want to change the base?
PHPStan fixes #296
Changes from 1 commit
3a1a397
1808073
00dd89c
4c52699
940576a
d234371
ce87b2e
f2ef81b
6022ee2
6d5733d
498163d
4ab30bf
bbefcb8
ba0fb92
511962e
7f20264
09a0ebe
404bad3
3845b73
5f40f54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Create a new resource | ||
* | ||
|
@@ -98,7 +115,7 @@ public function getIdentifierName() | |
*/ | ||
public function create($data) | ||
{ | ||
$this->response->setStatusCode(405); | ||
$this->getHttpResponse()->setStatusCode(405); | ||
|
||
return [ | ||
'content' => 'Method Not Allowed' | ||
|
@@ -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' | ||
|
@@ -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' | ||
|
@@ -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' | ||
|
@@ -159,7 +176,7 @@ public function get($id) | |
*/ | ||
public function getList() | ||
{ | ||
$this->response->setStatusCode(405); | ||
$this->getHttpResponse()->setStatusCode(405); | ||
|
||
return [ | ||
'content' => 'Method Not Allowed' | ||
|
@@ -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' | ||
|
@@ -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' | ||
|
@@ -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 | ||
thomasvargiu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param mixed $data | ||
* @return array | ||
*/ | ||
public function patch($id, $data) | ||
{ | ||
$this->response->setStatusCode(405); | ||
$this->getHttpResponse()->setStatusCode(405); | ||
|
||
return [ | ||
'content' => 'Method Not Allowed' | ||
|
@@ -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' | ||
|
@@ -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' | ||
|
@@ -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' | ||
|
@@ -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' | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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': | ||
|
@@ -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': | ||
|
@@ -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; | ||
} | ||
|
@@ -442,7 +472,6 @@ public function onDispatch(MvcEvent $e) | |
break; | ||
// All others... | ||
default: | ||
$response = $e->getResponse(); | ||
$response->setStatusCode(405); | ||
return $response; | ||
} | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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())); | ||
} | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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. | ||
|
There was a problem hiding this comment.
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 exceptionThere was a problem hiding this comment.
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 aRuntimeException
.