From ad556751ae011f30e6bf80ec48f841995b74ada7 Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 18 Apr 2025 18:25:32 +0530 Subject: [PATCH 1/2] Stan cleanup --- .phive/phars.xml | 3 +-- src/AuthenticationService.php | 6 ++---- src/Authenticator/ImpersonationInterface.php | 4 ++-- src/Authenticator/PersistenceInterface.php | 6 ++---- src/Controller/Component/AuthenticationComponent.php | 8 ++++---- src/Middleware/AuthenticationMiddleware.php | 6 +----- src/View/Helper/IdentityHelper.php | 10 ---------- 7 files changed, 12 insertions(+), 31 deletions(-) diff --git a/.phive/phars.xml b/.phive/phars.xml index a655ee46..4acd75dc 100644 --- a/.phive/phars.xml +++ b/.phive/phars.xml @@ -1,5 +1,4 @@ - - + diff --git a/src/AuthenticationService.php b/src/AuthenticationService.php index 8560e783..da88ee40 100644 --- a/src/AuthenticationService.php +++ b/src/AuthenticationService.php @@ -212,14 +212,13 @@ public function authenticate(ServerRequestInterface $request): ResultInterface * @param \Psr\Http\Message\ServerRequestInterface $request The request. * @param \Psr\Http\Message\ResponseInterface $response The response. * @return array Return an array containing the request and response objects. - * @psalm-return array{request: \Psr\Http\Message\ServerRequestInterface, response: \Psr\Http\Message\ResponseInterface} + * @return array{request: \Psr\Http\Message\ServerRequestInterface, response: \Psr\Http\Message\ResponseInterface} */ public function clearIdentity(ServerRequestInterface $request, ResponseInterface $response): array { foreach ($this->authenticators() as $authenticator) { if ($authenticator instanceof PersistenceInterface) { if ($authenticator instanceof ImpersonationInterface && $authenticator->isImpersonating($request)) { - /** @psalm-var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $stopImpersonationResult */ $stopImpersonationResult = $authenticator->stopImpersonating($request, $response); ['request' => $request, 'response' => $response] = $stopImpersonationResult; } @@ -241,8 +240,7 @@ public function clearIdentity(ServerRequestInterface $request, ResponseInterface * @param \Psr\Http\Message\ServerRequestInterface $request The request. * @param \Psr\Http\Message\ResponseInterface $response The response. * @param \ArrayAccess|array $identity Identity data. - * @return array - * @psalm-return array{request: \Psr\Http\Message\ServerRequestInterface, response: \Psr\Http\Message\ResponseInterface} + * @return array{request: \Psr\Http\Message\ServerRequestInterface, response: \Psr\Http\Message\ResponseInterface} */ public function persistIdentity( ServerRequestInterface $request, diff --git a/src/Authenticator/ImpersonationInterface.php b/src/Authenticator/ImpersonationInterface.php index 1a566384..fc8201d5 100644 --- a/src/Authenticator/ImpersonationInterface.php +++ b/src/Authenticator/ImpersonationInterface.php @@ -29,7 +29,7 @@ interface ImpersonationInterface * @param \Psr\Http\Message\ResponseInterface $response The response * @param \ArrayAccess $impersonator User who impersonates * @param \ArrayAccess $impersonated User impersonated - * @return array + * @return array{request: \Psr\Http\Message\ServerRequestInterface, response: \Psr\Http\Message\ResponseInterface} */ public function impersonate( ServerRequestInterface $request, @@ -43,7 +43,7 @@ public function impersonate( * * @param \Psr\Http\Message\ServerRequestInterface $request The request * @param \Psr\Http\Message\ResponseInterface $response The response - * @return array + * @return array{request: \Psr\Http\Message\ServerRequestInterface, response: \Psr\Http\Message\ResponseInterface} */ public function stopImpersonating(ServerRequestInterface $request, ResponseInterface $response): array; diff --git a/src/Authenticator/PersistenceInterface.php b/src/Authenticator/PersistenceInterface.php index 3b9e7ee7..5a925951 100644 --- a/src/Authenticator/PersistenceInterface.php +++ b/src/Authenticator/PersistenceInterface.php @@ -28,8 +28,7 @@ interface PersistenceInterface * @param \Psr\Http\Message\ServerRequestInterface $request The request object. * @param \Psr\Http\Message\ResponseInterface $response The response object. * @param \ArrayAccess|array $identity Identity data to persist. - * @return array - * @psalm-return array{request: \Psr\Http\Message\ServerRequestInterface, response: \Psr\Http\Message\ResponseInterface} Returns an array containing the request and response object + * @return array{request: \Psr\Http\Message\ServerRequestInterface, response: \Psr\Http\Message\ResponseInterface} Returns an array containing the request and response object */ public function persistIdentity( ServerRequestInterface $request, @@ -42,8 +41,7 @@ public function persistIdentity( * * @param \Psr\Http\Message\ServerRequestInterface $request The request object. * @param \Psr\Http\Message\ResponseInterface $response The response object. - * @return array - * @psalm-return array{request: \Psr\Http\Message\ServerRequestInterface, response: \Psr\Http\Message\ResponseInterface} Returns an array containing the request and response object + * @return array{request: \Psr\Http\Message\ServerRequestInterface, response: \Psr\Http\Message\ResponseInterface} Returns an array containing the request and response object */ public function clearIdentity(ServerRequestInterface $request, ResponseInterface $response): array; } diff --git a/src/Controller/Component/AuthenticationComponent.php b/src/Controller/Component/AuthenticationComponent.php index d9ab5114..d1a0b8b6 100644 --- a/src/Controller/Component/AuthenticationComponent.php +++ b/src/Controller/Component/AuthenticationComponent.php @@ -299,7 +299,7 @@ public function setIdentity(ArrayAccess $identity) $service->clearIdentity($controller->getRequest(), $controller->getResponse()); - /** @psalm-var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $result */ + /** @var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $result */ $result = $service->persistIdentity( $controller->getRequest(), $controller->getResponse(), @@ -322,7 +322,7 @@ public function setIdentity(ArrayAccess $identity) public function logout(): ?string { $controller = $this->getController(); - /** @psalm-var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $result */ + /** @var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $result */ $result = $this->getAuthenticationService()->clearIdentity( $controller->getRequest(), $controller->getResponse(), @@ -391,7 +391,7 @@ public function impersonate(ArrayAccess $impersonated) $impersonator = new ArrayObject($impersonator); } $controller = $this->getController(); - /** @psalm-var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $result */ + /** @var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $result */ $result = $service->impersonate( $controller->getRequest(), $controller->getResponse(), @@ -421,7 +421,7 @@ public function stopImpersonating() $controller = $this->getController(); - /** @psalm-var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $result */ + /** @var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $result */ $result = $service->stopImpersonating( $controller->getRequest(), $controller->getResponse(), diff --git a/src/Middleware/AuthenticationMiddleware.php b/src/Middleware/AuthenticationMiddleware.php index e4ae26b9..945af355 100644 --- a/src/Middleware/AuthenticationMiddleware.php +++ b/src/Middleware/AuthenticationMiddleware.php @@ -106,11 +106,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $response = $handler->handle($request); $authenticator = $service->getAuthenticationProvider(); - if ($authenticator !== null && !$authenticator instanceof StatelessInterface) { - /** - * @psalm-suppress PossiblyNullArgument - * @phpstan-ignore-next-line - */ + if ($authenticator !== null && !$authenticator instanceof StatelessInterface && $result->getData()) { $return = $service->persistIdentity($request, $response, $result->getData()); $response = $return['response']; } diff --git a/src/View/Helper/IdentityHelper.php b/src/View/Helper/IdentityHelper.php index 42a8b24a..7a189f2a 100644 --- a/src/View/Helper/IdentityHelper.php +++ b/src/View/Helper/IdentityHelper.php @@ -56,16 +56,6 @@ class IdentityHelper extends Helper public function initialize(array $config): void { $this->_identity = $this->_View->getRequest()->getAttribute($this->getConfig('identityAttribute')); - - if (empty($this->_identity)) { - return; - } - - if (!$this->_identity instanceof IdentityInterface) { - throw new RuntimeException( - sprintf('Identity found in request does not implement %s', IdentityInterface::class), - ); - } } /** From 40f7d9f9d3bf1fe5c6b711e6ed221a68edf9830d Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 18 Apr 2025 19:31:10 +0530 Subject: [PATCH 2/2] Fix CS error --- src/View/Helper/IdentityHelper.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/View/Helper/IdentityHelper.php b/src/View/Helper/IdentityHelper.php index 7a189f2a..24a94091 100644 --- a/src/View/Helper/IdentityHelper.php +++ b/src/View/Helper/IdentityHelper.php @@ -18,7 +18,6 @@ use Authentication\IdentityInterface; use Cake\Utility\Hash; use Cake\View\Helper; -use RuntimeException; /** * Identity Helper