Class: Reek::Smells::LongMethod

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

Overview

A Long Method is any method that has a large number of lines.

Currently LongMethod reports any method with more than 5 statements.

Constant Summary collapse

MAX_ALLOWED_STATEMENTS_KEY =

The name of the config field that sets the maximum number of statements permitted in any method.

'max_statements'

Constants inherited from SmellDetector

SmellDetector::ENABLED_KEY, SmellDetector::EXCLUDE_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

class_name, contexts, #examine, #exception?, listen, #smell_name

Constructor Details

#initialize(config = LongMethod.default_config) ⇒ LongMethod

Returns a new instance of LongMethod.



26
27
28
29
# File 'lib/reek/smells/long_method.rb', line 26

def initialize(config = LongMethod.default_config)
  super
  @max_statements = config[MAX_ALLOWED_STATEMENTS_KEY]
end

Class Method Details

.default_configObject



19
20
21
22
23
24
# File 'lib/reek/smells/long_method.rb', line 19

def self.default_config
  super.adopt(
    MAX_ALLOWED_STATEMENTS_KEY => 5,
    EXCLUDE_KEY => ['initialize']
  )
end

Instance Method Details

#examine_context(method, report) ⇒ Object

Checks the length of the given method. Any smells found are added to the report.



35
36
37
38
39
40
# File 'lib/reek/smells/long_method.rb', line 35

def examine_context(method, report)
  num = method.num_statements
  return false if num <= @max_statements
  report << SmellWarning.new(self, method,
              "has approx #{num} statements")
end