Skip to content

Commit 8b8ae9b

Browse files
author
Tom Maher
committed
rubocop: fix Style/GuardClause
1 parent d2d8536 commit 8b8ae9b

File tree

8 files changed

+31
-53
lines changed

8 files changed

+31
-53
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,6 @@ Style/GlobalVars:
334334
Exclude:
335335
- 'testserver/ldapserver.rb'
336336

337-
# Offense count: 2
338-
# Configuration parameters: MinBodyLength.
339-
Style/GuardClause:
340-
Exclude:
341-
- 'lib/net/ldap/connection.rb'
342-
- 'test/test_ldap_connection.rb'
343-
344337
# Offense count: 161
345338
# Cop supports --auto-correct.
346339
# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.

lib/net/ber.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,10 @@ def initialize args
327327
# Check the encoding of the newly created String and set the encoding
328328
# to 'UTF-8' (NOTE: we do NOT change the bytes, but only set the
329329
# encoding to 'UTF-8').
330+
return unless encoding == Encoding::BINARY
330331
current_encoding = encoding
331-
if current_encoding == Encoding::BINARY
332-
force_encoding('UTF-8')
333-
force_encoding(current_encoding) unless valid_encoding?
334-
end
332+
force_encoding('UTF-8')
333+
force_encoding(current_encoding) unless valid_encoding?
335334
end
336335
end
337336

lib/net/ber/ber_parser.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ def read_ber(syntax = nil)
172172
yield id, content_length if block_given?
173173

174174
if -1 == content_length
175-
raise Net::BER::BerError, "Indeterminite BER content length not implemented."
176-
else
177-
data = read(content_length)
175+
raise Net::BER::BerError,
176+
"Indeterminite BER content length not implemented."
178177
end
178+
data = read(content_length)
179179

180180
parse_ber_object(syntax, id, data)
181181
end

lib/net/ldap.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,11 +1286,9 @@ def use_connection(args)
12861286
else
12871287
begin
12881288
conn = new_connection
1289-
if (result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess
1290-
yield conn
1291-
else
1292-
return result
1293-
end
1289+
result = conn.bind(args[:auth] || @auth)
1290+
return result unless result.code == Net::LDAP::ResultCodeSuccess
1291+
yield conn
12941292
ensure
12951293
conn.close if conn
12961294
end

lib/net/ldap/connection.rb

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,13 @@ def self.wrap_with_ssl(io, tls_options = {}, timeout=nil)
9595
conn.connect
9696
end
9797
rescue IO::WaitReadable
98-
if IO.select([conn], nil, nil, timeout)
99-
retry
100-
else
101-
raise Errno::ETIMEDOUT, "OpenSSL connection read timeout"
102-
end
98+
raise Errno::ETIMEDOUT, "OpenSSL connection read timeout" unless
99+
IO.select([conn], nil, nil, timeout)
100+
retry
103101
rescue IO::WaitWritable
104-
if IO.select(nil, [conn], nil, timeout)
105-
retry
106-
else
107-
raise Errno::ETIMEDOUT, "OpenSSL connection write timeout"
108-
end
102+
raise Errno::ETIMEDOUT, "OpenSSL connection write timeout" unless
103+
IO.select(nil, [conn], nil, timeout)
104+
retry
109105
end
110106

111107
# Doesn't work:
@@ -163,11 +159,9 @@ def setup_encryption(args, timeout=nil)
163159
raise Net::LDAP::NoStartTLSResultError, "no start_tls result"
164160
end
165161

166-
if pdu.result_code.zero?
167-
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout)
168-
else
169-
raise Net::LDAP::StartTLSError, "start_tls failed: #{pdu.result_code}"
170-
end
162+
raise Net::LDAP::StartTLSError,
163+
"start_tls failed: #{pdu.result_code}" unless pdu.result_code.zero?
164+
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout)
171165
else
172166
raise Net::LDAP::EncMethodUnsupportedError, "unsupported encryption method #{args[:method]}"
173167
end
@@ -197,12 +191,10 @@ def queued_read(message_id)
197191

198192
# read messages until we have a match for the given message_id
199193
while pdu = read
200-
if pdu.message_id == message_id
201-
return pdu
202-
else
203-
message_queue[pdu.message_id].push pdu
204-
next
205-
end
194+
return pdu if pdu.message_id == message_id
195+
196+
message_queue[pdu.message_id].push pdu
197+
next
206198
end
207199

208200
pdu

lib/net/ldap/dn.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,10 @@ def each_pair
169169
end
170170

171171
# Last pair
172-
if [:value, :value_normal, :value_hexstring, :value_end].include? state
173-
yield key.string.strip, value.string.rstrip
174-
else
175-
raise "DN badly formed"
176-
end
172+
raise "DN badly formed" unless
173+
[:value, :value_normal, :value_hexstring, :value_end].include? state
174+
175+
yield key.string.strip, value.string.rstrip
177176
end
178177

179178
##

lib/net/ldap/entry.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,10 @@ def attribute_names
140140
# arguments to the block: a Symbol giving the name of the attribute, and a
141141
# (possibly empty) \Array of data values.
142142
def each # :yields: attribute-name, data-values-array
143-
if block_given?
144-
attribute_names.each do|a|
145-
attr_name, values = a, self[a]
146-
yield attr_name, values
147-
end
143+
return unless block_given?
144+
attribute_names.each do|a|
145+
attr_name, values = a, self[a]
146+
yield attr_name, values
148147
end
149148
end
150149
alias_method :each_attribute, :each

test/test_ldap_connection.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ def capture_stderr
1616
class FakeTCPSocket
1717
def initialize(host, port, socket_opts = {})
1818
status, error = host.split(".")
19-
if status == "fail"
20-
raise Object.const_get(error)
21-
end
19+
raise Object.const_get(error) if status == "fail"
2220
end
2321
end
2422

0 commit comments

Comments
 (0)