ExCrap (ex_crap v0.1.0)

Copy Markdown View Source

Public API for calculating CRAP scores from complexity and coverage data.

The preferred project-level workflow is mix crap, which scans exported Mix/Erlang coverdata, enforces a maximum CRAP score threshold (default 30), and fails with a non-zero exit when any function exceeds it or any score calculation error occurs.

Use score/2 to calculate a CRAP score directly. Use analyze_string/2 or analyze_file/2 to analyze Elixir source with explicit function coverage data.

Summary

Functions

Analyzes one Elixir source file and combines each discovered function with explicit coverage.

Analyzes Elixir source and combines each discovered function with explicit coverage.

Calculates the canonical CRAP score for a complexity and coverage percentage.

Functions

analyze_file(path, coverage_by_function)

Analyzes one Elixir source file and combines each discovered function with explicit coverage.

This is a single-file convenience wrapper around the internal complexity analyzer. Valid files with no analyzable function or macro bodies return {:ok, []}. It does not perform project-wide scanning or coverage discovery.

analyze_string(source, coverage_by_function)

Analyzes Elixir source and combines each discovered function with explicit coverage.

coverage_by_function must be a map keyed by {module, function_name, arity}:

%{{Example, :visible?, 1} => 75.0}

Coverage values are percentages from 0 to 100. Functions without a matching coverage entry are scored as 0% covered. This function does not discover or ingest coverage automatically.

Valid source with no analyzable function or macro bodies returns {:ok, []}.

score(complexity, coverage_percent)

Calculates the canonical CRAP score for a complexity and coverage percentage.

The formula is:

complexity^2 * (1 - coverage_percent / 100)^3 + complexity

complexity must be numeric and non-negative. coverage_percent must be numeric and between 0 and 100 inclusive. Fractional scores are preserved.