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
@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
@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
:statelessUnlike not-banged version, this one either returns a result or raises