From 0f7d47b0f3304e884a08df25afaf17600646fc34 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 26 Nov 2022 08:31:37 +0900 Subject: [PATCH 1/2] fix: $requset type in BaseController and BaseResource To suppress PHPStan error like Call to an undefined method CodeIgniter\HTTP\RequestInterface::getPost() --- app/Controllers/BaseController.php | 6 +++++- system/RESTful/BaseResource.php | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php index 4063ad1fd05a..729456abebf4 100644 --- a/app/Controllers/BaseController.php +++ b/app/Controllers/BaseController.php @@ -3,6 +3,8 @@ namespace App\Controllers; use CodeIgniter\Controller; +use CodeIgniter\HTTP\CLIRequest; +use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface; @@ -22,7 +24,7 @@ abstract class BaseController extends Controller /** * Instance of the main Request object. * - * @var RequestInterface + * @var CLIRequest|IncomingRequest */ protected $request; @@ -40,6 +42,8 @@ abstract class BaseController extends Controller */ public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { + assert($request instanceof IncomingRequest || $request instanceof CLIRequest); + // Do Not Edit This Line parent::initController($request, $response, $logger); diff --git a/system/RESTful/BaseResource.php b/system/RESTful/BaseResource.php index 65ad21251a3d..1d7d3bf0d4ab 100644 --- a/system/RESTful/BaseResource.php +++ b/system/RESTful/BaseResource.php @@ -12,12 +12,21 @@ namespace CodeIgniter\RESTful; use CodeIgniter\Controller; +use CodeIgniter\HTTP\CLIRequest; +use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface; abstract class BaseResource extends Controller { + /** + * Instance of the main Request object. + * + * @var CLIRequest|IncomingRequest + */ + protected $request; + /** * @var string|null The model that holding this resource's data */ @@ -33,7 +42,10 @@ abstract class BaseResource extends Controller */ public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { + assert($request instanceof IncomingRequest || $request instanceof CLIRequest); + parent::initController($request, $response, $logger); + $this->setModel($this->modelName); } From 738ec56fed7dd4f6375524423ab4458dfd6fbcf1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 7 Dec 2022 08:26:47 +0900 Subject: [PATCH 2/2] refactor: remove assert() It seems assert() is not needed to suppress PHPStan errors. --- app/Controllers/BaseController.php | 2 -- system/RESTful/BaseResource.php | 2 -- 2 files changed, 4 deletions(-) diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php index 729456abebf4..122db5f9a9f2 100644 --- a/app/Controllers/BaseController.php +++ b/app/Controllers/BaseController.php @@ -42,8 +42,6 @@ abstract class BaseController extends Controller */ public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { - assert($request instanceof IncomingRequest || $request instanceof CLIRequest); - // Do Not Edit This Line parent::initController($request, $response, $logger); diff --git a/system/RESTful/BaseResource.php b/system/RESTful/BaseResource.php index 1d7d3bf0d4ab..108c63ce9be6 100644 --- a/system/RESTful/BaseResource.php +++ b/system/RESTful/BaseResource.php @@ -42,8 +42,6 @@ abstract class BaseResource extends Controller */ public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { - assert($request instanceof IncomingRequest || $request instanceof CLIRequest); - parent::initController($request, $response, $logger); $this->setModel($this->modelName);