Skip to content

Commit

Permalink
✨ switch phan for phpstan (see chillerlan/php-qrcode#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Jul 16, 2024
1 parent 8ce1775 commit ee00916
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 99 deletions.
33 changes: 18 additions & 15 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/.build export-ignore
/.github export-ignore
/.phan export-ignore
/.phpdoc export-ignore
/docs export-ignore
/examples export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.readthedocs.yml export-ignore
/phpcs.xml.dist export-ignore
/phpdoc.xml.dist export-ignore
/phpmd.xml.dist export-ignore
/phpunit.xml.dist export-ignore
/.build export-ignore
/.github export-ignore
/.idea export-ignore
/.phan export-ignore
/.phpdoc export-ignore
/docs export-ignore
/examples export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.readthedocs.yml export-ignore
/phpcs.xml.dist export-ignore
/phpdoc.xml.dist export-ignore
/phpmd.xml.dist export-ignore
/phpunit.xml.dist export-ignore
/phpstan.dist.neon export-ignore
/phpstan-baseline.neon export-ignore

*.php diff=php
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: ast, ${{ env.PHP_EXTENSIONS }}
extensions: ${{ env.PHP_EXTENSIONS }}
ini-values: ${{ env.PHP_INI_VALUES }}
coverage: none

Expand All @@ -51,8 +51,8 @@ jobs:
- name: "Install dependencies with composer"
uses: ramsey/composer-install@v3

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


tests:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ phpcs.xml
phpdoc.xml
phpmd.xml
phpunit.xml
phpstan.neon
*.phpunit.result.cache
57 changes: 0 additions & 57 deletions .phan/config.php

This file was deleted.

11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@
"prefer-stable": true,
"require": {
"php": "^8.2",
"chillerlan/php-settings-container": "^3.2",
"chillerlan/php-settings-container": "^3.2.1",
"paragonie/constant_time_encoding": "^3.0"
},
"require-dev": {
"ext-curl": "*",
"ext-json": "*",
"ext-sodium": "*",
"phan/phan": "^5.4",
"phpmd/phpmd": "^2.15",
"phpstan/phpstan": "^1.11",
"phpstan/phpstan-deprecation-rules": "^1.2",
"phpunit/phpunit": "^11.2",
"squizlabs/php_codesniffer": "^3.9"
"squizlabs/php_codesniffer": "^3.10"
},
"suggest": {
"chillerlan/php-qrcode": "Create QR Codes for use with an authenticator app."
Expand All @@ -58,9 +59,9 @@
}
},
"scripts": {
"phan": "@php vendor/bin/phan --allow-polyfill-parser",
"phpcs": "@php vendor/bin/phpcs",
"phpunit": "@php vendor/bin/phpunit"
"phpunit": "@php vendor/bin/phpunit",
"phpstan": "@php vendor/bin/phpstan"
},
"config": {
"lock": false,
Expand Down
31 changes: 31 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
parameters:
ignoreErrors:
-
message: "#^Cannot access offset 'server_time' on mixed\\.$#"
count: 1
path: src/Authenticators/SteamGuard.php

-
message: "#^Cannot cast mixed to int\\.$#"
count: 1
path: src/Authenticators/SteamGuard.php

- # $response is always string here because CURLOPT_RETURNTRANSFER is set to true
message: "#^Parameter \\#1 \\$json of function json_decode expects string, string\\|true given\\.$#"
count: 1
path: src/Authenticators/SteamGuard.php

- # the value given to getHMAC() is always int - this is most likely a false positive
message: "#^Parameter \\#1 \\$counter of method chillerlan\\\\Authenticator\\\\Authenticators\\\\HOTP\\:\\:getHMAC\\(\\) expects int, float\\|int given\\.$#"
count: 1
path: src/Authenticators/TOTP.php

- # 32-bit system check
message: "#^Call to function is_int\\(\\) with 59\\|1111111109\\|1111111111\\|1234567890\\|2000000000\\|20000000000 will always evaluate to true\\.$#"
count: 1
path: tests/Authenticators/SteamGuardTest.php

- # 32-bit system check
message: "#^Call to function is_int\\(\\) with 59\\|1111111109\\|1111111111\\|1234567890\\|2000000000\\|20000000000 will always evaluate to true\\.$#"
count: 1
path: tests/Authenticators/TOTPTest.php
17 changes: 17 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# https://phpstan.org/config-reference

parameters:
level: 9
tmpDir: .build/phpstan-cache
paths:
- examples
- src
- tests

treatPhpDocTypesAsCertain: false

includes:
- phpstan-baseline.neon
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/chillerlan/php-settings-container/rules-magic-access.neon
18 changes: 7 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".build/phpunit.result.cache"
cacheDirectory=".build/phpunit-cache"
colors="true"
beStrictAboutOutputDuringTests="true"
>
<testsuites>
<testsuite name="php-authenticator test suite">
<directory suffix=".php">./tests/</directory>
<exclude>tests/Authenticators/AuthenticatorInterfaceTestAbstract.php</exclude>
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
<coverage>
<report>
<clover outputFile=".build/coverage/clover.xml"/>
<xml outputDirectory=".build/coverage/coverage-xml"/>
</report>
</coverage>
<logging>
<junit outputFile=".build/logs/junit.xml"/>
</logging>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
11 changes: 11 additions & 0 deletions src/AuthenticatorOptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
use function strtolower;
use function strtoupper;

/**
* @property int $digits
* @property int $period
* @property int $secret_length
* @property string $algorithm
* @property string $mode
* @property int $adjacent
* @property int $time_offset
* @property bool $useLocalTime
* @property bool $forceTimeRefresh
*/
trait AuthenticatorOptionsTrait{

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Authenticators/HOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ public function getHMAC(int $counter):string{
*/
public function getCode(#[SensitiveParameter] string $hmac):int{
$data = unpack('C*', $hmac);
$b = ($data[strlen($hmac)] & 0xF);

if($data === false){
throw new RuntimeException('error while unpacking HMAC'); // @codeCoverageIgnore
}

$b = ($data[strlen($hmac)] & 0xF);
// phpcs:ignore
return (($data[$b + 1] & 0x7F) << 24) | ($data[$b + 2] << 16) | ($data[$b + 3] << 8) | $data[$b + 4];
}
Expand Down
10 changes: 8 additions & 2 deletions src/Authenticators/SteamGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function curl_setopt_array;
use function floor;
use function intdiv;
use function is_array;
use function json_decode;
use function sprintf;
use function time;
Expand Down Expand Up @@ -129,21 +130,26 @@ public function getServerTime():int{

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

// it's almost impossible to run into this, but hey, phpstan happy
if($ch === false){
throw new RuntimeException('curl_init error'); // @codeCoverageIgnore
}

curl_setopt_array($ch, $options);

$response = curl_exec($ch);
$info = curl_getinfo($ch);

curl_close($ch);

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

$json = json_decode($response, true);

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

Expand Down
2 changes: 1 addition & 1 deletion tests/AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testGetUri():void{
);
}

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

Expand Down
7 changes: 5 additions & 2 deletions tests/Common/Base32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

class Base32Test extends TestCase{

/**
* @phpstan-return array<int, array<int, string>>
*/
public static function base32DataProvider():array{
return [
['a' , 'ME' ],
Expand All @@ -37,12 +40,12 @@ public function testEncode(string $str, string $base32):void{
}

#[DataProvider('base32DataProvider')]
public function testDecode(string $str, string $base32){
public function testDecode(string $str, string $base32):void{
$this::assertSame($str, Base32::decode($base32));
}

#[DataProvider('base32DataProvider')]
public function testCheckCharset(string $str, string $base32){
public function testCheckCharset(string $str, string $base32):void{
$this->expectNotToPerformAssertions();

Base32::checkCharacterSet($base32);
Expand Down
7 changes: 5 additions & 2 deletions tests/Common/Base64Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
class Base64Test extends TestCase{

/**
* @phpstan-return array<int, array<int, string>>
*/
public static function base64DataProvider():array{
return [
['a' , 'YQ==' ],
Expand All @@ -45,7 +48,7 @@ public function testEncode(string $str, string $base64):void{
}

#[DataProvider('base64DataProvider')]
public function testDecode(string $str, string $base64){
public function testDecode(string $str, string $base64):void{
$decoded = Base64::decode($base64);

$this::assertSame($str, $decoded);
Expand All @@ -54,7 +57,7 @@ public function testDecode(string $str, string $base64){
}

#[DataProvider('base64DataProvider')]
public function testCheckCharset(string $str, string $base64){
public function testCheckCharset(string $str, string $base64):void{
$this->expectNotToPerformAssertions();

Base64::checkCharacterSet($base64);
Expand Down
3 changes: 3 additions & 0 deletions tests/Common/HexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
class HexTest extends TestCase{

/**
* @phpstan-return array<int, array<int, string>>
*/
public static function hexDataProvider():array{
return [
['a' , '61' ],
Expand Down

0 comments on commit ee00916

Please sign in to comment.