Skip to content

Commit 0f6f6bb

Browse files
authored
Raise PHPStan's level from 3 to 4 (#535)
1 parent c72ce9a commit 0f6f6bb

File tree

6 files changed

+62
-58
lines changed

6 files changed

+62
-58
lines changed

Form/Model/Authorize.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ class Authorize
5252
* @param bool $accepted
5353
* @param array $query
5454
*/
55-
public function __construct($accepted, array $query = [])
55+
public function __construct(bool $accepted, array $query = [])
5656
{
5757
foreach ($query as $key => $value) {
5858
$this->{$key} = $value;
5959
}
6060

61-
$this->accepted = (bool) $accepted;
61+
$this->accepted = $accepted;
6262
}
6363
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ci: cs-full-check phpstan phpunit-coverage
66
lint: cs-full-check phpstan
77

88
phpstan:
9-
sh -c "${QA_DOCKER_COMMAND} phpstan analyse --configuration phpstan.neon --level 3 ."
9+
sh -c "${QA_DOCKER_COMMAND} phpstan analyse --configuration phpstan.neon --level 4 ."
1010

1111
cs:
1212
sh -c "${QA_DOCKER_COMMAND} php-cs-fixer fix -vvv --diff"

Security/Authentication/Provider/OAuthProvider.php

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -76,58 +76,57 @@ public function authenticate(TokenInterface $token)
7676
// TODO: this is nasty, create a proper interface here
7777
/** @var OAuthToken&TokenInterface&\OAuth2\Model\IOAuth2AccessToken $accessToken */
7878
$accessToken = $this->serverService->verifyAccessToken($tokenString);
79-
if (null !== $accessToken) {
80-
$scope = $accessToken->getScope();
81-
$user = $accessToken->getUser();
82-
83-
if (null !== $user) {
84-
try {
85-
$this->userChecker->checkPreAuth($user);
86-
} catch (AccountStatusException $e) {
87-
throw new OAuth2AuthenticateException(
88-
OAuth2::HTTP_UNAUTHORIZED,
89-
OAuth2::TOKEN_TYPE_BEARER,
90-
$this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM),
91-
'access_denied',
92-
$e->getMessage()
93-
);
94-
}
95-
96-
$token->setUser($user);
79+
80+
$scope = $accessToken->getScope();
81+
$user = $accessToken->getUser();
82+
83+
if (null !== $user) {
84+
try {
85+
$this->userChecker->checkPreAuth($user);
86+
} catch (AccountStatusException $e) {
87+
throw new OAuth2AuthenticateException(
88+
OAuth2::HTTP_UNAUTHORIZED,
89+
OAuth2::TOKEN_TYPE_BEARER,
90+
$this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM),
91+
'access_denied',
92+
$e->getMessage()
93+
);
9794
}
9895

99-
$roles = (null !== $user) ? $user->getRoles() : [];
96+
$token->setUser($user);
97+
}
98+
99+
$roles = (null !== $user) ? $user->getRoles() : [];
100100

101-
if (!empty($scope)) {
102-
foreach (explode(' ', $scope) as $role) {
103-
$roles[] = 'ROLE_'.mb_strtoupper($role);
104-
}
101+
if (!empty($scope)) {
102+
foreach (explode(' ', $scope) as $role) {
103+
$roles[] = 'ROLE_'.mb_strtoupper($role);
105104
}
105+
}
106106

107-
$roles = array_unique($roles, SORT_REGULAR);
108-
109-
$token = new OAuthToken($roles);
110-
$token->setAuthenticated(true);
111-
$token->setToken($tokenString);
112-
113-
if (null !== $user) {
114-
try {
115-
$this->userChecker->checkPostAuth($user);
116-
} catch (AccountStatusException $e) {
117-
throw new OAuth2AuthenticateException(
118-
OAuth2::HTTP_UNAUTHORIZED,
119-
OAuth2::TOKEN_TYPE_BEARER,
120-
$this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM),
121-
'access_denied',
122-
$e->getMessage()
123-
);
124-
}
125-
126-
$token->setUser($user);
107+
$roles = array_unique($roles, SORT_REGULAR);
108+
109+
$token = new OAuthToken($roles);
110+
$token->setAuthenticated(true);
111+
$token->setToken($tokenString);
112+
113+
if (null !== $user) {
114+
try {
115+
$this->userChecker->checkPostAuth($user);
116+
} catch (AccountStatusException $e) {
117+
throw new OAuth2AuthenticateException(
118+
OAuth2::HTTP_UNAUTHORIZED,
119+
OAuth2::TOKEN_TYPE_BEARER,
120+
$this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM),
121+
'access_denied',
122+
$e->getMessage()
123+
);
127124
}
128125

129-
return $token;
126+
$token->setUser($user);
130127
}
128+
129+
return $token;
131130
} catch (OAuth2ServerException $e) {
132131
throw new AuthenticationException('OAuth2 authentication failed', 0, $e);
133132
}

Storage/OAuthStorage.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,11 @@ public function checkUserCredentials(IOAuth2Client $client, $username, $password
164164
return false;
165165
}
166166

167-
if (null !== $user) {
168-
$encoder = $this->encoderFactory->getEncoder($user);
169-
170-
if ($encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
171-
return [
172-
'data' => $user,
173-
];
174-
}
167+
$encoder = $this->encoderFactory->getEncoder($user);
168+
if ($encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
169+
return [
170+
'data' => $user,
171+
];
175172
}
176173

177174
return false;

Tests/Form/Type/AuthorizeFormTypeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function setUp()
4444

4545
public function testSubmit()
4646
{
47-
$accepted = 'true';
47+
$accepted = true;
4848
$formData = [
4949
'client_id' => '1',
5050
'response_type' => 'code',
@@ -61,7 +61,7 @@ public function testSubmit()
6161

6262
$this->assertTrue($form->isSynchronized());
6363
$this->assertSame($authorize, $form->getData());
64-
$this->assertSame((bool) $accepted, $authorize->accepted);
64+
$this->assertSame($accepted, $authorize->accepted);
6565

6666
$view = $form->createView();
6767
$children = $view->children;

Tests/Storage/OAuthStorageTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use FOS\OAuthServerBundle\Model\Client;
1919
use FOS\OAuthServerBundle\Model\RefreshToken;
2020
use FOS\OAuthServerBundle\Storage\OAuthStorage;
21+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
2122
use Symfony\Component\Security\Core\User\UserInterface;
2223

2324
class OAuthStorageTest extends \PHPUnit\Framework\TestCase
@@ -339,6 +340,13 @@ public function testCheckUserCredentialsCatchesAuthenticationExceptions()
339340
{
340341
$client = new Client();
341342

343+
$this->userProvider
344+
->expects(self::once())
345+
->method('loadUserByUsername')
346+
->with('Joe')
347+
->willThrowException(new AuthenticationException('No such user'))
348+
;
349+
342350
$result = $this->storage->checkUserCredentials($client, 'Joe', 'baz');
343351

344352
$this->assertFalse($result);
@@ -427,7 +435,7 @@ public function testCheckUserCredentialsReturnsFalseIfUserNotExist()
427435
$this->userProvider->expects($this->once())
428436
->method('loadUserByUsername')
429437
->with('Joe')
430-
->will($this->returnValue(null))
438+
->willThrowException(new AuthenticationException('No such user'))
431439
;
432440

433441
$this->assertFalse($this->storage->checkUserCredentials($client, 'Joe', 'baz'));

0 commit comments

Comments
 (0)