Class: Reek::Smells::ClassVariable

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

Overview

Class variables form part of the global runtime state, and as such make it easy for one part of the system to accidentally or inadvertently depend on another part of the system. So the system becomes more prone to problems where changing something over here breaks something over there. In particular, class variables can make it hard to set up tests (because the context of the test includes all global state).

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, default_config, default_smell_category, #enabled?, #enabled_for?, #examine, #exception?, #initialize, #register, #report_on, smell_category, smell_type, #value

Constructor Details

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

Class Method Details

.contextsObject

:nodoc:


16
17
18
# File 'lib/reek/smells/class_variable.rb', line 16

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

Instance Method Details

#class_variables_in(ast) ⇒ Object

Collects the names of the class variables declared and/or used in the given module.


39
40
41
42
43
44
45
46
47
48
# File 'lib/reek/smells/class_variable.rb', line 39

def class_variables_in(ast)
  result = Hash.new { |hash, key| hash[key] = [] }
  collector = proc do |cvar_node|
    result[cvar_node.name].push(cvar_node.line)
  end
  [:cvar, :cvasgn, :cvdecl].each do |stmt_type|
    ast.each_node(stmt_type, [:class, :module], &collector)
  end
  result
end

#examine_context(ctx) ⇒ Array<SmellWarning>

Checks whether the given class or module declares any class variables.

Returns:


25
26
27
28
29
30
31
32
33
# File 'lib/reek/smells/class_variable.rb', line 25

def examine_context(ctx)
  class_variables_in(ctx.exp).map do |variable, lines|
    SmellWarning.new self,
                     context: ctx.full_name,
                     lines: lines,
                     message: "declares the class variable #{variable}",
                     parameters: { name: variable.to_s }
  end
end