Class: MOSAIK::Metrics::Complexity

Inherits:
MOSAIK::Metric show all
Defined in:
lib/mosaik/metrics/complexity.rb,
lib/mosaik/metrics/complexity/parser.rb,
lib/mosaik/metrics/complexity/processor.rb

Overview

Cyclomatic complexity (T.J. McCabe, 1976)

Defined Under Namespace

Classes: Parser, Processor

Instance Attribute Summary

Attributes inherited from MOSAIK::Metric

#graph, #options

Instance Method Summary collapse

Methods inherited from MOSAIK::Metric

#initialize

Constructor Details

This class inherits a constructor from MOSAIK::Metric

Instance Method Details

#evaluateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mosaik/metrics/complexity.rb', line 9

def evaluate
  # Total complexity
  complexity = 0.0

  # Iterate over each cluster
  graph.clusters.each_value do |cluster|
    # Find all vertices in the cluster
    vertices_in_cluster = cluster.vertices

    # Calculate complexity for the cluster
    complexity_c = 0.0

    # Iterate over all vertices in the cluster
    vertices_in_cluster.each do |v|
      # Resolve the constant name to a file
      file = resolver.resolve_constant(v.id)

      warn "Could not resolve constant #{v.id}" and next unless file
      warn "#{v.id} (#{file}) is a directory" and next if File.directory?(file)

      # Parse file to extract complexities
      complexities = Parser
        .new
        .parse(file)

      # Calculate the complexity for the vertex
      complexity_v = complexities.any? ? (complexities.values.sum.to_f / complexities.size) : 0.0

      # Store complexity value in the vertex
      v.attributes[:complexity] = complexity_v

      # Store complexity value in the cluster
      complexity_c += complexity_v
    end

    # Store complexity value in the cluster
    cluster.attributes[:complexity] = complexity_c

    # Calculate complexity contribution from this cluster
    complexity += complexity_c
  end

  # Store complexity value in the graph
  graph.attributes[:complexity] = complexity

  # Return the total complexity
  complexity
end