Skip to content

Commit bb8977f

Browse files
authored
Add SARIF output formatter for PHPStan (mongodb#2965)
1 parent a7aecf8 commit bb8977f

File tree

3 files changed

+144
-1
lines changed

3 files changed

+144
-1
lines changed

.github/workflows/coding-standards.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,13 @@ jobs:
108108
phpstan-result-cache-
109109
110110
- name: Run PHPStan
111-
run: ./vendor/bin/phpstan analyse --no-interaction --no-progress --ansi
111+
run: ./vendor/bin/phpstan analyse --no-interaction --no-progress --ansi --error-format=sarif > phpstan.sarif
112+
113+
- name: "Upload SARIF report"
114+
if: always()
115+
uses: "github/codeql-action/upload-sarif@v3"
116+
with:
117+
sarif_file: phpstan.sarif
112118

113119
- name: Save cache PHPStan results
114120
id: phpstan-cache-save

phpstan.neon.dist

+8
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ parameters:
1414
ignoreErrors:
1515
- '#Unsafe usage of new static#'
1616
- '#Call to an undefined method [a-zA-Z0-9\\_\<\>]+::[a-zA-Z]+\(\)#'
17+
18+
services:
19+
errorFormatter.sarif:
20+
class: MongoDB\Laravel\Tests\PHPStan\SarifErrorFormatter
21+
arguments:
22+
relativePathHelper: @simpleRelativePathHelper
23+
currentWorkingDirectory: %currentWorkingDirectory%
24+
pretty: true

tests/PHPStan/SarifErrorFormatter.php

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file was copied from https://github.com/jbelien/phpstan-sarif-formatter/blob/d8cf03abf5c8e209e55e10d6a80160f0d97cdb19/src/SarifErrorFormatter.php,
7+
* originally released under the MIT license.
8+
*
9+
* The code has been changed to export rules (without descriptions)
10+
*/
11+
12+
namespace MongoDB\Laravel\Tests\PHPStan;
13+
14+
use Nette\Utils\Json;
15+
use PHPStan\Command\AnalysisResult;
16+
use PHPStan\Command\ErrorFormatter\ErrorFormatter;
17+
use PHPStan\Command\Output;
18+
use PHPStan\File\RelativePathHelper;
19+
use PHPStan\Internal\ComposerHelper;
20+
21+
use function array_values;
22+
23+
/** @internal */
24+
class SarifErrorFormatter implements ErrorFormatter
25+
{
26+
private const URI_BASE_ID = 'WORKINGDIR';
27+
28+
public function __construct(
29+
private RelativePathHelper $relativePathHelper,
30+
private string $currentWorkingDirectory,
31+
private bool $pretty,
32+
) {
33+
}
34+
35+
public function formatErrors(AnalysisResult $analysisResult, Output $output): int
36+
{
37+
// @phpstan-ignore phpstanApi.method
38+
$phpstanVersion = ComposerHelper::getPhpStanVersion();
39+
40+
$originalUriBaseIds = [
41+
self::URI_BASE_ID => [
42+
'uri' => 'file://' . $this->currentWorkingDirectory . '/',
43+
],
44+
];
45+
46+
$results = [];
47+
$rules = [];
48+
49+
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
50+
$ruleId = $fileSpecificError->getIdentifier();
51+
$rules[$ruleId] = ['id' => $ruleId];
52+
53+
$result = [
54+
'ruleId' => $ruleId,
55+
'level' => 'error',
56+
'message' => [
57+
'text' => $fileSpecificError->getMessage(),
58+
],
59+
'locations' => [
60+
[
61+
'physicalLocation' => [
62+
'artifactLocation' => [
63+
'uri' => $this->relativePathHelper->getRelativePath($fileSpecificError->getFile()),
64+
'uriBaseId' => self::URI_BASE_ID,
65+
],
66+
'region' => [
67+
'startLine' => $fileSpecificError->getLine(),
68+
],
69+
],
70+
],
71+
],
72+
'properties' => [
73+
'ignorable' => $fileSpecificError->canBeIgnored(),
74+
],
75+
];
76+
77+
if ($fileSpecificError->getTip() !== null) {
78+
$result['properties']['tip'] = $fileSpecificError->getTip();
79+
}
80+
81+
$results[] = $result;
82+
}
83+
84+
foreach ($analysisResult->getNotFileSpecificErrors() as $notFileSpecificError) {
85+
$results[] = [
86+
'level' => 'error',
87+
'message' => [
88+
'text' => $notFileSpecificError,
89+
],
90+
];
91+
}
92+
93+
foreach ($analysisResult->getWarnings() as $warning) {
94+
$results[] = [
95+
'level' => 'warning',
96+
'message' => [
97+
'text' => $warning,
98+
],
99+
];
100+
}
101+
102+
$sarif = [
103+
'$schema' => 'https://json.schemastore.org/sarif-2.1.0.json',
104+
'version' => '2.1.0',
105+
'runs' => [
106+
[
107+
'tool' => [
108+
'driver' => [
109+
'name' => 'PHPStan',
110+
'fullName' => 'PHP Static Analysis Tool',
111+
'informationUri' => 'https://phpstan.org',
112+
'version' => $phpstanVersion,
113+
'semanticVersion' => $phpstanVersion,
114+
'rules' => array_values($rules),
115+
],
116+
],
117+
'originalUriBaseIds' => $originalUriBaseIds,
118+
'results' => $results,
119+
],
120+
],
121+
];
122+
123+
$json = Json::encode($sarif, $this->pretty ? Json::PRETTY : 0);
124+
125+
$output->writeRaw($json);
126+
127+
return $analysisResult->hasErrors() ? 1 : 0;
128+
}
129+
}

0 commit comments

Comments
 (0)