Skip to content

Commit 7088333

Browse files
authored
Merge pull request #1081 from kenjis/refactor-update-rector-config
refactor: replace empty() by rector
2 parents 9ae1651 + f2571a9 commit 7088333

File tree

13 files changed

+49
-56
lines changed

13 files changed

+49
-56
lines changed

phpstan-baseline.php

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
];
1515
$ignoreErrors[] = [
1616
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
17-
'count' => 2,
17+
'count' => 1,
1818
'path' => __DIR__ . '/src/Authentication/Actions/Email2FA.php',
1919
];
2020
$ignoreErrors[] = [
@@ -45,7 +45,7 @@
4545
];
4646
$ignoreErrors[] = [
4747
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
48-
'count' => 4,
48+
'count' => 2,
4949
'path' => __DIR__ . '/src/Authentication/Authenticators/AccessTokens.php',
5050
];
5151
$ignoreErrors[] = [
@@ -95,16 +95,6 @@
9595
];
9696
$ignoreErrors[] = [
9797
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
98-
'count' => 3,
99-
'path' => __DIR__ . '/src/Authentication/Authenticators/Session.php',
100-
];
101-
$ignoreErrors[] = [
102-
'message' => '#^Only booleans are allowed in an elseif condition, string\\|null given\\.$#',
103-
'count' => 1,
104-
'path' => __DIR__ . '/src/Authentication/Authenticators/Session.php',
105-
];
106-
$ignoreErrors[] = [
107-
'message' => '#^Only booleans are allowed in an if condition, CodeIgniter\\\\Shield\\\\Entities\\\\UserIdentity\\|null given\\.$#',
10898
'count' => 1,
10999
'path' => __DIR__ . '/src/Authentication/Authenticators/Session.php',
110100
];
@@ -130,29 +120,14 @@
130120
];
131121
$ignoreErrors[] = [
132122
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
133-
'count' => 1,
134-
'path' => __DIR__ . '/src/Authentication/Passwords.php',
135-
];
136-
$ignoreErrors[] = [
137-
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
138-
'count' => 1,
139-
'path' => __DIR__ . '/src/Authentication/Passwords/CompositionValidator.php',
140-
];
141-
$ignoreErrors[] = [
142-
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
143-
'count' => 5,
123+
'count' => 4,
144124
'path' => __DIR__ . '/src/Authentication/Passwords/NothingPersonalValidator.php',
145125
];
146126
$ignoreErrors[] = [
147127
'message' => '#^Call to method CodeIgniter\\\\Shield\\\\Result\\:\\:isOK\\(\\) with incorrect case\\: isOk$#',
148128
'count' => 2,
149129
'path' => __DIR__ . '/src/Authentication/Passwords/ValidationRules.php',
150130
];
151-
$ignoreErrors[] = [
152-
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
153-
'count' => 1,
154-
'path' => __DIR__ . '/src/Authentication/Passwords/ValidationRules.php',
155-
];
156131
$ignoreErrors[] = [
157132
'message' => '#^Only booleans are allowed in &&, CodeIgniter\\\\Shield\\\\Entities\\\\User\\|null given on the right side\\.$#',
158133
'count' => 1,
@@ -249,11 +224,6 @@
249224
'count' => 1,
250225
'path' => __DIR__ . '/src/Entities/Entity.php',
251226
];
252-
$ignoreErrors[] = [
253-
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
254-
'count' => 2,
255-
'path' => __DIR__ . '/src/Entities/Group.php',
256-
];
257227
$ignoreErrors[] = [
258228
'message' => '#^Call to function model with CodeIgniter\\\\Shield\\\\Models\\\\GroupModel\\:\\:class is discouraged\\.$#',
259229
'count' => 2,
@@ -276,7 +246,7 @@
276246
];
277247
$ignoreErrors[] = [
278248
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
279-
'count' => 8,
249+
'count' => 7,
280250
'path' => __DIR__ . '/src/Entities/User.php',
281251
];
282252
$ignoreErrors[] = [
@@ -366,11 +336,6 @@
366336
'count' => 8,
367337
'path' => __DIR__ . '/tests/Authentication/Authenticators/SessionAuthenticatorTest.php',
368338
];
369-
$ignoreErrors[] = [
370-
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
371-
'count' => 1,
372-
'path' => __DIR__ . '/tests/Authentication/Filters/AbstractFilterTestCase.php',
373-
];
374339
$ignoreErrors[] = [
375340
'message' => '#^Implicit array creation is not allowed \\- variable \\$users might not exist\\.$#',
376341
'count' => 1,

rector.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,28 @@
1111
* the LICENSE file that was distributed with this source code.
1212
*/
1313

14+
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
1415
use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
1516
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
17+
use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector;
1618
use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector;
1719
use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector;
1820
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector;
1921
use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector;
2022
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
23+
use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector;
2124
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
2225
use Rector\CodeQuality\Rector\If_\CombineIfRector;
26+
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
2327
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
2428
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
2529
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
30+
use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector;
2631
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
2732
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
2833
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
2934
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
35+
use Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector;
3036
use Rector\Config\RectorConfig;
3137
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
3238
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
@@ -38,12 +44,16 @@
3844
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
3945
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
4046
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
47+
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
4148
use Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector;
4249
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
4350
use Rector\PHPUnit\Set\PHPUnitSetList;
4451
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
4552
use Rector\Set\ValueObject\LevelSetList;
4653
use Rector\Set\ValueObject\SetList;
54+
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
55+
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
56+
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
4757
use Rector\ValueObject\PhpVersion;
4858

4959
return static function (RectorConfig $rectorConfig): void {
@@ -56,6 +66,12 @@
5666

5767
$rectorConfig->parallel();
5868

69+
// Github action cache
70+
$rectorConfig->cacheClass(FileCacheStorage::class);
71+
if (is_dir('/tmp')) {
72+
$rectorConfig->cacheDirectory('/tmp/rector');
73+
}
74+
5975
// The paths to refactor (can also be supplied with CLI arguments)
6076
$rectorConfig->paths([
6177
__DIR__ . '/src/',
@@ -89,6 +105,7 @@
89105
__DIR__ . '/src/Views',
90106

91107
JsonThrowOnErrorRector::class,
108+
StringifyStrNeedlesRector::class,
92109
YieldDataProviderRector::class,
93110

94111
// Note: requires php 8
@@ -140,7 +157,15 @@
140157
$rectorConfig->rule(FuncGetArgsToVariadicParamRector::class);
141158
$rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class);
142159
$rectorConfig->rule(SimplifyEmptyArrayCheckRector::class);
160+
$rectorConfig->rule(SimplifyEmptyCheckOnEmptyArrayRector::class);
161+
$rectorConfig->rule(TernaryEmptyArrayArrayDimFetchToCoalesceRector::class);
162+
$rectorConfig->rule(EmptyOnNullableObjectToInstanceOfRector::class);
163+
$rectorConfig->rule(DisallowedEmptyRuleFixerRector::class);
143164
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
144165
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
145166
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);
167+
$rectorConfig->rule(BooleanInIfConditionRuleFixerRector::class);
168+
$rectorConfig->rule(SingleInArrayToCompareRector::class);
169+
$rectorConfig->rule(VersionCompareFuncCallToConstantRector::class);
170+
$rectorConfig->rule(ExplicitBoolCompareRector::class);
146171
};

src/Authentication/Actions/Email2FA.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function handle(IncomingRequest $request)
7878

7979
$identity = $this->getIdentity($user);
8080

81-
if (empty($identity)) {
81+
if (! $identity instanceof UserIdentity) {
8282
return redirect()->route('auth-action-show')->with('error', lang('Auth.need2FA'));
8383
}
8484

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public function check(array $credentials): Result
193193
*/
194194
public function loggedIn(): bool
195195
{
196-
if (! empty($this->user)) {
196+
if ($this->user instanceof User) {
197197
return true;
198198
}
199199

@@ -226,7 +226,7 @@ public function loginById($userId): void
226226
{
227227
$user = $this->provider->findById($userId);
228228

229-
if (empty($user)) {
229+
if (! $user instanceof User) {
230230
throw AuthenticationException::forInvalidUser();
231231
}
232232

src/Authentication/Authenticators/Session.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function checkAction(UserIdentity $identity, string $token): bool
241241
throw new LogicException('Cannot get the User.');
242242
}
243243

244-
if (empty($token) || $token !== $identity->secret) {
244+
if ($token === '' || $token !== $identity->secret) {
245245
return false;
246246
}
247247

@@ -485,7 +485,7 @@ private function setAuthAction(): bool
485485

486486
$identity = $this->userIdentityModel->getIdentityByType($this->user, $action->getType());
487487

488-
if ($identity) {
488+
if ($identity instanceof UserIdentity) {
489489
$this->userState = self::STATE_PENDING;
490490

491491
$this->setSessionKey('auth_action', $actionClass);
@@ -767,7 +767,7 @@ private function issueRememberMeToken(): void
767767

768768
// Reset so it doesn't mess up future calls.
769769
$this->shouldRemember = false;
770-
} elseif ($this->getRememberMeToken()) {
770+
} elseif ($this->getRememberMeToken() !== null) {
771771
$this->removeRememberCookie();
772772

773773
// @TODO delete the token record.
@@ -804,7 +804,7 @@ public function loginById($userId): void
804804
{
805805
$user = $this->provider->findById($userId);
806806

807-
if (empty($user)) {
807+
if (! $user instanceof User) {
808808
throw AuthenticationException::forInvalidUser();
809809
}
810810

@@ -935,6 +935,9 @@ private function calcExpires(): Time
935935
return Time::createFromTimestamp($timestamp);
936936
}
937937

938+
/**
939+
* @param non-empty-string $rawToken
940+
*/
938941
private function setRememberMeCookie(string $rawToken): void
939942
{
940943
/** @var Response $response */

src/Authentication/Passwords.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function check(string $password, ?User $user = null): Result
112112

113113
$password = trim($password);
114114

115-
if (empty($password)) {
115+
if ($password === '') {
116116
return new Result([
117117
'success' => false,
118118
'reason' => lang('Auth.errorPasswordEmpty'),

src/Authentication/Passwords/CompositionValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CompositionValidator extends BaseValidator implements ValidatorInterface
3737
*/
3838
public function check(string $password, ?User $user = null): Result
3939
{
40-
if (empty($this->config->minimumPasswordLength)) {
40+
if ($this->config->minimumPasswordLength === 0) {
4141
throw AuthenticationException::forUnsetPasswordLength();
4242
}
4343

src/Authentication/Passwords/NothingPersonalValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function isNotPersonal(string $password, ?User $user): bool
8585

8686
// might be john.doe@example.com and we want all the needles we can get
8787
$emailParts = $this->strip_explode($localPart);
88-
if (! empty($domain)) {
88+
if ($domain !== null && $domain !== '') {
8989
$emailParts[] = $domain;
9090
}
9191
$needles = [...$needles, ...$emailParts];

src/Authentication/Passwords/ValidationRules.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public function strong_password(string $value, ?string &$error1 = null, array $d
4949
$user = auth()->user();
5050
} else {
5151
/** @phpstan-ignore-next-line */
52-
$user = empty($data) ? $this->buildUserFromRequest() : $this->buildUserFromData($data);
52+
$user = $data === [] ? $this->buildUserFromRequest() : $this->buildUserFromData($data);
5353
}
5454

5555
$result = $checker->check($value, $user);
5656

5757
if (! $result->isOk()) {
58-
if (empty($data)) {
58+
if ($data === []) {
5959
$error1 = $result->reason();
6060
} else {
6161
$error2 = $result->reason();

src/Authentication/Traits/HasAccessTokens.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function accessTokens(): array
9797
*/
9898
public function getAccessToken(?string $rawToken): ?AccessToken
9999
{
100-
if (empty($rawToken)) {
100+
if ($rawToken === null || $rawToken === '') {
101101
return null;
102102
}
103103

0 commit comments

Comments
 (0)