Skip to content

Commit f18c93b

Browse files
committed
Lint with PHPStan
1 parent 870f19d commit f18c93b

File tree

12 files changed

+392
-62
lines changed

12 files changed

+392
-62
lines changed

.github/workflows/lint.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,3 @@ permissions:
1919
jobs:
2020
php:
2121
uses: typisttech/.github/.github/workflows/lint-php.yml@v3
22-
with:
23-
phpstan: false

composer.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@
2929
"require": {
3030
"php": "^8.3",
3131
"composer/semver": "^3.4",
32-
"guzzlehttp/guzzle": "^7.9"
32+
"guzzlehttp/guzzle": "^7.9",
33+
"phpstan/extension-installer": "^1.4",
34+
"phpstan/phpstan": "^2.1",
35+
"phpstan/phpstan-deprecation-rules": "^2.0",
36+
"phpstan/phpstan-mockery": "^2.0",
37+
"phpstan/phpstan-strict-rules": "^2.0"
3338
},
3439
"require-dev": {
3540
"mockery/mockery": "^1.6",
@@ -48,7 +53,8 @@
4853
},
4954
"config": {
5055
"allow-plugins": {
51-
"pestphp/pest-plugin": true
56+
"pestphp/pest-plugin": true,
57+
"phpstan/extension-installer": true
5258
},
5359
"sort-packages": true
5460
},
@@ -57,6 +63,11 @@
5763
"curl -o ./tests/Fixtures/vulnerabilities.production.json https://www.wordfence.com/api/intelligence/v2/vulnerabilities/production",
5864
"curl -o ./tests/Fixtures/vulnerabilities.scanner.json https://www.wordfence.com/api/intelligence/v2/vulnerabilities/scanner"
5965
],
66+
"lint": [
67+
"@composer normalize --dry-run",
68+
"pint --test",
69+
"phpstan analyse"
70+
],
6071
"pest": "pest -d memory_limit=512M",
6172
"pest:e2e": "pest -d memory_limit=512M --group=e2e",
6273
"pest:feature": "pest -d memory_limit=512M --group=feature",

composer.lock

Lines changed: 246 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- src

src/AffectedVersionsParser.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,28 @@
1010

1111
readonly class AffectedVersionsParser
1212
{
13-
private const string UNKNOWN = 'unknown';
14-
1513
public function __construct(
1614
private VersionParser $parser = new VersionParser,
1715
) {}
1816

17+
/**
18+
* @param array{from_version?: mixed, from_inclusive?: mixed, to_version?: mixed, to_inclusive?: mixed}[] $data
19+
*/
1920
public function parse(array $data): ?ConstraintInterface
2021
{
2122
$constraints = array_map(function (array $affected): ?string {
22-
$fromVersion = (string) ($affected['from_version'] ?? self::UNKNOWN);
23+
$fromVersion = $affected['from_version'] ?? null;
24+
if (! is_string($fromVersion)) {
25+
return null;
26+
}
27+
2328
$fromInclusive = (bool) ($affected['from_inclusive'] ?? false);
24-
$toVersion = (string) ($affected['to_version'] ?? self::UNKNOWN);
29+
30+
$toVersion = $affected['to_version'] ?? null;
31+
if (! is_string($toVersion)) {
32+
return null;
33+
}
34+
2535
$toInclusive = (bool) ($affected['to_inclusive'] ?? false);
2636

2737
if ($fromVersion === '*' && $toVersion === '*') {
@@ -40,7 +50,7 @@ public function parse(array $data): ?ConstraintInterface
4050
}
4151

4252
if ($toVersion !== '*') {
43-
if (! empty($constraint)) {
53+
if ($constraint !== '') {
4454
$constraint .= ', ';
4555
}
4656

@@ -51,23 +61,18 @@ public function parse(array $data): ?ConstraintInterface
5161
return $constraint;
5262
}, $data);
5363

54-
$constraints = array_filter($constraints);
64+
$constraints = array_filter($constraints, static fn (?string $c) => $c !== null);
65+
$constraints = array_filter($constraints, static fn (string $c) => $c !== '');
5566
$constraints = array_values($constraints);
5667
$imploded = implode('||', $constraints);
5768

58-
if (empty($imploded)) {
59-
return null;
60-
}
61-
62-
return $this->parser->parseConstraints($imploded);
69+
return $imploded === ''
70+
? null
71+
: $this->parser->parseConstraints($imploded);
6372
}
6473

6574
private function isValid(string $version): bool
6675
{
67-
if ($version === self::UNKNOWN) {
68-
return false;
69-
}
70-
7176
if ($version === '*') {
7277
return true;
7378
}

0 commit comments

Comments
 (0)