tool/README_add_suppressions.md
Automatically adds @phan-suppress annotations to code based on Phan issue output.
# Generate suppressions for all Phan issues (JSON format)
./phan --output-mode json | php tool/add_suppressions.php
# Generate suppressions using checkstyle XML format
./phan --output-mode checkstyle | php tool/add_suppressions.php --from-checkstyle /dev/stdin
# Dry-run to preview changes
./phan --output-mode json | php tool/add_suppressions.php --dry-run
# Interactive mode with confirmation
./phan --output-mode json | php tool/add_suppressions.php --interactive
# Read from JSON file
./phan --output-mode json --no-progress-bar > issues.json
php tool/add_suppressions.php --from-json issues.json
# Read from checkstyle XML file
./phan --output-mode checkstyle --no-progress-bar > issues.xml
php tool/add_suppressions.php --from-checkstyle issues.xml
--dry-run - Show what would be changed without modifying files--interactive - Confirm each file before making changes--from-json FILE - Read issues from Phan JSON file instead of stdin--from-checkstyle FILE - Read issues from Phan checkstyle XML file instead of stdin--config FILE - Load configuration from file (default: .phan/suppress_config.php)--verbose - Show detailed output--help - Show help messageCreate a configuration file at .phan/suppress_config.php:
<?php
return [
// Minimum occurrences before using function-level suppression
'function_threshold' => 3,
// Minimum occurrences before using file-level suppression
'file_threshold' => 10,
// Maximum line length
'max_line_length' => 120,
// Issue types to never suppress
'never_suppress' => [
'PhanUndeclaredVariable',
'PhanTypeMismatchReturn',
],
// Issue types to always file-suppress
'always_file_suppress' => [
'PhanUnreferencedFunction',
],
// Enable verbose output
'verbose' => false,
];
Next-Line (default):
// @phan-suppress-next-line PhanTypeInvalidLeftOperandOfAdd
$result = "string" + 5;
Current-Line (for short lines):
$x = "str" + 1; // @phan-suppress-current-line PhanTypeInvalidLeftOperandOfAdd
Used when 3+ occurrences of the same issue type appear in a function:
/**
* @suppress PhanTypeInvalidLeftOperandOfAdd
*/
function example() {
$x = "a" + 1;
$y = "b" + 2;
$z = "c" + 3;
}
Used when 10+ occurrences of the same issue type appear in a file:
<?php
// @phan-file-suppress PhanUnreferencedFunction
function helper1() { }
function helper2() { }
// ... many more unreferenced functions
PhanUndeclaredVariable--dry-run to preview changesnever_suppress./phan -n --output-mode json path/to/file.php | php tool/add_suppressions.php
./phan --output-mode json | jq '. | map(select(.check_name | startswith("PhanUnreferenced")))' | php tool/add_suppressions.php
./phan --output-mode json --directory src/ | php tool/add_suppressions.php --dry-run --verbose
If your CI pipeline already captures Phan output in checkstyle format, you can feed it directly:
# From a saved checkstyle XML file
php tool/add_suppressions.php --from-checkstyle phan-report.xml --dry-run
The tool needs absolute paths. Use:
./phan --output-mode json --absolute-paths | php tool/add_suppressions.php
Verify the suppression was added correctly:
./phan -n path/to/file.php
If issues still appear, the suppression format may be incorrect. Check that:
The tool automatically uses next-line suppressions when adding current-line would exceed max_line_length. Adjust this in your config file if needed.
Enhancements welcome! Consider adding:
Same as Phan (MIT)