Skip to content

Commit 6a21162

Browse files
Make library compatible with PHPStan 2.x (#25)
* Bump phpstan/phpstan from 1.12.3 to 2.0.3 Bumps [phpstan/phpstan](https://github.com/phpstan/phpstan) from 1.12.3 to 2.0.3. - [Release notes](https://github.com/phpstan/phpstan/releases) - [Changelog](https://github.com/phpstan/phpstan/blob/2.0.x/CHANGELOG.md) - [Commits](phpstan/phpstan@1.12.3...2.0.3) --- updated-dependencies: - dependency-name: phpstan/phpstan dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Update composer.json * Update SarifErrorFormatter.php * Update tests.yml * Update README.md * Update tests.yml * Update tests.yml * Update tests.yml --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Jonathan Beliën <1150563+jbelien@users.noreply.github.com>
1 parent 167d64c commit 6a21162

File tree

6 files changed

+66
-177
lines changed

6 files changed

+66
-177
lines changed

.github/workflows/tests.yml

+40-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ permissions:
1010
contents: read
1111

1212
jobs:
13-
local:
14-
name: Local (${{ matrix.php-version }})
13+
local-phpstant-1x:
14+
name: Local PHPStan 1.x (${{ matrix.php-version }})
1515
runs-on: ubuntu-latest
1616
permissions:
1717
contents: read # for checkout to fetch code
@@ -20,7 +20,7 @@ jobs:
2020
strategy:
2121
fail-fast: false
2222
matrix:
23-
php-version: ['8.1', '8.2']
23+
php-version: [8.1, 8.2, 8.3, 8.4]
2424
steps:
2525
- uses: actions/checkout@v4
2626
- name: Use PHP ${{ matrix.php-version }}
@@ -30,13 +30,46 @@ jobs:
3030
- name: Validate composer.json and composer.lock
3131
run: composer validate --strict
3232
- name: Install dependencies
33-
run: composer install --prefer-dist --no-progress
33+
run: composer update --prefer-lowest --no-progress
3434
- name: Run PHPStan
3535
continue-on-error: true
3636
run: vendor/bin/phpstan analyze --configuration=tests/phpstan.neon --error-format=sarif --xdebug "tests/" > local-results.sarif
3737
- uses: actions/upload-artifact@v4
3838
with:
39-
name: local-${{ matrix.php-version }}
39+
name: local-v1-${{ matrix.php-version }}
40+
path: local-results.sarif
41+
- name: Upload analysis results to GitHub
42+
uses: github/codeql-action/upload-sarif@v3
43+
with:
44+
sarif_file: local-results.sarif
45+
wait-for-processing: true
46+
local-phpstant-2x:
47+
name: Local PHPStan 2.x (${{ matrix.php-version }})
48+
runs-on: ubuntu-latest
49+
permissions:
50+
contents: read # for checkout to fetch code
51+
security-events: write # for github/codeql-action/upload-sarif to upload SARIF results
52+
actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
53+
strategy:
54+
fail-fast: false
55+
matrix:
56+
php-version: [8.1, 8.2, 8.3, 8.4]
57+
steps:
58+
- uses: actions/checkout@v4
59+
- name: Use PHP ${{ matrix.php-version }}
60+
uses: shivammathur/setup-php@v2
61+
with:
62+
php-version: ${{ matrix.php-version }}
63+
- name: Validate composer.json and composer.lock
64+
run: composer validate --strict
65+
- name: Install dependencies
66+
run: composer update --prefer-dist --no-progress
67+
- name: Run PHPStan
68+
continue-on-error: true
69+
run: vendor/bin/phpstan analyze --configuration=tests/phpstan.neon --error-format=sarif --xdebug "tests/" > local-results.sarif
70+
- uses: actions/upload-artifact@v4
71+
with:
72+
name: local-v2-${{ matrix.php-version }}
4073
path: local-results.sarif
4174
- name: Upload analysis results to GitHub
4275
uses: github/codeql-action/upload-sarif@v3
@@ -54,8 +87,8 @@ jobs:
5487
strategy:
5588
fail-fast: false
5689
matrix:
57-
php-version: ['8.1', '8.2']
58-
version: ['dev-master', '1.0']
90+
php-version: [8.1, 8.2, 8.3, 8.4]
91+
version: ['dev-master', 1.0, 1.1]
5992
steps:
6093
- uses: actions/checkout@v4
6194
- name: Use PHP ${{ matrix.php-version }}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vendor/
2+
composer.lock

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# SARIF formatter for PHPStan
22

3+
🔖 **Compatible with PHPStan 1.x and 2.x**
4+
35
## PHPStan
46

57
PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code. It moves PHP closer to compiled languages in the sense that the correctness of each line of the code can be checked before you run the actual line.

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"require": {
66
"php": "^8.1",
77
"nette/utils": "^3.0 || ^4.0",
8-
"phpstan/phpstan": "^1.9"
8+
"phpstan/phpstan": "^1.9 || ^2.0"
99
},
1010
"autoload": {
1111
"psr-4": {

composer.lock

-165
This file was deleted.

src/SarifErrorFormatter.php

+22-4
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
2727
{
2828
$phpstanVersion = ComposerHelper::getPhpStanVersion();
2929

30+
$rules = [];
31+
3032
$tool = [
3133
'driver' => [
3234
'name' => 'PHPStan',
3335
'fullName' => 'PHP Static Analysis Tool',
3436
'informationUri' => 'https://phpstan.org',
3537
'version' => $phpstanVersion,
3638
'semanticVersion' => $phpstanVersion,
37-
'rules' => [],
39+
'rules' => $rules,
3840
],
3941
];
4042

@@ -48,7 +50,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
4850

4951
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
5052
$result = [
51-
'level' => 'error',
53+
'level' => $fileSpecificError->canBeIgnored() ? 'warning' : 'error',
5254
'message' => [
5355
'text' => $fileSpecificError->getMessage(),
5456
],
@@ -67,11 +69,25 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
6769
],
6870
'properties' => [
6971
'ignorable' => $fileSpecificError->canBeIgnored(),
70-
// 'identifier' => $fileSpecificError->getIdentifier(),
71-
// 'metadata' => $fileSpecificError->getMetadata(),
7272
],
7373
];
7474

75+
if ($fileSpecificError->getIdentifier() !== null) {
76+
$result['ruleId'] = $fileSpecificError->getIdentifier();
77+
78+
$ruleID = $fileSpecificError->getIdentifier();
79+
$rules[$ruleID] = [
80+
'id' => $ruleID,
81+
'shortDescription' => [
82+
'text' => $fileSpecificError->getMessage()
83+
],
84+
];
85+
86+
if ($fileSpecificError->getTip() !== null) {
87+
$rules[$ruleID]['help']['text'] = $fileSpecificError->getTip();
88+
}
89+
}
90+
7591
if ($fileSpecificError->getTip() !== null) {
7692
$result['properties']['tip'] = $fileSpecificError->getTip();
7793
}
@@ -97,6 +113,8 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
97113
];
98114
}
99115

116+
$tool['driver']['rules'] = array_values($rules);
117+
100118
$sarif = [
101119
'$schema' => 'https://json.schemastore.org/sarif-2.1.0.json',
102120
'version' => '2.1.0',

0 commit comments

Comments
 (0)