Search documentation of Metastatic

# Metastatic.Analysis.Encapsulation.Result (Metastatic v0.13.2)

View Source

Result structure for encapsulation analysis.

Encapsulation is the principle of hiding internal state and requiring all interaction to go through well-defined interfaces (methods). This analyzer detects violations of encapsulation principles.

## Violation Types

- Public State - Instance variables accessible without getters/setters
- Missing Accessors - State modified directly instead of through methods
- Excessive Accessors - Too many getters/setters (data class smell)
- Leaky Abstraction - Internal implementation details exposed
- Visibility Violations - Private methods called externally

## Severity Levels

- `:critical` - Severe encapsulation breach
- `:high` - Significant encapsulation issue
- `:medium` - Moderate concern
- `:low` - Minor issue or code smell

## Examples

    iex> result = %Metastatic.Analysis.Encapsulation.Result{
    ...>   container_name: "User",
    ...>   violations: [],
    ...>   score: 100,
    ...>   assessment: :excellent
    ...> }
    iex> result.assessment
    :excellent

## Summary

### Types

assessment()
severity()
t()
violation()
violation_type()

### Functions

assess(score)

Assess encapsulation quality based on score.

calculate_score(violations)

Calculate encapsulation score based on violations.

generate_recommendations(violations, public_count, accessor_count)

Generate recommendations based on violations.

violation(type, severity, message, location \\ nil, suggestion \\ nil)

Create a violation record.

## Types

# assessment()

    @type assessment() :: :excellent | :good | :fair | :poor | :very_poor

# severity()

    @type severity() :: :critical | :high | :medium | :low

# t()

    @type t() :: %Metastatic.Analysis.Encapsulation.Result{
      accessor_count: non_neg_integer(),
      assessment: assessment(),
      container_name: String.t() | nil,
      container_type: :module | :class | :namespace | nil,
      private_methods: non_neg_integer(),
      public_methods: non_neg_integer(),
      recommendations: [String.t()],
      score: non_neg_integer(),
      state_variables: [String.t()],
      violations: [violation()]
    }

# violation()

    @type violation() :: %{
      type: violation_type(),
      severity: severity(),
      message: String.t(),
      location: String.t() | nil,
      suggestion: String.t() | nil
    }

# violation_type()

    @type violation_type() ::
      :public_state
      | :missing_accessor
      | :excessive_accessors
      | :leaky_abstraction
      | :visibility_violation
      | :direct_state_access

## Functions

# assess(score)

    @spec assess(non_neg_integer()) :: assessment()

Assess encapsulation quality based on score.

## Examples

    iex> Metastatic.Analysis.Encapsulation.Result.assess(95)
    :excellent

    iex> Metastatic.Analysis.Encapsulation.Result.assess(65)
    :fair

    iex> Metastatic.Analysis.Encapsulation.Result.assess(35)
    :very_poor

# calculate_score(violations)

    @spec calculate_score([violation()]) :: non_neg_integer()

Calculate encapsulation score based on violations.

Score starts at 100 and is reduced based on violation severity:

- Critical: -20 points
- High: -10 points
- Medium: -5 points
- Low: -2 points

## Examples

    iex> violations = [
    ...>   %{type: :public_state, severity: :critical, message: "Public var", location: nil, suggestion: nil},
    ...>   %{type: :missing_accessor, severity: :high, message: "Direct access", location: nil, suggestion: nil}
    ...> ]
    iex> Metastatic.Analysis.Encapsulation.Result.calculate_score(violations)
    70

# generate_recommendations(violations, public_count, accessor_count)

    @spec generate_recommendations([violation()], non_neg_integer(), non_neg_integer()) ::
      [String.t()]

Generate recommendations based on violations.

## Examples

    iex> violations = [%{type: :public_state, severity: :critical, message: "x", location: nil, suggestion: nil}]
    iex> recs = Metastatic.Analysis.Encapsulation.Result.generate_recommendations(violations, 5, 0)
    iex> Enum.any?(recs, &String.contains?(&1, "private"))
    true

# violation(type, severity, message, location \\ nil, suggestion \\ nil)

    @spec violation(
      violation_type(),
      severity(),
      String.t(),
      String.t() | nil,
      String.t() | nil
    ) ::
      violation()

Create a violation record.

## Examples

    iex> Metastatic.Analysis.Encapsulation.Result.violation(:public_state, :critical, "Variable 'x' is public", "x", "Make 'x' private")
    %{type: :public_state, severity: :critical, message: "Variable 'x' is public", location: "x", suggestion: "Make 'x' private"}