Skip to content

Commit cf0bf09

Browse files
committed
feature #7 add psalm static analysis (azjezz)
This PR was squashed before being merged into the 0.1-dev branch. Discussion ---------- add psalm static analysis this PR contains 3 commits. 1. setting up psalm 2. fixing all issues in the project 3. adds psalm to github actions ( +sends type coverage to https://shepherd.dev/ ), and type coverage badge in README from 350 errors down to 0, at the strictest level, and 99.8% type coverage 😄 ![image](https://user-images.githubusercontent.com/29315886/110499174-456dca80-80f8-11eb-91f8-7e3d3c78dfc7.png) Commits ------- 467c85d add psalm static analysis
2 parents 80561ef + 467c85d commit cf0bf09

Some content is hidden

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

54 files changed

+613
-226
lines changed

.github/workflows/static-analysis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: "static analysis"
2+
3+
on: ["pull_request", "push"]
4+
5+
jobs:
6+
static-analysis:
7+
name: "static analysis"
8+
runs-on: "ubuntu-latest"
9+
steps:
10+
- name: "checkout"
11+
uses: "actions/checkout@v2"
12+
13+
- name: "installing PHP"
14+
uses: "shivammathur/setup-php@v2"
15+
with:
16+
php-version: "7.4"
17+
ini-values: memory_limit=-1
18+
tools: composer:v2, cs2pr
19+
extensions: intl, bcmath, curl, openssl, mbstring, pdo, pdo_sqlite
20+
21+
- name: "installing dependencies"
22+
run: "composer update --no-interaction --no-progress"
23+
24+
- name: "running static analysis"
25+
run: "vendor/bin/psalm --shepherd --stats"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
![Unit tests status](https://github.com/thephpleague/oauth2-server-bundle/workflows/unit%20tests/badge.svg)
44
![Coding standards status](https://github.com/thephpleague/oauth2-server-bundle/workflows/coding%20standards/badge.svg)
5+
[![Type Coverage](https://shepherd.dev/github/thephpleague/oauth2-server-bundle/coverage.svg)](https://shepherd.dev/github/thephpleague/oauth2-server-bundle)
56
[![Latest Stable Version](https://poser.pugx.org/league/oauth2-server-bundle/v/stable)](https://packagist.org/packages/thephpleague/oauth2-server-bundle)
67

78
OAuth2ServerBundle is a Symfony bundle integrating the [oauth2-server](https://github.com/thephpleague/oauth2-server) library into Symfony applications.

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
"ext-pdo_sqlite": "*",
3232
"laminas/laminas-diactoros": "^2.2",
3333
"nyholm/psr7": "^1.2",
34+
"psalm/plugin-symfony": "^2.2",
3435
"symfony/browser-kit": "^4.4|^5.0",
35-
"symfony/phpunit-bridge": "^5.2"
36+
"symfony/phpunit-bridge": "^5.2",
37+
"vimeo/psalm": "^4.6"
3638
},
3739
"autoload": {
3840
"psr-4": { "League\\Bundle\\OAuth2ServerBundle\\": "src/" }

psalm.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
totallyTyped="true"
4+
resolveFromConfigFile="true"
5+
forbidEcho="true"
6+
strictBinaryOperands="true"
7+
phpVersion="7.1"
8+
allowPhpStormGenerics="true"
9+
allowStringToStandInForClass="true"
10+
rememberPropertyAssignmentsAfterCall="false"
11+
checkForThrowsInGlobalScope="true"
12+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
13+
xmlns="https://getpsalm.org/schema/config"
14+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
15+
>
16+
<projectFiles>
17+
<directory name="src"/>
18+
<ignoreFiles>
19+
<directory name="src/DependencyInjection"/>
20+
<directory name="vendor"/>
21+
</ignoreFiles>
22+
</projectFiles>
23+
24+
<issueHandlers>
25+
<LessSpecificReturnType errorLevel="error"/>
26+
<DeprecatedMethod errorLevel="error"/>
27+
<DeprecatedProperty errorLevel="error"/>
28+
<DeprecatedClass errorLevel="error"/>
29+
<DeprecatedConstant errorLevel="error"/>
30+
<DeprecatedInterface errorLevel="error"/>
31+
<DeprecatedTrait errorLevel="error"/>
32+
<ForbiddenCode errorLevel="error"/>
33+
<InternalMethod errorLevel="error"/>
34+
<InternalProperty errorLevel="error"/>
35+
<InternalClass errorLevel="error"/>
36+
<MissingClosureReturnType errorLevel="error"/>
37+
<MissingReturnType errorLevel="error"/>
38+
<MissingPropertyType errorLevel="error"/>
39+
<InvalidDocblock errorLevel="error"/>
40+
<PropertyNotSetInConstructor errorLevel="error"/>
41+
<MissingConstructor errorLevel="error"/>
42+
<MissingClosureParamType errorLevel="error"/>
43+
<MissingParamType errorLevel="error"/>
44+
<DocblockTypeContradiction errorLevel="error"/>
45+
<RawObjectIteration errorLevel="error"/>
46+
<InvalidStringClass errorLevel="error"/>
47+
<UnresolvableInclude errorLevel="error"/>
48+
</issueHandlers>
49+
50+
<plugins>
51+
<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin"/>
52+
</plugins>
53+
</psalm>

src/Command/ClearExpiredTokensCommand.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@ final class ClearExpiredTokensCommand extends Command
1717
{
1818
protected static $defaultName = 'league:oauth2-server:clear-expired-tokens';
1919

20+
/**
21+
* @var AccessTokenManagerInterface
22+
*/
2023
private $accessTokenManager;
24+
25+
/**
26+
* @var RefreshTokenManagerInterface
27+
*/
2128
private $refreshTokenManager;
29+
30+
/**
31+
* @var AuthorizationCodeManagerInterface
32+
*/
2233
private $authorizationCodeManager;
2334

2435
public function __construct(
@@ -74,15 +85,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7485
return 0;
7586
}
7687

77-
if (true === $clearExpiredAccessTokens) {
88+
if ($clearExpiredAccessTokens) {
7889
$this->clearExpiredAccessTokens($io);
7990
}
8091

81-
if (true === $clearExpiredRefreshTokens) {
92+
if ($clearExpiredRefreshTokens) {
8293
$this->clearExpiredRefreshTokens($io);
8394
}
8495

85-
if (true === $clearExpiredAuthCodes) {
96+
if ($clearExpiredAuthCodes) {
8697
$this->clearExpiredAuthCodes($io);
8798
}
8899

src/Command/CreateClientCommand.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ final class CreateClientCommand extends Command
2020
{
2121
protected static $defaultName = 'league:oauth2-server:create-client';
2222

23+
/**
24+
* @var ClientManagerInterface
25+
*/
2326
private $clientManager;
2427

2528
public function __construct(ClientManagerInterface $clientManager)
@@ -105,38 +108,40 @@ protected function execute(InputInterface $input, OutputInterface $output): int
105108

106109
private function buildClientFromInput(InputInterface $input): Client
107110
{
111+
/** @var string $identifier */
108112
$identifier = $input->getArgument('identifier') ?? hash('md5', random_bytes(16));
109113

114+
/** @var bool $isPublic */
110115
$isPublic = $input->getOption('public');
111116

112-
if (null !== $input->getArgument('secret') && $isPublic) {
117+
if ($isPublic && null !== $input->getArgument('secret')) {
113118
throw new \InvalidArgumentException('The client cannot have a secret and be public.');
114119
}
115120

121+
/** @var string $secret */
116122
$secret = $isPublic ? null : $input->getArgument('secret') ?? hash('sha512', random_bytes(32));
117123

118124
$client = new Client($identifier, $secret);
119125
$client->setActive(true);
120126
$client->setAllowPlainTextPkce($input->getOption('allow-plain-text-pkce'));
121127

122-
$redirectUris = array_map(
123-
static function (string $redirectUri): RedirectUri { return new RedirectUri($redirectUri); },
124-
$input->getOption('redirect-uri')
125-
);
126-
$client->setRedirectUris(...$redirectUris);
127-
128-
$grants = array_map(
129-
static function (string $grant): Grant { return new Grant($grant); },
130-
$input->getOption('grant-type')
131-
);
132-
$client->setGrants(...$grants);
133-
134-
$scopes = array_map(
135-
static function (string $scope): Scope { return new Scope($scope); },
136-
$input->getOption('scope')
137-
);
138-
$client->setScopes(...$scopes);
139-
140-
return $client;
128+
/** @var list<string> $redirectUriStrings */
129+
$redirectUriStrings = $input->getOption('redirect-uri');
130+
/** @var list<string> $grantStrings */
131+
$grantStrings = $input->getOption('grant-type');
132+
/** @var list<string> $scopeStrings */
133+
$scopeStrings = $input->getOption('scope');
134+
135+
return $client
136+
->setRedirectUris(...array_map(static function (string $redirectUri): RedirectUri {
137+
return new RedirectUri($redirectUri);
138+
}, $redirectUriStrings))
139+
->setGrants(...array_map(static function (string $grant): Grant {
140+
return new Grant($grant);
141+
}, $grantStrings))
142+
->setScopes(...array_map(static function (string $scope): Scope {
143+
return new Scope($scope);
144+
}, $scopeStrings))
145+
;
141146
}
142147
}

src/Command/DeleteClientCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ final class DeleteClientCommand extends Command
1515
{
1616
protected static $defaultName = 'league:oauth2-server:delete-client';
1717

18+
/**
19+
* @var ClientManagerInterface
20+
*/
1821
private $clientManager;
1922

2023
public function __construct(ClientManagerInterface $clientManager)

src/Command/ListClientsCommand.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ final class ListClientsCommand extends Command
2222

2323
protected static $defaultName = 'league:oauth2-server:list-clients';
2424

25+
/**
26+
* @var ClientManagerInterface
27+
*/
2528
private $clientManager;
2629

2730
public function __construct(ClientManagerInterface $clientManager)
@@ -77,18 +80,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7780

7881
private function getFindByCriteria(InputInterface $input): ClientFilter
7982
{
83+
/** @var list<string> $grantStrings */
84+
$grantStrings = $input->getOption('grant-type');
85+
/** @var list<string> $redirectUriStrings */
86+
$redirectUriStrings = $input->getOption('redirect-uri');
87+
/** @var list<string> $scopeStrings */
88+
$scopeStrings = $input->getOption('scope');
89+
8090
return
8191
ClientFilter
8292
::create()
8393
->addGrantCriteria(...array_map(static function (string $grant): Grant {
8494
return new Grant($grant);
85-
}, $input->getOption('grant-type')))
95+
}, $grantStrings))
8696
->addRedirectUriCriteria(...array_map(static function (string $redirectUri): RedirectUri {
8797
return new RedirectUri($redirectUri);
88-
}, $input->getOption('redirect-uri')))
98+
}, $redirectUriStrings))
8999
->addScopeCriteria(...array_map(static function (string $scope): Scope {
90100
return new Scope($scope);
91-
}, $input->getOption('scope')))
101+
}, $scopeStrings))
92102
;
93103
}
94104

src/Command/UpdateClientCommand.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ final class UpdateClientCommand extends Command
2020
{
2121
protected static $defaultName = 'league:oauth2-server:update-client';
2222

23+
/**
24+
* @var ClientManagerInterface
25+
*/
2326
private $clientManager;
2427

2528
public function __construct(ClientManagerInterface $clientManager)
@@ -89,24 +92,23 @@ private function updateClientFromInput(Client $client, InputInterface $input): C
8992
{
9093
$client->setActive(!$input->getOption('deactivated'));
9194

92-
$redirectUris = array_map(
93-
static function (string $redirectUri): RedirectUri { return new RedirectUri($redirectUri); },
94-
$input->getOption('redirect-uri')
95-
);
96-
$client->setRedirectUris(...$redirectUris);
97-
98-
$grants = array_map(
99-
static function (string $grant): Grant { return new Grant($grant); },
100-
$input->getOption('grant-type')
101-
);
102-
$client->setGrants(...$grants);
103-
104-
$scopes = array_map(
105-
static function (string $scope): Scope { return new Scope($scope); },
106-
$input->getOption('scope')
107-
);
108-
$client->setScopes(...$scopes);
109-
110-
return $client;
95+
/** @var list<string> $redirectUriStrings */
96+
$redirectUriStrings = $input->getOption('redirect-uri');
97+
/** @var list<string> $grantStrings */
98+
$grantStrings = $input->getOption('grant-type');
99+
/** @var list<string> $scopeStrings */
100+
$scopeStrings = $input->getOption('scope');
101+
102+
return $client
103+
->setRedirectUris(...array_map(static function (string $redirectUri): RedirectUri {
104+
return new RedirectUri($redirectUri);
105+
}, $redirectUriStrings))
106+
->setGrants(...array_map(static function (string $grant): Grant {
107+
return new Grant($grant);
108+
}, $grantStrings))
109+
->setScopes(...array_map(static function (string $scope): Scope {
110+
return new Scope($scope);
111+
}, $scopeStrings))
112+
;
111113
}
112114
}

src/Controller/AuthorizationController.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use League\Bundle\OAuth2ServerBundle\Event\AuthorizationRequestResolveEvent;
99
use League\Bundle\OAuth2ServerBundle\Event\AuthorizationRequestResolveEventFactory;
1010
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
11+
use League\Bundle\OAuth2ServerBundle\Model\Client;
1112
use League\Bundle\OAuth2ServerBundle\OAuth2Events;
1213
use League\OAuth2\Server\AuthorizationServer;
1314
use League\OAuth2\Server\Exception\OAuthServerException;
@@ -18,10 +19,29 @@
1819

1920
final class AuthorizationController
2021
{
22+
/**
23+
* @var AuthorizationServer
24+
*/
2125
private $server;
26+
27+
/**
28+
* @var EventDispatcherInterface
29+
*/
2230
private $eventDispatcher;
31+
32+
/**
33+
* @var AuthorizationRequestResolveEventFactory
34+
*/
2335
private $eventFactory;
36+
37+
/**
38+
* @var UserConverterInterface
39+
*/
2440
private $userConverter;
41+
42+
/**
43+
* @var ClientManagerInterface
44+
*/
2545
private $clientManager;
2646

2747
public function __construct(
@@ -46,6 +66,7 @@ public function indexAction(ServerRequestInterface $serverRequest, ResponseFacto
4666
$authRequest = $this->server->validateAuthorizationRequest($serverRequest);
4767

4868
if ('plain' === $authRequest->getCodeChallengeMethod()) {
69+
/** @var Client $client */
4970
$client = $this->clientManager->find($authRequest->getClient()->getIdentifier());
5071
if (!$client->isPlainTextPkceAllowed()) {
5172
return OAuthServerException::invalidRequest(
@@ -64,6 +85,7 @@ public function indexAction(ServerRequestInterface $serverRequest, ResponseFacto
6485
$authRequest->setUser($this->userConverter->toLeague($event->getUser()));
6586

6687
if ($event->hasResponse()) {
88+
/** @var ResponseInterface */
6789
return $event->getResponse();
6890
}
6991

0 commit comments

Comments
 (0)