Skip to content

Commit 1029336

Browse files
authored
Merge pull request #2032 from xabbuh/code-style
fix code style issues
2 parents 553f342 + a0ffe6b commit 1029336

File tree

60 files changed

+210
-149
lines changed

Some content is hidden

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

60 files changed

+210
-149
lines changed

phpstan.neon

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ parameters:
1919
- '#Parameter \$event of method HWI\\Bundle\\OAuthBundle\\Controller\\Connect\\AbstractController::dispatch\(\) has invalid type Symfony\\Component\\EventDispatcher\\Event.#'
2020
- '#Parameter \#2 \$array of function implode expects array<string>, array<int, array\|string\|null> given.#'
2121
- '#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#'
22+
- '#Call to function method_exists\(\) with Symfony\\Component\\Security\\Http\\Authenticator\\Debug\\TraceableAuthenticator#'

src/Controller/Connect/AbstractController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(
5757
TokenStorageInterface $tokenStorage,
5858
UserCheckerInterface $userChecker,
5959
Environment $twig,
60-
?AccountConnectorInterface $accountConnector
60+
?AccountConnectorInterface $accountConnector,
6161
) {
6262
$this->resourceOwnerMapLocator = $resourceOwnerMapLocator;
6363
$this->requestStack = $requestStack;
@@ -81,7 +81,7 @@ protected function getResourceOwnerByName(string $name): ResourceOwnerInterface
8181
}
8282
}
8383

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

8787
/**

src/Controller/Connect/ConnectController.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace HWI\Bundle\OAuthBundle\Controller\Connect;
1313

14+
use Exception;
1415
use HWI\Bundle\OAuthBundle\Connect\AccountConnectorInterface;
1516
use HWI\Bundle\OAuthBundle\Event\GetResponseUserEvent;
1617
use HWI\Bundle\OAuthBundle\HWIOAuthEvents;
@@ -62,7 +63,7 @@ public function __construct(
6263
bool $failedUseReferer,
6364
string $failedAuthPath,
6465
bool $enableConnectConfirmation,
65-
?AccountConnectorInterface $accountConnector
66+
?AccountConnectorInterface $accountConnector,
6667
) {
6768
parent::__construct(
6869
$resourceOwnerMapLocator,
@@ -89,7 +90,7 @@ public function __construct(
8990
*
9091
* @param string $service name of the resource owner to connect to
9192
*
92-
* @throws \Exception
93+
* @throws Exception
9394
* @throws NotFoundHttpException if `connect` functionality was not enabled
9495
* @throws AccessDeniedException if no user is authenticated
9596
*/

src/Controller/Connect/RegisterController.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace HWI\Bundle\OAuthBundle\Controller\Connect;
1313

14+
use Exception;
1415
use HWI\Bundle\OAuthBundle\Connect\AccountConnectorInterface;
1516
use HWI\Bundle\OAuthBundle\Event\FilterUserResponseEvent;
1617
use HWI\Bundle\OAuthBundle\Event\FormEvent;
@@ -19,6 +20,8 @@
1920
use HWI\Bundle\OAuthBundle\HWIOAuthEvents;
2021
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException;
2122
use HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMapLocator;
23+
use InvalidArgumentException;
24+
use RuntimeException;
2225
use Symfony\Component\Form\FormFactoryInterface;
2326
use Symfony\Component\HttpFoundation\RedirectResponse;
2427
use Symfony\Component\HttpFoundation\Request;
@@ -58,7 +61,7 @@ public function __construct(
5861
string $grantRule,
5962
?string $registrationForm,
6063
?AccountConnectorInterface $accountConnector,
61-
?RegistrationFormHandlerInterface $formHandler
64+
?RegistrationFormHandlerInterface $formHandler,
6265
) {
6366
parent::__construct(
6467
$resourceOwnerMapLocator,
@@ -85,7 +88,7 @@ public function __construct(
8588
*
8689
* @throws NotFoundHttpException if `connect` functionality was not enabled
8790
* @throws AccessDeniedException if any user is authenticated
88-
* @throws \RuntimeException
91+
* @throws RuntimeException
8992
*/
9093
public function registrationAction(Request $request, string $key): Response
9194
{
@@ -109,11 +112,11 @@ public function registrationAction(Request $request, string $key): Response
109112
}
110113

111114
if (!$error instanceof AccountNotLinkedException) {
112-
throw new \RuntimeException('Cannot register an account.', 0, $error instanceof \Exception ? $error : null);
115+
throw new RuntimeException('Cannot register an account.', 0, $error instanceof Exception ? $error : null);
113116
}
114117

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

119122
$userInformation = $this

src/Controller/LoginController.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace HWI\Bundle\OAuthBundle\Controller;
1313

1414
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException;
15+
use LogicException;
1516
use Symfony\Component\HttpFoundation\RedirectResponse;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\RequestStack;
@@ -46,7 +47,7 @@ public function __construct(
4647
RequestStack $requestStack,
4748
Environment $twig,
4849
bool $connect,
49-
string $grantRule
50+
string $grantRule,
5051
) {
5152
$this->authenticationUtils = $authenticationUtils;
5253
$this->router = $router;
@@ -61,7 +62,7 @@ public function __construct(
6162
* Action that handles the login 'form'. If connecting is enabled the
6263
* user will be redirected to the appropriate login urls or registration forms.
6364
*
64-
* @throws \LogicException
65+
* @throws LogicException
6566
*/
6667
public function connectAction(Request $request): Response
6768
{

src/Controller/RedirectToServiceController.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMapLocator;
1515
use HWI\Bundle\OAuthBundle\Security\OAuthUtils;
1616
use HWI\Bundle\OAuthBundle\Util\DomainWhitelist;
17+
use RuntimeException;
1718
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
1819
use Symfony\Component\HttpFoundation\RedirectResponse;
1920
use Symfony\Component\HttpFoundation\Request;
@@ -33,7 +34,7 @@ public function __construct(
3334
private readonly ResourceOwnerMapLocator $resourceOwnerMapLocator,
3435
private readonly ?string $targetPathParameter,
3536
private readonly bool $failedUseReferer,
36-
private readonly bool $useReferer
37+
private readonly bool $useReferer,
3738
) {
3839
}
3940

@@ -44,7 +45,7 @@ public function redirectToServiceAction(Request $request, string $service): Redi
4445
{
4546
try {
4647
$authorizationUrl = $this->oauthUtils->getAuthorizationUrl($request, $service);
47-
} catch (\RuntimeException $e) {
48+
} catch (RuntimeException $e) {
4849
throw new NotFoundHttpException($e->getMessage(), $e);
4950
}
5051

src/DependencyInjection/CompilerPass/ResourceOwnerCompilerPass.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use HWI\Bundle\OAuthBundle\DependencyInjection\Configuration;
1515
use HWI\Bundle\OAuthBundle\DependencyInjection\HWIOAuthExtension;
16+
use InvalidArgumentException;
1617
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1718
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -66,18 +67,18 @@ private function registerResourceOwnerTypeClassParameters(ContainerBuilder $cont
6667
$resourceOwnerClass = $definition->getClass();
6768

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

73-
if (!($match['type'] ?? null)) {
74+
if (0 === $result || !$match['type']) {
7475
continue;
7576
}
7677

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

80-
throw new InvalidConfigurationException(sprintf('Invalid configuration for path "hwi_oauth.resource_owners.%s.type": %s', $resourceOwnerName, $e->getMessage()), $e->getCode(), $e);
81+
throw new InvalidConfigurationException(\sprintf('Invalid configuration for path "hwi_oauth.resource_owners.%s.type": %s', $resourceOwnerName, $e->getMessage()), $e->getCode(), $e);
8182
}
8283
}
8384
}

src/DependencyInjection/Configuration.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth1ResourceOwner;
1515
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner;
1616
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
17+
use LogicException;
18+
use ReflectionClass;
1719
use Symfony\Component\Config\Definition\BaseNode;
1820
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1921
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@@ -73,17 +75,17 @@ public static function isResourceOwnerSupported(string $resourceOwner): bool
7375

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

8183
$type = \defined("$resourceOwnerClass::TYPE") ? $resourceOwnerClass::TYPE : null;
8284
if (null === $type) {
8385
if (preg_match('~(?P<resource_owner>[^\\\\]+)ResourceOwner$~', $resourceOwnerClass, $match)) {
8486
$type = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $match['resource_owner']));
8587
} else {
86-
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));
88+
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));
8789
}
8890
}
8991

src/DependencyInjection/HWIOAuthExtension.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
namespace HWI\Bundle\OAuthBundle\DependencyInjection;
1313

14+
use ArrayIterator;
15+
use Exception;
1416
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
17+
use RuntimeException;
1518
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1619
use Symfony\Component\Config\Definition\Processor;
1720
use Symfony\Component\Config\FileLocator;
@@ -35,15 +38,15 @@
3538
final class HWIOAuthExtension extends Extension
3639
{
3740
/**
38-
* @var \ArrayIterator<string, true>
41+
* @var ArrayIterator<string, true>
3942
*/
40-
private \ArrayIterator $firewallNames;
43+
private ArrayIterator $firewallNames;
4144

4245
private bool $refreshTokenListenerEnabled = false;
4346

4447
public function __construct()
4548
{
46-
$this->firewallNames = new \ArrayIterator();
49+
$this->firewallNames = new ArrayIterator();
4750
}
4851

4952
public function getConfiguration(array $config, ContainerBuilder $container): Configuration
@@ -54,8 +57,8 @@ public function getConfiguration(array $config, ContainerBuilder $container): Co
5457
/**
5558
* {@inheritdoc}
5659
*
57-
* @throws \Exception
58-
* @throws \RuntimeException
60+
* @throws Exception
61+
* @throws RuntimeException
5962
* @throws InvalidConfigurationException
6063
* @throws BadMethodCallException
6164
* @throws InvalidArgumentException
@@ -138,7 +141,7 @@ public function createResourceOwnerService(ContainerBuilder $container, string $
138141
// handle external resource owners with given class
139142
if (isset($options['class'])) {
140143
if (!is_subclass_of($options['class'], ResourceOwnerInterface::class, true)) {
141-
throw new InvalidConfigurationException(sprintf('Class "%s" must implement interface "HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface".', $options['class']));
144+
throw new InvalidConfigurationException(\sprintf('Class "%s" must implement interface "HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface".', $options['class']));
142145
}
143146

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

169-
public function getFirewallNames(): \ArrayIterator
172+
public function getFirewallNames(): ArrayIterator
170173
{
171174
return $this->firewallNames;
172175
}

src/DependencyInjection/Security/Factory/OAuthAuthenticatorFactory.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

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

14+
use ArrayIterator;
1415
use HWI\Bundle\OAuthBundle\Security\Http\Authenticator\OAuthAuthenticator;
1516
use HWI\Bundle\OAuthBundle\Security\Http\Firewall\RefreshAccessTokenListener;
1617
use HWI\Bundle\OAuthBundle\Security\Http\Firewall\RefreshAccessTokenListenerOld;
@@ -31,7 +32,7 @@
3132
*/
3233
final class OAuthAuthenticatorFactory extends AbstractFactory implements AuthenticatorFactoryInterface, FirewallListenerFactoryInterface
3334
{
34-
public function __construct(private \ArrayIterator $firewallNames)
35+
public function __construct(private ArrayIterator $firewallNames)
3536
{
3637
}
3738

@@ -58,7 +59,7 @@ public function createAuthenticator(
5859
ContainerBuilder $container,
5960
string $firewallName,
6061
array $config,
61-
string $userProviderId
62+
string $userProviderId,
6263
): string {
6364
$authenticatorId = 'security.authenticator.oauth.'.$firewallName;
6465

@@ -130,7 +131,7 @@ public function getPosition(): string
130131
return 'http';
131132
}
132133

133-
public function getFirewallNames(): \ArrayIterator
134+
public function getFirewallNames(): ArrayIterator
134135
{
135136
return $this->firewallNames;
136137
}

src/HWIOAuthBundle.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use HWI\Bundle\OAuthBundle\DependencyInjection\CompilerPass\EnableRefreshOAuthTokenListenerCompilerPass;
1515
use HWI\Bundle\OAuthBundle\DependencyInjection\CompilerPass\ResourceOwnerCompilerPass;
1616
use HWI\Bundle\OAuthBundle\DependencyInjection\Security\Factory\OAuthAuthenticatorFactory;
17+
use RuntimeException;
1718
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
1920
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
@@ -43,7 +44,7 @@ public function build(ContainerBuilder $container): void
4344
// Symfony < 5.4 BC layer
4445
$extension->addSecurityListenerFactory(new OAuthAuthenticatorFactory($firewallNames));
4546
} else {
46-
throw new \RuntimeException('Unsupported Symfony Security component version');
47+
throw new RuntimeException('Unsupported Symfony Security component version');
4748
}
4849

4950
$container->addCompilerPass(new ResourceOwnerCompilerPass());

src/OAuth/Exception/HttpTransportException.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
namespace HWI\Bundle\OAuthBundle\OAuth\Exception;
1313

1414
use Symfony\Component\Security\Core\Exception\AuthenticationException;
15+
use Throwable;
1516

1617
final class HttpTransportException extends AuthenticationException
1718
{
1819
private string $ownerName;
1920

20-
public function __construct(string $message, string $ownerName, int $code = 0, ?\Throwable $previous = null)
21+
public function __construct(string $message, string $ownerName, int $code = 0, ?Throwable $previous = null)
2122
{
2223
parent::__construct($message, $code, $previous);
2324
$this->ownerName = $ownerName;

src/OAuth/Exception/StateRetrievalException.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
namespace HWI\Bundle\OAuthBundle\OAuth\Exception;
1313

14-
final class StateRetrievalException extends \InvalidArgumentException
14+
use InvalidArgumentException;
15+
16+
final class StateRetrievalException extends InvalidArgumentException
1517
{
1618
/**
1719
* @param string $key The provided string key
1820
*/
1921
public static function forKey(string $key): self
2022
{
21-
return new static(sprintf('No value found in state for key [%s]', $key));
23+
return new static(\sprintf('No value found in state for key [%s]', $key));
2224
}
2325
}

src/OAuth/RequestDataStorage/SessionStorage.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use HWI\Bundle\OAuthBundle\OAuth\RequestDataStorageInterface;
1515
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
16+
use InvalidArgumentException;
17+
use LogicException;
1618
use Symfony\Component\HttpFoundation\RequestStack;
1719
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1820

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

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

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

7981
/**
@@ -116,6 +118,6 @@ private function getSession(): SessionInterface
116118
return $request->getSession();
117119
}
118120

119-
throw new \LogicException('There is currently no session available.');
121+
throw new LogicException('There is currently no session available.');
120122
}
121123
}

0 commit comments

Comments
 (0)