Metastatic.Analysis.StateManagement (Metastatic v0.10.4)

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: ["x", "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(language_or_doc, source_or_ast_or_opts \\ [], opts \\ [])

@spec analyze(Metastatic.language(), term(), keyword()) ::
  {:ok, map()} | {: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!(language_or_doc, source_or_ast_or_opts \\ [], opts \\ [])

@spec analyze!(Metastatic.language(), term(), keyword()) :: map()

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