Sha256: 948c96eca7baebeb3172d46cec45a0e135a227cd2f7534bf2b9a9c152eebf800

Contents?: true

Size: 1.19 KB

Versions: 30

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require_relative 'base_detector'

module Reek
  module SmellDetectors
    #
    # 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.
    #
    # TODO: Catch attributes declared "by hand"
    class Attribute < BaseDetector
      def self.contexts # :nodoc:
        [:sym]
      end

      #
      # Checks whether the given class declares any attributes.
      #
      # @return [Array<SmellWarning>]
      #
      def sniff
        attributes_in_context.map do |_attribute, line|
          smell_warning(
            lines: [line],
            message: 'is a writable attribute')
        end
      end

      private

      def attributes_in_context
        if context.visibility == :public
          call_node = expression
          [[call_node.name, call_node.line]]
        else
          []
        end
      end
    end
  end
end

Version data entries

30 entries across 28 versions & 2 rubygems

Version Path
reek-6.3.0 lib/reek/smell_detectors/attribute.rb
reek-6.2.0 lib/reek/smell_detectors/attribute.rb
reek-6.1.4 lib/reek/smell_detectors/attribute.rb
reek-6.1.3 lib/reek/smell_detectors/attribute.rb
reek-6.1.2 lib/reek/smell_detectors/attribute.rb
reek-6.1.1 lib/reek/smell_detectors/attribute.rb
reek-6.1.0 lib/reek/smell_detectors/attribute.rb
reek-6.0.6 lib/reek/smell_detectors/attribute.rb
reek-6.0.5 lib/reek/smell_detectors/attribute.rb
reek-6.0.4 lib/reek/smell_detectors/attribute.rb
reek-6.0.3 lib/reek/smell_detectors/attribute.rb
reek-6.0.2 lib/reek/smell_detectors/attribute.rb
reek-6.0.1 lib/reek/smell_detectors/attribute.rb
reek-6.0.0 lib/reek/smell_detectors/attribute.rb
reek-5.6.0 lib/reek/smell_detectors/attribute.rb
reek-5.5.0 lib/reek/smell_detectors/attribute.rb
reek-5.4.1 lib/reek/smell_detectors/attribute.rb
reek-5.4.0 lib/reek/smell_detectors/attribute.rb
reek-5.3.2 lib/reek/smell_detectors/attribute.rb
reek-5.3.1 lib/reek/smell_detectors/attribute.rb