awesome-print/awesome_print

View on GitHub
lib/awesome_print/formatters/object_formatter.rb

Summary

Maintainability
B
5 hrs
Test Coverage

Assignment Branch Condition size for format is too high. [44.02/15]
Open

      def format
        vars = variables.map do |var|
          property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
          accessor = if object.respond_to?(:"#{property}=")
            object.respond_to?(property) ? :accessor : :writer

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. [33/10]
Open

      def format
        vars = variables.map do |var|
          property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
          accessor = if object.respond_to?(:"#{property}=")
            object.respond_to?(property) ? :accessor : :writer

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.

Method format has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
Open

      def format
        vars = variables.map do |var|
          property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
          accessor = if object.respond_to?(:"#{property}=")
            object.respond_to?(property) ? :accessor : :writer
Severity: Minor
Found in lib/awesome_print/formatters/object_formatter.rb - About 2 hrs 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

Perceived complexity for format is too high. [13/7]
Open

      def format
        vars = variables.map do |var|
          property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
          accessor = if object.respond_to?(:"#{property}=")
            object.respond_to?(property) ? :accessor : :writer

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

Cyclomatic complexity for format is too high. [9/6]
Open

      def format
        vars = variables.map do |var|
          property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
          accessor = if object.respond_to?(:"#{property}=")
            object.respond_to?(property) ? :accessor : :writer

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 format has 33 lines of code (exceeds 25 allowed). Consider refactoring.
Open

      def format
        vars = variables.map do |var|
          property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
          accessor = if object.respond_to?(:"#{property}=")
            object.respond_to?(property) ? :accessor : :writer
Severity: Minor
Found in lib/awesome_print/formatters/object_formatter.rb - About 1 hr to fix

    Similar blocks of code found in 2 locations. Consider refactoring.
    Open

              key = left_aligned do
                align(declaration, declaration.size)
              end
    
              unless options[:plain]
    Severity: Major
    Found in lib/awesome_print/formatters/object_formatter.rb and 1 other location - About 1 hr to fix
    lib/awesome_print/formatters/struct_formatter.rb on lines 32..45

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 48.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Identical blocks of code found in 2 locations. Consider refactoring.
    Open

            if options[:multiline]
              "#<#{awesome_instance}\n#{data.join(%Q/,\n/)}\n#{outdent}>"
            else
              "#<#{awesome_instance} #{data.join(', ')}>"
    Severity: Minor
    Found in lib/awesome_print/formatters/object_formatter.rb and 1 other location - About 15 mins to fix
    lib/awesome_print/formatters/struct_formatter.rb on lines 49..52

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 25.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

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

                object.respond_to?(property) ? :accessor : :writer

    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. [81/80]
    Open

            data = (options[:sort_vars] ? vars.sort : vars).map do |declaration, var|

    Extra empty line detected at class body beginning.
    Open

    
          attr_reader :object, :variables, :inspector, :options

    This cops checks if empty lines around the bodies of classes match the configuration.

    Example: EnforcedStyle: empty_lines

    # good
    
    class Foo
    
      def bar
        # ...
      end
    
    end

    Example: EnforcedStyle: emptylinesexcept_namespace

    # good
    
    class Foo
      class Bar
    
        # ...
    
      end
    end

    Example: EnforcedStyle: emptylinesspecial

    # good
    class Foo
    
      def bar; end
    
    end

    Example: EnforcedStyle: noemptylines (default)

    # good
    
    class Foo
      def bar
        # ...
      end
    end

    end at 23, 10 is not aligned with if at 19, 21.
    Open

              end

    This cop checks whether the end keywords are aligned properly.

    Three modes are supported through the EnforcedStyleAlignWith configuration parameter:

    If it's set to keyword (which is the default), the end shall be aligned with the start of the keyword (if, class, etc.).

    If it's set to variable the end shall be aligned with the left-hand-side of the variable assignment, if there is one.

    If it's set to start_of_line, the end shall be aligned with the start of the line where the matching keyword appears.

    Example: EnforcedStyleAlignWith: keyword (default)

    # bad
    
    variable = if true
        end
    
    # good
    
    variable = if true
               end

    Example: EnforcedStyleAlignWith: variable

    # bad
    
    variable = if true
        end
    
    # good
    
    variable = if true
    end

    Example: EnforcedStyleAlignWith: startofline

    # bad
    
    variable = if true
        end
    
    # good
    
    puts(if true
    end)

    Line is too long. [105/80]
    Open

                  key.sub!(/(attr_\w+)\s(\:\w+)/, "#{colorize('\\1', :keyword)} #{colorize('\\2', :method)}")

    Line is too long. [98/80]
    Open

              property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.

    Align else with if.
    Open

              else

    This cops checks the alignment of else keywords. Normally they should be aligned with an if/unless/while/until/begin/def keyword, but there are special cases when they should follow the same rules as the alignment of end.

    Example:

    # bad
    if something
      code
     else
      code
    end
    
    # bad
    if something
      code
     elsif something
      code
    end
    
    # good
    if something
      code
    else
      code
    end

    Prefer single-quoted strings when you don't need string interpolation or special symbols.
    Open

            str << ":0x%08x" % (object.__id__ * 2) if options[:object_id]

    Checks if uses of quotes match the configured preference.

    Example: EnforcedStyle: single_quotes (default)

    # bad
    "No special symbols"
    "No string interpolation"
    "Just text"
    
    # good
    'No special symbols'
    'No string interpolation'
    'Just text'
    "Wait! What's #{this}!"

    Example: EnforcedStyle: double_quotes

    # bad
    'Just some text'
    'No special chars or interpolation'
    
    # good
    "Just some text"
    "No special chars or interpolation"
    "Every string in #{project} uses double_quotes"

    Avoid the use of Perl-style backrefs.
    Open

                  key.sub!($1, colorize($1, :variable))

    This cop looks for uses of Perl-style regexp match backreferences like $1, $2, etc.

    Example:

    # bad
    puts $1
    
    # good
    puts Regexp.last_match(1)

    Use % instead of %Q.
    Open

              "#<#{awesome_instance}\n#{data.join(%Q/,\n/)}\n#{outdent}>"

    This cop checks if usage of %() or %Q() matches configuration.

    Example: EnforcedStyle: bare_percent (default)

    # bad
    %Q(He said: "#{greeting}")
    %q{She said: 'Hi'}
    
    # good
    %(He said: "#{greeting}")
    %{She said: 'Hi'}

    Example: EnforcedStyle: percent_q

    # bad
    %|He said: "#{greeting}"|
    %/She said: 'Hi'/
    
    # good
    %Q|He said: "#{greeting}"|
    %q/She said: 'Hi'/

    Missing top-level class documentation comment.
    Open

        class ObjectFormatter < BaseFormatter

    This cop checks for missing top-level documentation of classes and modules. Classes with no body are exempt from the check and so are namespace modules - modules that have nothing in their bodies except classes, other modules, or constant definitions.

    The documentation requirement is annulled if the class or module has a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the same for all its children.

    Example:

    # bad
    class Person
      # ...
    end
    
    # good
    # Description/Explanation of Person class
    class Person
      # ...
    end

    Use string literal '' instead of String.new.
    Open

            str = String.new

    This cop checks for the use of a method, the result of which would be a literal, like an empty array, hash or string.

    Example:

    # bad
    a = Array.new
    h = Hash.new
    s = String.new
    
    # good
    a = []
    h = {}
    s = ''

    Favor format over String#%.
    Open

            str << ":0x%08x" % (object.__id__ * 2) if options[:object_id]

    This cop enforces the use of a single string formatting utility. Valid options include Kernel#format, Kernel#sprintf and String#%.

    The detection of String#% cannot be implemented in a reliable manner for all cases, so only two scenarios are considered - if the first argument is a string literal and if the second argument is an array literal.

    Example: EnforcedStyle: format(default)

    # bad
    puts sprintf('%10s', 'hoge')
    puts '%10s' % 'hoge'
    
    # good
    puts format('%10s', 'hoge')

    Example: EnforcedStyle: sprintf

    # bad
    puts format('%10s', 'hoge')
    puts '%10s' % 'hoge'
    
    # good
    puts sprintf('%10s', 'hoge')

    Example: EnforcedStyle: percent

    # bad
    puts format('%10s', 'hoge')
    puts sprintf('%10s', 'hoge')
    
    # good
    puts '%10s' % 'hoge'

    Avoid the use of Perl-style backrefs.
    Open

                  key.sub!($1, colorize($1, :variable))

    This cop looks for uses of Perl-style regexp match backreferences like $1, $2, etc.

    Example:

    # bad
    puts $1
    
    # good
    puts Regexp.last_match(1)

    %Q-literals should be delimited by ( and ).
    Open

              "#<#{awesome_instance}\n#{data.join(%Q/,\n/)}\n#{outdent}>"

    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)

    Line is too long. [96/80]
    Open

                key << colorize(' = ', :hash) + inspector.awesome(object.instance_variable_get(var))

    Use %Q only for strings that contain both single quotes and double quotes, or for dynamic strings that contain double quotes.
    Open

              "#<#{awesome_instance}\n#{data.join(%Q/,\n/)}\n#{outdent}>"

    There are no issues that match your filters.

    Category
    Status