tool/SuppressionsToolImplementation.md
A comprehensive tool for automatically adding @phan-suppress annotations to PHP code based on Phan static analysis output. Completed implementation includes all planned features with intelligent suppression selection, configuration support, and multiple output modes.
All phases implemented successfully:
The tool automatically chooses the most appropriate suppression type based on code analysis:
Line-Level Suppressions:
@phan-suppress-next-line - Default for single occurrences (most readable)@phan-suppress-current-line - Used for short lines (< 80 chars with suppression)Function-Level Suppressions:
@suppress in PHPDoc blocksFile-Level Suppressions:
@phan-file-suppress at file beginningPhanUnreferencedFunction in test files)By File:
By Function:
Smart Conflict Resolution:
-next-line instead of -current-line when adding suppression would exceed limitConfiguration File: .phan/suppress_config.php
Configurable Options:
[
'default_scope' => 'next-line', // Default suppression type
'function_threshold' => 3, // Min issues for function-level
'file_threshold' => 10, // Min issues for file-level
'max_line_length' => 120, // Line length limit
'never_suppress' => [...], // Issues to never auto-suppress
'always_file_suppress' => [...], // Issues to always file-suppress
'verbose' => false, // Detailed output
]
Example Configuration Provided: .phan/suppress_config.example.php
--dry-run flag shows what would change without modifying files--interactive flag prompts for confirmation before each filey - Apply changes to this fileN - Skip this fileq - Quit entirelySTDIN (default):
./phan --output-mode json | php tool/add_suppressions.php
JSON File:
./phan --output-mode json > issues.json
php tool/add_suppressions.php --from-json issues.json
Both JSON array and line-delimited JSON supported
Uses Phan's FileEdit Infrastructure:
PHPDoc Handling:
@suppress into existing blocks--verbose flag enables detailed output:
SuppressionConfig
PhanIssue
Suppression
SuppressionTool
Function Boundary Detection:
Issue Grouping:
Suppression Selection:
Edit Application:
tests/tool_test/add_suppressions_test.phpTest Coverage:
Test Case 1: Single Issue
$x = "string" + 5; // 1 issue
Result: Added @phan-suppress-next-line ✅
Test Case 2: Function with 3+ Issues
function test() {
$x = "a" + 1; // Issue 1
$y = "b" + 2; // Issue 2
$z = "c" + 3; // Issue 3
}
Result: Added PHPDoc @suppress to function ✅
Test Case 3: Short Line
$x = "str" + 1; // Short line
Result: Added -current-line suppression ✅
tool/README_add_suppressions.md
.phan/suppress_config.example.php
tool/IMPLEMENTATION_SUMMARY.md (this file)
# Suppress all issues to establish baseline
./phan --output-mode json | php tool/add_suppressions.php
# See what would change
./phan --output-mode json | php tool/add_suppressions.php --dry-run --verbose
# Only suppress unreferenced function warnings
./phan --output-mode json | \
jq '. | map(select(.check_name | startswith("PhanUnreferenced")))' | \
php tool/add_suppressions.php
# Review and confirm each file
./phan --output-mode json | php tool/add_suppressions.php --interactive
Optimization Techniques:
Benchmarks (estimated):
Memory Usage:
Potential improvements for future versions:
Consolidate Multiple Suppressions
@phan-suppress-next-line IssueType1, IssueType2Detect Existing Suppressions
Smart PHPDoc Merging
Class-Level Suppressions
@phan-suppress at class levelSuppression Comments
@phan-suppress IssueType legacy code, fix in v2.0Git Integration
Suppression Tracking
The Phan Suppression Auto-Fixer is a fully-featured, production-ready tool that intelligently adds suppressions to PHP code. It provides:
The tool is ready for use in real-world codebases and provides a solid foundation for establishing Phan baselines in legacy projects.
Total Implementation Time: Completed in single session Lines of Code: ~750 lines (tool) + ~200 lines (tests) + ~250 lines (documentation) Test Coverage: Core functionality verified
tool/
├── add_suppressions.php # Main tool (executable)
├── README_add_suppressions.md # User documentation
└── IMPLEMENTATION_SUMMARY.md # This file
.phan/
└── suppress_config.example.php # Example configuration
tests/tool_test/
└── add_suppressions_test.php # PHPUnit test suite
Built using:
Follows Phan's coding standards and architectural patterns.