class Reek::SmellDetectors::ControlParameterHelpers::CallInConditionFinder
CallInConditionFinder
finds usages of the given parameter in the context of a method call in a condition, e.g.:
def alfa(bravo)
if charlie(bravo) delta end
end
or
def alfa(bravo)
if bravo.charlie? delta end
end
Those usages are legit and should not trigger the ControlParameter
smell warning.
Constants
- COMPARISON_METHOD_NAMES
Attributes
Public Class Methods
Source
# File lib/reek/smell_detectors/control_parameter_helpers/call_in_condition_finder.rb, line 37 def initialize(node, parameter) @node = node @parameter = parameter end
@param node [Reek::AST::Node] the node in our current scope,
e.g. s(:def, :alfa, s(:args, s(:arg, :bravo),
@param parameter [Symbol] the parameter name in question
e.g. in the example above this would be :bravo
Public Instance Methods
Source
# File lib/reek/smell_detectors/control_parameter_helpers/call_in_condition_finder.rb, line 46 def uses_param_in_call_in_condition? condition = node.condition return false unless condition condition.each_node(:send) do |inner| return true if regular_call_involving_param?(inner) end false end
@return [Boolean] if the parameter in question has been used in the context of a
method call in a condition
Private Instance Methods
Source
# File lib/reek/smell_detectors/control_parameter_helpers/call_in_condition_finder.rb, line 77 def call_involving_param?(call_node) call_node.each_node(:lvar).any? { |it| it.var_name == parameter } end
@return [Boolean] if the parameter is used in the given method call
Source
# File lib/reek/smell_detectors/control_parameter_helpers/call_in_condition_finder.rb, line 85 def comparison_call?(call_node) COMPARISON_METHOD_NAMES.include? call_node.name end
@return [Boolean] if the given method call is a comparison call
:reek: UtilityFunction
Source
# File lib/reek/smell_detectors/control_parameter_helpers/call_in_condition_finder.rb, line 70 def regular_call_involving_param?(call_node) call_involving_param?(call_node) && !comparison_call?(call_node) end
@return [Boolean] if the parameter is used in a method call that is not a comparison call
e.g. this would return true given that "bravo" is the parameter in question: if charlie(bravo) then delta end while this would return false (since its a comparison): if bravo == charlie then charlie end