Skip to content

Commit 6a6025b

Browse files
authored
chore: add cs config, fix git-attributes, removed unused entrypoint.sh (#424)
1 parent 0ac5041 commit 6a6025b

File tree

9 files changed

+79
-62
lines changed

9 files changed

+79
-62
lines changed

.gitattributes

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
* text=auto
22

3-
/tests export-ignore
43
/.gitattributes export-ignore
54
/.gitignore export-ignore
6-
/.travis.yml export-ignore
7-
/phpunit.xml.dist export-ignore
85
/.github export-ignore
6+
/.php-cs-fixer.dist.php export-ignore
7+
/phpstan.neon.dist export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/tests export-ignore

.github/actions/entrypoint.sh

-21
This file was deleted.

.github/workflows/tests.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ jobs:
3838
php-version: "8.0"
3939
- name: Run Script
4040
run: |
41-
composer require friendsofphp/php-cs-fixer
42-
vendor/bin/php-cs-fixer fix --diff --dry-run .
43-
vendor/bin/php-cs-fixer fix --rules=native_function_invocation --allow-risky=yes --diff src
41+
composer global require friendsofphp/php-cs-fixer
42+
~/.composer/vendor/bin/php-cs-fixer fix --diff --dry-run --allow-risky=yes .
4443
4544
staticanalysis:
4645
runs-on: ubuntu-latest

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ phpunit.phar.asc
44
composer.phar
55
composer.lock
66
.phpunit.result.cache
7+
.php-cs-fixer.cache

.php-cs-fixer.dist.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
return (new PhpCsFixer\Config())
4+
->setRules([
5+
'@PSR2' => true,
6+
'concat_space' => ['spacing' => 'one'],
7+
'no_unused_imports' => true,
8+
'ordered_imports' => true,
9+
'new_with_braces' => true,
10+
'method_argument_space' => false,
11+
'whitespace_after_comma_in_array' => true,
12+
'return_type_declaration' => [
13+
'space_before' => 'none'
14+
],
15+
'single_quote' => true,
16+
'native_function_invocation' => [
17+
'strict' => false
18+
],
19+
])
20+
->setFinder(
21+
PhpCsFixer\Finder::create()
22+
->in(__DIR__)
23+
)
24+
;

src/JWT.php

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
namespace Firebase\JWT;
44

5-
use ArrayAccess;
5+
use DateTime;
66
use DomainException;
77
use Exception;
88
use InvalidArgumentException;
99
use OpenSSLAsymmetricKey;
1010
use OpenSSLCertificate;
11+
use stdClass;
1112
use TypeError;
1213
use UnexpectedValueException;
13-
use DateTime;
14-
use stdClass;
1514

1615
/**
1716
* JSON Web Token implementation, based on this spec:
@@ -111,7 +110,7 @@ public static function decode(
111110
if (null === ($payload = static::jsonDecode($payloadRaw))) {
112111
throw new UnexpectedValueException('Invalid claims encoding');
113112
}
114-
if (is_array($payload)) {
113+
if (\is_array($payload)) {
115114
// prevent PHP Fatal Error in edge-cases when payload is empty array
116115
$payload = (object) $payload;
117116
}
@@ -229,7 +228,7 @@ public static function sign(
229228
list($function, $algorithm) = static::$supported_algs[$alg];
230229
switch ($function) {
231230
case 'hash_hmac':
232-
if (!is_string($key)) {
231+
if (!\is_string($key)) {
233232
throw new InvalidArgumentException('key must be a string when using hmac');
234233
}
235234
return \hash_hmac($algorithm, $msg, $key, true);
@@ -246,10 +245,10 @@ public static function sign(
246245
}
247246
return $signature;
248247
case 'sodium_crypto':
249-
if (!function_exists('sodium_crypto_sign_detached')) {
248+
if (!\function_exists('sodium_crypto_sign_detached')) {
250249
throw new DomainException('libsodium is not available');
251250
}
252-
if (!is_string($key)) {
251+
if (!\is_string($key)) {
253252
throw new InvalidArgumentException('key must be a string when using EdDSA');
254253
}
255254
try {
@@ -302,10 +301,10 @@ private static function verify(
302301
'OpenSSL error: ' . \openssl_error_string()
303302
);
304303
case 'sodium_crypto':
305-
if (!function_exists('sodium_crypto_sign_verify_detached')) {
304+
if (!\function_exists('sodium_crypto_sign_verify_detached')) {
306305
throw new DomainException('libsodium is not available');
307306
}
308-
if (!is_string($keyMaterial)) {
307+
if (!\is_string($keyMaterial)) {
309308
throw new InvalidArgumentException('key must be a string when using EdDSA');
310309
}
311310
try {
@@ -318,7 +317,7 @@ private static function verify(
318317
}
319318
case 'hash_hmac':
320319
default:
321-
if (!is_string($keyMaterial)) {
320+
if (!\is_string($keyMaterial)) {
322321
throw new InvalidArgumentException('key must be a string when using hmac');
323322
}
324323
$hash = \hash_hmac($algorithm, $msg, $keyMaterial, true);

src/Key.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Firebase\JWT;
44

5+
use InvalidArgumentException;
56
use OpenSSLAsymmetricKey;
67
use OpenSSLCertificate;
78
use TypeError;
8-
use InvalidArgumentException;
99

1010
class Key
1111
{
@@ -23,10 +23,10 @@ public function __construct(
2323
string $algorithm
2424
) {
2525
if (
26-
!is_string($keyMaterial)
26+
!\is_string($keyMaterial)
2727
&& !$keyMaterial instanceof OpenSSLAsymmetricKey
2828
&& !$keyMaterial instanceof OpenSSLCertificate
29-
&& !is_resource($keyMaterial)
29+
&& !\is_resource($keyMaterial)
3030
) {
3131
throw new TypeError('Key material must be a string, resource, or OpenSSLAsymmetricKey');
3232
}

tests/JWKTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Firebase\JWT;
44

5-
use PHPUnit\Framework\TestCase;
65
use InvalidArgumentException;
6+
use PHPUnit\Framework\TestCase;
77
use UnexpectedValueException;
88

99
class JWKTest extends TestCase
@@ -69,7 +69,7 @@ public function testParseKeyWithEmptyDValue()
6969
$jwkSet['keys'][0]['d'] = null;
7070

7171
$keys = JWK::parseKeySet($jwkSet);
72-
$this->assertTrue(is_array($keys));
72+
$this->assertTrue(\is_array($keys));
7373
}
7474

7575
public function testParseJwkKeySet()
@@ -79,7 +79,7 @@ public function testParseJwkKeySet()
7979
true
8080
);
8181
$keys = JWK::parseKeySet($jwkSet);
82-
$this->assertTrue(is_array($keys));
82+
$this->assertTrue(\is_array($keys));
8383
$this->assertArrayHasKey('jwk1', $keys);
8484
self::$keys = $keys;
8585
}

tests/JWTTest.php

+33-19
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace Firebase\JWT;
44

55
use ArrayObject;
6-
use PHPUnit\Framework\TestCase;
76
use DomainException;
87
use InvalidArgumentException;
8+
use PHPUnit\Framework\TestCase;
9+
use stdClass;
910
use TypeError;
1011
use UnexpectedValueException;
11-
use stdClass;
1212

1313
class JWTTest extends TestCase
1414
{
@@ -37,7 +37,8 @@ public function testExpiredToken()
3737
$this->expectException(ExpiredException::class);
3838
$payload = [
3939
'message' => 'abc',
40-
'exp' => time() - 20]; // time in the past
40+
'exp' => time() - 20, // time in the past
41+
];
4142
$encoded = JWT::encode($payload, 'my_key', 'HS256');
4243
JWT::decode($encoded, new Key('my_key', 'HS256'));
4344
}
@@ -47,7 +48,8 @@ public function testBeforeValidTokenWithNbf()
4748
$this->expectException(BeforeValidException::class);
4849
$payload = [
4950
'message' => 'abc',
50-
"nbf" => time() + 20]; // time in the future
51+
'nbf' => time() + 20, // time in the future
52+
];
5153
$encoded = JWT::encode($payload, 'my_key', 'HS256');
5254
JWT::decode($encoded, new Key('my_key', 'HS256'));
5355
}
@@ -57,7 +59,8 @@ public function testBeforeValidTokenWithIat()
5759
$this->expectException(BeforeValidException::class);
5860
$payload = [
5961
'message' => 'abc',
60-
"iat" => time() + 20]; // time in the future
62+
'iat' => time() + 20, // time in the future
63+
];
6164
$encoded = JWT::encode($payload, 'my_key', 'HS256');
6265
JWT::decode($encoded, new Key('my_key', 'HS256'));
6366
}
@@ -66,7 +69,8 @@ public function testValidToken()
6669
{
6770
$payload = [
6871
'message' => 'abc',
69-
'exp' => time() + JWT::$leeway + 20]; // time in the future
72+
'exp' => time() + JWT::$leeway + 20, // time in the future
73+
];
7074
$encoded = JWT::encode($payload, 'my_key', 'HS256');
7175
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
7276
$this->assertEquals($decoded->message, 'abc');
@@ -77,7 +81,8 @@ public function testValidTokenWithLeeway()
7781
JWT::$leeway = 60;
7882
$payload = [
7983
'message' => 'abc',
80-
'exp' => time() - 20]; // time in the past
84+
'exp' => time() - 20, // time in the past
85+
];
8186
$encoded = JWT::encode($payload, 'my_key', 'HS256');
8287
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
8388
$this->assertEquals($decoded->message, 'abc');
@@ -89,7 +94,8 @@ public function testExpiredTokenWithLeeway()
8994
JWT::$leeway = 60;
9095
$payload = [
9196
'message' => 'abc',
92-
'exp' => time() - 70]; // time far in the past
97+
'exp' => time() - 70, // time far in the past
98+
];
9399
$this->expectException(ExpiredException::class);
94100
$encoded = JWT::encode($payload, 'my_key', 'HS256');
95101
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
@@ -101,9 +107,10 @@ public function testValidTokenWithNbf()
101107
{
102108
$payload = [
103109
'message' => 'abc',
104-
"iat" => time(),
110+
'iat' => time(),
105111
'exp' => time() + 20, // time in the future
106-
"nbf" => time() - 20];
112+
'nbf' => time() - 20
113+
];
107114
$encoded = JWT::encode($payload, 'my_key', 'HS256');
108115
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
109116
$this->assertEquals($decoded->message, 'abc');
@@ -114,7 +121,8 @@ public function testValidTokenWithNbfLeeway()
114121
JWT::$leeway = 60;
115122
$payload = [
116123
'message' => 'abc',
117-
"nbf" => time() + 20]; // not before in near (leeway) future
124+
'nbf' => time() + 20, // not before in near (leeway) future
125+
];
118126
$encoded = JWT::encode($payload, 'my_key', 'HS256');
119127
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
120128
$this->assertEquals($decoded->message, 'abc');
@@ -126,7 +134,8 @@ public function testInvalidTokenWithNbfLeeway()
126134
JWT::$leeway = 60;
127135
$payload = [
128136
'message' => 'abc',
129-
"nbf" => time() + 65]; // not before too far in future
137+
'nbf' => time() + 65, // not before too far in future
138+
];
130139
$encoded = JWT::encode($payload, 'my_key', 'HS256');
131140
$this->expectException(BeforeValidException::class);
132141
JWT::decode($encoded, new Key('my_key', 'HS256'));
@@ -138,7 +147,8 @@ public function testValidTokenWithIatLeeway()
138147
JWT::$leeway = 60;
139148
$payload = [
140149
'message' => 'abc',
141-
"iat" => time() + 20]; // issued in near (leeway) future
150+
'iat' => time() + 20, // issued in near (leeway) future
151+
];
142152
$encoded = JWT::encode($payload, 'my_key', 'HS256');
143153
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
144154
$this->assertEquals($decoded->message, 'abc');
@@ -150,7 +160,8 @@ public function testInvalidTokenWithIatLeeway()
150160
JWT::$leeway = 60;
151161
$payload = [
152162
'message' => 'abc',
153-
"iat" => time() + 65]; // issued too far in future
163+
'iat' => time() + 65, // issued too far in future
164+
];
154165
$encoded = JWT::encode($payload, 'my_key', 'HS256');
155166
$this->expectException(BeforeValidException::class);
156167
JWT::decode($encoded, new Key('my_key', 'HS256'));
@@ -161,7 +172,8 @@ public function testInvalidToken()
161172
{
162173
$payload = [
163174
'message' => 'abc',
164-
'exp' => time() + 20]; // time in the future
175+
'exp' => time() + 20, // time in the future
176+
];
165177
$encoded = JWT::encode($payload, 'my_key', 'HS256');
166178
$this->expectException(SignatureInvalidException::class);
167179
JWT::decode($encoded, new Key('my_key2', 'HS256'));
@@ -171,7 +183,8 @@ public function testNullKeyFails()
171183
{
172184
$payload = [
173185
'message' => 'abc',
174-
'exp' => time() + JWT::$leeway + 20]; // time in the future
186+
'exp' => time() + JWT::$leeway + 20, // time in the future
187+
];
175188
$encoded = JWT::encode($payload, 'my_key', 'HS256');
176189
$this->expectException(TypeError::class);
177190
JWT::decode($encoded, new Key(null, 'HS256'));
@@ -181,7 +194,8 @@ public function testEmptyKeyFails()
181194
{
182195
$payload = [
183196
'message' => 'abc',
184-
'exp' => time() + JWT::$leeway + 20]; // time in the future
197+
'exp' => time() + JWT::$leeway + 20, // time in the future
198+
];
185199
$encoded = JWT::encode($payload, 'my_key', 'HS256');
186200
$this->expectException(InvalidArgumentException::class);
187201
JWT::decode($encoded, new Key('', 'HS256'));
@@ -250,7 +264,7 @@ public function testInvalidSegmentCount()
250264

251265
public function testInvalidSignatureEncoding()
252266
{
253-
$msg = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImZvbyJ9.Q4Kee9E8o0Xfo4ADXvYA8t7dN_X_bU9K5w6tXuiSjlUxx";
267+
$msg = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImZvbyJ9.Q4Kee9E8o0Xfo4ADXvYA8t7dN_X_bU9K5w6tXuiSjlUxx';
254268
$this->expectException(UnexpectedValueException::class);
255269
JWT::decode($msg, new Key('secret', 'HS256'));
256270
}
@@ -333,7 +347,7 @@ public function testDecodesEmptyArrayAsObject()
333347
public function testDecodesArraysInJWTAsArray()
334348
{
335349
$key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk';
336-
$payload = ['foo' => [1,2,3]];
350+
$payload = ['foo' => [1, 2, 3]];
337351
$jwt = JWT::encode($payload, $key, 'HS256');
338352
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
339353
$this->assertEquals($payload['foo'], $decoded->foo);

0 commit comments

Comments
 (0)