Sha256: 428884a2a14c322ad1f7bb1b35abcdb0e3f50c762dffc52c76bdbdca26682813

Contents?: true

Size: 1.51 KB

Versions: 28

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require_relative 'base_detector'

module Reek
  module SmellDetectors
    #
    # The +InstanceVariableAssumption+ class is responsible for
    # detecting directly access of instance variables in a class
    # that does not define them in its initialize method.
    #
    class InstanceVariableAssumption < BaseDetector
      def self.contexts
        [:class]
      end

      # Checks +klass+ for instance
      # variables assumptions.
      #
      # @return [Array<SmellWarning>]
      #
      def sniff
        assumptions = (variables_from_context - variables_from_initialize).uniq

        assumptions.map do |assumption|
          build_smell_warning(assumption)
        end
      end

      private

      def method_expressions
        @method_expressions ||= context.node_instance_methods
      end

      def build_smell_warning(assumption)
        message = "assumes too much for instance variable '#{assumption}'"

        smell_warning(
          lines: [source_line],
          message: message,
          parameters: { assumption: assumption.to_s })
      end

      def variables_from_initialize
        initialize_exp = method_expressions.detect do |method|
          method.name == :initialize
        end

        return [] unless initialize_exp

        initialize_exp.each_node(:ivasgn).map(&:name)
      end

      def variables_from_context
        method_expressions.map do |method|
          method.each_node(:ivar, [:or_asgn]).map(&:name)
        end.flatten
      end
    end
  end
end

Version data entries

28 entries across 26 versions & 2 rubygems

Version Path
reek-6.1.4 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-6.1.3 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-6.1.2 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-6.1.1 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-6.1.0 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-6.0.6 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-6.0.5 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-6.0.4 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-6.0.3 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-6.0.2 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-6.0.1 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-6.0.0 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-5.6.0 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-5.5.0 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-5.4.1 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-5.4.0 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-5.3.2 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-5.3.1 lib/reek/smell_detectors/instance_variable_assumption.rb
reek-5.3.0 lib/reek/smell_detectors/instance_variable_assumption.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/lib/reek/smell_detectors/instance_variable_assumption.rb