Skip to main content

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

ParameterTypeRequiredDefaultDescription
sortBystringNodensitySort method: density, lines, blocks
pageSizenumberNo100Files to analyze (1-500)
maxResultsnumberNo10Limit output to top N files (1-50)

Sort Options

OptionDescriptionBest For
densityPercentage of file that's duplicatedFinding worst offenders
linesTotal duplicate line countFinding biggest impact
blocksNumber of duplicate blocksFinding 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

LevelDensityAction
🔴 HIGHMore than 25%Refactor immediately
🟠 MEDIUM15-25%Plan refactoring
🟡 LOW10-15%Consider when touching
🟢 MINIMALLess 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

PatternSolution
Similar handlersExtract base class
Repeated validationCreate validator utility
Copy-pasted configsUse shared constants
Similar componentsCreate generic component
Repeated API callsCreate service abstraction

Best Practices

  1. Start with high density - Files with 25%+ duplication
  2. Consider impact - High line count = bigger savings
  3. Look for patterns - Similar files often share code
  4. 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.