Metastatic.Analysis.StateManagement (Metastatic v0.21.3)

View Source

State management analysis for containers (modules/classes).

Analyzes how containers manage state, identifying patterns and potential issues.

Patterns Detected

  • Stateless: No instance state (best for immutability)
  • Immutable State: State set once, never modified
  • Controlled Mutation: State modified through encapsulated methods
  • Uncontrolled Mutation: Direct state modification

Examples

# Stateless container
ast = {:container, [container_type: :class, name: "Math"], [
  {:function_def, [name: "add", params: [{:param, [], "x"}, {:param, [], "y"}], visibility: :public], [
   {:binary_op, [category: :arithmetic, operator: :+], [{:variable, [], "x"}, {:variable, [], "y"}]}]}
]}

doc = Document.new(ast, :python)
{:ok, result} = StateManagement.analyze(doc)
result.pattern  # => :stateless

# Controlled mutation
ast = {:container, [container_type: :class, name: "Counter"], [
  {:function_def, [name: "increment", params: [], visibility: :public], [
   {:augmented_assignment, [operator: :+], [
    {:attribute_access, [], [{:variable, [], "self"}, "count"]}, {:literal, [subtype: :integer], 1}]}]}
]}

{:ok, result} = StateManagement.analyze(doc)
result.pattern  # => :controlled_mutation

Summary

Functions

Analyze state management of a container.

Analyze state management of a container.

Functions

analyze(doc)

@spec analyze(Metastatic.Document.t()) :: {:ok, term()} | {:error, term()}

Analyze state management of a container.

Examples

iex> ast = {:container, [container_type: :class, name: "Empty"], [[]]}
iex> doc = Metastatic.Document.new(ast, :python)
iex> {:ok, result} = Metastatic.Analysis.StateManagement.analyze(doc)
iex> result.pattern
:stateless

analyze!(doc)

@spec analyze!(Metastatic.Document.t()) :: term()

Analyze state management of a container.

Examples

iex> ast = {:container, [container_type: :class, name: "Empty"], [[]]}
iex> doc = Metastatic.Document.new(ast, :python)
iex> {:ok, result} = Metastatic.Analysis.StateManagement.analyze(doc)
iex> result.pattern
:stateless

Unlike not-banged version, this one either returns a result or raises