Skip to content

Commit 488b114

Browse files
committed
✨ switch phan for phpstan (see chillerlan/php-qrcode#277)
(cherry picked from commit ee00916)
1 parent eb7e949 commit 488b114

15 files changed

+113
-87
lines changed

.gitattributes

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
/.build export-ignore
2-
/.github export-ignore
3-
/.phan export-ignore
4-
/.phpdoc export-ignore
5-
/docs export-ignore
6-
/examples export-ignore
7-
/tests export-ignore
8-
/.editorconfig export-ignore
9-
/.gitattributes export-ignore
10-
/.gitignore export-ignore
11-
/.readthedocs.yml export-ignore
12-
/phpcs.xml.dist export-ignore
13-
/phpdoc.xml.dist export-ignore
14-
/phpmd.xml.dist export-ignore
15-
/phpunit.xml.dist export-ignore
1+
/.build export-ignore
2+
/.github export-ignore
3+
/.idea export-ignore
4+
/.phan export-ignore
5+
/.phpdoc export-ignore
6+
/docs export-ignore
7+
/examples export-ignore
8+
/tests export-ignore
9+
/.editorconfig export-ignore
10+
/.gitattributes export-ignore
11+
/.gitignore export-ignore
12+
/.readthedocs.yml export-ignore
13+
/phpcs.xml.dist export-ignore
14+
/phpdoc.xml.dist export-ignore
15+
/phpmd.xml.dist export-ignore
16+
/phpunit.xml.dist export-ignore
17+
/phpstan.dist.neon export-ignore
18+
/phpstan-baseline.neon export-ignore
1619

1720
*.php diff=php

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
uses: shivammathur/setup-php@v2
4545
with:
4646
php-version: ${{ matrix.php-version }}
47-
extensions: ast, ${{ env.PHP_EXTENSIONS }}
47+
extensions: ${{ env.PHP_EXTENSIONS }}
4848
ini-values: ${{ env.PHP_INI_VALUES }}
4949
coverage: none
5050

@@ -54,8 +54,8 @@ jobs:
5454
- name: "Install dependencies with composer"
5555
uses: ramsey/composer-install@v3
5656

57-
- name: "Run phan"
58-
run: php vendor/bin/phan --target-php-version=${{ matrix.php-version }}
57+
- name: "Run PHPStan"
58+
run: php vendor/bin/phpstan
5959

6060

6161
tests:

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ phpcs.xml
66
phpdoc.xml
77
phpmd.xml
88
phpunit.xml
9+
phpstan.neon
910
*.phpunit.result.cache

.phan/config.php

-58
This file was deleted.

composer.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@
3232
"prefer-stable": true,
3333
"require": {
3434
"php": "^7.4 || ^8.0",
35-
"chillerlan/php-settings-container": "^2.1.5 || ^3.2",
35+
"chillerlan/php-settings-container": "^2.1.6 || ^3.2.1",
3636
"paragonie/constant_time_encoding": "^2.6"
3737
},
3838
"require-dev": {
3939
"ext-curl": "*",
4040
"ext-json": "*",
4141
"ext-sodium": "*",
42-
"phan/phan": "^5.4",
4342
"phpmd/phpmd": "^2.15",
43+
"phpstan/phpstan": "^1.11",
44+
"phpstan/phpstan-deprecation-rules": "^1.2",
4445
"phpunit/phpunit": "^9.6",
45-
"squizlabs/php_codesniffer": "^3.9"
46+
"squizlabs/php_codesniffer": "^3.10"
4647
},
4748
"suggest": {
4849
"chillerlan/php-qrcode": "Create QR Codes for use with an authenticator app."
@@ -58,9 +59,9 @@
5859
}
5960
},
6061
"scripts": {
61-
"phan": "@php vendor/bin/phan --allow-polyfill-parser",
6262
"phpcs": "@php vendor/bin/phpcs",
63-
"phpunit": "@php vendor/bin/phpunit"
63+
"phpunit": "@php vendor/bin/phpunit",
64+
"phpstan": "@php vendor/bin/phpstan"
6465
},
6566
"config": {
6667
"lock": false,

phpstan-baseline.neon

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: "#^Cannot access offset 'server_time' on mixed\\.$#"
5+
count: 1
6+
path: src/Authenticators/SteamGuard.php
7+
8+
-
9+
message: "#^Cannot cast mixed to int\\.$#"
10+
count: 1
11+
path: src/Authenticators/SteamGuard.php
12+
13+
- # $response is always string here because CURLOPT_RETURNTRANSFER is set to true
14+
message: "#^Parameter \\#1 \\$json of function json_decode expects string, string\\|true given\\.$#"
15+
count: 1
16+
path: src/Authenticators/SteamGuard.php
17+
18+
- # the value given to getHMAC() is always int - this is most likely a false positive
19+
message: "#^Parameter \\#1 \\$counter of method chillerlan\\\\Authenticator\\\\Authenticators\\\\HOTP\\:\\:getHMAC\\(\\) expects int, float\\|int given\\.$#"
20+
count: 1
21+
path: src/Authenticators/TOTP.php
22+
23+
- # 32-bit system check
24+
message: "#^Call to function is_int\\(\\) with 59\\|1111111109\\|1111111111\\|1234567890\\|2000000000\\|20000000000 will always evaluate to true\\.$#"
25+
count: 1
26+
path: tests/Authenticators/SteamGuardTest.php
27+
28+
- # 32-bit system check
29+
message: "#^Call to function is_int\\(\\) with 59\\|1111111109\\|1111111111\\|1234567890\\|2000000000\\|20000000000 will always evaluate to true\\.$#"
30+
count: 1
31+
path: tests/Authenticators/TOTPTest.php

phpstan.dist.neon

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# https://phpstan.org/config-reference
2+
3+
parameters:
4+
level: 9
5+
tmpDir: .build/phpstan-cache
6+
paths:
7+
- examples
8+
- src
9+
- tests
10+
11+
treatPhpDocTypesAsCertain: false
12+
13+
includes:
14+
- phpstan-baseline.neon
15+
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
16+
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
17+
- vendor/chillerlan/php-settings-container/rules-magic-access.neon

phpunit.xml.dist

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
>
99
<testsuites>
1010
<testsuite name="php-authenticator test suite">
11-
<directory suffix=".php">./tests</directory>
11+
<directory>tests</directory>
1212
</testsuite>
1313
</testsuites>
1414
<coverage processUncoveredFiles="true">
1515
<include>
16-
<directory suffix=".php">./src</directory>
16+
<directory suffix=".php">src</directory>
1717
</include>
1818
<report>
1919
<clover outputFile=".build/coverage/clover.xml"/>

src/AuthenticatorOptionsTrait.php

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
use function strtolower;
1818
use function strtoupper;
1919

20+
/**
21+
* @property int $digits
22+
* @property int $period
23+
* @property int $secret_length
24+
* @property string $algorithm
25+
* @property string $mode
26+
* @property int $adjacent
27+
* @property int $time_offset
28+
* @property bool $useLocalTime
29+
* @property bool $forceTimeRefresh
30+
*/
2031
trait AuthenticatorOptionsTrait{
2132

2233
/**

src/Authenticators/HOTP.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ public function getHMAC(int $counter):string{
5454
*/
5555
public function getCode(string $hmac):int{
5656
$data = unpack('C*', $hmac);
57-
$b = ($data[strlen($hmac)] & 0xF);
57+
58+
if($data === false){
59+
throw new RuntimeException('error while unpacking HMAC'); // @codeCoverageIgnore
60+
}
61+
62+
$b = ($data[strlen($hmac)] & 0xF);
5863
// phpcs:ignore
5964
return (($data[$b + 1] & 0x7F) << 24) | ($data[$b + 2] << 16) | ($data[$b + 3] << 8) | $data[$b + 4];
6065
}

src/Authenticators/SteamGuard.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use function curl_setopt_array;
2323
use function floor;
2424
use function intdiv;
25+
use function is_array;
2526
use function json_decode;
2627
use function sprintf;
2728
use function time;
@@ -128,21 +129,26 @@ public function getServerTime():int{
128129

129130
$ch = curl_init($this::steamTimeURL);
130131

132+
// it's almost impossible to run into this, but hey, phpstan happy
133+
if($ch === false){
134+
throw new RuntimeException('curl_init error'); // @codeCoverageIgnore
135+
}
136+
131137
curl_setopt_array($ch, $options);
132138

133139
$response = curl_exec($ch);
134140
$info = curl_getinfo($ch);
135141

136142
curl_close($ch);
137143

138-
if($info['http_code'] !== 200){
144+
if($info['http_code'] !== 200 || $response === false){
139145
// I'm not going to investigate the error further as this shouldn't happen usually
140146
throw new RuntimeException(sprintf('Steam API request error: HTTP/%s', $info['http_code'])); // @codeCoverageIgnore
141147
}
142148

143149
$json = json_decode($response, true);
144150

145-
if(empty($json) || !isset($json['response']['server_time'])){
151+
if(!is_array($json) || !isset($json['response']['server_time'])){
146152
throw new RuntimeException('Unable to decode Steam API response'); // @codeCoverageIgnore
147153
}
148154

tests/AuthenticatorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testGetUri():void{
8484
);
8585
}
8686

87-
public function testGetUriEmptyLabelException(){
87+
public function testGetUriEmptyLabelException():void{
8888
$this->expectException(InvalidArgumentException::class);
8989
$this->expectExceptionMessage('$label and $issuer cannot be empty');
9090

tests/Common/Base32Test.php

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
class Base32Test extends TestCase{
1919

20+
/**
21+
* @phpstan-return array<int, array<int, string>>
22+
*/
2023
public static function base32DataProvider():array{
2124
return [
2225
['a' , 'ME' ],

tests/Common/Base64Test.php

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
class Base64Test extends TestCase{
2424

25+
/**
26+
* @phpstan-return array<int, array<int, string>>
27+
*/
2528
public static function base64DataProvider():array{
2629
return [
2730
['a' , 'YQ==' ],

tests/Common/HexTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
class HexTest extends TestCase{
2424

25+
/**
26+
* @phpstan-return array<int, array<int, string>>
27+
*/
2528
public static function hexDataProvider():array{
2629
return [
2730
['a' , '61' ],

0 commit comments

Comments
 (0)