Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 02a2193

Browse files
committed
Extract classify_arity method to avoid duplication
1 parent 074a363 commit 02a2193

File tree

1 file changed

+18
-34
lines changed

1 file changed

+18
-34
lines changed

lib/rspec/support/method_signature_verifier.rb

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ def valid_non_kw_args?(positional_arg_count, optional_max_arg_count=positional_a
3333
optional_max_arg_count <= max_non_kw_args
3434
end
3535

36+
def classify_arity(arity=@method.arity)
37+
if arity < 0
38+
# `~` inverts the one's complement and gives us the
39+
# number of required args
40+
@min_non_kw_args = ~arity
41+
@max_non_kw_args = INFINITY
42+
else
43+
@min_non_kw_args = arity
44+
@max_non_kw_args = arity
45+
end
46+
end
47+
3648
if RubyFeatures.optional_and_splat_args_supported?
3749
def description
3850
@description ||= begin
@@ -137,18 +149,7 @@ def unlimited_args?
137149
false
138150
end
139151

140-
def classify_parameters
141-
arity = @method.arity
142-
if arity < 0
143-
# `~` inverts the one's complement and gives us the
144-
# number of required args
145-
@min_non_kw_args = ~arity
146-
@max_non_kw_args = INFINITY
147-
else
148-
@min_non_kw_args = arity
149-
@max_non_kw_args = arity
150-
end
151-
end
152+
alias_method :classify_parameters, :classify_arity
152153
end
153154

154155
INFINITY = 1 / 0.0
@@ -166,13 +167,7 @@ class MethodSignature < remove_const(:MethodSignature)
166167
def classify_parameters
167168
super
168169
if (arity = @method.arity) != 0 && @method.parameters.empty?
169-
if arity < 0
170-
@min_non_kw_args = ~arity
171-
@max_non_kw_args = INFINITY
172-
else
173-
@min_non_kw_args = arity
174-
@max_non_kw_args = arity
175-
end
170+
classify_arity(arity)
176171
end
177172
end
178173
end
@@ -185,25 +180,14 @@ class MethodSignature < remove_const(:MethodSignature)
185180

186181
def classify_parameters
187182
super
188-
if @method.arity == -1
189-
arity = java_single_overload_arity
190-
if arity < 0
191-
@min_non_kw_args = ~arity
192-
@max_non_kw_args = INFINITY
193-
else
194-
@min_non_kw_args = arity
195-
@max_non_kw_args = arity
196-
end
197-
end
198-
end
199-
200-
def java_single_overload_arity
201-
return @method.arity unless @method.owner.respond_to?(:java_class)
183+
return unless @method.arity == -1 && @method.owner.respond_to?(:java_class)
202184
java_instance_methods = @method.owner.java_class.java_instance_methods
203185
compatible_overloads = java_instance_methods.select do |java_method|
204186
@method == @method.owner.instance_method(java_method.name)
205187
end
206-
(compatible_overloads.size == 1) ? compatible_overloads.first.arity : @method.arity
188+
if compatible_overloads.size == 1
189+
classify_arity(compatible_overloads.first.arity)
190+
end
207191
end
208192
end
209193
end

0 commit comments

Comments
 (0)