Sha256: bcb4ff98928e011218f5dfed9a3a591202392a8fe8102b52330d36417c02d61d

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

require 'reek/smells/smell_detector'
require 'reek/smell_warning'
require 'reek/core/smell_configuration'

module Reek
  module Smells
    #
    # A class that publishes a getter or setter for an instance variable
    # invites client classes to become too intimate with its inner workings,
    # and in particular with its representation of state.
    #
    # Currently this detector raises a warning for every +attr+,
    # +attr_reader+, +attr_writer+ and +attr_accessor+ -- including those
    # that are private.
    #
    # TODO: Eliminate private attributes
    # TODO: Catch attributes declared "by hand"
    #
    class Attribute < SmellDetector
      SMELL_CLASS = name.split(/::/)[-1]
      SMELL_SUBCLASS = SMELL_CLASS

      ATTRIBUTE_KEY = 'attribute'

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

      def self.default_config
        super.merge(Core::SmellConfiguration::ENABLED_KEY => false)
      end

      #
      # Checks whether the given class declares any attributes.
      #
      # @return [Array<SmellWarning>]
      #
      def examine_context(ctx)
        attributes_in(ctx).map do |attr, line|
          smell = SmellWarning.new(SMELL_CLASS, ctx.full_name, [line],
                                   "declares the attribute #{attr}",
                                   @source, SMELL_SUBCLASS,
                                   ATTRIBUTE_KEY => attr.to_s)
          smell
        end
      end

      private

      def attributes_in(module_ctx)
        result = Set.new
        attr_defn_methods = [:attr, :attr_reader, :attr_writer, :attr_accessor]
        module_ctx.local_nodes(:call) do |call_node|
          if attr_defn_methods.include?(call_node.method_name)
            call_node.arg_names.each { |arg| result << [arg, call_node.line] }
          end
        end
        result
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reek-1.4.0 lib/reek/smells/attribute.rb