From 6c58455aa6989d17c33d4e3afb82d33a01bfe965 Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 19 Jul 2023 13:15:08 +0530 Subject: [PATCH] Update for Cake 5. --- .github/workflows/ci.yml | 6 +++--- .gitignore | 4 ++++ composer.json | 12 +++++++----- phpunit.xml.dist | 3 +-- psalm.xml | 3 +++ src/Action/ForgotPasswordAction.php | 13 ++++++++----- src/Action/LoginAction.php | 14 +++++++++----- src/Action/LogoutAction.php | 5 +++-- src/Action/RegisterAction.php | 12 +++++++----- src/Action/ResetPasswordAction.php | 21 ++++++++++++--------- src/Action/VerifyAction.php | 16 +++++++++------- src/Traits/VerifyTrait.php | 11 +++++++---- 12 files changed, 73 insertions(+), 47 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bcd0126..5a8b835 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,13 +13,13 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '7.4' + php-version: '8.1' extensions: mbstring, intl coverage: none - tools: phpstan:1, vimeo/psalm:4 + tools: phpstan:1.10, vimeo/psalm:5 - name: Composer Install - run: composer require --dev cakephp/cakephp-codesniffer:^4.0 + run: composer require --dev cakephp/cakephp-codesniffer:^5.0 - name: Run PHP CodeSniffer run: vendor/bin/phpcs --standard=vendor/cakephp/cakephp-codesniffer/CakePHP -p src/ tests/ diff --git a/.gitignore b/.gitignore index e96516b..cf1045a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ composer.lock vendor .phpunit.result.cache +.phpunit.cache +phpstan.neon +phpunit.xml.dist +psalm.xml diff --git a/composer.json b/composer.json index 62876a4..efa0dba 100644 --- a/composer.json +++ b/composer.json @@ -22,12 +22,12 @@ } ], "require":{ - "cakephp/cakephp":"^4.0", - "friendsofcake/crud":"^6.0" + "cakephp/cakephp":"5.x-dev", + "friendsofcake/crud":"dev-cake-5" }, "require-dev":{ - "phpunit/phpunit":"^8.5 || ^9.3", - "friendsofcake/cakephp-test-utilities":"^2.0" + "phpunit/phpunit":"^10.1", + "friendsofcake/cakephp-test-utilities":"dev-cake-5.x" }, "autoload":{ "psr-4":{ @@ -44,5 +44,7 @@ "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } - } + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 76ddaee..fc39c42 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,9 +2,8 @@ - - + ./tests/ diff --git a/psalm.xml b/psalm.xml index 09cd5b1..ec91e46 100644 --- a/psalm.xml +++ b/psalm.xml @@ -5,6 +5,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" + findUnusedBaselineEntry="true" + findUnusedCode="false" + usePhpDocMethodsWithoutMagicCall="true" > diff --git a/src/Action/ForgotPasswordAction.php b/src/Action/ForgotPasswordAction.php index 8188997..b69d6a4 100644 --- a/src/Action/ForgotPasswordAction.php +++ b/src/Action/ForgotPasswordAction.php @@ -11,6 +11,9 @@ use Crud\Traits\ViewTrait; use Crud\Traits\ViewVarTrait; +/** + * @method \Cake\ORM\Table _model() + */ class ForgotPasswordAction extends BaseAction { use FindMethodTrait; @@ -18,7 +21,7 @@ class ForgotPasswordAction extends BaseAction use ViewTrait; use ViewVarTrait; - protected $_defaultConfig = [ + protected array $_defaultConfig = [ 'enabled' => true, 'scope' => 'entity', 'findMethod' => 'all', @@ -57,9 +60,9 @@ protected function _get(): void /** * HTTP POST handler * - * @return \Cake\Http\Response|null|void + * @return \Cake\Http\Response|null */ - protected function _post() + protected function _post(): ?Response { $subject = $this->_subject([ 'findMethod' => $this->_getFindConfig(), @@ -67,14 +70,14 @@ protected function _post() $this->_trigger('beforeForgotPassword', $subject); - $entity = $this->_table() + $entity = $this->_model() ->find($subject->findMethod[0], $subject->findMethod[1]) ->first(); if (empty($entity)) { $this->_error($subject); - return; + return null; } $subject->set(['entity' => $entity]); diff --git a/src/Action/LoginAction.php b/src/Action/LoginAction.php index 81b3543..6cedebf 100644 --- a/src/Action/LoginAction.php +++ b/src/Action/LoginAction.php @@ -12,7 +12,7 @@ class LoginAction extends BaseAction { use RedirectTrait; - protected $_defaultConfig = [ + protected array $_defaultConfig = [ 'enabled' => true, 'messages' => [ 'success' => [ @@ -28,9 +28,9 @@ class LoginAction extends BaseAction /** * HTTP GET handler * - * @return \Cake\Http\Response|null|void + * @return \Cake\Http\Response|null */ - protected function _get() + protected function _get(): ?Response { $result = $this->_controller()->Authentication->getResult(); $subject = $this->_subject([ @@ -43,14 +43,16 @@ protected function _get() } $this->_trigger('beforeRender', $subject); + + return null; } /** * HTTP POST handler * - * @return \Cake\Http\Response|null|void + * @return \Cake\Http\Response|null */ - protected function _post() + protected function _post(): ?Response { $result = $this->_controller()->Authentication->getResult(); $subject = $this->_subject([ @@ -62,6 +64,8 @@ protected function _post() } $this->_error($subject); + + return null; } /** diff --git a/src/Action/LogoutAction.php b/src/Action/LogoutAction.php index 469e6f0..07374e1 100644 --- a/src/Action/LogoutAction.php +++ b/src/Action/LogoutAction.php @@ -3,6 +3,7 @@ namespace CrudUsers\Action; +use Cake\Http\Response; use Crud\Action\BaseAction; use Crud\Traits\RedirectTrait; @@ -10,7 +11,7 @@ class LogoutAction extends BaseAction { use RedirectTrait; - protected $_defaultConfig = [ + protected array $_defaultConfig = [ 'enabled' => true, 'messages' => [ 'success' => [ @@ -25,7 +26,7 @@ class LogoutAction extends BaseAction * * @return \Cake\Http\Response|null */ - protected function _get() + protected function _get(): ?Response { $subject = $this->_subject(); $this->_trigger('beforeLogout', $subject); diff --git a/src/Action/RegisterAction.php b/src/Action/RegisterAction.php index 23991b9..b444351 100644 --- a/src/Action/RegisterAction.php +++ b/src/Action/RegisterAction.php @@ -19,7 +19,7 @@ class RegisterAction extends BaseAction use ViewTrait; use ViewVarTrait; - protected $_defaultConfig = [ + protected array $_defaultConfig = [ 'enabled' => true, 'scope' => 'entity', 'inflection' => 'singular', @@ -60,7 +60,7 @@ class RegisterAction extends BaseAction * * @return void */ - protected function _get() + protected function _get(): void { $subject = $this->_subject([ 'success' => true, @@ -76,9 +76,9 @@ protected function _get() /** * HTTP POST handler * - * @return \Cake\Http\Response|null|void + * @return \Cake\Http\Response|null */ - protected function _post() + protected function _post(): ?Response { $subject = $this->_subject([ 'entity' => $this->_entity($this->_request()->getData(), $this->saveOptions()), @@ -89,12 +89,14 @@ protected function _post() $this->_trigger('beforeRegister', $subject); /** @var callable $callback */ - $callback = [$this->_table(), $subject->saveMethod]; + $callback = [$this->_model(), $subject->saveMethod]; if ($callback($subject->entity, $subject->saveOptions)) { return $this->_success($subject); } $this->_error($subject); + + return null; } /** diff --git a/src/Action/ResetPasswordAction.php b/src/Action/ResetPasswordAction.php index 5deb13c..9712bf4 100644 --- a/src/Action/ResetPasswordAction.php +++ b/src/Action/ResetPasswordAction.php @@ -3,6 +3,7 @@ namespace CrudUsers\Action; +use Cake\Datasource\EntityInterface; use Cake\Http\Exception\BadRequestException; use Cake\Http\Exception\NotFoundException; use Cake\Http\Response; @@ -25,7 +26,7 @@ class ResetPasswordAction extends BaseAction use ViewTrait; use ViewVarTrait; - protected $_defaultConfig = [ + protected array $_defaultConfig = [ 'enabled' => true, 'scope' => 'entity', 'findMethod' => 'all', @@ -79,7 +80,7 @@ protected function _get(?string $token = null): void $subject = $this->_subject([ 'success' => true, - 'entity' => $this->_table()->newEmptyEntity(), + 'entity' => $this->_model()->newEmptyEntity(), 'token' => $token, ]); @@ -92,9 +93,9 @@ protected function _get(?string $token = null): void * Thin proxy for _put * * @param string|null $token Token - * @return \Cake\Http\Response|null|void + * @return \Cake\Http\Response|null */ - protected function _post($token = null) + protected function _post(?string $token = null): ?Response { return $this->_put($token); } @@ -103,9 +104,9 @@ protected function _post($token = null) * HTTP PUT handler * * @param string|null $token Token - * @return \Cake\Http\Response|null|void + * @return \Cake\Http\Response|null */ - protected function _put($token = null) + protected function _put(?string $token = null): ?Response { $entity = $this->_verify($this->_token($token)); @@ -115,6 +116,8 @@ protected function _put($token = null) } $this->_error($subject); + + return null; } /** @@ -123,9 +126,9 @@ protected function _put($token = null) * @param \Crud\Event\Subject $subject Event subject * @return \Cake\Datasource\EntityInterface|false */ - protected function _save(Subject $subject) + protected function _save(Subject $subject): EntityInterface|false { - $entity = $this->_table()->patchEntity( + $entity = $this->_model()->patchEntity( $subject->entity, $this->_request()->getData(), $this->saveOptions() @@ -135,7 +138,7 @@ protected function _save(Subject $subject) $this->_trigger('beforeSave', $subject); /** @var callable $callable */ - $callable = [$this->_table(), $this->saveMethod()]; + $callable = [$this->_model(), $this->saveMethod()]; /** @var \Cake\Datasource\EntityInterface|false $success */ $success = $callable($entity, $this->saveOptions()); diff --git a/src/Action/VerifyAction.php b/src/Action/VerifyAction.php index da1d7e9..93c2363 100644 --- a/src/Action/VerifyAction.php +++ b/src/Action/VerifyAction.php @@ -25,7 +25,7 @@ class VerifyAction extends BaseAction use ViewTrait; use ViewVarTrait; - protected $_defaultConfig = [ + protected array $_defaultConfig = [ 'enabled' => true, 'scope' => 'entity', 'findMethod' => 'all', @@ -70,10 +70,10 @@ class VerifyAction extends BaseAction /** * HTTP GET handler * - * @param string $token Token - * @return \Cake\Http\Response|null|void + * @param string|null $token Token + * @return \Cake\Http\Response|null */ - protected function _get($token = null) + protected function _get(?string $token = null): ?Response { $token = $this->_token($token); $entity = $this->_verify($token); @@ -83,6 +83,8 @@ protected function _get($token = null) } $this->_error(); + + return null; } /** @@ -91,9 +93,9 @@ protected function _get($token = null) * @param \Cake\Datasource\EntityInterface $entity Entity * @return \Cake\Datasource\EntityInterface|false */ - protected function _save(EntityInterface $entity) + protected function _save(EntityInterface $entity): EntityInterface|false { - $entity = $this->_table()->patchEntity( + $entity = $this->_model()->patchEntity( $entity, ['verified' => true], $this->saveOptions() @@ -103,7 +105,7 @@ protected function _save(EntityInterface $entity) $this->_trigger('beforeSave', $subject); /** @var callable $callback */ - $callback = [$this->_table(), $this->saveMethod()]; + $callback = [$this->_model(), $this->saveMethod()]; /** @var \Cake\Datasource\EntityInterface|false $success */ $success = $callback($entity, $this->saveOptions()); diff --git a/src/Traits/VerifyTrait.php b/src/Traits/VerifyTrait.php index c91e48c..01d9956 100644 --- a/src/Traits/VerifyTrait.php +++ b/src/Traits/VerifyTrait.php @@ -5,6 +5,9 @@ use Cake\Datasource\EntityInterface; +/** + * @method \Cake\ORM\Table _model() + */ trait VerifyTrait { /** @@ -51,12 +54,12 @@ protected function _verify(?string $token): EntityInterface $subject = $this->_subject(); $subject->set([ 'token' => $token, - 'repository' => $this->_table(), - 'query' => $this->_table()->find( + 'repository' => $this->_model(), + 'query' => $this->_model()->find( $this->findMethod(), [ 'token' => $token, - 'conditions' => [$this->_table()->aliasField($this->getConfig('tokenField')) => $token], + 'conditions' => [$this->_model()->aliasField($this->getConfig('tokenField')) => $token], ] ), ]); @@ -90,7 +93,7 @@ protected function _verify(?string $token): EntityInterface * @return void * @throws \Exception */ - protected function _tokenError($error = 'tokenNotFound'): void + protected function _tokenError(string $error = 'tokenNotFound'): void { $subject = $this->_subject(['success' => false]); $this->_trigger($error, $subject);