Class: Reek::SmellDetectors::SubclassedFromCoreClass
- Inherits:
-
BaseDetector
- Object
- BaseDetector
- Reek::SmellDetectors::SubclassedFromCoreClass
- 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
Class Method Summary collapse
Instance Method Summary collapse
- #build_smell_warning(ancestor_name) ⇒ Object private
-
#sniff ⇒ Array<SmellWarning>
Checks
ctx
for either expressions:. - #sniff_superclass(superclass_name) ⇒ Object private
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
.contexts ⇒ Object
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 |
#sniff ⇒ Array<SmellWarning>
Checks ctx
for either expressions:
Foo = Class.new(Bar)
class Foo < Bar; end;
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 |