Class: Reek::Smells::DataClump

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

Overview

A Data Clump occurs when the same two or three items frequently appear together in classes and parameter lists, or when a group of instance variable names start or end with similar substrings.

The recurrence of the items often means there is duplicate code spread around to handle them. There may be an abstraction missing from the code, making the system harder to understand.

Currently Reek looks for a group of two or more parameters with the same names that are expected by three or more methods of a class.

Constant Summary collapse

MAX_COPIES_KEY =

The name of the config field that sets the maximum allowed copies of any clump. No group of common parameters will be reported as a DataClump unless there are more than this many methods containing those parameters.

'max_copies'
DEFAULT_MAX_COPIES =
2
MIN_CLUMP_SIZE_KEY =

The name of the config field that sets the minimum clump size. No group of common parameters will be reported as a DataClump unless it contains at least this many parameters.

'min_clump_size'
DEFAULT_MIN_CLUMP_SIZE =
2

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, 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

.contextsObject

:nodoc:


37
38
39
# File 'lib/reek/smells/data_clump.rb', line 37

def self.contexts # :nodoc:
  [:class, :module]
end

.default_configObject


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

def self.default_config
  super.merge(
    MAX_COPIES_KEY => DEFAULT_MAX_COPIES,
    MIN_CLUMP_SIZE_KEY => DEFAULT_MIN_CLUMP_SIZE
  )
end

Instance Method Details

#examine_context(ctx) ⇒ Array<SmellWarning>

Checks the given class or module for multiple identical parameter sets.

Returns:


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/reek/smells/data_clump.rb', line 53

def examine_context(ctx)
  @max_copies = value(MAX_COPIES_KEY, ctx, DEFAULT_MAX_COPIES)
  @min_clump_size = value(MIN_CLUMP_SIZE_KEY, ctx, DEFAULT_MIN_CLUMP_SIZE)
  MethodGroup.new(ctx, @min_clump_size, @max_copies).clumps.map do |clump, methods|
    print_clump = DataClump.print_clump(clump)
    SmellWarning.new self,
                     context: ctx.full_name,
                     lines: methods.map(&:line),
                     message: "takes parameters #{print_clump} " \
                              "to #{methods.length} methods",
                     parameters: {
                       parameters: clump.map(&:to_s),
                       count: methods.length,
                       methods: methods.map(&:name)
                     }
  end
end