Skip to content

Commit 48bc50c

Browse files
committed
Switch to php-cs-fixer, apply Symfony style
1 parent 62626be commit 48bc50c

10 files changed

+113
-68
lines changed

.github/workflows/check.yml

+6-4
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,21 @@ jobs:
4040
- name: Composer install
4141
run: composer install
4242

43-
- name: PHP CodeSniffer
44-
run: vendor/bin/phpcs --standard=PSR2 --report=checkstyle src/ | cs2pr
43+
- name: PHP Coding Standards Fixer
44+
# Set to the latest version supported by php-cs-fixer.
45+
if: matrix.php-versions == '8.1'
46+
run: composer run php-cs-fixer -- --dry-run --format=checkstyle | cs2pr
4547

4648
- name: PHPStan
47-
run: vendor/bin/phpstan analyse -l max -c phpstan.neon --error-format=checkstyle src/ | cs2pr
49+
run: composer run phpstan -- --error-format=checkstyle | cs2pr
4850

4951
- name: Configure PHPUnit matchers
5052
uses: mheap/phpunit-matcher-action@v1
5153

5254
- name: PHPUnit
5355
# TODO: Waiting for a compatible Prophecy release.
5456
if: matrix.php-versions != '8.2'
55-
run: vendor/bin/phpunit --teamcity
57+
run: composer run phpunit -- --teamcity
5658

5759
- name: Set up Go
5860
uses: actions/setup-go@v3

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor
22
composer.lock
3+
.php-cs-fixer.cache
34
.phpunit*

.php-cs-fixer.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$config = new PhpCsFixer\Config();
4+
5+
return $config
6+
->setRules([
7+
'@Symfony' => true,
8+
'@PHP74Migration' => true,
9+
])
10+
->setFinder(
11+
PhpCsFixer\Finder::create()
12+
->in(__DIR__ . '/src')
13+
->in(__DIR__ . '/tests')
14+
);

composer.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"email": "stephan@kochen.nl"
1010
}
1111
],
12+
"scripts": {
13+
"php-cs-fixer": "php-cs-fixer fix",
14+
"phpstan": "phpstan analyse -l max -c phpstan.neon src/",
15+
"phpunit": "phpunit"
16+
},
1217
"autoload": {
1318
"psr-4": {
1419
"Portier\\Client\\": "src/"
@@ -23,7 +28,7 @@
2328
"require-dev": {
2429
"phpunit/phpunit": "^9.5",
2530
"phpstan/phpstan": "^1.2.0",
26-
"squizlabs/php_codesniffer": "^3.6",
27-
"phpspec/prophecy-phpunit": "^2.0"
31+
"phpspec/prophecy-phpunit": "^2.0",
32+
"friendsofphp/php-cs-fixer": "^3.14"
2833
}
2934
}

src/AbstractStore.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,41 @@ abstract class AbstractStore implements StoreInterface
1111
{
1212
/**
1313
* The Guzzle instance to use.
14+
*
1415
* @var \GuzzleHttp\Client
1516
*/
1617
public $guzzle;
1718

1819
/**
1920
* Lifespan of a nonce.
21+
*
2022
* @var float
2123
*/
2224
public $nonceTtl = 15 * 60;
2325

2426
/**
25-
* Minimum time to cache a HTTP response
27+
* Minimum time to cache a HTTP response.
28+
*
2629
* @var float
2730
*/
2831
public $cacheMinTtl = 60 * 60;
2932

3033
/**
31-
* Constructor
34+
* Constructor.
3235
*/
3336
public function __construct()
3437
{
3538
$this->guzzle = new \GuzzleHttp\Client([
36-
'timeout' => 10
39+
'timeout' => 10,
3740
]);
3841
}
3942

4043
/**
4144
* Generate a nonce value.
42-
* @param string $email Optional email context
43-
* @return string The generated nonce.
45+
*
46+
* @param string $email Optional email context
47+
*
48+
* @return string the generated nonce
4449
*/
4550
public function generateNonce(string $email): string
4651
{
@@ -49,8 +54,10 @@ public function generateNonce(string $email): string
4954

5055
/**
5156
* Fetch a URL using HTTP GET.
52-
* @param string $url The URL to fetch.
53-
* @return \stdClass An object with `ttl` and `data` properties.
57+
*
58+
* @param string $url the URL to fetch
59+
*
60+
* @return \stdClass an object with `ttl` and `data` properties
5461
*/
5562
public function fetch(string $url): \stdClass
5663
{

src/Client.php

+36-26
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Portier\Client;
44

55
use Lcobucci\JWT\Configuration as JwtConfig;
6-
use Lcobucci\JWT\Validation\Constraint as JwtConstraint;
76
use Lcobucci\JWT\Signer as JwtSigner;
7+
use Lcobucci\JWT\Validation\Constraint as JwtConstraint;
88

99
/**
1010
* Client for a Portier broker.
@@ -13,6 +13,7 @@ class Client
1313
{
1414
/**
1515
* Default Portier broker origin.
16+
*
1617
* @var string
1718
*/
1819
public const DEFAULT_BROKER = 'https://broker.portier.io';
@@ -25,20 +26,23 @@ class Client
2526

2627
/**
2728
* The origin of the Portier broker.
29+
*
2830
* @var string
2931
*/
3032
public $broker = self::DEFAULT_BROKER;
3133

3234
/**
3335
* The number of seconds of clock drift to allow.
36+
*
3437
* @var int
3538
*/
3639
public $leeway = 3 * 60;
3740

3841
/**
39-
* Constructor
40-
* @param StoreInterface $store Store implementation to use.
41-
* @param string $redirectUri URL that Portier will redirect to.
42+
* Constructor.
43+
*
44+
* @param StoreInterface $store store implementation to use
45+
* @param string $redirectUri URL that Portier will redirect to
4246
*/
4347
public function __construct(StoreInterface $store, string $redirectUri)
4448
{
@@ -62,7 +66,7 @@ public static function normalize(string $email): string
6266
assert(defined('MB_CASE_FOLD') && function_exists('idn_to_ascii'));
6367

6468
$localEnd = strrpos($email, '@');
65-
if ($localEnd === false) {
69+
if (false === $localEnd) {
6670
return '';
6771
}
6872

@@ -79,8 +83,8 @@ public static function normalize(string $email): string
7983
IDNA_USE_STD3_RULES | IDNA_CHECK_BIDI,
8084
INTL_IDNA_VARIANT_UTS46
8185
);
82-
if (empty($host) || $host[0] === '[' ||
83-
filter_var($host, FILTER_VALIDATE_IP) !== false) {
86+
if (empty($host) || '[' === $host[0] ||
87+
false !== filter_var($host, FILTER_VALIDATE_IP)) {
8488
return '';
8589
}
8690

@@ -89,8 +93,10 @@ public static function normalize(string $email): string
8993

9094
/**
9195
* Start authentication of an email address.
92-
* @param string $email Email address to authenticate.
93-
* @return string URL to redirect the browser to.
96+
*
97+
* @param string $email email address to authenticate
98+
*
99+
* @return string URL to redirect the browser to
94100
*/
95101
public function authenticate(string $email): string
96102
{
@@ -109,13 +115,16 @@ public function authenticate(string $email): string
109115
'client_id' => $this->clientId,
110116
'redirect_uri' => $this->redirectUri,
111117
]);
112-
return $authEndpoint . '?' . $query;
118+
119+
return $authEndpoint.'?'.$query;
113120
}
114121

115122
/**
116123
* Verify a token received on our `redirect_uri`.
117-
* @param string $token The received `id_token` parameter value.
118-
* @return string The verified email address.
124+
*
125+
* @param string $token the received `id_token` parameter value
126+
*
127+
* @return string the verified email address
119128
*/
120129
public function verify(string $token): string
121130
{
@@ -145,20 +154,20 @@ public function verify(string $token): string
145154
$publicKey = null;
146155
foreach ($keysDoc->keys as $key) {
147156
if ($key instanceof \stdClass &&
148-
isset($key->alg) && $key->alg === 'RS256' &&
157+
isset($key->alg) && 'RS256' === $key->alg &&
149158
isset($key->kid) && $key->kid === $kid &&
150159
isset($key->n) && isset($key->e)) {
151160
$publicKey = self::parseJwk($key);
152161
break;
153162
}
154163
}
155-
if ($publicKey === null) {
164+
if (null === $publicKey) {
156165
throw new \Exception('Cannot find the public key used to sign the token');
157166
}
158167

159168
// Validate the token claims.
160169
$clock = \Lcobucci\Clock\SystemClock::fromUTC();
161-
$leeway = new \DateInterval('PT' . $this->leeway . 'S');
170+
$leeway = new \DateInterval('PT'.$this->leeway.'S');
162171
$constraints = [
163172
new JwtConstraint\SignedWith(new JwtSigner\Rsa\Sha256(), $publicKey),
164173
new JwtConstraint\IssuedBy($this->broker),
@@ -201,7 +210,8 @@ public function verify(string $token): string
201210
*/
202211
private function fetchDiscovery(): \stdClass
203212
{
204-
$discoveryUrl = $this->broker . '/.well-known/openid-configuration';
213+
$discoveryUrl = $this->broker.'/.well-known/openid-configuration';
214+
205215
return $this->store->fetchCached('discovery', $discoveryUrl);
206216
}
207217

@@ -221,19 +231,19 @@ private static function parseJwk(\stdClass $jwk): JwtSigner\Key
221231
$encoded = base64_encode($pkey->getBinary());
222232

223233
return JwtSigner\Key\InMemory::plainText(
224-
"-----BEGIN PUBLIC KEY-----\n" .
225-
chunk_split($encoded, 64, "\n") .
234+
"-----BEGIN PUBLIC KEY-----\n".
235+
chunk_split($encoded, 64, "\n").
226236
"-----END PUBLIC KEY-----\n"
227237
);
228238
}
229239

230240
/**
231-
* Get the origin for a URL
241+
* Get the origin for a URL.
232242
*/
233243
private static function getOrigin(string $url): string
234244
{
235245
$components = parse_url($url);
236-
if ($components === false) {
246+
if (false === $components) {
237247
throw new \Exception('Could not parse the redirect URI');
238248
}
239249

@@ -247,12 +257,12 @@ private static function getOrigin(string $url): string
247257
}
248258
$host = $components['host'];
249259

250-
$res = $scheme . '://' . $host;
260+
$res = $scheme.'://'.$host;
251261
if (isset($components['port'])) {
252262
$port = $components['port'];
253-
if (($scheme === 'http' && $port !== 80) ||
254-
($scheme === 'https' && $port !== 443)) {
255-
$res .= ':' . $port;
263+
if (('http' === $scheme && 80 !== $port) ||
264+
('https' === $scheme && 443 !== $port)) {
265+
$res .= ':'.$port;
256266
}
257267
}
258268

@@ -262,8 +272,8 @@ private static function getOrigin(string $url): string
262272
private static function decodeBase64Url(string $input): string
263273
{
264274
$output = base64_decode(strtr($input, '-_', '+/'), true);
265-
if ($output === false) {
266-
throw new \Exception("Invalid base64");
275+
if (false === $output) {
276+
throw new \Exception('Invalid base64');
267277
}
268278

269279
return $output;

src/MemoryStore.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MemoryStore extends AbstractStore
1616
private $nonces;
1717

1818
/**
19-
* Constructor
19+
* Constructor.
2020
*/
2121
public function __construct()
2222
{
@@ -32,7 +32,7 @@ public function __construct()
3232
public function fetchCached(string $cacheId, string $url): \stdClass
3333
{
3434
$item = $this->cache[$cacheId] ?? null;
35-
if ($item !== null && time() < $item->expires) {
35+
if (null !== $item && time() < $item->expires) {
3636
return $item->data;
3737
}
3838

@@ -67,7 +67,7 @@ public function createNonce(string $email): string
6767
public function consumeNonce(string $nonce, string $email): void
6868
{
6969
$item = $this->nonces[$nonce] ?? null;
70-
if ($item !== null) {
70+
if (null !== $item) {
7171
unset($this->nonces[$nonce]);
7272

7373
if ($item->email === $email && time() < $item->expires) {

src/RedisStore.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ class RedisStore extends AbstractStore
1010
public \Redis $redis;
1111

1212
/**
13-
* Constructor
14-
* @param \Redis $redis The Redis instance to use.
13+
* Constructor.
14+
*
15+
* @param \Redis $redis the Redis instance to use
1516
*/
1617
public function __construct(\Redis $redis)
1718
{
@@ -25,7 +26,7 @@ public function __construct(\Redis $redis)
2526
*/
2627
public function fetchCached(string $cacheId, string $url): \stdClass
2728
{
28-
$key = 'cache:' . $cacheId;
29+
$key = 'cache:'.$cacheId;
2930

3031
$data = $this->redis->get($key);
3132
if ($data) {
@@ -38,7 +39,7 @@ public function fetchCached(string $cacheId, string $url): \stdClass
3839
$res = $this->fetch($url);
3940

4041
$encoded = json_encode($res->data);
41-
if ($encoded === false) {
42+
if (false === $encoded) {
4243
throw new \Exception('JSON encoding failed');
4344
}
4445

@@ -54,7 +55,7 @@ public function createNonce(string $email): string
5455
{
5556
$nonce = $this->generateNonce($email);
5657

57-
$key = 'nonce:' . $nonce;
58+
$key = 'nonce:'.$nonce;
5859
$this->redis->setex($key, (int) $this->nonceTtl, $email);
5960

6061
return $nonce;
@@ -65,7 +66,7 @@ public function createNonce(string $email): string
6566
*/
6667
public function consumeNonce(string $nonce, string $email): void
6768
{
68-
$key = 'nonce:' . $nonce;
69+
$key = 'nonce:'.$nonce;
6970
$res = $this->redis->multi()
7071
->get($key)
7172
->del($key)

0 commit comments

Comments
 (0)