class Reek::SmellDetectors::ControlParameterHelpers::ControlParameterFinder
Finds cases of ControlParameter
in a particular node for a particular parameter
Constants
- CONDITIONAL_NODE_TYPES
Attributes
Public Class Methods
Source
# File lib/reek/smell_detectors/control_parameter_helpers/control_parameter_finder.rb, line 20 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/control_parameter_finder.rb, line 28 def find_matches return [] if legitimate_uses? nested_finders.flat_map(&:find_matches) + uses_of_param_in_condition end
@return [Array<Reek::AST::Node>] all nodes where the parameter is used for control flow
Source
# File lib/reek/smell_detectors/control_parameter_helpers/control_parameter_finder.rb, line 37 def legitimate_uses? return true if CallInConditionFinder.new(node, parameter).uses_param_in_call_in_condition? return true if parameter_used_in_body? return true if nested_finders.any?(&:legitimate_uses?) false end
@return [Boolean] true if the parameter is not used for control flow
Private Instance Methods
Source
# File lib/reek/smell_detectors/control_parameter_helpers/control_parameter_finder.rb, line 88 def conditional_nodes node.body_nodes(CONDITIONAL_NODE_TYPES) end
@return [Array<Reek::AST::Node>] the conditional nodes scoped below the current node
Source
# File lib/reek/smell_detectors/control_parameter_helpers/control_parameter_finder.rb, line 68 def nested_finders @nested_finders ||= conditional_nodes.flat_map do |node| self.class.new(node, parameter) end end
@return [Array<ControlParameterFinder>]
Source
# File lib/reek/smell_detectors/control_parameter_helpers/control_parameter_finder.rb, line 60 def parameter_used_in_body? nodes = node.body_nodes([:lvar], CONDITIONAL_NODE_TYPES) nodes.any? { |lvar_node| lvar_node.var_name == parameter } end
@return [Boolean] if the parameter is used in the body of the method
e.g. this def alfa(bravo) puts bravo if bravo then charlie end end would cause this method to return true because of the "puts bravo"
Source
# File lib/reek/smell_detectors/control_parameter_helpers/control_parameter_finder.rb, line 78 def uses_of_param_in_condition condition = node.condition return [] unless condition condition.each_node(:lvar).select { |inner| inner.var_name == parameter } end
@return [Array<Reek::AST::Node>] all nodes where the parameter is part of a condition,
e.g. [s(:lvar, :charlie)]