Sha256: 9f8aa723d78a68c744e423a477ba885a7e88ce1314735a8083fdbfe72e991840

Contents?: true

Size: 1.06 KB

Versions: 7

Compression:

Stored size: 1.06 KB

Contents

require 'roodi/checks/check'

module Roodi
  module Checks
    # 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. 
    class ParameterNumberCheck < Check
      DEFAULT_PARAMETER_COUNT = 5
      
      def initialize(options = {})
        super()
        @parameter_count = options['parameter_count'] || DEFAULT_PARAMETER_COUNT
      end
      
      def interesting_nodes
        [:defn]
      end

      def evaluate(node)
        method_name = node[1]
        arguments = node[2][1][1]
        parameter_count = arguments.inject(-1) { |count, each| count = count + (each.class == Symbol ? 1 : 0) }
        add_error "Method name \"#{method_name}\" has #{parameter_count} parameters.  It should have #{@parameter_count} or less." unless parameter_count <= @parameter_count
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
roodi-1.3.2 lib/roodi/checks/parameter_number_check.rb
roodi-1.3.0 lib/roodi/checks/parameter_number_check.rb
roodi-1.3.6 lib/roodi/checks/parameter_number_check.rb
roodi-1.3.3 lib/roodi/checks/parameter_number_check.rb
roodi-1.3.4 lib/roodi/checks/parameter_number_check.rb
roodi-1.3.7 lib/roodi/checks/parameter_number_check.rb
roodi-1.3.5 lib/roodi/checks/parameter_number_check.rb