Class: ImproveYourCode::SmellDetectors::TooManyInstanceVariables

Inherits:
BaseDetector
  • Object
show all
Defined in:
lib/improve_your_code/smell_detectors/too_many_instance_variables.rb

Constant Summary collapse

MAX_ALLOWED_IVARS_KEY =
'max_instance_variables'
DEFAULT_MAX_IVARS =
3

Constants inherited from BaseDetector

BaseDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from BaseDetector

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseDetector

configuration_keys, descendants, inherited, #initialize, #run, #smell_type, smell_type, to_detector, todo_configuration_for, valid_detector?

Constructor Details

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

Class Method Details

.contextsObject



11
12
13
# File 'lib/improve_your_code/smell_detectors/too_many_instance_variables.rb', line 11

def self.contexts
  [:class]
end

.default_configObject



15
16
17
18
19
20
# File 'lib/improve_your_code/smell_detectors/too_many_instance_variables.rb', line 15

def self.default_config
  super.merge(
    MAX_ALLOWED_IVARS_KEY => DEFAULT_MAX_IVARS,
    EXCLUDE_KEY => []
  )
end

Instance Method Details

#sniffObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/improve_your_code/smell_detectors/too_many_instance_variables.rb', line 22

def sniff
  variables = context.local_nodes(:ivasgn, [:or_asgn]).map(&:name)
  count = variables.uniq.size

  return [] if count <= max_allowed_ivars

  message = "has at least #{count} instance variables. "\
            "We propose to use Facade Pattern"

  [
    smell_warning(
      context: context,
      lines: [source_line],
      message: message,
      parameters: { count: count }
    )
  ]
end