codeclimate/codeclimate-duplication

View on GitHub
lib/cc/engine/analyzers/analyzer_base.rb

Summary

Maintainability
B
4 hrs
Test Coverage

Class Base has 23 methods (exceeds 20 allowed). Consider refactoring.
Open

      class Base
        RESCUABLE_ERRORS = [
          ::CC::Engine::Analyzers::ParserError,
          ::Errno::ENOENT,
          ::Racc::ParseError,
Severity: Minor
Found in lib/cc/engine/analyzers/analyzer_base.rb - About 2 hrs to fix

    Method parse_sexp has 32 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

          def parse_sexp
            token = next_token
    
            case token
            when "(" then
    Severity: Minor
    Found in lib/cc/engine/analyzers/analyzer_base.rb - About 1 hr to fix

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

            def parse_sexp
              token = next_token
      
              case token
              when "(" then
      Severity: Minor
      Found in lib/cc/engine/analyzers/analyzer_base.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 run has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
      Open

              def run(file)
                if (skip_reason = skip?(file))
                  CC.logger.info("Skipping file #{file} because #{skip_reason}")
                  nil
                else
      Severity: Minor
      Found in lib/cc/engine/analyzers/analyzer_base.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

      Avoid the use of Perl-style backrefs.
      Open

                    $1.to_sym  # or return $1 as a string

      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)

      Prefer do...end over {...} for procedural blocks.
      Open

                CC.logger.debug { "Contents:\n#{processed_source.raw_source}" }

      Check for uses of braces or do/end around single line or multi-line blocks.

      Example: EnforcedStyle: linecountbased (default)

      # bad - single line block
      items.each do |item| item / 5 end
      
      # good - single line block
      items.each { |item| item / 5 }
      
      # bad - multi-line block
      things.map { |thing|
        something = thing.some_method
        process(something)
      }
      
      # good - multi-line block
      things.map do |thing|
        something = thing.some_method
        process(something)
      end

      Example: EnforcedStyle: semantic

      # Prefer `do...end` over `{...}` for procedural blocks.
      
      # return value is used/assigned
      # bad
      foo = map do |x|
        x
      end
      puts (map do |x|
        x
      end)
      
      # return value is not used out of scope
      # good
      map do |x|
        x
      end
      
      # Prefer `{...}` over `do...end` for functional blocks.
      
      # return value is not used out of scope
      # bad
      each { |x|
        x
      }
      
      # return value is used/assigned
      # good
      foo = map { |x|
        x
      }
      map { |x|
        x
      }.inspect

      Example: EnforcedStyle: bracesforchaining

      # bad
      words.each do |word|
        word.flip.flop
      end.join("-")
      
      # good
      words.each { |word|
        word.flip.flop
      }.join("-")

      Avoid the use of Perl-style backrefs.
      Open

                    Object.const_get $1

      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)

      Do not use empty case condition, instead use an if expression.
      Open

                case

      This cop checks for case statements with an empty condition.

      Example:

      # bad:
      case
      when x == 0
        puts 'x is 0'
      when y == 0
        puts 'y is 0'
      else
        puts 'neither is 0'
      end
      
      # good:
      if x == 0
        puts 'x is 0'
      elsif y == 0
        puts 'y is 0'
      else
        puts 'neither is 0'
      end
      
      # good: (the case condition node is not empty)
      case n
      when 0
        puts 'zero'
      when 1
        puts 'one'
      else
        puts 'more'
      end

      Do not freeze immutable objects, as freezing them has no effect.
      Open

                MAJOR = "major".freeze,

      This cop check for uses of Object#freeze on immutable objects.

      Example:

      # bad
      CONST = 1.freeze
      
      # good
      CONST = 1

      Avoid comma after the last item of an array.
      Open

                ::RuntimeError,

      This cop checks for trailing comma in array and hash literals.

      Example: EnforcedStyleForMultiline: consistent_comma

      # bad
      a = [1, 2,]
      
      # good
      a = [
        1, 2,
        3,
      ]
      
      # good
      a = [
        1,
        2,
      ]

      Example: EnforcedStyleForMultiline: comma

      # bad
      a = [1, 2,]
      
      # good
      a = [
        1,
        2,
      ]

      Example: EnforcedStyleForMultiline: no_comma (default)

      # bad
      a = [1, 2,]
      
      # good
      a = [
        1,
        2
      ]

      end at 38, 12 is not aligned with if at 33, 10.
      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)

      Avoid the use of Perl-style backrefs.
      Open

                $1

      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)

      Avoid the use of Perl-style backrefs.
      Open

                $1.to_sym

      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)

      Do not freeze immutable objects, as freezing them has no effect.
      Open

                MINOR = "minor".freeze,

      This cop check for uses of Object#freeze on immutable objects.

      Example:

      # bad
      CONST = 1.freeze
      
      # good
      CONST = 1

      Avoid rescuing without specifying an error class.
      Open

              rescue => ex

      This cop checks for rescuing StandardError. There are two supported styles implicit and explicit. This cop will not register an offense if any error other than StandardError is specified.

      Example: EnforcedStyle: implicit

      # `implicit` will enforce using `rescue` instead of
      # `rescue StandardError`.
      
      # bad
      begin
        foo
      rescue StandardError
        bar
      end
      
      # good
      begin
        foo
      rescue
        bar
      end
      
      # good
      begin
        foo
      rescue OtherError
        bar
      end
      
      # good
      begin
        foo
      rescue StandardError, SecurityError
        bar
      end

      Example: EnforcedStyle: explicit (default)

      # `explicit` will enforce using `rescue StandardError`
      # instead of `rescue`.
      
      # bad
      begin
        foo
      rescue
        bar
      end
      
      # good
      begin
        foo
      rescue StandardError
        bar
      end
      
      # good
      begin
        foo
      rescue OtherError
        bar
      end
      
      # good
      begin
        foo
      rescue StandardError, SecurityError
        bar
      end

      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

      Avoid rescuing without specifying an error class.
      Open

              rescue => ex

      This cop checks for rescuing StandardError. There are two supported styles implicit and explicit. This cop will not register an offense if any error other than StandardError is specified.

      Example: EnforcedStyle: implicit

      # `implicit` will enforce using `rescue` instead of
      # `rescue StandardError`.
      
      # bad
      begin
        foo
      rescue StandardError
        bar
      end
      
      # good
      begin
        foo
      rescue
        bar
      end
      
      # good
      begin
        foo
      rescue OtherError
        bar
      end
      
      # good
      begin
        foo
      rescue StandardError, SecurityError
        bar
      end

      Example: EnforcedStyle: explicit (default)

      # `explicit` will enforce using `rescue StandardError`
      # instead of `rescue`.
      
      # bad
      begin
        foo
      rescue
        bar
      end
      
      # good
      begin
        foo
      rescue StandardError
        bar
      end
      
      # good
      begin
        foo
      rescue OtherError
        bar
      end
      
      # good
      begin
        foo
      rescue StandardError, SecurityError
        bar
      end

      Use 2 (not 4) spaces for indentation.
      Open

                    Object.const_get $1

      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

      Favor format over String#%.
      Open

                raise SyntaxError, "unhandled token: %p" % [token]

      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'

      %r-literals should be delimited by { and }.
      Open

              when %r%^/(.*)/$% then

      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)

      Avoid comma after the last item of an array.
      Open

                MINOR = "minor".freeze,

      This cop checks for trailing comma in array and hash literals.

      Example: EnforcedStyleForMultiline: consistent_comma

      # bad
      a = [1, 2,]
      
      # good
      a = [
        1, 2,
        3,
      ]
      
      # good
      a = [
        1,
        2,
      ]

      Example: EnforcedStyleForMultiline: comma

      # bad
      a = [1, 2,]
      
      # good
      a = [
        1,
        2,
      ]

      Example: EnforcedStyleForMultiline: no_comma (default)

      # bad
      a = [1, 2,]
      
      # good
      a = [
        1,
        2
      ]

      Prefer do...end over {...} for procedural blocks.
      Open

                  CC.logger.debug { "Response:\n#{ex.response_body}" }

      Check for uses of braces or do/end around single line or multi-line blocks.

      Example: EnforcedStyle: linecountbased (default)

      # bad - single line block
      items.each do |item| item / 5 end
      
      # good - single line block
      items.each { |item| item / 5 }
      
      # bad - multi-line block
      things.map { |thing|
        something = thing.some_method
        process(something)
      }
      
      # good - multi-line block
      things.map do |thing|
        something = thing.some_method
        process(something)
      end

      Example: EnforcedStyle: semantic

      # Prefer `do...end` over `{...}` for procedural blocks.
      
      # return value is used/assigned
      # bad
      foo = map do |x|
        x
      end
      puts (map do |x|
        x
      end)
      
      # return value is not used out of scope
      # good
      map do |x|
        x
      end
      
      # Prefer `{...}` over `do...end` for functional blocks.
      
      # return value is not used out of scope
      # bad
      each { |x|
        x
      }
      
      # return value is used/assigned
      # good
      foo = map { |x|
        x
      }
      map { |x|
        x
      }.inspect

      Example: EnforcedStyle: bracesforchaining

      # bad
      words.each do |word|
        word.flip.flop
      end.join("-")
      
      # good
      words.each { |word|
        word.flip.flop
      }.join("-")

      Favor format over String#%.
      Open

                raise SyntaxError, "Not allowed: /%p/" % [re] unless

      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

                re = $1

      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)

      Avoid the use of Perl-style backrefs.
      Open

                if Object.const_defined?($1)

      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)

      There are no issues that match your filters.

      Category
      Status