daddyz/phonelib

View on GitHub
lib/phonelib/phone_formatter.rb

Summary

Maintainability
A
3 hrs
Test Coverage
A
98%

Module has too many lines. [112/100]
Open

  module PhoneFormatter
    # Returns formatted national number
    # @param formatted [Boolean] whether to return numbers only or formatted
    # @return [String] formatted national number
    def national(formatted = true)
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop checks if the length a module exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Assignment Branch Condition size for international is too high. [22.93/15]
Open

    def international(formatted = true, prefix = '+')
      prefix = formatted if formatted.is_a?(String)
      return nil if sanitized.empty?
      return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless possible?
      return "#{prefix}#{data_country_code}#{@national_number}" unless formatted
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Assignment Branch Condition size for country_code is too high. [20.27/15]
Open

    def country_code
      return @country_code if @country_code

      code = Phonelib.phone_data[country] && Phonelib.phone_data[country][Core::COUNTRY_CODE]
      return @country_code = code unless code == '1' && Phonelib.phone_data[country][Core::LEADING_DIGITS]
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Method has too many lines. [15/10]
Open

    def formatting_data
      return @formatting_data if defined?(@formatting_data)

      data = @data[country]
      format = data[:format]
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Assignment Branch Condition size for formatting_data is too high. [17.03/15]
Open

    def formatting_data
      return @formatting_data if defined?(@formatting_data)

      data = @data[country]
      format = data[:format]
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Cyclomatic complexity for international is too high. [8/6]
Open

    def international(formatted = true, prefix = '+')
      prefix = formatted if formatted.is_a?(String)
      return nil if sanitized.empty?
      return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless possible?
      return "#{prefix}#{data_country_code}#{@national_number}" unless formatted
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.

Perceived complexity for international is too high. [8/7]
Open

    def international(formatted = true, prefix = '+')
      prefix = formatted if formatted.is_a?(String)
      return nil if sanitized.empty?
      return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless possible?
      return "#{prefix}#{data_country_code}#{@national_number}" unless formatted
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop tries to produce a complexity score that's a measure of the complexity the reader experiences when looking at a method. For that reason it considers when nodes as something that doesn't add as much complexity as an if or a &&. Except if it's one of those special case/when constructs where there's no expression after case. Then the cop treats it as an if/elsif/elsif... and lets all the when nodes count. In contrast to the CyclomaticComplexity cop, this cop considers else nodes as adding complexity.

Example:

def my_method                   # 1
  if cond                       # 1
    case var                    # 2 (0.8 + 4 * 0.2, rounded)
    when 1 then func_one
    when 2 then func_two
    when 3 then func_three
    when 4..10 then func_other
    end
  else                          # 1
    do_something until a && b   # 2
  end                           # ===
end                             # 7 complexity points

Method has too many lines. [11/10]
Open

    def international(formatted = true, prefix = '+')
      prefix = formatted if formatted.is_a?(String)
      return nil if sanitized.empty?
      return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless possible?
      return "#{prefix}#{data_country_code}#{@national_number}" unless formatted
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Cyclomatic complexity for area_code_possible? is too high. [7/6]
Open

    def area_code_possible?
      return false if impossible?

      # has national prefix
      return false unless @data[country][Core::NATIONAL_PREFIX] || country == 'IT'
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.

Method international has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
Open

    def international(formatted = true, prefix = '+')
      prefix = formatted if formatted.is_a?(String)
      return nil if sanitized.empty?
      return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless possible?
      return "#{prefix}#{data_country_code}#{@national_number}" unless formatted
Severity: Minor
Found in lib/phonelib/phone_formatter.rb - About 1 hr to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Method area_code_possible? has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

    def area_code_possible?
      return false if impossible?

      # has national prefix
      return false unless @data[country][Core::NATIONAL_PREFIX] || country == 'IT'
Severity: Minor
Found in lib/phonelib/phone_formatter.rb - About 45 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Method national has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
Open

    def national(formatted = true)
      return @national_number unless possible?
      format_match, format_string = formatting_data

      if format_match
Severity: Minor
Found in lib/phonelib/phone_formatter.rb - About 35 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Method raw_national has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Open

    def raw_national
      return nil if sanitized.nil? || sanitized.empty?
      if valid?
        @national_number
      elsif data_country_code && sanitized.start_with?(data_country_code)
Severity: Minor
Found in lib/phonelib/phone_formatter.rb - About 25 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Method country_code has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Open

    def country_code
      return @country_code if @country_code

      code = Phonelib.phone_data[country] && Phonelib.phone_data[country][Core::COUNTRY_CODE]
      return @country_code = code unless code == '1' && Phonelib.phone_data[country][Core::LEADING_DIGITS]
Severity: Minor
Found in lib/phonelib/phone_formatter.rb - About 25 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Tab detected.
Open

    format_string.gsub! '$1', rule
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Line is too long. [109/80]
Open

      @data_country_code ||= Phonelib.phone_data[country] && Phonelib.phone_data[country][Core::COUNTRY_CODE]
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Line is too long. [90/80]
Open

      match = e164.match(/\A\+(1(#{Phonelib.phone_data[country][Core::LEADING_DIGITS]}))/)
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Line is too long. [82/80]
Open

    # @param prefix [String] prefix to be placed before the number, "+" by default
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Line is too long. [82/80]
Open

    # @param prefix [String] prefix to be placed before the number, "+" by default
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Use 2 (not -5) spaces for indentation.
Open

    format_string = rule.gsub('$1', '') + format_string
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cops checks for indentation that doesn't use the specified number of spaces.

See also the IndentationConsistency cop which is the companion to this one.

Example:

# bad
class A
 def test
  puts 'hello'
 end
end

# good
class A
  def test
    puts 'hello'
  end
end

Example: IgnoredPatterns: ['^\s*module']

# bad
module A
class B
  def test
  puts 'hello'
  end
end
end

# good
module A
class B
  def test
    puts 'hello'
  end
end
end

Missing magic comment # frozen_string_literal: true.
Open

module Phonelib
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop is designed to help upgrade to Ruby 3.0. It will add the comment # frozen_string_literal: true to the top of files to enable frozen string literals. Frozen string literals may be default in Ruby 3.0. The comment will be added below a shebang and encoding comment. The frozen string literal comment is only valid in Ruby 2.3+.

Example: EnforcedStyle: when_needed (default)

# The `when_needed` style will add the frozen string literal comment
# to files only when the `TargetRubyVersion` is set to 2.3+.
# bad
module Foo
  # ...
end

# good
# frozen_string_literal: true

module Foo
  # ...
end

Example: EnforcedStyle: always

# The `always` style will always add the frozen string literal comment
# to a file, regardless of the Ruby version or if `freeze` or `<<` are
# called on a string literal.
# bad
module Bar
  # ...
end

# good
# frozen_string_literal: true

module Bar
  # ...
end

Example: EnforcedStyle: never

# The `never` will enforce that the frozen string literal comment does
# not exist in a file.
# bad
# frozen_string_literal: true

module Baz
  # ...
end

# good
module Baz
  # ...
end

Use 2 (not -5) spaces for indentation.
Open

    format_string.gsub! '$1', rule
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cops checks for indentation that doesn't use the specified number of spaces.

See also the IndentationConsistency cop which is the companion to this one.

Example:

# bad
class A
 def test
  puts 'hello'
 end
end

# good
class A
  def test
    puts 'hello'
  end
end

Example: IgnoredPatterns: ['^\s*module']

# bad
module A
class B
  def test
  puts 'hello'
  end
end
end

# good
module A
class B
  def test
    puts 'hello'
  end
end
end

Line is too long. [106/80]
Open

      return @country_code = code unless code == '1' && Phonelib.phone_data[country][Core::LEADING_DIGITS]
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Line is too long. [93/80]
Open

      code = Phonelib.phone_data[country] && Phonelib.phone_data[country][Core::COUNTRY_CODE]
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Line is too long. [82/80]
Open

    # @param prefix [String] prefix to be placed before the number, "+" by default
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Line is too long. [82/80]
Open

    # @param prefix [String] prefix to be placed before the number, "+" by default
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Line is too long. [97/80]
Open

      return false if type == Core::MOBILE && !Core::AREA_CODE_MOBILE_COUNTRIES.include?(country)
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Use the return of the conditional for variable assignment and comparison.
Open

      if match
        @country_code = match[1]
      else
        @country_code = '1'
      end
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Indent the first line of the right-hand-side of a multi-line assignment.
Open

          [@national_number.match(/#{format[Core::PATTERN]}/), format_string]
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop checks the indentation of the first line of the right-hand-side of a multi-line assignment.

Example:

# bad
value =
if foo
  'bar'
end

# good
value =
  if foo
    'bar'
  end

The indentation of the remaining lines can be corrected with other cops such as IndentationConsistency and EndAlignment.

When using method_missing, define respond_to_missing?.
Open

    def method_missing(method, *args)
      prefix_methods = %w(international_ full_international_ e164_ full_e164_)
      method_s = method.to_s
      prefix_methods.each do |key|
        return send(key[0..-2], method_s.gsub(key, '')) if method_s.start_with?(key)
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop checks for the presence of method_missing without also defining respond_to_missing? and falling back on super.

Example:

#bad
def method_missing(name, *args)
  # ...
end

#good
def respond_to_missing?(name, include_private)
  # ...
end

def method_missing(name, *args)
  # ...
  super
end

Line is too long. [116/80]
Open

    # Returns e164 formatted phone number. Method can receive single string parameter that will be defined as prefix
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Tab detected.
Open

    format_string = rule.gsub('$1', '') + format_string
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Line is too long. [84/80]
Open

        return send(key[0..-2], method_s.gsub(key, '')) if method_s.start_with?(key)
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Line is too long. [82/80]
Open

      return false unless @data[country][Core::NATIONAL_PREFIX] || country == 'IT'
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

Line is too long. [83/80]
Open

        national = fmt.gsub(/\$\d/) { |el| matches[el[1].to_i] } unless fmt == 'NA'
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

%w-literals should be delimited by [ and ].
Open

      prefix_methods = %w(international_ full_international_ e164_ full_e164_)
Severity: Minor
Found in lib/phonelib/phone_formatter.rb by rubocop

This cop enforces the consistent usage of %-literal delimiters.

Specify the 'default' key to set all preferred delimiters at once. You can continue to specify individual preferred delimiters to override the default.

Example:

# Style/PercentLiteralDelimiters:
#   PreferredDelimiters:
#     default: '[]'
#     '%i':    '()'

# good
%w[alpha beta] + %i(gamma delta)

# bad
%W(alpha #{beta})

# bad
%I(alpha beta)

There are no issues that match your filters.

Category
Status