Metastatic.Analysis.Duplication.Result (Metastatic v0.10.4)

View Source

Result struct for code duplication detection analysis.

Contains information about detected clones including type, similarity score, locations, and metrics.

Fields

  • duplicate? - Whether code is duplicated
  • clone_type - Type of clone detected (:type_i, :type_ii, :type_iii, :type_iv, or nil)
  • similarity_score - Similarity score between 0.0 and 1.0
  • differences - List of differences for Type III clones
  • locations - Source locations of the clones
  • fingerprints - Structural fingerprints of the clones
  • metrics - Size and complexity metrics
  • summary - Human-readable summary

Examples

iex> result = Metastatic.Analysis.Duplication.Result.no_duplicate()
iex> result.duplicate?
false

iex> result = Metastatic.Analysis.Duplication.Result.exact_clone()
iex> result.clone_type
:type_i
iex> result.similarity_score
1.0

Summary

Types

Difference information for Type III clones.

Location information for a code clone.

Metrics information for clones.

t()

Result struct for duplication detection.

Functions

Creates a new result indicating an exact clone (Type I).

Creates a new result indicating a near-miss clone (Type III).

Creates a new result indicating no duplication.

Creates a new result indicating a renamed clone (Type II).

Creates a new result indicating a semantic clone (Type IV).

Adds differences to a result (for Type III clones).

Adds multiple differences to a result.

Adds fingerprints to a result.

Adds location information to a result.

Adds multiple locations to a result.

Adds metrics to a result.

Updates the summary of a result.

Types

difference()

@type difference() :: %{
  type: atom(),
  description: String.t(),
  location: location() | nil
}

Difference information for Type III clones.

location()

@type location() :: %{
  file: String.t() | nil,
  start_line: non_neg_integer() | nil,
  end_line: non_neg_integer() | nil,
  language: atom() | nil
}

Location information for a code clone.

metrics()

@type metrics() :: %{
  size: non_neg_integer(),
  complexity: non_neg_integer() | nil,
  variables: non_neg_integer() | nil
}

Metrics information for clones.

t()

@type t() :: %Metastatic.Analysis.Duplication.Result{
  clone_type: Metastatic.Analysis.Duplication.Types.clone_type() | nil,
  differences: [difference()],
  duplicate?: boolean(),
  fingerprints: %{exact: String.t() | nil, normalized: String.t() | nil},
  locations: [location()],
  metrics: metrics() | nil,
  similarity_score: float(),
  summary: String.t()
}

Result struct for duplication detection.

Functions

exact_clone()

@spec exact_clone() :: t()

Creates a new result indicating an exact clone (Type I).

Examples

iex> result = Metastatic.Analysis.Duplication.Result.exact_clone()
iex> result.duplicate?
true
iex> result.clone_type
:type_i
iex> result.similarity_score
1.0

near_miss_clone(similarity)

@spec near_miss_clone(float()) :: t()

Creates a new result indicating a near-miss clone (Type III).

Examples

iex> result = Metastatic.Analysis.Duplication.Result.near_miss_clone(0.85)
iex> result.duplicate?
true
iex> result.clone_type
:type_iii
iex> result.similarity_score
0.85

no_duplicate()

@spec no_duplicate() :: t()

Creates a new result indicating no duplication.

Examples

iex> result = Metastatic.Analysis.Duplication.Result.no_duplicate()
iex> result.duplicate?
false
iex> result.similarity_score
0.0

renamed_clone()

@spec renamed_clone() :: t()

Creates a new result indicating a renamed clone (Type II).

Examples

iex> result = Metastatic.Analysis.Duplication.Result.renamed_clone()
iex> result.duplicate?
true
iex> result.clone_type
:type_ii
iex> result.similarity_score
1.0

semantic_clone(similarity)

@spec semantic_clone(float()) :: t()

Creates a new result indicating a semantic clone (Type IV).

Examples

iex> result = Metastatic.Analysis.Duplication.Result.semantic_clone(0.9)
iex> result.duplicate?
true
iex> result.clone_type
:type_iv
iex> result.similarity_score
0.9

with_difference(result, difference)

@spec with_difference(t(), difference()) :: t()

Adds differences to a result (for Type III clones).

Examples

iex> result = Metastatic.Analysis.Duplication.Result.near_miss_clone(0.85)
iex> diff = %{type: :statement_added, description: "Extra return statement"}
iex> result = Metastatic.Analysis.Duplication.Result.with_difference(result, diff)
iex> [d] = result.differences
iex> d.type
:statement_added

with_differences(result, differences)

@spec with_differences(t(), [difference()]) :: t()

Adds multiple differences to a result.

Examples

iex> result = Metastatic.Analysis.Duplication.Result.near_miss_clone(0.85)
iex> diffs = [
...>   %{type: :statement_added, description: "Extra return"},
...>   %{type: :variable_renamed, description: "x renamed to y"}
...> ]
iex> result = Metastatic.Analysis.Duplication.Result.with_differences(result, diffs)
iex> length(result.differences)
2

with_fingerprints(result, fingerprints)

@spec with_fingerprints(t(), map()) :: t()

Adds fingerprints to a result.

Examples

iex> result = Metastatic.Analysis.Duplication.Result.exact_clone()
iex> fingerprints = %{exact: "abc123", normalized: "def456"}
iex> result = Metastatic.Analysis.Duplication.Result.with_fingerprints(result, fingerprints)
iex> result.fingerprints.exact
"abc123"

with_location(result, location)

@spec with_location(t(), location()) :: t()

Adds location information to a result.

Examples

iex> result = Metastatic.Analysis.Duplication.Result.exact_clone()
iex> location = %{file: "test.ex", start_line: 10, end_line: 20, language: :elixir}
iex> result = Metastatic.Analysis.Duplication.Result.with_location(result, location)
iex> [loc] = result.locations
iex> loc.file
"test.ex"

with_locations(result, locations)

@spec with_locations(t(), [location()]) :: t()

Adds multiple locations to a result.

Examples

iex> result = Metastatic.Analysis.Duplication.Result.exact_clone()
iex> locations = [
...>   %{file: "test1.ex", start_line: 10, end_line: 20, language: :elixir},
...>   %{file: "test2.ex", start_line: 30, end_line: 40, language: :elixir}
...> ]
iex> result = Metastatic.Analysis.Duplication.Result.with_locations(result, locations)
iex> length(result.locations)
2

with_metrics(result, metrics)

@spec with_metrics(t(), metrics()) :: t()

Adds metrics to a result.

Examples

iex> result = Metastatic.Analysis.Duplication.Result.exact_clone()
iex> metrics = %{size: 100, complexity: 5, variables: 3}
iex> result = Metastatic.Analysis.Duplication.Result.with_metrics(result, metrics)
iex> result.metrics.size
100

with_summary(result, summary)

@spec with_summary(t(), String.t()) :: t()

Updates the summary of a result.

Examples

iex> result = Metastatic.Analysis.Duplication.Result.exact_clone()
iex> result = Metastatic.Analysis.Duplication.Result.with_summary(result, "Custom summary")
iex> result.summary
"Custom summary"