Class: Reek::SmellDetectors::SubclassedFromCoreClass

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

Overview

Subclassing core classes in Ruby can lead to unexpected side effects. Knowing that Ruby has a core library, which is written in C, and a standard library, which is written in Ruby, if you do not know exactly how these core classes operate at the C level, you are gonna have a bad time.

Source: words.steveklabnik.com/beware-subclassing-ruby-core-classes

Constant Summary collapse

CORE_CLASSES =
['Array', 'Hash', 'String'].freeze

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



18
19
20
# File 'lib/reek/smell_detectors/subclassed_from_core_class.rb', line 18

def self.contexts
  [:class, :casgn]
end

Instance Method Details

#build_smell_warning(ancestor_name) ⇒ Object (private)



45
46
47
48
49
# File 'lib/reek/smell_detectors/subclassed_from_core_class.rb', line 45

def build_smell_warning(ancestor_name)
  smell_warning(lines:      [source_line],
                message:    "inherits from core class '#{ancestor_name}'",
                parameters: { ancestor: ancestor_name })
end

#sniffArray<SmellWarning>

Checks ctx for either expressions:

Foo = Class.new(Bar)

class Foo < Bar; end;

Returns:



29
30
31
32
33
34
35
# File 'lib/reek/smell_detectors/subclassed_from_core_class.rb', line 29

def sniff
  superclass = expression.superclass

  return [] unless superclass

  sniff_superclass superclass.name
end

#sniff_superclass(superclass_name) ⇒ Object (private)



39
40
41
42
43
# File 'lib/reek/smell_detectors/subclassed_from_core_class.rb', line 39

def sniff_superclass(superclass_name)
  return [] unless CORE_CLASSES.include?(superclass_name)

  [build_smell_warning(superclass_name)]
end