Class: Reek::SmellDetectors::ControlParameterHelpers::ControlParameterFinder
- Inherits:
-
Object
- Object
- Reek::SmellDetectors::ControlParameterHelpers::ControlParameterFinder
- Defined in:
- lib/reek/smell_detectors/control_parameter_helpers/control_parameter_finder.rb
Overview
Finds cases of ControlParameter in a particular node for a particular parameter
Constant Summary collapse
- CONDITIONAL_NODE_TYPES =
[:if, :case, :and, :or].freeze
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
private
Returns the value of attribute node.
-
#parameter ⇒ Object
readonly
private
Returns the value of attribute parameter.
Instance Method Summary collapse
-
#conditional_nodes ⇒ Array<Reek::AST::Node>
private
The conditional nodes scoped below the current node.
-
#find_matches ⇒ Array<Reek::AST::Node>
All nodes where the parameter is used for control flow.
-
#initialize(node, parameter) ⇒ ControlParameterFinder
constructor
A new instance of ControlParameterFinder.
-
#legitimate_uses? ⇒ Boolean
True if the parameter is not used for control flow.
- #nested_finders ⇒ Array<ControlParameterFinder> private
-
#parameter_used_in_body? ⇒ Boolean
private
If the parameter is used in the body of the method e.g.
-
#uses_of_param_in_condition ⇒ Array<Reek::AST::Node>
private
All nodes where the parameter is part of a condition, e.g.
Constructor Details
#initialize(node, parameter) ⇒ ControlParameterFinder
Returns a new instance of ControlParameterFinder.
20 21 22 23 |
# File 'lib/reek/smell_detectors/control_parameter_helpers/control_parameter_finder.rb', line 20 def initialize(node, parameter) @node = node @parameter = parameter end |
Instance Attribute Details
#node ⇒ Object (readonly, private)
Returns the value of attribute node.
47 48 49 |
# File 'lib/reek/smell_detectors/control_parameter_helpers/control_parameter_finder.rb', line 47 def node @node end |
#parameter ⇒ Object (readonly, private)
Returns the value of attribute parameter.
47 48 49 |
# File 'lib/reek/smell_detectors/control_parameter_helpers/control_parameter_finder.rb', line 47 def parameter @parameter end |
Instance Method Details
#conditional_nodes ⇒ Array<Reek::AST::Node> (private)
Returns the conditional nodes scoped below the current node.
88 89 90 |
# File 'lib/reek/smell_detectors/control_parameter_helpers/control_parameter_finder.rb', line 88 def conditional_nodes node.body_nodes(CONDITIONAL_NODE_TYPES) end |
#find_matches ⇒ Array<Reek::AST::Node>
Returns all nodes where the parameter is used for control flow.
28 29 30 31 32 |
# 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 |
#legitimate_uses? ⇒ Boolean
Returns true if the parameter is not used for control flow.
37 38 39 40 41 42 43 |
# 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 |
#nested_finders ⇒ Array<ControlParameterFinder> (private)
68 69 70 71 72 |
# 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 |
#parameter_used_in_body? ⇒ Boolean (private)
Returns 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”.
60 61 62 63 |
# 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 |
#uses_of_param_in_condition ⇒ Array<Reek::AST::Node> (private)
Returns all nodes where the parameter is part of a condition, e.g. [s(:lvar, :charlie)].
78 79 80 81 82 83 |
# 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 |