Class: Reek::Smells::TooManyStatements

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

Overview

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

TooManyStatements 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'
DEFAULT_MAX_STATEMENTS =
5

Constants inherited from SmellDetector

SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from SmellDetector

#smell_category, #smell_type, #smells_found, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

#config_for, #configure_with, contexts, default_smell_category, #enabled?, #enabled_for?, #examine, #exception?, #initialize, #register, #report_on, smell_type, #value

Constructor Details

This class inherits a constructor from Reek::Smells::SmellDetector

Class Method Details

.default_configObject


21
22
23
24
25
26
# File 'lib/reek/smells/too_many_statements.rb', line 21

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

.smell_categoryObject


17
18
19
# File 'lib/reek/smells/too_many_statements.rb', line 17

def self.smell_category
  'LongMethod'
end

Instance Method Details

#examine_context(ctx) ⇒ Array<SmellWarning>

Checks the length of the given method.

Returns:


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/reek/smells/too_many_statements.rb', line 33

def examine_context(ctx)
  @max_allowed_statements = value(MAX_ALLOWED_STATEMENTS_KEY,
                                  ctx,
                                  DEFAULT_MAX_STATEMENTS)
  count = ctx.num_statements
  return [] if count <= @max_allowed_statements
  [SmellWarning.new(self,
                    context: ctx.full_name,
                    lines: [ctx.exp.line],
                    message: "has approx #{count} statements",
                    parameters: { count: count })]
end