Class: RuboCop::Cop::Metrics::Utils::AbcSizeCalculator
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Inherits: | Object |
Defined in: | lib/rubocop/cop/metrics/utils/abc_size_calculator.rb |
Overview
ABC is .. a software size metric .. computed by counting the number of assignments, branches and conditions for a section of code. http://c2.com/cgi/wiki?AbcMetric
We separate the calculator from the cop so that the calculation, the formula itself, is easier to test.
Constant Summary
-
BRANCH_NODES =
private
Branch — an explicit forward program branch out of scope — a function call, class method call .. http://c2.com/cgi/wiki?AbcMetric
%i[send csend yield].freeze
-
CONDITION_NODES =
private
Condition — a logical/Boolean test, == != ⇐ >= < > else case default try catch ? and unary conditionals. http://c2.com/cgi/wiki?AbcMetric
CyclomaticComplexity::COUNTED_NODES.freeze
IteratingBlock
- Included
RepeatedAttributeDiscount
- Included
Class Method Summary
Instance Attribute Summary
Instance Method Summary
- #calculate
- #else_branch?(node) ⇒ Boolean
- #evaluate_branch_nodes(node)
- #evaluate_condition_node(node)
- #argument?(node) ⇒ Boolean private
- #assignment?(node) ⇒ Boolean private
- #branch?(node) ⇒ Boolean private
- #calculate_node(node) private
- #capturing_variable?(name) ⇒ Boolean private
- #compound_assignment(node) private
- #condition?(node) ⇒ Boolean private
- #simple_assignment?(node) ⇒ Boolean private
- #visit_depth_last(node) {|node| ... } private
RepeatedAttributeDiscount
- Included
#calculate_node, #evaluate_branch_nodes, | |
#initialize | Plug into the calculator. |
#attribute_call?, #discount_repeated_attribute?, | |
#find_attributes | Returns the "known_attributes" for the |
#root_node?, | |
#setter_to_getter | or |
#update_repeated_attribute |
RepeatedCsendDiscount
- Included
IteratingBlock
- Included
#block_method_name | Returns the name of the method called with a block if node is a block node, or a block-pass node. |
#iterating_block? | Returns nil if node is neither a block node or a block-pass node. |
#iterating_method? | Returns true iff name is a known iterating type (e.g. |
Constructor Details
.new(node) ⇒ AbcSizeCalculator
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 34
def initialize(node) @assignment = 0 @branch = 0 @condition = 0 @node = node reset_repeated_csend end
Class Method Details
.calculate(node, discount_repeated_attributes: false)
[ GitHub ]# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 30
def self.calculate(node, discount_repeated_attributes: false) new(node, discount_repeated_attributes: discount_repeated_attributes).calculate end
Instance Method Details
#argument?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 127
def argument?(node) node.argument_type? && capturing_variable?(node.children.first) end
#assignment?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 86
def assignment?(node) return compound_assignment(node) if node.masgn_type? || node.shorthand_asgn? node.for_type? || (node.respond_to?(:setter_method?) && node.setter_method?) || simple_assignment?(node) || argument?(node) end
#branch?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 123
def branch?(node) BRANCH_NODES.include?(node.type) end
#calculate
[ GitHub ]# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 42
def calculate visit_depth_last(@node) { |child| calculate_node(child) } [ Math.sqrt((@assignment**2) + (@branch**2) + (@condition**2)).round(2), "<#{@assignment}, #{@branch}, #{@condition}>" ] end
#calculate_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 76
def calculate_node(node) @assignment += 1 if assignment?(node) if branch?(node) evaluate_branch_nodes(node) elsif condition?(node) evaluate_condition_node(node) end end
#capturing_variable?(name) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 119
def capturing_variable?(name) name && !name.start_with?('_') end
#compound_assignment(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 95
def compound_assignment(node) # Methods setter cannot be detected for multiple assignments # and shorthand assigns, so we'll count them here instead children = node.masgn_type? ? node.assignments : node.children will_be_miscounted = children.count do |child| child.respond_to?(:setter_method?) && !child.setter_method? end @assignment += will_be_miscounted false end
#condition?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 131
def condition?(node) return false if (node) == false CONDITION_NODES.include?(node.type) end
#else_branch?(node) ⇒ Boolean
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 65
def else_branch?(node) %i[case if].include?(node.type) && node.else? && node.loc.else.is?('else') end
#evaluate_branch_nodes(node)
[ GitHub ]# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 51
def evaluate_branch_nodes(node) if node.comparison_method? @condition += 1 else @branch += 1 @condition += 1 if node.csend_type? && !discount_for_repeated_csend?(node) end end
#evaluate_condition_node(node)
[ GitHub ]# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 60
def evaluate_condition_node(node) @condition += 1 if else_branch?(node) @condition += 1 end
#simple_assignment?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 108
def simple_assignment?(node) if !node.equals_asgn? false elsif node.lvasgn_type? reset_on_lvasgn(node) capturing_variable?(node.children.first) else true end end
#visit_depth_last(node) {|node| ... } (private)
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 71
def visit_depth_last(node, &block) node.each_child_node { |child| visit_depth_last(child, &block) } yield node end