Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
"stan": "@phpstan",
"stan-baseline": "tools/phpstan --generate-baseline",
"stan-setup": "phive install",
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.3.1\" && mv composer.backup composer.json",
"rector-check": "vendor/bin/rector process --dry-run",
"rector-fix": "vendor/bin/rector process",
"test": "phpunit",
"test-coverage": "phpunit --coverage-clover=clover.xml"
}
Expand Down
54 changes: 54 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);

use Rector\Caching\ValueObject\Storage\FileCacheStorage;
use Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector;
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector;

$cacheDir = getenv('RECTOR_CACHE_DIR') ?: sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'rector';

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])

->withCache(
cacheClass: FileCacheStorage::class,
cacheDirectory: $cacheDir,
)

->withPhpSets()
->withAttributesSets()

->withSets([
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::DEAD_CODE,
SetList::EARLY_RETURN,
SetList::INSTANCEOF,
SetList::TYPE_DECLARATION,
])

->withSkip([
ClassPropertyAssignToConstructorPromotionRector::class,
CatchExceptionNameMatchingTypeRector::class,
ClosureToArrowFunctionRector::class,
RemoveUselessReturnTagRector::class,
CompactToVariablesRector::class,
ReturnTypeFromStrictFluentReturnRector::class,
SplitDoubleAssignRector::class,
NewlineAfterStatementRector::class,
ExplicitBoolCompareRector::class,
TypedPropertyFromCreateMockAssignRector::class,
]);
4 changes: 2 additions & 2 deletions src/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class AbstractCollection extends ObjectRegistry
*/
public function __construct(array $config = [])
{
$configOptions = array_filter($config, fn($key) => is_string($key), ARRAY_FILTER_USE_KEY);
$configOptions = array_filter($config, is_string(...), ARRAY_FILTER_USE_KEY);
$this->setConfig($configOptions);

foreach ($config as $key => $value) {
Expand All @@ -60,6 +60,6 @@ public function __construct(array $config = [])
*/
public function isEmpty(): bool
{
return empty($this->_loaded);
return $this->_loaded === [];
}
}
6 changes: 0 additions & 6 deletions src/AuthenticationPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,16 @@ class AuthenticationPlugin extends BasePlugin
{
/**
* Do bootstrapping or not
*
* @var bool
*/
protected bool $bootstrapEnabled = false;

/**
* Load routes or not
*
* @var bool
*/
protected bool $routesEnabled = false;

/**
* Console middleware
*
* @var bool
*/
protected bool $consoleEnabled = false;
}
42 changes: 16 additions & 26 deletions src/AuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,16 @@ class AuthenticationService implements AuthenticationServiceInterface, Impersona

/**
* Authenticator collection
*
* @var \Authentication\Authenticator\AuthenticatorCollection|null
*/
protected ?AuthenticatorCollection $_authenticators = null;

/**
* Authenticator that successfully authenticated the identity.
*
* @var \Authentication\Authenticator\AuthenticatorInterface|null
*/
protected ?AuthenticatorInterface $_successfulAuthenticator = null;

/**
* Result of the last authenticate() call.
*
* @var \Authentication\Authenticator\ResultInterface|null
*/
protected ?ResultInterface $_result = null;

Expand Down Expand Up @@ -129,7 +123,7 @@ public function __construct(array $config = [])
*/
public function authenticators(): AuthenticatorCollection
{
if ($this->_authenticators === null) {
if (!$this->_authenticators instanceof AuthenticatorCollection) {
$authenticators = $this->getConfig('authenticators');
$this->_authenticators = new AuthenticatorCollection($authenticators);
}
Expand Down Expand Up @@ -259,7 +253,7 @@ public function getAuthenticationProvider(): ?AuthenticatorInterface
*/
public function getIdentificationProvider(): ?IdentifierInterface
{
if ($this->_successfulAuthenticator === null) {
if (!$this->_successfulAuthenticator instanceof AuthenticatorInterface) {
return null;
}

Expand All @@ -283,7 +277,7 @@ public function getResult(): ?ResultInterface
*/
public function getIdentity(): ?IdentityInterface
{
if ($this->_result === null) {
if (!$this->_result instanceof ResultInterface) {
return null;
}

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

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

if (is_callable($class)) {
$identity = $class($identityData);
} else {
$identity = new $class($identityData);
}
$identity = is_callable($class) ? $class($identityData) : new $class($identityData);

if (!($identity instanceof IdentityInterface)) {
throw new RuntimeException(sprintf(
'Object `%s` does not implement `%s`',
get_class($identity),
$identity::class,
IdentityInterface::class,
));
}
Expand Down Expand Up @@ -373,17 +363,17 @@ public function getUnauthenticatedRedirectUrl(ServerRequestInterface $request):
if ($uri->getQuery()) {
$redirect .= '?' . $uri->getQuery();
}
$query = urlencode($param) . '=' . urlencode($redirect);
$query = urlencode((string)$param) . '=' . urlencode($redirect);

/** @var array<string, mixed> $url */
$url = parse_url($target);
if (isset($url['query']) && strlen($url['query'])) {
$url = parse_url((string)$target);
if (isset($url['query']) && strlen((string)$url['query'])) {
$url['query'] .= '&' . $query;
} else {
$url['query'] = $query;
}
$fragment = isset($url['fragment']) ? '#' . $url['fragment'] : '';
$url['path'] = $url['path'] ?? '/';
$url['path'] ??= '/';

return $url['path'] . '?' . $url['query'] . $fragment;
}
Expand All @@ -404,12 +394,12 @@ public function getLoginRedirect(ServerRequestInterface $request): ?string
if (
empty($redirectParam) ||
!isset($params[$redirectParam]) ||
strlen($params[$redirectParam]) === 0
(string)$params[$redirectParam] === ''
) {
return null;
}

$parsed = parse_url($params[$redirectParam]);
$parsed = parse_url((string)$params[$redirectParam]);
if ($parsed === false) {
return null;
}
Expand All @@ -418,10 +408,10 @@ public function getLoginRedirect(ServerRequestInterface $request): ?string
}
$parsed += ['path' => '/', 'query' => ''];
if (strlen($parsed['path']) && $parsed['path'][0] !== '/') {
$parsed['path'] = "/{$parsed['path']}";
$parsed['path'] = '/' . $parsed['path'];
}
if ($parsed['query']) {
$parsed['query'] = "?{$parsed['query']}";
$parsed['query'] = '?' . $parsed['query'];
}

$redirect = $parsed['path'] . $parsed['query'];
Expand Down Expand Up @@ -525,13 +515,13 @@ public function isImpersonating(ServerRequestInterface $request): bool
protected function getImpersonationProvider(): ImpersonationInterface
{
$provider = $this->getAuthenticationProvider();
if ($provider === null) {
if (!$provider instanceof AuthenticatorInterface) {
throw new InvalidArgumentException('No AuthenticationProvider present.');
}
if (!($provider instanceof ImpersonationInterface)) {
$className = get_class($provider);
$className = $provider::class;
throw new InvalidArgumentException(
"The {$className} Provider must implement ImpersonationInterface in order to use impersonation.",
sprintf('The %s Provider must implement ImpersonationInterface in order to use impersonation.', $className),
);
}

Expand Down
4 changes: 1 addition & 3 deletions src/Authenticator/AbstractAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ abstract class AbstractAuthenticator implements AuthenticatorInterface

/**
* Identifier instance.
*
* @var \Authentication\Identifier\IdentifierInterface|null
*/
protected ?IdentifierInterface $_identifier = null;

Expand All @@ -69,7 +67,7 @@ public function __construct(?IdentifierInterface $identifier = null, array $conf
*/
public function getIdentifier(): IdentifierInterface
{
if ($this->_identifier === null) {
if (!$this->_identifier instanceof IdentifierInterface) {
throw new RuntimeException(
sprintf(
'Identifier is required for `%s`. Please provide an identifier instance.',
Expand Down
3 changes: 0 additions & 3 deletions src/Authenticator/AuthenticationRequiredException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class AuthenticationRequiredException extends HttpException
*/
protected array $headers = [];

/**
* @var string
*/
protected string $body = '';

/**
Expand Down
8 changes: 2 additions & 6 deletions src/Authenticator/CookieAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CookieAuthenticator extends AbstractAuthenticator implements PersistenceIn
*/
public function getIdentifier(): IdentifierInterface
{
if ($this->_identifier === null) {
if (!$this->_identifier instanceof IdentifierInterface) {
$identifierConfig = [];
if ($this->getConfig('fields')) {
$identifierConfig['fields'] = $this->getConfig('fields');
Expand All @@ -90,11 +90,7 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
]);
}

if (is_array($cookies[$cookieName])) {
$token = $cookies[$cookieName];
} else {
$token = json_decode($cookies[$cookieName], true);
}
$token = is_array($cookies[$cookieName]) ? $cookies[$cookieName] : json_decode((string)$cookies[$cookieName], true);

if ($token === null || count($token) !== 2) {
return new Result(null, Result::FAILURE_CREDENTIALS_INVALID, [
Expand Down
8 changes: 2 additions & 6 deletions src/Authenticator/EnvironmentAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,11 @@ protected function _buildLoginUrlErrorResult(ServerRequestInterface $request): R
$uri = $request->getUri();
$base = $request->getAttribute('base');
if ($base !== null) {
$uri = $uri->withPath((string)$base . $uri->getPath());
$uri = $uri->withPath($base . $uri->getPath());
}

$checkFullUrl = $this->getConfig('urlChecker.checkFullUrl', false);
if ($checkFullUrl) {
$uri = (string)$uri;
} else {
$uri = $uri->getPath();
}
$uri = $checkFullUrl ? (string)$uri : $uri->getPath();

$loginUrls = (array)$this->getConfig('loginUrl');
foreach ($loginUrls as $key => $loginUrl) {
Expand Down
10 changes: 3 additions & 7 deletions src/Authenticator/FormAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class FormAuthenticator extends AbstractAuthenticator
*/
public function getIdentifier(): IdentifierInterface
{
if ($this->_identifier === null) {
if (!$this->_identifier instanceof IdentifierInterface) {
$identifierConfig = [];
if ($this->getConfig('fields')) {
$identifierConfig['fields'] = $this->getConfig('fields');
Expand Down Expand Up @@ -109,15 +109,11 @@ protected function _buildLoginUrlErrorResult(ServerRequestInterface $request): R
$uri = $request->getUri();
$base = $request->getAttribute('base');
if ($base !== null) {
$uri = $uri->withPath((string)$base . $uri->getPath());
$uri = $uri->withPath($base . $uri->getPath());
}

$checkFullUrl = $this->getConfig('urlChecker.checkFullUrl', false);
if ($checkFullUrl) {
$uri = (string)$uri;
} else {
$uri = $uri->getPath();
}
$uri = $checkFullUrl ? (string)$uri : $uri->getPath();

$loginUrls = (array)$this->getConfig('loginUrl');
foreach ($loginUrls as $key => $loginUrl) {
Expand Down
2 changes: 1 addition & 1 deletion src/Authenticator/HttpBasicAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class HttpBasicAuthenticator extends AbstractAuthenticator implements StatelessI
*/
public function getIdentifier(): IdentifierInterface
{
if ($this->_identifier === null) {
if (!$this->_identifier instanceof IdentifierInterface) {
$identifierConfig = [];
if ($this->getConfig('fields')) {
$identifierConfig['fields'] = $this->getConfig('fields');
Expand Down
Loading
Loading