Class: Reek::Smells::UncommunicativeParameterName

Inherits:
SmellDetector
  • Object
show all
Defined in:
lib/reek/smells/uncommunicative_parameter_name.rb

Overview

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 UncommunicativeParameterName checks for

  • 1-character names

  • names ending with a number

Constant Summary collapse

REJECT_KEY =

The name of the config field that lists the regexps of smelly names to be reported.

'reject'
DEFAULT_REJECT_SET =
[/^.$/, /[0-9]$/, /[A-Z]/, /^_/]
ACCEPT_KEY =

The name of the config field that lists the specific names that are to be treated as exceptions; these names will not be reported as uncommunicative.

'accept'
DEFAULT_ACCEPT_SET =
[]

Constants inherited from SmellDetector

SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from SmellDetector

#smell_category, #smell_type, #smells_found, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

#config_for, #configure_with, contexts, default_smell_category, #enabled?, #enabled_for?, #examine, #exception?, #initialize, #register, #report_on, smell_type, #value

Constructor Details

This class inherits a constructor from Reek::Smells::SmellDetector

Class Method Details

.default_configObject


35
36
37
38
39
40
# File 'lib/reek/smells/uncommunicative_parameter_name.rb', line 35

def self.default_config
  super.merge(
    REJECT_KEY => DEFAULT_REJECT_SET,
    ACCEPT_KEY => DEFAULT_ACCEPT_SET
  )
end

.smell_categoryObject


31
32
33
# File 'lib/reek/smells/uncommunicative_parameter_name.rb', line 31

def self.smell_category
  'UncommunicativeName'
end

Instance Method Details

#bad_name?(name) ⇒ Boolean

Returns:

  • (Boolean)

62
63
64
65
66
# File 'lib/reek/smells/uncommunicative_parameter_name.rb', line 62

def bad_name?(name)
  var = name.to_s.gsub(/^[@\*\&]*/, '')
  return false if var == '*' || @accept_names.include?(var)
  @reject_names.find { |patt| patt =~ var }
end

#examine_context(ctx) ⇒ Array<SmellWarning>

Checks the given context for uncommunicative names.

Returns:


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/reek/smells/uncommunicative_parameter_name.rb', line 47

def examine_context(ctx)
  @reject_names = value(REJECT_KEY, ctx, DEFAULT_REJECT_SET)
  @accept_names = value(ACCEPT_KEY, ctx, DEFAULT_ACCEPT_SET)
  context_expression = ctx.exp
  context_expression.parameter_names.select do |name|
    bad_name?(name) && ctx.uses_param?(name)
  end.map do |name|
    SmellWarning.new(self,
                     context: ctx.full_name,
                     lines: [context_expression.line],
                     message: "has the parameter name '#{name}'",
                     parameters: { name: name.to_s })
  end
end