Packages
metastatic
0.4.2
0.26.0
0.25.0
0.24.1
0.24.0
0.23.0
0.22.2
0.22.1
0.22.0
0.21.3
0.21.2
0.21.1
0.21.0
0.20.3
0.20.2
0.20.1
0.20.0
0.19.0
0.18.0
0.17.0
0.16.0
0.15.1
0.15.0
0.14.2
0.14.1
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.0
0.11.0
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.2
0.9.1
0.9.0
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.2
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
Cross-language code meta-model library using unified MetaAST representation. Parse, transform, and translate code across Python, Elixir, Ruby, Erlang, Haskell, and more via a shared three-tuple AST format.
Current section
Files
Jump to
Current section
Files
lib/metastatic/analysis/cohesion/formatter.ex
defmodule Metastatic.Analysis.Cohesion.Formatter do
@moduledoc """
Formatters for cohesion analysis results.
Supports multiple output formats:
- `:text` - Human-readable text format
- `:json` - Machine-readable JSON format
- `:detailed` - Detailed text with all metrics and recommendations
## Examples
iex> result = %Metastatic.Analysis.Cohesion.Result{
...> container_name: "Calculator",
...> lcom: 0,
...> tcc: 1.0,
...> assessment: :excellent
...> }
iex> Metastatic.Analysis.Cohesion.Formatter.format(result, :text)
"Calculator: Excellent cohesion (LCOM: 0, TCC: 1.00)"
"""
alias Metastatic.Analysis.Cohesion.Result
@doc """
Format cohesion analysis result.
## Formats
- `:text` - Single line summary
- `:json` - JSON object
- `:detailed` - Multi-line detailed report
## Examples
iex> result = %Metastatic.Analysis.Cohesion.Result{container_name: "Foo", lcom: 0, tcc: 1.0, assessment: :excellent}
iex> Metastatic.Analysis.Cohesion.Formatter.format(result, :text)
"Foo: Excellent cohesion (LCOM: 0, TCC: 1.00)"
"""
@spec format(Result.t(), :text | :json | :detailed) :: String.t()
def format(result, :text), do: format_text(result)
def format(result, :json), do: format_json(result)
def format(result, :detailed), do: format_detailed(result)
# Private formatters
defp format_text(result) do
assessment_text = format_assessment(result.assessment)
"#{result.container_name}: #{assessment_text} cohesion (LCOM: #{result.lcom}, TCC: #{format_float(result.tcc)})"
end
defp format_json(result) do
Jason.encode!(%{
container_name: result.container_name,
container_type: result.container_type,
metrics: %{
lcom: result.lcom,
tcc: result.tcc,
lcc: result.lcc,
method_count: result.method_count,
method_pairs: result.method_pairs,
connected_pairs: result.connected_pairs
},
shared_state: result.shared_state,
assessment: result.assessment,
warnings: result.warnings,
recommendations: result.recommendations
})
end
defp format_detailed(result) do
lines = [
"Cohesion Analysis: #{result.container_name} (#{result.container_type})",
"",
"Metrics:",
" LCOM (Lack of Cohesion): #{result.lcom}",
" TCC (Tight Class Cohesion): #{format_float(result.tcc)} (#{format_percentage(result.tcc)})",
" LCC (Loose Class Cohesion): #{format_float(result.lcc)} (#{format_percentage(result.lcc)})",
" Methods: #{result.method_count}",
" Method Pairs: #{result.method_pairs}",
" Connected Pairs: #{result.connected_pairs}",
"",
"Shared State Variables: #{format_list(result.shared_state)}",
"",
"Assessment: #{format_assessment(result.assessment)}"
]
lines =
if Enum.empty?(result.warnings) do
lines
else
lines ++
[
"",
"Warnings:"
] ++ Enum.map(result.warnings, fn w -> " - #{w}" end)
end
lines =
if Enum.empty?(result.recommendations) do
lines
else
lines ++
[
"",
"Recommendations:"
] ++ Enum.map(result.recommendations, fn r -> " - #{r}" end)
end
Enum.join(lines, "\n")
end
defp format_assessment(assessment) do
case assessment do
:excellent -> "Excellent"
:good -> "Good"
:fair -> "Fair"
:poor -> "Poor"
:very_poor -> "Very Poor"
end
end
defp format_float(value) when is_float(value), do: :erlang.float_to_binary(value, decimals: 2)
defp format_float(value), do: to_string(value)
defp format_percentage(value) when is_float(value) do
percentage = value * 100
"#{:erlang.float_to_binary(percentage, decimals: 0)}%"
end
defp format_list([]), do: "none"
defp format_list(items), do: Enum.join(items, ", ")
end