jbelien/phpstan-sarif-formatter
SARIF error formatter for PHPStan (1.x/2.x). Outputs analysis results as SARIF JSON for easy integration with GitHub Code Scanning and CI pipelines. Configure via phpstan.neon and run phpstan analyze --error-format=sarif.
Installation Add the package via Composer (ensure compatibility with PHPStan 2.x):
composer require --dev jbelien/phpstan-sarif-formatter
Requires PHPStan 2.x (verified compatibility). Install PHPStan 2.x via:
composer require --dev phpstan/phpstan "^2.0"
First Use Case Run PHPStan 2.x with SARIF output:
vendor/bin/phpstan analyse src --error-format=SarifJson
Outputs a .sarif file (e.g., phpstan-results.sarif) for tools like GitHub Advanced Security, SonarQube, or VS Code.
Where to Look First
phpstan.neon to PHPStan 2.x syntax (e.g., parameters instead of includes):
parameters:
level: 5
rules:
PhpStan\Rules\Arrays\ArrayShapeRule: strict
phpstan-baseline (updated for PHPStan 2.x) or phpstan-extension-installer.CI/CD Pipelines (PHPStan 2.x)
- name: Run PHPStan 2.x
run: vendor/bin/phpstan analyse --error-format=SarifJson -c phpstan.neon
- name: Upload SARIF
uses: actions/upload-artifact@v3
with:
name: phpstan-results
path: phpstan-results.sarif
sonar.phpstan.reportPaths (PHPStan 2.x generates SARIF identically to v1.x).Local Development
$sarif = json_decode(file_get_contents('phpstan-results.sarif'), true);
foreach ($sarif['runs'][0]['results'] as $result) {
if ($result['level'] === 'error') {
echo "Error in {$result['locations'][0]['physicalLocation']['artifactLocation']['uri']}\n";
}
}
Custom Reporting
PhpStan\Rules\Arrays\ArrayShapeRule).phpstan.neon to PHPStan 2.x syntax:
parameters:
level: 5
rules:
PhpStan\Rules\Arrays\ArrayShapeRule: strict
phpstan-baseline (v2.x) to exclude known issues:
vendor/bin/phpstan analyse --generate-baseline --error-format=SarifJson
PHPStan 2.x Migration
includes in favor of parameters. Update configs:
- includes: vendor/phpstan/phpstan/src/Rules
+ parameters:
+ rules:
+ PhpStan\Rules\Arrays\ArrayShapeRule: strict
PhpStan\Rules\... instead of Arrays\ArrayShapeRule). Verify SARIF rule IDs match.Performance Overhead
--memory-limit:
vendor/bin/phpstan analyse --error-format=SarifJson --memory-limit=1G
node_modules/.cache/phpstan.sarif).Tooling Compatibility
phpstan-results.sarif (GitHub’s default expectation).sonar.phpstan.reportPaths=phpstan-results.sarif
npx @microsoft/sarif-vscode validate phpstan-results.sarif
--error-format=SarifJson is the last flag (order matters in PHPStan 2.x).phpstan diagnose to list available rules and verify SARIF mappings:
vendor/bin/phpstan diagnose
Custom Rules (PHPStan 2.x)
Jbelien\PhpStanSarifFormatter\SarifFormatter to override rule IDs for PHPStan 2.x:
class CustomSarifFormatter extends SarifFormatter {
protected function getRuleId(string $ruleName): string {
return match ($ruleName) {
'PhpStan\Rules\Arrays\ArrayShapeRule' => 'custom-array-shape-v2',
default => parent::getRuleId($ruleName),
};
}
}
phpstan.neon:
services:
- Jbelien\PhpStanSarifFormatter\CustomSarifFormatter
Dynamic Rule Mapping
getToolComponent() to customize SARIF metadata for PHPStan 2.x rules:
protected function getToolComponent(): array {
return [
'name' => 'Custom PHPStan 2.x',
'informationUri' => 'https://github.com/your/repo',
'rules' => [
'PhpStan\Rules\Arrays\ArrayShapeRule' => [
'id' => 'array-shape-v2',
'name' => 'Array Shape Rule (PHPStan 2.x)',
'helpUri' => 'https://phpstan.org/rules/arrays/array-shape',
],
],
];
}
Parallel Execution
phpstan-parallel (PHPStan 2.x compatible) for large codebases:
vendor/bin/phpstan analyse --parallel --error-format=SarifJson
--generate-baseline to stabilize outputs.How can I help you explore Laravel packages today?