Class: Roodi::Checks::ParameterNumberCheck

Inherits:
Check
  • Object
show all
Defined in:
lib/roodi/checks/parameter_number_check.rb

Overview

Checks a method to make sure the number of parameters it has is under the specified limit.

A method taking too many parameters is a code smell that indicates it might be doing too much, or that the parameters should be grouped into one or more objects of their own. It probably needs some refactoring.

Constant Summary collapse

DEFAULT_PARAMETER_COUNT =
5

Constants inherited from Check

Check::NODE_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Check

#add_error, #end_file, #errors, #evaluate_end, #evaluate_node, #evaluate_node_end, #evaluate_node_start, make, #position, #start_file

Constructor Details

#initializeParameterNumberCheck

Returns a new instance of ParameterNumberCheck.


16
17
18
19
# File 'lib/roodi/checks/parameter_number_check.rb', line 16

def initialize
  super()
  self.parameter_count = DEFAULT_PARAMETER_COUNT
end

Instance Attribute Details

#parameter_countObject

Returns the value of attribute parameter_count


14
15
16
# File 'lib/roodi/checks/parameter_number_check.rb', line 14

def parameter_count
  @parameter_count
end

Instance Method Details

#evaluate_start(node) ⇒ Object


25
26
27
28
29
30
# File 'lib/roodi/checks/parameter_number_check.rb', line 25

def evaluate_start(node)
  method_name = node[1]
  arguments = node[2]
  actual_parameter_count = arguments.select {|arg| [Sexp, Symbol].include? arg.class}.count - 1
  add_error "Method name \"#{method_name}\" has #{actual_parameter_count} parameters.  It should have #{@parameter_count} or less." unless actual_parameter_count <= @parameter_count
end

#interesting_nodesObject


21
22
23
# File 'lib/roodi/checks/parameter_number_check.rb', line 21

def interesting_nodes
  [:defn]
end