Skip to content

Commit 9b6305a

Browse files
committed
add rector
1 parent d6c30fe commit 9b6305a

File tree

67 files changed

+519
-529
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+519
-529
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
"stan": "@phpstan",
7676
"stan-baseline": "tools/phpstan --generate-baseline",
7777
"stan-setup": "phive install",
78+
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.3.1\" && mv composer.backup composer.json",
79+
"rector-check": "vendor/bin/rector process --dry-run",
80+
"rector-fix": "vendor/bin/rector process",
7881
"test": "phpunit",
7982
"test-coverage": "phpunit --coverage-clover=clover.xml"
8083
}

rector.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
5+
use Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector;
6+
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
7+
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
8+
use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
9+
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
10+
use Rector\Config\RectorConfig;
11+
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
12+
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
13+
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
14+
use Rector\Set\ValueObject\SetList;
15+
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector;
16+
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector;
17+
18+
$cacheDir = getenv('RECTOR_CACHE_DIR') ?: sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'rector';
19+
20+
return RectorConfig::configure()
21+
->withPaths([
22+
__DIR__ . '/src',
23+
__DIR__ . '/tests',
24+
])
25+
26+
->withCache(
27+
cacheClass: FileCacheStorage::class,
28+
cacheDirectory: $cacheDir,
29+
)
30+
31+
->withPhpSets()
32+
->withAttributesSets()
33+
34+
->withSets([
35+
SetList::CODE_QUALITY,
36+
SetList::CODING_STYLE,
37+
SetList::DEAD_CODE,
38+
SetList::EARLY_RETURN,
39+
SetList::INSTANCEOF,
40+
SetList::TYPE_DECLARATION,
41+
])
42+
43+
->withSkip([
44+
ClassPropertyAssignToConstructorPromotionRector::class,
45+
CatchExceptionNameMatchingTypeRector::class,
46+
ClosureToArrowFunctionRector::class,
47+
RemoveUselessReturnTagRector::class,
48+
CompactToVariablesRector::class,
49+
ReturnTypeFromStrictFluentReturnRector::class,
50+
SplitDoubleAssignRector::class,
51+
NewlineAfterStatementRector::class,
52+
ExplicitBoolCompareRector::class,
53+
TypedPropertyFromCreateMockAssignRector::class,
54+
]);

src/AbstractCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ abstract class AbstractCollection extends ObjectRegistry
4141
*/
4242
public function __construct(array $config = [])
4343
{
44-
$configOptions = array_filter($config, fn($key) => is_string($key), ARRAY_FILTER_USE_KEY);
44+
$configOptions = array_filter($config, is_string(...), ARRAY_FILTER_USE_KEY);
4545
$this->setConfig($configOptions);
4646

4747
foreach ($config as $key => $value) {
@@ -60,6 +60,6 @@ public function __construct(array $config = [])
6060
*/
6161
public function isEmpty(): bool
6262
{
63-
return empty($this->_loaded);
63+
return $this->_loaded === [];
6464
}
6565
}

src/AuthenticationPlugin.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,16 @@ class AuthenticationPlugin extends BasePlugin
2424
{
2525
/**
2626
* Do bootstrapping or not
27-
*
28-
* @var bool
2927
*/
3028
protected bool $bootstrapEnabled = false;
3129

3230
/**
3331
* Load routes or not
34-
*
35-
* @var bool
3632
*/
3733
protected bool $routesEnabled = false;
3834

3935
/**
4036
* Console middleware
41-
*
42-
* @var bool
4337
*/
4438
protected bool $consoleEnabled = false;
4539
}

src/AuthenticationService.php

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,16 @@ class AuthenticationService implements AuthenticationServiceInterface, Impersona
4040

4141
/**
4242
* Authenticator collection
43-
*
44-
* @var \Authentication\Authenticator\AuthenticatorCollection|null
4543
*/
4644
protected ?AuthenticatorCollection $_authenticators = null;
4745

4846
/**
4947
* Authenticator that successfully authenticated the identity.
50-
*
51-
* @var \Authentication\Authenticator\AuthenticatorInterface|null
5248
*/
5349
protected ?AuthenticatorInterface $_successfulAuthenticator = null;
5450

5551
/**
5652
* Result of the last authenticate() call.
57-
*
58-
* @var \Authentication\Authenticator\ResultInterface|null
5953
*/
6054
protected ?ResultInterface $_result = null;
6155

@@ -129,7 +123,7 @@ public function __construct(array $config = [])
129123
*/
130124
public function authenticators(): AuthenticatorCollection
131125
{
132-
if ($this->_authenticators === null) {
126+
if (!$this->_authenticators instanceof AuthenticatorCollection) {
133127
$authenticators = $this->getConfig('authenticators');
134128
$this->_authenticators = new AuthenticatorCollection($authenticators);
135129
}
@@ -259,7 +253,7 @@ public function getAuthenticationProvider(): ?AuthenticatorInterface
259253
*/
260254
public function getIdentificationProvider(): ?IdentifierInterface
261255
{
262-
if ($this->_successfulAuthenticator === null) {
256+
if (!$this->_successfulAuthenticator instanceof AuthenticatorInterface) {
263257
return null;
264258
}
265259

@@ -283,7 +277,7 @@ public function getResult(): ?ResultInterface
283277
*/
284278
public function getIdentity(): ?IdentityInterface
285279
{
286-
if ($this->_result === null) {
280+
if (!$this->_result instanceof ResultInterface) {
287281
return null;
288282
}
289283

@@ -319,16 +313,12 @@ public function buildIdentity(ArrayAccess|array $identityData): IdentityInterfac
319313

320314
$class = $this->getConfig('identityClass');
321315

322-
if (is_callable($class)) {
323-
$identity = $class($identityData);
324-
} else {
325-
$identity = new $class($identityData);
326-
}
316+
$identity = is_callable($class) ? $class($identityData) : new $class($identityData);
327317

328318
if (!($identity instanceof IdentityInterface)) {
329319
throw new RuntimeException(sprintf(
330320
'Object `%s` does not implement `%s`',
331-
get_class($identity),
321+
$identity::class,
332322
IdentityInterface::class,
333323
));
334324
}
@@ -373,17 +363,17 @@ public function getUnauthenticatedRedirectUrl(ServerRequestInterface $request):
373363
if ($uri->getQuery()) {
374364
$redirect .= '?' . $uri->getQuery();
375365
}
376-
$query = urlencode($param) . '=' . urlencode($redirect);
366+
$query = urlencode((string)$param) . '=' . urlencode($redirect);
377367

378368
/** @var array<string, mixed> $url */
379-
$url = parse_url($target);
380-
if (isset($url['query']) && strlen($url['query'])) {
369+
$url = parse_url((string)$target);
370+
if (isset($url['query']) && strlen((string)$url['query'])) {
381371
$url['query'] .= '&' . $query;
382372
} else {
383373
$url['query'] = $query;
384374
}
385375
$fragment = isset($url['fragment']) ? '#' . $url['fragment'] : '';
386-
$url['path'] = $url['path'] ?? '/';
376+
$url['path'] ??= '/';
387377

388378
return $url['path'] . '?' . $url['query'] . $fragment;
389379
}
@@ -404,12 +394,12 @@ public function getLoginRedirect(ServerRequestInterface $request): ?string
404394
if (
405395
empty($redirectParam) ||
406396
!isset($params[$redirectParam]) ||
407-
strlen($params[$redirectParam]) === 0
397+
(string)$params[$redirectParam] === ''
408398
) {
409399
return null;
410400
}
411401

412-
$parsed = parse_url($params[$redirectParam]);
402+
$parsed = parse_url((string)$params[$redirectParam]);
413403
if ($parsed === false) {
414404
return null;
415405
}
@@ -418,10 +408,10 @@ public function getLoginRedirect(ServerRequestInterface $request): ?string
418408
}
419409
$parsed += ['path' => '/', 'query' => ''];
420410
if (strlen($parsed['path']) && $parsed['path'][0] !== '/') {
421-
$parsed['path'] = "/{$parsed['path']}";
411+
$parsed['path'] = '/' . $parsed['path'];
422412
}
423413
if ($parsed['query']) {
424-
$parsed['query'] = "?{$parsed['query']}";
414+
$parsed['query'] = '?' . $parsed['query'];
425415
}
426416

427417
$redirect = $parsed['path'] . $parsed['query'];
@@ -525,13 +515,13 @@ public function isImpersonating(ServerRequestInterface $request): bool
525515
protected function getImpersonationProvider(): ImpersonationInterface
526516
{
527517
$provider = $this->getAuthenticationProvider();
528-
if ($provider === null) {
518+
if (!$provider instanceof AuthenticatorInterface) {
529519
throw new InvalidArgumentException('No AuthenticationProvider present.');
530520
}
531521
if (!($provider instanceof ImpersonationInterface)) {
532-
$className = get_class($provider);
522+
$className = $provider::class;
533523
throw new InvalidArgumentException(
534-
"The {$className} Provider must implement ImpersonationInterface in order to use impersonation.",
524+
sprintf('The %s Provider must implement ImpersonationInterface in order to use impersonation.', $className),
535525
);
536526
}
537527

src/Authenticator/AbstractAuthenticator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ abstract class AbstractAuthenticator implements AuthenticatorInterface
4141

4242
/**
4343
* Identifier instance.
44-
*
45-
* @var \Authentication\Identifier\IdentifierInterface|null
4644
*/
4745
protected ?IdentifierInterface $_identifier = null;
4846

@@ -69,7 +67,7 @@ public function __construct(?IdentifierInterface $identifier = null, array $conf
6967
*/
7068
public function getIdentifier(): IdentifierInterface
7169
{
72-
if ($this->_identifier === null) {
70+
if (!$this->_identifier instanceof IdentifierInterface) {
7371
throw new RuntimeException(
7472
sprintf(
7573
'Identifier is required for `%s`. Please provide an identifier instance.',

src/Authenticator/AuthenticationRequiredException.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ class AuthenticationRequiredException extends HttpException
3232
*/
3333
protected array $headers = [];
3434

35-
/**
36-
* @var string
37-
*/
3835
protected string $body = '';
3936

4037
/**

src/Authenticator/CookieAuthenticator.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class CookieAuthenticator extends AbstractAuthenticator implements PersistenceIn
6666
*/
6767
public function getIdentifier(): IdentifierInterface
6868
{
69-
if ($this->_identifier === null) {
69+
if (!$this->_identifier instanceof IdentifierInterface) {
7070
$identifierConfig = [];
7171
if ($this->getConfig('fields')) {
7272
$identifierConfig['fields'] = $this->getConfig('fields');
@@ -90,11 +90,7 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
9090
]);
9191
}
9292

93-
if (is_array($cookies[$cookieName])) {
94-
$token = $cookies[$cookieName];
95-
} else {
96-
$token = json_decode($cookies[$cookieName], true);
97-
}
93+
$token = is_array($cookies[$cookieName]) ? $cookies[$cookieName] : json_decode((string)$cookies[$cookieName], true);
9894

9995
if ($token === null || count($token) !== 2) {
10096
return new Result(null, Result::FAILURE_CREDENTIALS_INVALID, [

src/Authenticator/EnvironmentAuthenticator.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,11 @@ protected function _buildLoginUrlErrorResult(ServerRequestInterface $request): R
120120
$uri = $request->getUri();
121121
$base = $request->getAttribute('base');
122122
if ($base !== null) {
123-
$uri = $uri->withPath((string)$base . $uri->getPath());
123+
$uri = $uri->withPath($base . $uri->getPath());
124124
}
125125

126126
$checkFullUrl = $this->getConfig('urlChecker.checkFullUrl', false);
127-
if ($checkFullUrl) {
128-
$uri = (string)$uri;
129-
} else {
130-
$uri = $uri->getPath();
131-
}
127+
$uri = $checkFullUrl ? (string)$uri : $uri->getPath();
132128

133129
$loginUrls = (array)$this->getConfig('loginUrl');
134130
foreach ($loginUrls as $key => $loginUrl) {

src/Authenticator/FormAuthenticator.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class FormAuthenticator extends AbstractAuthenticator
5858
*/
5959
public function getIdentifier(): IdentifierInterface
6060
{
61-
if ($this->_identifier === null) {
61+
if (!$this->_identifier instanceof IdentifierInterface) {
6262
$identifierConfig = [];
6363
if ($this->getConfig('fields')) {
6464
$identifierConfig['fields'] = $this->getConfig('fields');
@@ -109,15 +109,11 @@ protected function _buildLoginUrlErrorResult(ServerRequestInterface $request): R
109109
$uri = $request->getUri();
110110
$base = $request->getAttribute('base');
111111
if ($base !== null) {
112-
$uri = $uri->withPath((string)$base . $uri->getPath());
112+
$uri = $uri->withPath($base . $uri->getPath());
113113
}
114114

115115
$checkFullUrl = $this->getConfig('urlChecker.checkFullUrl', false);
116-
if ($checkFullUrl) {
117-
$uri = (string)$uri;
118-
} else {
119-
$uri = $uri->getPath();
120-
}
116+
$uri = $checkFullUrl ? (string)$uri : $uri->getPath();
121117

122118
$loginUrls = (array)$this->getConfig('loginUrl');
123119
foreach ($loginUrls as $key => $loginUrl) {

0 commit comments

Comments
 (0)