Class: Reek::Smells::NestedIterators

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

Overview

A Nested Iterator occurs when a block contains another block.

NestedIterators reports failing methods only once.

Constant Summary collapse

MAX_ALLOWED_NESTING_KEY =

The name of the config field that sets the maximum depth of nested iterators to be permitted within any single method.

'max_allowed_nesting'
DEFAULT_MAX_ALLOWED_NESTING =
1
IGNORE_ITERATORS_KEY =

The name of the config field that sets the names of any methods for which nesting should not be considered

'ignore_iterators'
DEFAULT_IGNORE_ITERATORS =
[]

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_category, smell_type, #value

Constructor Details

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

Class Method Details

.default_configObject


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

def self.default_config
  super.merge(
    MAX_ALLOWED_NESTING_KEY => DEFAULT_MAX_ALLOWED_NESTING,
    IGNORE_ITERATORS_KEY => DEFAULT_IGNORE_ITERATORS
  )
end

Instance Method Details

#examine_context(ctx) ⇒ Array<SmellWarning>

Checks whether the given block is inside another.

Returns:


34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/reek/smells/nested_iterators.rb', line 34

def examine_context(ctx)
  exp, depth = *find_deepest_iterator(ctx)

  if depth && depth > value(MAX_ALLOWED_NESTING_KEY, ctx, DEFAULT_MAX_ALLOWED_NESTING)
    [SmellWarning.new(self,
                      context: ctx.full_name,
                      lines: [exp.line],
                      message: "contains iterators nested #{depth} deep",
                      parameters: { count: depth })]
  else
    []
  end
  # BUG: no longer reports nesting outside methods (eg. in Optparse)
end