sonar_get_duplication_details
Analyze specific file duplication with exact line ranges and targeted refactoring recommendations.
Description
Provides detailed analysis of duplicate code blocks within a specific file. Shows exact line ranges, affected files, and actionable refactoring recommendations for each duplicate block.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
fileKey | string | Yes | - | SonarQube file key (e.g., project:src/file.ts) |
includeRecommendations | boolean | No | true | Include refactoring suggestions |
Finding File Keys
File keys follow the format: {projectKey}:{filePath}
Example: my-project-a1b2c3d4:src/utils/validators.ts
Get file keys from:
sonar_get_duplication_summaryresultssonar_scan_projectoutput- SonarQube web interface
Usage
Basic Usage
Show me duplicate code details in validators.ts
Tool call:
{
"fileKey": "my-project:src/utils/validators.ts"
}
Without Recommendations
Just show me the duplicate blocks, no suggestions
Tool call:
{
"fileKey": "my-project:src/utils/validators.ts",
"includeRecommendations": false
}
Response Format
The tool returns detailed information about each duplicate block:
Header Information:
- File path and key
- Overall duplication density percentage
- Total duplicate lines and blocks count
Per Block Details:
- Line range (e.g., Lines 15-45)
- List of other files containing the same code
- Code preview showing the actual duplicated content
- Refactoring recommendation with suggested implementation
Summary Section:
- Total blocks found
- Unique patterns identified
- Files affected count
- Estimated line reduction after refactoring
Action Plan:
- Prioritized list of refactoring steps
- Grouped recommendations for related blocks
Block Analysis
Each block includes:
| Element | Description |
|---|---|
| Line range | Exact location in file |
| Duplicated in | Other files with same code |
| Code preview | Actual duplicate code |
| Recommendation | How to refactor |
Examples
Example 1: Investigate High Duplication
Prompt:
validators.ts shows 45% duplication. Show me what's duplicated.
Tool Call:
{
"fileKey": "my-project:src/utils/validators.ts"
}
Example 2: Quick Block View
Prompt:
Just show me the duplicate blocks, I'll decide how to fix them
Tool Call:
{
"fileKey": "my-project:src/services/PaymentService.ts",
"includeRecommendations": false
}
Example 3: Refactoring Planning
Prompt:
I need to refactor userHandler.ts - show me all duplications with suggestions
Tool Call:
{
"fileKey": "my-project:src/api/handlers/userHandler.ts",
"includeRecommendations": true
}
Refactoring Patterns
| Pattern | When to Use | Example |
|---|---|---|
| Extract function | Same logic repeated | Validation helpers |
| Create module | Related functions duplicated | Shared utilities |
| Use inheritance | Similar class methods | Base service class |
| Configuration | Repeated config objects | Shared constants |
| Composition | Similar components | Generic components |
Best Practices
- Review all locations - Understand where code is duplicated
- Consider context - Test duplication may be intentional
- Plan holistically - One fix can address multiple blocks
- Test after refactoring - Ensure behavior is preserved
Common Issues
"File not found"
Cause: Invalid file key format.
Solution: Use exact key from sonar_get_duplication_summary.
"No duplication data"
Cause: File has no detected duplication.
Solution: File is clean or below detection threshold.
"Recommendations seem wrong"
Cause: AI suggestions are heuristic-based.
Solution: Use as starting point, adapt to your architecture.
Related Tools
sonar_get_duplication_summary- Find files to analyzesonar_get_issue_details- Related code smellssonar_scan_project- Re-scan after refactoring