From c9bd934fd8595186aa64c6c67fcbeb89bace5d3e Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 5 Apr 2024 10:18:04 +0900 Subject: [PATCH 1/4] chore: update rector.php --- rector.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rector.php b/rector.php index 634665e51..5a5fc78ef 100644 --- a/rector.php +++ b/rector.php @@ -11,22 +11,28 @@ * the LICENSE file that was distributed with this source code. */ +use Rector\Caching\ValueObject\Storage\FileCacheStorage; use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector; use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector; +use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector; use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector; use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector; use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector; use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector; use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector; +use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector; use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector; use Rector\CodeQuality\Rector\If_\CombineIfRector; +use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector; use Rector\CodeQuality\Rector\If_\ShortenElseIfRector; use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector; use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector; +use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector; use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector; use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector; use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector; use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector; +use Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector; use Rector\Config\RectorConfig; use Rector\DeadCode\Rector\Cast\RecastingRemovalRector; use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector; @@ -38,12 +44,16 @@ use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector; use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector; use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector; +use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector; use Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector; use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector; use Rector\Set\ValueObject\LevelSetList; use Rector\Set\ValueObject\SetList; +use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector; +use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector; +use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector; use Rector\ValueObject\PhpVersion; return static function (RectorConfig $rectorConfig): void { @@ -56,6 +66,12 @@ $rectorConfig->parallel(); + // Github action cache + $rectorConfig->cacheClass(FileCacheStorage::class); + if (is_dir('/tmp')) { + $rectorConfig->cacheDirectory('/tmp/rector'); + } + // The paths to refactor (can also be supplied with CLI arguments) $rectorConfig->paths([ __DIR__ . '/src/', @@ -89,6 +105,7 @@ __DIR__ . '/src/Views', JsonThrowOnErrorRector::class, + StringifyStrNeedlesRector::class, YieldDataProviderRector::class, // Note: requires php 8 @@ -140,7 +157,15 @@ $rectorConfig->rule(FuncGetArgsToVariadicParamRector::class); $rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class); $rectorConfig->rule(SimplifyEmptyArrayCheckRector::class); + $rectorConfig->rule(SimplifyEmptyCheckOnEmptyArrayRector::class); + $rectorConfig->rule(TernaryEmptyArrayArrayDimFetchToCoalesceRector::class); + $rectorConfig->rule(EmptyOnNullableObjectToInstanceOfRector::class); + $rectorConfig->rule(DisallowedEmptyRuleFixerRector::class); $rectorConfig->rule(StringClassNameToClassConstantRector::class); $rectorConfig->rule(PrivatizeFinalClassPropertyRector::class); $rectorConfig->rule(CompleteDynamicPropertiesRector::class); + $rectorConfig->rule(BooleanInIfConditionRuleFixerRector::class); + $rectorConfig->rule(SingleInArrayToCompareRector::class); + $rectorConfig->rule(VersionCompareFuncCallToConstantRector::class); + $rectorConfig->rule(ExplicitBoolCompareRector::class); }; From fb8531416244f41df60204478409558cc087aa38 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 5 Apr 2024 10:18:32 +0900 Subject: [PATCH 2/4] refactor: vendor/bin/rector --- src/Authentication/Actions/Email2FA.php | 2 +- src/Authentication/Authenticators/AccessTokens.php | 4 ++-- src/Authentication/Authenticators/Session.php | 8 ++++---- src/Authentication/Passwords.php | 2 +- src/Authentication/Passwords/CompositionValidator.php | 2 +- src/Authentication/Passwords/NothingPersonalValidator.php | 2 +- src/Authentication/Passwords/ValidationRules.php | 4 ++-- src/Authentication/Traits/HasAccessTokens.php | 2 +- src/Authorization/Traits/Authorizable.php | 2 +- src/Entities/Group.php | 4 ++-- tests/Authentication/Filters/AbstractFilterTestCase.php | 2 +- 11 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Authentication/Actions/Email2FA.php b/src/Authentication/Actions/Email2FA.php index 4b38318a8..fc0958441 100644 --- a/src/Authentication/Actions/Email2FA.php +++ b/src/Authentication/Actions/Email2FA.php @@ -78,7 +78,7 @@ public function handle(IncomingRequest $request) $identity = $this->getIdentity($user); - if (empty($identity)) { + if (! $identity instanceof UserIdentity) { return redirect()->route('auth-action-show')->with('error', lang('Auth.need2FA')); } diff --git a/src/Authentication/Authenticators/AccessTokens.php b/src/Authentication/Authenticators/AccessTokens.php index be9f6ba9d..6356ab1a8 100644 --- a/src/Authentication/Authenticators/AccessTokens.php +++ b/src/Authentication/Authenticators/AccessTokens.php @@ -193,7 +193,7 @@ public function check(array $credentials): Result */ public function loggedIn(): bool { - if (! empty($this->user)) { + if ($this->user instanceof User) { return true; } @@ -226,7 +226,7 @@ public function loginById($userId): void { $user = $this->provider->findById($userId); - if (empty($user)) { + if (! $user instanceof User) { throw AuthenticationException::forInvalidUser(); } diff --git a/src/Authentication/Authenticators/Session.php b/src/Authentication/Authenticators/Session.php index 6712f8070..6f6594d67 100644 --- a/src/Authentication/Authenticators/Session.php +++ b/src/Authentication/Authenticators/Session.php @@ -241,7 +241,7 @@ public function checkAction(UserIdentity $identity, string $token): bool throw new LogicException('Cannot get the User.'); } - if (empty($token) || $token !== $identity->secret) { + if ($token === '' || $token === '0' || $token !== $identity->secret) { return false; } @@ -485,7 +485,7 @@ private function setAuthAction(): bool $identity = $this->userIdentityModel->getIdentityByType($this->user, $action->getType()); - if ($identity) { + if ($identity instanceof UserIdentity) { $this->userState = self::STATE_PENDING; $this->setSessionKey('auth_action', $actionClass); @@ -767,7 +767,7 @@ private function issueRememberMeToken(): void // Reset so it doesn't mess up future calls. $this->shouldRemember = false; - } elseif ($this->getRememberMeToken()) { + } elseif ($this->getRememberMeToken() !== null && $this->getRememberMeToken() !== '' && $this->getRememberMeToken() !== '0') { $this->removeRememberCookie(); // @TODO delete the token record. @@ -804,7 +804,7 @@ public function loginById($userId): void { $user = $this->provider->findById($userId); - if (empty($user)) { + if (! $user instanceof User) { throw AuthenticationException::forInvalidUser(); } diff --git a/src/Authentication/Passwords.php b/src/Authentication/Passwords.php index 42f9fe0ed..0705b6ec7 100644 --- a/src/Authentication/Passwords.php +++ b/src/Authentication/Passwords.php @@ -112,7 +112,7 @@ public function check(string $password, ?User $user = null): Result $password = trim($password); - if (empty($password)) { + if ($password === '' || $password === '0') { return new Result([ 'success' => false, 'reason' => lang('Auth.errorPasswordEmpty'), diff --git a/src/Authentication/Passwords/CompositionValidator.php b/src/Authentication/Passwords/CompositionValidator.php index 49ae3738a..7d50913a7 100644 --- a/src/Authentication/Passwords/CompositionValidator.php +++ b/src/Authentication/Passwords/CompositionValidator.php @@ -37,7 +37,7 @@ class CompositionValidator extends BaseValidator implements ValidatorInterface */ public function check(string $password, ?User $user = null): Result { - if (empty($this->config->minimumPasswordLength)) { + if ($this->config->minimumPasswordLength === 0) { throw AuthenticationException::forUnsetPasswordLength(); } diff --git a/src/Authentication/Passwords/NothingPersonalValidator.php b/src/Authentication/Passwords/NothingPersonalValidator.php index 90112bf2d..d1fa4484f 100644 --- a/src/Authentication/Passwords/NothingPersonalValidator.php +++ b/src/Authentication/Passwords/NothingPersonalValidator.php @@ -85,7 +85,7 @@ protected function isNotPersonal(string $password, ?User $user): bool // might be john.doe@example.com and we want all the needles we can get $emailParts = $this->strip_explode($localPart); - if (! empty($domain)) { + if ($domain !== null && $domain !== '' && $domain !== '0') { $emailParts[] = $domain; } $needles = [...$needles, ...$emailParts]; diff --git a/src/Authentication/Passwords/ValidationRules.php b/src/Authentication/Passwords/ValidationRules.php index ca6f07638..58ebf9b90 100644 --- a/src/Authentication/Passwords/ValidationRules.php +++ b/src/Authentication/Passwords/ValidationRules.php @@ -49,13 +49,13 @@ public function strong_password(string $value, ?string &$error1 = null, array $d $user = auth()->user(); } else { /** @phpstan-ignore-next-line */ - $user = empty($data) ? $this->buildUserFromRequest() : $this->buildUserFromData($data); + $user = $data === [] ? $this->buildUserFromRequest() : $this->buildUserFromData($data); } $result = $checker->check($value, $user); if (! $result->isOk()) { - if (empty($data)) { + if ($data === []) { $error1 = $result->reason(); } else { $error2 = $result->reason(); diff --git a/src/Authentication/Traits/HasAccessTokens.php b/src/Authentication/Traits/HasAccessTokens.php index 02027ef27..55cb92686 100644 --- a/src/Authentication/Traits/HasAccessTokens.php +++ b/src/Authentication/Traits/HasAccessTokens.php @@ -97,7 +97,7 @@ public function accessTokens(): array */ public function getAccessToken(?string $rawToken): ?AccessToken { - if (empty($rawToken)) { + if ($rawToken === null || $rawToken === '' || $rawToken === '0') { return null; } diff --git a/src/Authorization/Traits/Authorizable.php b/src/Authorization/Traits/Authorizable.php index ee4999a12..79e313951 100644 --- a/src/Authorization/Traits/Authorizable.php +++ b/src/Authorization/Traits/Authorizable.php @@ -264,7 +264,7 @@ public function can(string ...$permissions): bool return true; } - if (! count($this->groupCache)) { + if (count($this->groupCache) === 0) { return false; } diff --git a/src/Entities/Group.php b/src/Entities/Group.php index 62384a159..b63707929 100644 --- a/src/Entities/Group.php +++ b/src/Entities/Group.php @@ -80,14 +80,14 @@ public function can(string $permission): bool $this->populatePermissions(); // Check exact match - if (! empty($this->permissions) && in_array($permission, $this->permissions, true)) { + if ($this->permissions !== null && $this->permissions !== [] && in_array($permission, $this->permissions, true)) { return true; } // Check wildcard match $check = substr($permission, 0, strpos($permission, '.')) . '.*'; - return ! empty($this->permissions) && in_array($check, $this->permissions, true); + return $this->permissions !== null && $this->permissions !== [] && in_array($check, $this->permissions, true); } /** diff --git a/tests/Authentication/Filters/AbstractFilterTestCase.php b/tests/Authentication/Filters/AbstractFilterTestCase.php index 246fc7b93..1c3ec8bfc 100644 --- a/tests/Authentication/Filters/AbstractFilterTestCase.php +++ b/tests/Authentication/Filters/AbstractFilterTestCase.php @@ -61,7 +61,7 @@ private function addRoutes(): void { $routes = service('routes'); - $filterString = ! empty($this->routeFilter) + $filterString = isset($this->routeFilter) && ($this->routeFilter !== '' && $this->routeFilter !== '0') ? $this->routeFilter : $this->alias; From 76c43036bf18e3922c55a980331d82ca1f9220c4 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 5 Apr 2024 10:31:09 +0900 Subject: [PATCH 3/4] fix: remove unneeded conditions --- src/Authentication/Authenticators/Session.php | 7 +++++-- src/Authentication/Passwords.php | 2 +- src/Authentication/Passwords/NothingPersonalValidator.php | 2 +- src/Authentication/Traits/HasAccessTokens.php | 2 +- tests/Authentication/Filters/AbstractFilterTestCase.php | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Authentication/Authenticators/Session.php b/src/Authentication/Authenticators/Session.php index 6f6594d67..342adef67 100644 --- a/src/Authentication/Authenticators/Session.php +++ b/src/Authentication/Authenticators/Session.php @@ -241,7 +241,7 @@ public function checkAction(UserIdentity $identity, string $token): bool throw new LogicException('Cannot get the User.'); } - if ($token === '' || $token === '0' || $token !== $identity->secret) { + if ($token === '' || $token !== $identity->secret) { return false; } @@ -767,7 +767,7 @@ private function issueRememberMeToken(): void // Reset so it doesn't mess up future calls. $this->shouldRemember = false; - } elseif ($this->getRememberMeToken() !== null && $this->getRememberMeToken() !== '' && $this->getRememberMeToken() !== '0') { + } elseif ($this->getRememberMeToken() !== null) { $this->removeRememberCookie(); // @TODO delete the token record. @@ -935,6 +935,9 @@ private function calcExpires(): Time return Time::createFromTimestamp($timestamp); } + /** + * @param non-empty-string $rawToken + */ private function setRememberMeCookie(string $rawToken): void { /** @var Response $response */ diff --git a/src/Authentication/Passwords.php b/src/Authentication/Passwords.php index 0705b6ec7..6de08c8fa 100644 --- a/src/Authentication/Passwords.php +++ b/src/Authentication/Passwords.php @@ -112,7 +112,7 @@ public function check(string $password, ?User $user = null): Result $password = trim($password); - if ($password === '' || $password === '0') { + if ($password === '') { return new Result([ 'success' => false, 'reason' => lang('Auth.errorPasswordEmpty'), diff --git a/src/Authentication/Passwords/NothingPersonalValidator.php b/src/Authentication/Passwords/NothingPersonalValidator.php index d1fa4484f..8d7f6d07d 100644 --- a/src/Authentication/Passwords/NothingPersonalValidator.php +++ b/src/Authentication/Passwords/NothingPersonalValidator.php @@ -85,7 +85,7 @@ protected function isNotPersonal(string $password, ?User $user): bool // might be john.doe@example.com and we want all the needles we can get $emailParts = $this->strip_explode($localPart); - if ($domain !== null && $domain !== '' && $domain !== '0') { + if ($domain !== null && $domain !== '') { $emailParts[] = $domain; } $needles = [...$needles, ...$emailParts]; diff --git a/src/Authentication/Traits/HasAccessTokens.php b/src/Authentication/Traits/HasAccessTokens.php index 55cb92686..bb9d47975 100644 --- a/src/Authentication/Traits/HasAccessTokens.php +++ b/src/Authentication/Traits/HasAccessTokens.php @@ -97,7 +97,7 @@ public function accessTokens(): array */ public function getAccessToken(?string $rawToken): ?AccessToken { - if ($rawToken === null || $rawToken === '' || $rawToken === '0') { + if ($rawToken === null || $rawToken === '') { return null; } diff --git a/tests/Authentication/Filters/AbstractFilterTestCase.php b/tests/Authentication/Filters/AbstractFilterTestCase.php index 1c3ec8bfc..906f4ea85 100644 --- a/tests/Authentication/Filters/AbstractFilterTestCase.php +++ b/tests/Authentication/Filters/AbstractFilterTestCase.php @@ -61,7 +61,7 @@ private function addRoutes(): void { $routes = service('routes'); - $filterString = isset($this->routeFilter) && ($this->routeFilter !== '' && $this->routeFilter !== '0') + $filterString = isset($this->routeFilter) && ($this->routeFilter !== '') ? $this->routeFilter : $this->alias; From f2571a94a1b2d0eb4582d5de3ff6d48332c24643 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 5 Apr 2024 10:32:39 +0900 Subject: [PATCH 4/4] chore: vendor/bin/phpstan analyze --generate-baseline phpstan-baseline.php --- phpstan-baseline.php | 43 ++++--------------------------------------- 1 file changed, 4 insertions(+), 39 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 65f52c677..0b47303f5 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -14,7 +14,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, + 'count' => 1, 'path' => __DIR__ . '/src/Authentication/Actions/Email2FA.php', ]; $ignoreErrors[] = [ @@ -45,7 +45,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 4, + 'count' => 2, 'path' => __DIR__ . '/src/Authentication/Authenticators/AccessTokens.php', ]; $ignoreErrors[] = [ @@ -95,16 +95,6 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, - 'path' => __DIR__ . '/src/Authentication/Authenticators/Session.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Only booleans are allowed in an elseif condition, string\\|null given\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/src/Authentication/Authenticators/Session.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Only booleans are allowed in an if condition, CodeIgniter\\\\Shield\\\\Entities\\\\UserIdentity\\|null given\\.$#', 'count' => 1, 'path' => __DIR__ . '/src/Authentication/Authenticators/Session.php', ]; @@ -130,17 +120,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/src/Authentication/Passwords.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/src/Authentication/Passwords/CompositionValidator.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 5, + 'count' => 4, 'path' => __DIR__ . '/src/Authentication/Passwords/NothingPersonalValidator.php', ]; $ignoreErrors[] = [ @@ -148,11 +128,6 @@ 'count' => 2, 'path' => __DIR__ . '/src/Authentication/Passwords/ValidationRules.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/src/Authentication/Passwords/ValidationRules.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in &&, CodeIgniter\\\\Shield\\\\Entities\\\\User\\|null given on the right side\\.$#', 'count' => 1, @@ -249,11 +224,6 @@ 'count' => 1, 'path' => __DIR__ . '/src/Entities/Entity.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, - 'path' => __DIR__ . '/src/Entities/Group.php', -]; $ignoreErrors[] = [ 'message' => '#^Call to function model with CodeIgniter\\\\Shield\\\\Models\\\\GroupModel\\:\\:class is discouraged\\.$#', 'count' => 2, @@ -276,7 +246,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 8, + 'count' => 7, 'path' => __DIR__ . '/src/Entities/User.php', ]; $ignoreErrors[] = [ @@ -366,11 +336,6 @@ 'count' => 8, 'path' => __DIR__ . '/tests/Authentication/Authenticators/SessionAuthenticatorTest.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/tests/Authentication/Filters/AbstractFilterTestCase.php', -]; $ignoreErrors[] = [ 'message' => '#^Implicit array creation is not allowed \\- variable \\$users might not exist\\.$#', 'count' => 1,