sonar_get_duplication_summary
Get ranked list of files with code duplication, sortable by density, lines, or blocks.
Description
Identifies files with the most code duplication in your project. Shows priority indicators and refactoring recommendations to help focus cleanup efforts on high-impact areas.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
sortBy | string | No | density | Sort method: density, lines, blocks |
pageSize | number | No | 100 | Files to analyze (1-500) |
maxResults | number | No | 10 | Limit output to top N files (1-50) |
Sort Options
| Option | Description | Best For |
|---|---|---|
density | Percentage of file that's duplicated | Finding worst offenders |
lines | Total duplicate line count | Finding biggest impact |
blocks | Number of duplicate blocks | Finding scattered duplication |
Usage
Basic Usage
Show me files with the most duplication
Tool call:
{}
By Impact (Lines)
Which files have the most duplicated code by volume?
Tool call:
{
"sortBy": "lines",
"maxResults": 15
}
Top Offenders Only
Show me the 5 worst duplicated files
Tool call:
{
"sortBy": "density",
"maxResults": 5
}
Response Format
DUPLICATION SUMMARY
━━━━━━━━━━━━━━━━━━━
Project: my-awesome-app
Overall Duplication: 4.2%
📊 TOP FILES BY DENSITY:
┌─────┬────────────────────────────────────────┬─────────┬───────┬────────┐
│ Rank│ File │ Density │ Lines │ Blocks │
├─────┼────────────────────────────────────────┼─────────┼───────┼────────┤
│ 🔴 1│ src/utils/validators.ts │ 45.2% │ 234 │ 12 │
│ 🔴 2│ src/services/PaymentService.ts │ 38.7% │ 189 │ 8 │
│ 🟠 3│ src/api/handlers/userHandler.ts │ 28.3% │ 156 │ 6 │
│ 🟠 4│ src/api/handlers/orderHandler.ts │ 25.1% │ 142 │ 5 │
│ 🟡 5│ src/components/DataTable.tsx │ 18.4% │ 98 │ 4 │
│ 🟡 6│ src/components/UserTable.tsx │ 17.9% │ 92 │ 4 │
│ 🟢 7│ src/hooks/useApi.ts │ 8.2% │ 34 │ 2 │
│ 🟢 8│ src/hooks/useFetch.ts │ 7.8% │ 31 │ 2 │
│ 🟢 9│ src/config/routes.ts │ 5.1% │ 22 │ 1 │
│ 🟢10│ src/types/api.ts │ 3.4% │ 15 │ 1 │
└─────┴────────────────────────────────────────┴─────────┴───────┴────────┘
PRIORITY INDICATORS:
🔴 HIGH (>25%) - Immediate refactoring recommended
🟠 MEDIUM (15-25%) - Should refactor soon
🟡 LOW (10-15%) - Consider refactoring
🟢 MINIMAL (<10%) - Acceptable levels
📋 REFACTORING RECOMMENDATIONS:
High Priority:
1. src/utils/validators.ts (45.2%)
→ Extract common validation logic to shared module
→ Estimated cleanup: 234 lines reducible
2. src/services/PaymentService.ts (38.7%)
→ Abstract payment processing patterns
→ Consider strategy pattern for payment types
Pattern Detected:
• userHandler.ts and orderHandler.ts share similar patterns
→ Extract base handler class or shared utilities
SUMMARY:
Files analyzed: 89
Files with duplication: 10
Total duplicate lines: 1,013
Potential reduction: ~650 lines (64%)
Priority Levels
| Level | Density | Action |
|---|---|---|
| 🔴 HIGH | More than 25% | Refactor immediately |
| 🟠 MEDIUM | 15-25% | Plan refactoring |
| 🟡 LOW | 10-15% | Consider when touching |
| 🟢 MINIMAL | Less than 10% | Acceptable |
Examples
Example 1: Sprint Planning
Prompt:
What duplication should we tackle this sprint?
Tool Call:
{
"sortBy": "lines",
"maxResults": 10
}
Example 2: Code Review Focus
Prompt:
Show me the worst duplicate offenders
Tool Call:
{
"sortBy": "density",
"maxResults": 5
}
Example 3: Large Codebase Analysis
Prompt:
Analyze duplication across our entire codebase
Tool Call:
{
"pageSize": 500,
"maxResults": 50
}
Refactoring Strategies
| Pattern | Solution |
|---|---|
| Similar handlers | Extract base class |
| Repeated validation | Create validator utility |
| Copy-pasted configs | Use shared constants |
| Similar components | Create generic component |
| Repeated API calls | Create service abstraction |
Best Practices
- Start with high density - Files with 25%+ duplication
- Consider impact - High line count = bigger savings
- Look for patterns - Similar files often share code
- Don't over-abstract - Some duplication is acceptable
Common Issues
"No duplication data"
Cause: Project hasn't been scanned with duplication detection.
Solution: Run sonar_scan_project first.
"Results seem incomplete"
Cause: Page size too small for large projects.
Solution: Increase pageSize parameter.
Related Tools
sonar_get_duplication_details- Detailed block analysissonar_get_project_metrics- Overall metricssonar_analyze_patterns- Issue patterns