Skip to content

Commit

Permalink
rubocop: fix Style/GuardClause
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Maher committed Aug 17, 2016
1 parent d2d8536 commit 8b8ae9b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 53 deletions.
7 changes: 0 additions & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,6 @@ Style/GlobalVars:
Exclude:
- 'testserver/ldapserver.rb'

# Offense count: 2
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/net/ldap/connection.rb'
- 'test/test_ldap_connection.rb'

# Offense count: 161
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
Expand Down
7 changes: 3 additions & 4 deletions lib/net/ber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,10 @@ def initialize args
# Check the encoding of the newly created String and set the encoding
# to 'UTF-8' (NOTE: we do NOT change the bytes, but only set the
# encoding to 'UTF-8').
return unless encoding == Encoding::BINARY
current_encoding = encoding
if current_encoding == Encoding::BINARY
force_encoding('UTF-8')
force_encoding(current_encoding) unless valid_encoding?
end
force_encoding('UTF-8')
force_encoding(current_encoding) unless valid_encoding?
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/net/ber/ber_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ def read_ber(syntax = nil)
yield id, content_length if block_given?

if -1 == content_length
raise Net::BER::BerError, "Indeterminite BER content length not implemented."
else
data = read(content_length)
raise Net::BER::BerError,
"Indeterminite BER content length not implemented."
end
data = read(content_length)

parse_ber_object(syntax, id, data)
end
Expand Down
8 changes: 3 additions & 5 deletions lib/net/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1286,11 +1286,9 @@ def use_connection(args)
else
begin
conn = new_connection
if (result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess
yield conn
else
return result
end
result = conn.bind(args[:auth] || @auth)
return result unless result.code == Net::LDAP::ResultCodeSuccess
yield conn
ensure
conn.close if conn
end
Expand Down
34 changes: 13 additions & 21 deletions lib/net/ldap/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,13 @@ def self.wrap_with_ssl(io, tls_options = {}, timeout=nil)
conn.connect
end
rescue IO::WaitReadable
if IO.select([conn], nil, nil, timeout)
retry
else
raise Errno::ETIMEDOUT, "OpenSSL connection read timeout"
end
raise Errno::ETIMEDOUT, "OpenSSL connection read timeout" unless
IO.select([conn], nil, nil, timeout)
retry
rescue IO::WaitWritable
if IO.select(nil, [conn], nil, timeout)
retry
else
raise Errno::ETIMEDOUT, "OpenSSL connection write timeout"
end
raise Errno::ETIMEDOUT, "OpenSSL connection write timeout" unless
IO.select(nil, [conn], nil, timeout)
retry
end

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

if pdu.result_code.zero?
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout)
else
raise Net::LDAP::StartTLSError, "start_tls failed: #{pdu.result_code}"
end
raise Net::LDAP::StartTLSError,
"start_tls failed: #{pdu.result_code}" unless pdu.result_code.zero?
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout)
else
raise Net::LDAP::EncMethodUnsupportedError, "unsupported encryption method #{args[:method]}"
end
Expand Down Expand Up @@ -197,12 +191,10 @@ def queued_read(message_id)

# read messages until we have a match for the given message_id
while pdu = read
if pdu.message_id == message_id
return pdu
else
message_queue[pdu.message_id].push pdu
next
end
return pdu if pdu.message_id == message_id

message_queue[pdu.message_id].push pdu
next
end

pdu
Expand Down
9 changes: 4 additions & 5 deletions lib/net/ldap/dn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,10 @@ def each_pair
end

# Last pair
if [:value, :value_normal, :value_hexstring, :value_end].include? state
yield key.string.strip, value.string.rstrip
else
raise "DN badly formed"
end
raise "DN badly formed" unless
[:value, :value_normal, :value_hexstring, :value_end].include? state

yield key.string.strip, value.string.rstrip
end

##
Expand Down
9 changes: 4 additions & 5 deletions lib/net/ldap/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,10 @@ def attribute_names
# arguments to the block: a Symbol giving the name of the attribute, and a
# (possibly empty) \Array of data values.
def each # :yields: attribute-name, data-values-array
if block_given?
attribute_names.each do|a|
attr_name, values = a, self[a]
yield attr_name, values
end
return unless block_given?
attribute_names.each do|a|
attr_name, values = a, self[a]
yield attr_name, values
end
end
alias_method :each_attribute, :each
Expand Down
4 changes: 1 addition & 3 deletions test/test_ldap_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ def capture_stderr
class FakeTCPSocket
def initialize(host, port, socket_opts = {})
status, error = host.split(".")
if status == "fail"
raise Object.const_get(error)
end
raise Object.const_get(error) if status == "fail"
end
end

Expand Down

0 comments on commit 8b8ae9b

Please sign in to comment.