Sha256: 9831ddb5103369580286afb53f9a4860999e1a40dd42af174cbd83ba5a641b12

Contents?: true

Size: 1.81 KB

Versions: 31

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require_relative 'base_detector'

module Reek
  module SmellDetectors
    #
    # An Uncommunicative Name is a name that doesn't communicate its intent
    # well enough.
    #
    # Poor names make it hard for the reader to build a mental picture
    # of what's going on in the code. They can also be mis-interpreted;
    # and they hurt the flow of reading, because the reader must slow
    # down to interpret the names.
    #
    # Currently +UncommunicativeMethodName+ checks for
    # * 1-character names
    # * names ending with a number
    # * names containing a capital letter (assuming camelCase)
    #
    # See {file:docs/Uncommunicative-Method-Name.md} for details.
    class UncommunicativeMethodName < BaseDetector
      REJECT_KEY = 'reject'
      ACCEPT_KEY = 'accept'
      DEFAULT_REJECT_PATTERNS = [/^[a-z]$/, /[0-9]$/, /[A-Z]/].freeze
      DEFAULT_ACCEPT_PATTERNS = [].freeze

      def self.default_config
        super.merge(
          REJECT_KEY => DEFAULT_REJECT_PATTERNS,
          ACCEPT_KEY => DEFAULT_ACCEPT_PATTERNS)
      end

      #
      # Checks the given +context+ for uncommunicative names.
      #
      # @return [Array<SmellWarning>]
      #
      def sniff
        name = context.name.to_s
        return [] if acceptable_name?(name)

        [smell_warning(
          lines: [source_line],
          message: "has the name '#{name}'",
          parameters: { name: name })]
      end

      private

      def acceptable_name?(name)
        accept_patterns.any? { |accept_pattern| name.match accept_pattern } ||
          reject_patterns.none? { |reject_pattern| name.match reject_pattern }
      end

      def reject_patterns
        Array value(REJECT_KEY, context)
      end

      def accept_patterns
        Array value(ACCEPT_KEY, context)
      end
    end
  end
end

Version data entries

31 entries across 29 versions & 2 rubygems

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