Class: Reek::Smells::ClassVariable Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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).

See Class-Variable for details.

Constant Summary

Constants inherited from SmellDetector

SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from SmellDetector

#smells_found, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

#config_for, #configure_with, default_config, default_smell_category, descendants, #enabled?, #enabled_for?, #examine, #exception?, #initialize, #register, #report_on, smell_category, #smell_category, smell_type, #smell_type, #value

Constructor Details

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

Class Method Details

.contextsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



18
19
20
# File 'lib/reek/smells/class_variable.rb', line 18

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

Instance Method Details

#class_variables_in(ast) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



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

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>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:



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

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