Class: Reek::SmellDetectors::RepeatedConditional

Inherits:
BaseDetector
  • Object
show all
Defined in:
lib/reek/smell_detectors/repeated_conditional.rb

Overview

Simulated Polymorphism occurs when

  • code uses a case statement (especially on a type field);

  • or code has several if statements in a row (especially if they’re comparing against the same value);

  • or code uses instance_of?, kind_of?, is_a?, or === to decide what type it’s working with;

  • or multiple conditionals in different places test the same value.

Conditional code is hard to read and understand, because the reader must hold more state in his head. When the same value is tested in multiple places throughout an application, any change to the set of possible values will require many methods and classes to change. Tests for the type of an object may indicate that the abstraction represented by that type is not completely defined (or understood).

RepeatedConditional checks for multiple conditionals testing the same value throughout a single class.

See Repeated-Conditional for details.

Constant Summary collapse

MAX_IDENTICAL_IFS_KEY =

The name of the config field that sets the maximum number of identical conditionals permitted within any single class.

'max_ifs'
DEFAULT_MAX_IFS =
2
BLOCK_GIVEN_CONDITION =
::Parser::AST::Node.new(:send, [nil, :block_given?])

Constants inherited from BaseDetector

BaseDetector::DEFAULT_EXCLUDE_SET, BaseDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from BaseDetector

#config, #context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseDetector

#config_for, configuration_keys, descendants, #enabled?, #exception?, #expression, inherited, #initialize, #run, smell_type, #smell_type, #smell_warning, #source_line, to_detector, todo_configuration_for, #value

Constructor Details

This class inherits a constructor from Reek::SmellDetectors::BaseDetector

Class Method Details

.contextsObject

:nodoc:



36
37
38
# File 'lib/reek/smell_detectors/repeated_conditional.rb', line 36

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

.default_configObject



40
41
42
# File 'lib/reek/smell_detectors/repeated_conditional.rb', line 40

def self.default_config
  super.merge(MAX_IDENTICAL_IFS_KEY => DEFAULT_MAX_IFS)
end

Instance Method Details

#conditional_countsObject (private)

Returns a Hash listing all of the conditional expressions in the given syntax tree together with the number of times each occurs. Ignores nested classes and modules.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/reek/smell_detectors/repeated_conditional.rb', line 75

def conditional_counts
  result = Hash.new { |hash, key| hash[key] = [] }
  collector = proc do |node|
    next unless (condition = node.condition)
    next if condition == BLOCK_GIVEN_CONDITION

    result[condition].push(condition.line)
  end
  [:if, :case].each { |stmt| context.local_nodes(stmt, &collector) }
  result
end

#max_identical_ifsObject (private)



65
66
67
# File 'lib/reek/smell_detectors/repeated_conditional.rb', line 65

def max_identical_ifs
  @max_identical_ifs ||= value(MAX_IDENTICAL_IFS_KEY, context)
end

#sniffArray<SmellWarning>

Checks the given class for multiple identical conditional tests.

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/reek/smell_detectors/repeated_conditional.rb', line 50

def sniff
  conditional_counts.select do |_key, lines|
    lines.length > max_identical_ifs
  end.map do |key, lines|
    occurs = lines.length
    expression = key.format_to_ruby
    smell_warning(
      lines: lines,
      message: "tests '#{expression}' at least #{occurs} times",
      parameters: { name: expression, count: occurs })
  end
end