Class: Reek::Smells::UtilityFunction

Inherits:
SmellDetector show all
Defined in:
lib/reek/smells/utility_function.rb

Overview

A Utility Function is any instance method that has no dependency on the state of the instance.

Currently UtilityFunction will warn about any method that:

  • is non-empty, and

  • does not override an inherited method, and

  • calls at least one method on another object, and

  • doesn’t use any of self’s instance variables, and

  • doesn’t use any of self’s methods

Constant Summary collapse

HELPER_CALLS_LIMIT_KEY =

The name of the config field that sets the maximum number of calls permitted within a helper method. Any method with more than this number of method calls on other objects will be considered a candidate Utility Function.

'max_helper_calls'
DEFAULT_HELPER_CALLS_LIMIT =
1

Constants inherited from SmellDetector

SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

class_name, #configure, #configure_with, contexts, #copy, create, #enabled?, #examine, #exception?, #found, #has_smell?, listen, #listen_to, #num_smells, #report_on, #smell_name, #smelly?, #supersede_with, #value

Constructor Details

#initialize(config = Duplication.default_config) ⇒ UtilityFunction

Returns a new instance of UtilityFunction.



33
34
35
# File 'lib/reek/smells/utility_function.rb', line 33

def initialize(config = Duplication.default_config)
  super(config)
end

Class Method Details

.default_configObject



29
30
31
# File 'lib/reek/smells/utility_function.rb', line 29

def self.default_config
  super.adopt(HELPER_CALLS_LIMIT_KEY => DEFAULT_HELPER_CALLS_LIMIT)
end

Instance Method Details

#examine_context(method) ⇒ Object

Checks whether the given method is a utility function. Remembers any smells found.



41
42
43
44
45
46
47
# File 'lib/reek/smells/utility_function.rb', line 41

def examine_context(method)
  return false if method.num_statements == 0 or
    method.depends_on_instance? or
    method.calls.keys.length <= value(HELPER_CALLS_LIMIT_KEY, method, DEFAULT_HELPER_CALLS_LIMIT)
    # SMELL: loads of calls to value{} with the above pattern
  found(method, "doesn't depend on instance state")
end