Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix code style issues #2032

Merged
merged 2 commits into from
Jan 1, 2025
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
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ parameters:
- '#Parameter \$event of method HWI\\Bundle\\OAuthBundle\\Controller\\Connect\\AbstractController::dispatch\(\) has invalid type Symfony\\Component\\EventDispatcher\\Event.#'
- '#Parameter \#2 \$array of function implode expects array<string>, array<int, array\|string\|null> given.#'
- '#PHPDoc type DOMNode of property HWI\\Bundle\\OAuthBundle\\OAuth\\Response\\SensioConnectUserResponse::\$data is not covariant with PHPDoc type array of overridden property HWI\\Bundle\\OAuthBundle\\OAuth\\Response\\AbstractUserResponse::\$data#'
- '#Call to function method_exists\(\) with Symfony\\Component\\Security\\Http\\Authenticator\\Debug\\TraceableAuthenticator#'
4 changes: 2 additions & 2 deletions src/Controller/Connect/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(
TokenStorageInterface $tokenStorage,
UserCheckerInterface $userChecker,
Environment $twig,
?AccountConnectorInterface $accountConnector
?AccountConnectorInterface $accountConnector,
) {
$this->resourceOwnerMapLocator = $resourceOwnerMapLocator;
$this->requestStack = $requestStack;
Expand All @@ -81,7 +81,7 @@ protected function getResourceOwnerByName(string $name): ResourceOwnerInterface
}
}

throw new NotFoundHttpException(sprintf("No resource owner with name '%s'.", $name));
throw new NotFoundHttpException(\sprintf("No resource owner with name '%s'.", $name));
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/Connect/ConnectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace HWI\Bundle\OAuthBundle\Controller\Connect;

use Exception;
use HWI\Bundle\OAuthBundle\Connect\AccountConnectorInterface;
use HWI\Bundle\OAuthBundle\Event\GetResponseUserEvent;
use HWI\Bundle\OAuthBundle\HWIOAuthEvents;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function __construct(
bool $failedUseReferer,
string $failedAuthPath,
bool $enableConnectConfirmation,
?AccountConnectorInterface $accountConnector
?AccountConnectorInterface $accountConnector,
) {
parent::__construct(
$resourceOwnerMapLocator,
Expand All @@ -89,7 +90,7 @@ public function __construct(
*
* @param string $service name of the resource owner to connect to
*
* @throws \Exception
* @throws Exception
* @throws NotFoundHttpException if `connect` functionality was not enabled
* @throws AccessDeniedException if no user is authenticated
*/
Expand Down
11 changes: 7 additions & 4 deletions src/Controller/Connect/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace HWI\Bundle\OAuthBundle\Controller\Connect;

use Exception;
use HWI\Bundle\OAuthBundle\Connect\AccountConnectorInterface;
use HWI\Bundle\OAuthBundle\Event\FilterUserResponseEvent;
use HWI\Bundle\OAuthBundle\Event\FormEvent;
Expand All @@ -19,6 +20,8 @@
use HWI\Bundle\OAuthBundle\HWIOAuthEvents;
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException;
use HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMapLocator;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -58,7 +61,7 @@ public function __construct(
string $grantRule,
?string $registrationForm,
?AccountConnectorInterface $accountConnector,
?RegistrationFormHandlerInterface $formHandler
?RegistrationFormHandlerInterface $formHandler,
) {
parent::__construct(
$resourceOwnerMapLocator,
Expand All @@ -85,7 +88,7 @@ public function __construct(
*
* @throws NotFoundHttpException if `connect` functionality was not enabled
* @throws AccessDeniedException if any user is authenticated
* @throws \RuntimeException
* @throws RuntimeException
*/
public function registrationAction(Request $request, string $key): Response
{
Expand All @@ -109,11 +112,11 @@ public function registrationAction(Request $request, string $key): Response
}

if (!$error instanceof AccountNotLinkedException) {
throw new \RuntimeException('Cannot register an account.', 0, $error instanceof \Exception ? $error : null);
throw new RuntimeException('Cannot register an account.', 0, $error instanceof Exception ? $error : null);
}

if (!$this->registrationForm) {
throw new \InvalidArgumentException('Registration form class must be set.');
throw new InvalidArgumentException('Registration form class must be set.');
}

$userInformation = $this
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace HWI\Bundle\OAuthBundle\Controller;

use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException;
use LogicException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function __construct(
RequestStack $requestStack,
Environment $twig,
bool $connect,
string $grantRule
string $grantRule,
) {
$this->authenticationUtils = $authenticationUtils;
$this->router = $router;
Expand All @@ -61,7 +62,7 @@ public function __construct(
* Action that handles the login 'form'. If connecting is enabled the
* user will be redirected to the appropriate login urls or registration forms.
*
* @throws \LogicException
* @throws LogicException
*/
public function connectAction(Request $request): Response
{
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/RedirectToServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMapLocator;
use HWI\Bundle\OAuthBundle\Security\OAuthUtils;
use HWI\Bundle\OAuthBundle\Util\DomainWhitelist;
use RuntimeException;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -33,7 +34,7 @@ public function __construct(
private readonly ResourceOwnerMapLocator $resourceOwnerMapLocator,
private readonly ?string $targetPathParameter,
private readonly bool $failedUseReferer,
private readonly bool $useReferer
private readonly bool $useReferer,
) {
}

Expand All @@ -44,7 +45,7 @@ public function redirectToServiceAction(Request $request, string $service): Redi
{
try {
$authorizationUrl = $this->oauthUtils->getAuthorizationUrl($request, $service);
} catch (\RuntimeException $e) {
} catch (RuntimeException $e) {
throw new NotFoundHttpException($e->getMessage(), $e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use HWI\Bundle\OAuthBundle\DependencyInjection\Configuration;
use HWI\Bundle\OAuthBundle\DependencyInjection\HWIOAuthExtension;
use InvalidArgumentException;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -66,18 +67,18 @@ private function registerResourceOwnerTypeClassParameters(ContainerBuilder $cont
$resourceOwnerClass = $definition->getClass();

// Check whether a ResourceOwner class exists only if resource owner was set by its "options.type"
if (false === preg_match('~^%(?P<parameter>hwi_oauth.resource_owner.(?P<type>.+).class)%$~', $resourceOwnerClass, $match)) {
if (false === $result = preg_match('~^%(?P<parameter>hwi_oauth.resource_owner.(?P<type>.+).class)%$~', $resourceOwnerClass, $match)) {
return;
}

if (!($match['type'] ?? null)) {
if (0 === $result || !$match['type']) {
continue;
}

if (!Configuration::isResourceOwnerSupported($match['type'])) {
$e = new \InvalidArgumentException(sprintf('Unknown resource owner type "%s"', $match['type']));
$e = new InvalidArgumentException(\sprintf('Unknown resource owner type "%s"', $match['type']));

throw new InvalidConfigurationException(sprintf('Invalid configuration for path "hwi_oauth.resource_owners.%s.type": %s', $resourceOwnerName, $e->getMessage()), $e->getCode(), $e);
throw new InvalidConfigurationException(\sprintf('Invalid configuration for path "hwi_oauth.resource_owners.%s.type": %s', $resourceOwnerName, $e->getMessage()), $e->getCode(), $e);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth1ResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
use LogicException;
use ReflectionClass;
use Symfony\Component\Config\Definition\BaseNode;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand Down Expand Up @@ -73,17 +75,17 @@ public static function isResourceOwnerSupported(string $resourceOwner): bool

public static function registerResourceOwner(string $resourceOwnerClass): void
{
$reflection = new \ReflectionClass($resourceOwnerClass);
$reflection = new ReflectionClass($resourceOwnerClass);
if (!$reflection->implementsInterface(ResourceOwnerInterface::class)) {
throw new \LogicException('Resource owner class should implement "ResourceOwnerInterface", or extended class "GenericOAuth1ResourceOwner"/"GenericOAuth2ResourceOwner".');
throw new LogicException('Resource owner class should implement "ResourceOwnerInterface", or extended class "GenericOAuth1ResourceOwner"/"GenericOAuth2ResourceOwner".');
}

$type = \defined("$resourceOwnerClass::TYPE") ? $resourceOwnerClass::TYPE : null;
if (null === $type) {
if (preg_match('~(?P<resource_owner>[^\\\\]+)ResourceOwner$~', $resourceOwnerClass, $match)) {
$type = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $match['resource_owner']));
} else {
throw new \LogicException(sprintf('Resource owner class either should have "TYPE" const defined or end with "ResourceOwner" so that type can be calculated by converting its class name without suffix to "snake_case". Given class name is "%s"', $resourceOwnerClass));
throw new LogicException(\sprintf('Resource owner class either should have "TYPE" const defined or end with "ResourceOwner" so that type can be calculated by converting its class name without suffix to "snake_case". Given class name is "%s"', $resourceOwnerClass));
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/DependencyInjection/HWIOAuthExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace HWI\Bundle\OAuthBundle\DependencyInjection;

use ArrayIterator;
use Exception;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
use RuntimeException;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
Expand All @@ -35,15 +38,15 @@
final class HWIOAuthExtension extends Extension
{
/**
* @var \ArrayIterator<string, true>
* @var ArrayIterator<string, true>
*/
private \ArrayIterator $firewallNames;
private ArrayIterator $firewallNames;

private bool $refreshTokenListenerEnabled = false;

public function __construct()
{
$this->firewallNames = new \ArrayIterator();
$this->firewallNames = new ArrayIterator();
}

public function getConfiguration(array $config, ContainerBuilder $container): Configuration
Expand All @@ -54,8 +57,8 @@ public function getConfiguration(array $config, ContainerBuilder $container): Co
/**
* {@inheritdoc}
*
* @throws \Exception
* @throws \RuntimeException
* @throws Exception
* @throws RuntimeException
* @throws InvalidConfigurationException
* @throws BadMethodCallException
* @throws InvalidArgumentException
Expand Down Expand Up @@ -138,7 +141,7 @@ public function createResourceOwnerService(ContainerBuilder $container, string $
// handle external resource owners with given class
if (isset($options['class'])) {
if (!is_subclass_of($options['class'], ResourceOwnerInterface::class, true)) {
throw new InvalidConfigurationException(sprintf('Class "%s" must implement interface "HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface".', $options['class']));
throw new InvalidConfigurationException(\sprintf('Class "%s" must implement interface "HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface".', $options['class']));
}

$definition = new Definition($options['class']);
Expand Down Expand Up @@ -166,7 +169,7 @@ public function getAlias(): string
return 'hwi_oauth';
}

public function getFirewallNames(): \ArrayIterator
public function getFirewallNames(): ArrayIterator
{
return $this->firewallNames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace HWI\Bundle\OAuthBundle\DependencyInjection\Security\Factory;

use ArrayIterator;
use HWI\Bundle\OAuthBundle\Security\Http\Authenticator\OAuthAuthenticator;
use HWI\Bundle\OAuthBundle\Security\Http\Firewall\RefreshAccessTokenListener;
use HWI\Bundle\OAuthBundle\Security\Http\Firewall\RefreshAccessTokenListenerOld;
Expand All @@ -31,7 +32,7 @@
*/
final class OAuthAuthenticatorFactory extends AbstractFactory implements AuthenticatorFactoryInterface, FirewallListenerFactoryInterface
{
public function __construct(private \ArrayIterator $firewallNames)
public function __construct(private ArrayIterator $firewallNames)
{
}

Expand All @@ -58,7 +59,7 @@ public function createAuthenticator(
ContainerBuilder $container,
string $firewallName,
array $config,
string $userProviderId
string $userProviderId,
): string {
$authenticatorId = 'security.authenticator.oauth.'.$firewallName;

Expand Down Expand Up @@ -130,7 +131,7 @@ public function getPosition(): string
return 'http';
}

public function getFirewallNames(): \ArrayIterator
public function getFirewallNames(): ArrayIterator
{
return $this->firewallNames;
}
Expand Down
3 changes: 2 additions & 1 deletion src/HWIOAuthBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use HWI\Bundle\OAuthBundle\DependencyInjection\CompilerPass\EnableRefreshOAuthTokenListenerCompilerPass;
use HWI\Bundle\OAuthBundle\DependencyInjection\CompilerPass\ResourceOwnerCompilerPass;
use HWI\Bundle\OAuthBundle\DependencyInjection\Security\Factory\OAuthAuthenticatorFactory;
use RuntimeException;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function build(ContainerBuilder $container): void
// Symfony < 5.4 BC layer
$extension->addSecurityListenerFactory(new OAuthAuthenticatorFactory($firewallNames));
} else {
throw new \RuntimeException('Unsupported Symfony Security component version');
throw new RuntimeException('Unsupported Symfony Security component version');
}

$container->addCompilerPass(new ResourceOwnerCompilerPass());
Expand Down
3 changes: 2 additions & 1 deletion src/OAuth/Exception/HttpTransportException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
namespace HWI\Bundle\OAuthBundle\OAuth\Exception;

use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Throwable;

final class HttpTransportException extends AuthenticationException
{
private string $ownerName;

public function __construct(string $message, string $ownerName, int $code = 0, ?\Throwable $previous = null)
public function __construct(string $message, string $ownerName, int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->ownerName = $ownerName;
Expand Down
6 changes: 4 additions & 2 deletions src/OAuth/Exception/StateRetrievalException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

namespace HWI\Bundle\OAuthBundle\OAuth\Exception;

final class StateRetrievalException extends \InvalidArgumentException
use InvalidArgumentException;

final class StateRetrievalException extends InvalidArgumentException
{
/**
* @param string $key The provided string key
*/
public static function forKey(string $key): self
{
return new static(sprintf('No value found in state for key [%s]', $key));
return new static(\sprintf('No value found in state for key [%s]', $key));
}
}
10 changes: 6 additions & 4 deletions src/OAuth/RequestDataStorage/SessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use HWI\Bundle\OAuthBundle\OAuth\RequestDataStorageInterface;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
use InvalidArgumentException;
use LogicException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

Expand All @@ -39,7 +41,7 @@ public function fetch(ResourceOwnerInterface $resourceOwner, $key, $type = 'toke
{
$key = $this->generateKey($resourceOwner, $key, $type);
if (null === $data = $this->getSession()->get($key)) {
throw new \InvalidArgumentException('No data available in storage.');
throw new InvalidArgumentException('No data available in storage.');
}

// Request tokens are one time use only
Expand All @@ -57,7 +59,7 @@ public function save(ResourceOwnerInterface $resourceOwner, $value, $type = 'tok
{
if ('token' === $type) {
if (!\is_array($value) || !isset($value['oauth_token'])) {
throw new \InvalidArgumentException('Invalid request token.');
throw new InvalidArgumentException('Invalid request token.');
}

$key = $this->generateKey($resourceOwner, $value['oauth_token'], 'token');
Expand All @@ -73,7 +75,7 @@ public function save(ResourceOwnerInterface $resourceOwner, $value, $type = 'tok
*/
private function generateKey(ResourceOwnerInterface $resourceOwner, string $key, string $type): string
{
return sprintf('_hwi_oauth.%s.%s.%s.%s', $resourceOwner->getName(), $resourceOwner->getOption('client_id'), $type, $key);
return \sprintf('_hwi_oauth.%s.%s.%s.%s', $resourceOwner->getName(), $resourceOwner->getOption('client_id'), $type, $key);
}

/**
Expand Down Expand Up @@ -116,6 +118,6 @@ private function getSession(): SessionInterface
return $request->getSession();
}

throw new \LogicException('There is currently no session available.');
throw new LogicException('There is currently no session available.');
}
}
Loading
Loading