Class: Reek::Smells::NilCheck

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

Overview

Checking for nil is a special kind of type check, and therefore a case of SimulatedPolymorphism.

Defined Under Namespace

Modules: NilCallNodeDetector, NilWhenNodeDetector Classes: NodeFinder

Constant Summary

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

.smell_categoryObject


9
10
11
# File 'lib/reek/smells/nil_check.rb', line 9

def self.smell_category
  'SimulatedPolymorphism'
end

Instance Method Details

#examine_context(ctx) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/reek/smells/nil_check.rb', line 13

def examine_context(ctx)
  call_node_finder = NodeFinder.new(ctx, :send, NilCallNodeDetector)
  case_node_finder = NodeFinder.new(ctx, :when, NilWhenNodeDetector)
  smelly_nodes = call_node_finder.smelly_nodes + case_node_finder.smelly_nodes

  smelly_nodes.map do |node|
    SmellWarning.new self,
                     context: ctx.full_name,
                     lines: [node.line],
                     message: 'performs a nil-check.'
  end
end