Sha256: 5c87108f95aed145cc248f0184c74a056ca45c1deae064dc448f06bb90d26995

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require_relative 'smell_configuration'
require_relative 'smell_detector'
require_relative 'smell_warning'

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.
    #
    # This detector raises a warning for every public +attr_writer+,
    # +attr_accessor+, and +attr+ with the writable flag set to +true+.
    #
    # See {file:docs/Attribute.md} for details.
    # @api private
    #
    # TODO: Catch attributes declared "by hand"
    class Attribute < SmellDetector
      def initialize(*args)
        super
      end

      def self.contexts # :nodoc:
        [:sym]
      end

      #
      # Checks whether the given class declares any attributes.
      #
      # @return [Array<SmellWarning>]
      #
      def examine_context(ctx)
        attributes_in(ctx).map do |attribute, line|
          SmellWarning.new self,
                           context: ctx.full_name,
                           lines: [line],
                           message: 'is a writable attribute',
                           parameters: { name: attribute.to_s }
        end
      end

      private

      def attributes_in(module_ctx)
        if module_ctx.visibility == :public
          call_node = module_ctx.exp
          [[call_node.name, call_node.line]]
        else
          []
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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