Skip to main content

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

ParameterTypeRequiredDefaultDescription
fileKeystringYes-SonarQube file key (e.g., project:src/file.ts)
includeRecommendationsbooleanNotrueInclude 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_summary results
  • sonar_scan_project output
  • 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:

ElementDescription
Line rangeExact location in file
Duplicated inOther files with same code
Code previewActual duplicate code
RecommendationHow 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

PatternWhen to UseExample
Extract functionSame logic repeatedValidation helpers
Create moduleRelated functions duplicatedShared utilities
Use inheritanceSimilar class methodsBase service class
ConfigurationRepeated config objectsShared constants
CompositionSimilar componentsGeneric components

Best Practices

  1. Review all locations - Understand where code is duplicated
  2. Consider context - Test duplication may be intentional
  3. Plan holistically - One fix can address multiple blocks
  4. 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.