Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove negative check for nil #446

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 35 additions & 36 deletions lib/addressable/uri.rb
Expand Up @@ -126,10 +126,10 @@ def self.parse(uri)
password = nil
host = nil
port = nil
if authority != nil
if authority
# The Regexp above doesn't split apart the authority.
userinfo = authority[/^([^\[\]]*)@/, 1]
if userinfo != nil
if userinfo
user = userinfo.strip[/^([^:]*):?/, 1]
password = userinfo.strip[/:(.*)$/, 1]
end
Expand Down Expand Up @@ -686,7 +686,7 @@ def self.normalized_encode(uri, return_type=String)
:fragment => self.unencode_component(uri_object.fragment)
}
components.each do |key, value|
if value != nil
if value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/Next: Use next to skip iteration.

begin
components[key] =
Addressable::IDNA.unicode_normalize_kc(value.to_str)
Expand Down Expand Up @@ -970,7 +970,7 @@ def user=(new_user)
@user = new_user ? new_user.to_str : nil

# You can't have a nil user with a non-nil password
if password != nil
if password
@user = EMPTY_STR if @user.nil?
end

Expand Down Expand Up @@ -1031,7 +1031,7 @@ def password=(new_password)
# You can't have a nil user with a non-nil password
@password ||= nil
@user ||= nil
if @password != nil
if @password
@user = EMPTY_STR if @user.nil?
end

Expand Down Expand Up @@ -1242,11 +1242,11 @@ def domain
def authority
self.host && @authority ||= begin
authority = String.new
if self.userinfo != nil
if self.userinfo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

authority << "#{self.userinfo}@"
end
authority << self.host
if self.port != nil
if self.port
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

authority << ":#{self.port}"
end
authority
Expand All @@ -1261,11 +1261,11 @@ def normalized_authority
return nil unless self.authority
@normalized_authority ||= begin
authority = String.new
if self.normalized_userinfo != nil
if self.normalized_userinfo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

authority << "#{self.normalized_userinfo}@"
end
authority << self.normalized_host
if self.normalized_port != nil
if self.normalized_port
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

authority << ":#{self.normalized_port}"
end
authority
Expand Down Expand Up @@ -1418,17 +1418,16 @@ def normalized_port
#
# @param [String, Integer, #to_s] new_port The new port component.
def port=(new_port)
if new_port != nil && new_port.respond_to?(:to_str)
if new_port && new_port.respond_to?(:to_str)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SafeNavigation: Use safe navigation (&.) instead of checking if an object exists before calling the method.

new_port = Addressable::URI.unencode_component(new_port.to_str)
end

if new_port.respond_to?(:valid_encoding?) && !new_port.valid_encoding?
raise InvalidURIError, "Invalid encoding in port"
end

if new_port != nil && !(new_port.to_s =~ /^\d+$/)
raise InvalidURIError,
"Invalid port number: #{new_port.inspect}"
if new_port && !(new_port.to_s =~ /^\d+$/)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/InverseMethods: Use !~ instead of inverting =~.

raise InvalidURIError, "Invalid port number: #{new_port.inspect}"
end

@port = new_port.to_s.to_i
Expand Down Expand Up @@ -1479,8 +1478,8 @@ def default_port
def site
(self.scheme || self.authority) && @site ||= begin
site_string = "".dup
site_string << "#{self.scheme}:" if self.scheme != nil
site_string << "//#{self.authority}" if self.authority != nil
site_string << "#{self.scheme}:" if self.scheme
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

site_string << "//#{self.authority}" if self.authority
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

site_string
end
end
Expand All @@ -1498,10 +1497,10 @@ def normalized_site
return nil unless self.site
@normalized_site ||= begin
site_string = "".dup
if self.normalized_scheme != nil
if self.normalized_scheme
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

site_string << "#{self.normalized_scheme}:"
end
if self.normalized_authority != nil
if self.normalized_authority
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

site_string << "//#{self.normalized_authority}"
end
site_string
Expand Down Expand Up @@ -1583,7 +1582,7 @@ def path=(new_path)
raise TypeError, "Can't convert #{new_path.class} into String."
end
@path = (new_path || EMPTY_STR).to_str
if !@path.empty? && @path[0..0] != SLASH && host != nil
if !@path.empty? && @path[0..0] != SLASH && host
@path = "/#{@path}"
end

Expand Down Expand Up @@ -1928,7 +1927,7 @@ def join(uri)
joined_fragment = nil

# Section 5.2.2 of RFC 3986
if uri.scheme != nil
if uri.scheme
joined_scheme = uri.scheme
joined_user = uri.user
joined_password = uri.password
Expand All @@ -1937,7 +1936,7 @@ def join(uri)
joined_path = URI.normalize_path(uri.path)
joined_query = uri.query
else
if uri.authority != nil
if uri.authority
joined_user = uri.user
joined_password = uri.password
joined_host = uri.host
Expand All @@ -1947,7 +1946,7 @@ def join(uri)
else
if uri.path == nil || uri.path.empty?
joined_path = self.path
if uri.query != nil
if uri.query
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/BlockNesting: Avoid more than 3 levels of block nesting.
Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.

joined_query = uri.query
else
joined_query = self.query
Expand All @@ -1971,7 +1970,7 @@ def join(uri)

# If the base path is empty and an authority segment has been
# defined, use a base path of SLASH
if base_path.empty? && self.authority != nil
if base_path.empty? && self.authority
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

base_path = SLASH
end

Expand Down Expand Up @@ -2143,7 +2142,7 @@ def route_from(uri)
end
end
# Avoid network-path references.
if components[:host] != nil
if components[:host]
components[:scheme] = normalized_self.scheme
end
return Addressable::URI.new(
Expand Down Expand Up @@ -2359,18 +2358,18 @@ def empty?
#
# @return [String] The URI's <code>String</code> representation.
def to_s
if self.scheme == nil && self.path != nil && !self.path.empty? &&
if self.scheme == nil && self.path && !self.path.empty? &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.
Style/NilComparison: Prefer the use of the nil? predicate.

self.path =~ NORMPATH
raise InvalidURIError,
"Cannot assemble URI string with ambiguous path: '#{self.path}'"
end
@uri_string ||= begin
uri_string = String.new
uri_string << "#{self.scheme}:" if self.scheme != nil
uri_string << "//#{self.authority}" if self.authority != nil
uri_string << "#{self.scheme}:" if self.scheme
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

uri_string << "//#{self.authority}" if self.authority
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

uri_string << self.path.to_s
uri_string << "?#{self.query}" if self.query != nil
uri_string << "##{self.fragment}" if self.fragment != nil
uri_string << "?#{self.query}" if self.query
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

uri_string << "##{self.fragment}" if self.fragment
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

uri_string.force_encoding(Encoding::UTF_8)
uri_string
end
Expand Down Expand Up @@ -2475,25 +2474,25 @@ def self.normalize_path(path)
# Ensures that the URI is valid.
def validate
return if !!@validation_deferred
if self.scheme != nil && self.ip_based? &&
if self.scheme && self.ip_based? &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

(self.host == nil || self.host.empty?) &&
(self.path == nil || self.path.empty?)
raise InvalidURIError,
"Absolute URI missing hierarchical segment: '#{self.to_s}'"
end
if self.host == nil
if self.port != nil ||
self.user != nil ||
self.password != nil
if self.port ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

self.user ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/MultilineOperationIndentation: Align the operands of a condition in an if statement spanning multiple lines.
Style/RedundantSelf: Redundant self detected.

self.password
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/MultilineOperationIndentation: Align the operands of a condition in an if statement spanning multiple lines.
Style/RedundantSelf: Redundant self detected.

raise InvalidURIError, "Hostname not supplied: '#{self.to_s}'"
end
end
if self.path != nil && !self.path.empty? && self.path[0..0] != SLASH &&
self.authority != nil
if self.path && !self.path.empty? && self.path[0..0] != SLASH &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

self.authority
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/MultilineOperationIndentation: Align the operands of a condition in an if statement spanning multiple lines.
Style/RedundantSelf: Redundant self detected.

raise InvalidURIError,
"Cannot have a relative path with an authority set: '#{self.to_s}'"
end
if self.path != nil && !self.path.empty? &&
if self.path && !self.path.empty? &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

self.path[0..1] == SLASH + SLASH && self.authority == nil
raise InvalidURIError,
"Cannot have a path with two leading slashes " +
Expand All @@ -2502,7 +2501,7 @@ def validate
unreserved = CharacterClasses::UNRESERVED
sub_delims = CharacterClasses::SUB_DELIMS
if !self.host.nil? && (self.host =~ /[<>{}\/\\\?\#\@"[[:space:]]]/ ||
(self.host[/^\[(.*)\]$/, 1] != nil && self.host[/^\[(.*)\]$/, 1] !~
(self.host[/^\[(.*)\]$/, 1] && self.host[/^\[(.*)\]$/, 1] !~
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

This comment was marked as spam.

Regexp.new("^[#{unreserved}#{sub_delims}:]*$")))
raise InvalidURIError, "Invalid character in host: '#{self.host.to_s}'"
end
Expand Down