Class: Reek::Smells::RepeatedConditional Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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 =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

'max_ifs'
DEFAULT_MAX_IFS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

2
BLOCK_GIVEN_CONDITION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

::Parser::AST::Node.new(:send, [nil, :block_given?])

Constants inherited from SmellDetector

SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from SmellDetector

#smells_found, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

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

Constructor Details

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

Class Method Details

.contextsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



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

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

.default_configObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



44
45
46
# File 'lib/reek/smells/repeated_conditional.rb', line 44

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

.smell_categoryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
# File 'lib/reek/smells/repeated_conditional.rb', line 34

def self.smell_category
  'SimulatedPolymorphism'
end

Instance Method Details

#conditional_counts(sexp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



73
74
75
76
77
78
79
80
81
82
# File 'lib/reek/smells/repeated_conditional.rb', line 73

def conditional_counts(sexp)
  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| sexp.local_nodes(stmt, &collector) }
  result
end

#examine_context(ctx) ⇒ Array<SmellWarning>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks the given class for multiple identical conditional tests.

Returns:



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

def examine_context(ctx)
  @max_identical_ifs = value(MAX_IDENTICAL_IFS_KEY, ctx, DEFAULT_MAX_IFS)
  conditional_counts(ctx).select do |_key, lines|
    lines.length > @max_identical_ifs
  end.map do |key, lines|
    occurs = lines.length
    expression = key.format_to_ruby
    SmellWarning.new self,
                     context: ctx.full_name,
                     lines: lines,
                     message: "tests #{expression} at least #{occurs} times",
                     parameters: { name: expression, count: occurs }
  end
end