codeclimate/codeclimate-yaml

View on GitHub
lib/cc/yaml/serializer/json.rb

Summary

Maintainability
A
25 mins
Test Coverage

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

      def lines(wrapper, lines)
        return wrapper % lines.join(",") unless pretty?
        return wrapper % "" if lines.empty?
        return wrapper % " #{lines.first} " unless lines.size > 1 or  lines.first.include?("\n") or lines.first.size > 50
        lines = "\n  " + lines.join(",\n").strip.gsub("\n", "\n  ") + "\n"
Severity: Minor
Found in lib/cc/yaml/serializer/json.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

Inconsistent indentation detected.
Open

      def serialize_str(value)
        string = value.encode("utf-8")
        string.force_encoding("binary")
        string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
        string.force_encoding("utf-8")
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Inconsistent indentation detected.
Open

      def serialize_float(value)
        raise NotSupportedError, "cannot serialize infinity as JSON" if value.infinite?
        "#{value}"
      end
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Inconsistent indentation detected.
Open

      def serialize_sequence(node)
        lines("[%s]", super)
      end
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Inconsistent indentation detected.
Open

      def lines(wrapper, lines)
        return wrapper % lines.join(",") unless pretty?
        return wrapper % "" if lines.empty?
        return wrapper % " #{lines.first} " unless lines.size > 1 or  lines.first.include?("\n") or lines.first.size > 50
        lines = "\n  " + lines.join(",\n").strip.gsub("\n", "\n  ") + "\n"
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Indent the right brace the same as the start of the line where the left brace is.
Open

      }
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks the indentation of the first key in a hash literal where the opening brace and the first key are on separate lines. The other keys' indentations are handled by the AlignHash cop.

By default, Hash literals that are arguments in a method call with parentheses, and where the opening curly brace of the hash is on the same line as the opening parenthesis of the method call, shall have their first key indented one step (two spaces) more than the position inside the opening parenthesis.

Other hash literals shall have their first key indented one step more than the start of the line where the opening curly brace is.

This default style is called 'specialinsideparentheses'. Alternative styles are 'consistent' and 'align_braces'. Here are examples:

Example: EnforcedStyle: specialinsideparentheses (default)

# The `special_inside_parentheses` style enforces that the first key
# in a hash literal where the opening brace and the first key are on
# separate lines is indented one step (two spaces) more than the
# position inside the opening parentheses.

# bad
hash = {
  key: :value
}
and_in_a_method_call({
  no: :difference
                     })

# good
special_inside_parentheses
hash = {
  key: :value
}
but_in_a_method_call({
                       its_like: :this
                     })

Example: EnforcedStyle: consistent

# The `consistent` style enforces that the first key in a hash
# literal where the opening brace and the first key are on
# seprate lines is indented the same as a hash literal which is not
# defined inside a method call.

# bad
hash = {
  key: :value
}
but_in_a_method_call({
                       its_like: :this
                      })

# good
hash = {
  key: :value
}
and_in_a_method_call({
  no: :difference
})

Example: EnforcedStyle: align_braces

# The `align_brackets` style enforces that the opening and closing
# braces are indented to the same position.

# bad
and_now_for_something = {
                          completely: :different
}

# good
and_now_for_something = {
                          completely: :different
                        }

Avoid the use of double negation (!!).
Open

        !!options[:pretty]
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cop checks for uses of double negation (!!) to convert something to a boolean value. As this is both cryptic and usually redundant, it should be avoided.

Example:

# bad
!!something

# good
!something.nil?

Please, note that when something is a boolean value !!something and !something.nil? are not the same thing. As you're unlikely to write code that can accept values of any type this is rarely a problem in practice.

Inconsistent indentation detected.
Open

      def pretty?
        !!options[:pretty]
      end
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Prefer annotated tokens (like %<foo>s</foo>) over unannotated tokens (like %s).
Open

      def key_value(key, value, wrapper = "%s")
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

Use a consistent style for named format string tokens.

Note: unannotated style cop only works for strings which are passed as arguments to those methods: sprintf, format, %. The reason is that unannotated format is very similar to encoded URLs or Date/Time formatting strings.

Example: EnforcedStyle: annotated (default)

# bad
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')

# good
format('%<greeting>s', greeting: 'Hello')</greeting>

Example: EnforcedStyle: template

# bad
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')

# good
format('%{greeting}', greeting: 'Hello')</greeting>

Example: EnforcedStyle: unannotated

# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', 'Hello')

# good
format('%s', 'Hello')</greeting>

Freeze mutable objects assigned to constants.
Open

        MAP = { # mapping stolen from json gem
        "\x0"  => '\u0000', "\x1"  => '\u0001', "\x2"  => '\u0002', "\x3"  => '\u0003', "\x4"  => '\u0004', "\x5"  => '\u0005',
        "\x6"  => '\u0006', "\x7"  => '\u0007', "\b"   =>  '\b',    "\t"   =>  '\t',    "\n"   => '\n',     "\xb"  => '\u000b',
        "\f"   => '\f',     "\r"   =>  '\r',    "\xe"  => '\u000e', "\xf"  => '\u000f', "\x10" => '\u0010', "\x11" => '\u0011',
        "\x12" => '\u0012', "\x13" => '\u0013', "\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016', "\x17" => '\u0017',
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cop checks whether some constant value isn't a mutable literal (e.g. array or hash).

Example:

# bad
CONST = [1, 2, 3]

# good
CONST = [1, 2, 3].freeze

Inconsistent indentation detected.
Open

      def serialize_key(value)
        value.to_s
      end
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.
Open

        "\x0"  => '\u0000', "\x1"  => '\u0001', "\x2"  => '\u0002', "\x3"  => '\u0003', "\x4"  => '\u0004', "\x5"  => '\u0005',
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks the indentation of the first key in a hash literal where the opening brace and the first key are on separate lines. The other keys' indentations are handled by the AlignHash cop.

By default, Hash literals that are arguments in a method call with parentheses, and where the opening curly brace of the hash is on the same line as the opening parenthesis of the method call, shall have their first key indented one step (two spaces) more than the position inside the opening parenthesis.

Other hash literals shall have their first key indented one step more than the start of the line where the opening curly brace is.

This default style is called 'specialinsideparentheses'. Alternative styles are 'consistent' and 'align_braces'. Here are examples:

Example: EnforcedStyle: specialinsideparentheses (default)

# The `special_inside_parentheses` style enforces that the first key
# in a hash literal where the opening brace and the first key are on
# separate lines is indented one step (two spaces) more than the
# position inside the opening parentheses.

# bad
hash = {
  key: :value
}
and_in_a_method_call({
  no: :difference
                     })

# good
special_inside_parentheses
hash = {
  key: :value
}
but_in_a_method_call({
                       its_like: :this
                     })

Example: EnforcedStyle: consistent

# The `consistent` style enforces that the first key in a hash
# literal where the opening brace and the first key are on
# seprate lines is indented the same as a hash literal which is not
# defined inside a method call.

# bad
hash = {
  key: :value
}
but_in_a_method_call({
                       its_like: :this
                      })

# good
hash = {
  key: :value
}
and_in_a_method_call({
  no: :difference
})

Example: EnforcedStyle: align_braces

# The `align_brackets` style enforces that the opening and closing
# braces are indented to the same position.

# bad
and_now_for_something = {
                          completely: :different
}

# good
and_now_for_something = {
                          completely: :different
                        }

Prefer annotated tokens (like %<foo>s</foo>) over unannotated tokens (like %s).
Open

        key_value("secure", serialize_str(value.encrypted_string), "{%s}")
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

Use a consistent style for named format string tokens.

Note: unannotated style cop only works for strings which are passed as arguments to those methods: sprintf, format, %. The reason is that unannotated format is very similar to encoded URLs or Date/Time formatting strings.

Example: EnforcedStyle: annotated (default)

# bad
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')

# good
format('%<greeting>s', greeting: 'Hello')</greeting>

Example: EnforcedStyle: template

# bad
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')

# good
format('%{greeting}', greeting: 'Hello')</greeting>

Example: EnforcedStyle: unannotated

# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', 'Hello')

# good
format('%s', 'Hello')</greeting>

Prefer annotated tokens (like %<foo>s</foo>) over unannotated tokens (like %s).
Open

        lines("[%s]", super)
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

Use a consistent style for named format string tokens.

Note: unannotated style cop only works for strings which are passed as arguments to those methods: sprintf, format, %. The reason is that unannotated format is very similar to encoded URLs or Date/Time formatting strings.

Example: EnforcedStyle: annotated (default)

# bad
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')

# good
format('%<greeting>s', greeting: 'Hello')</greeting>

Example: EnforcedStyle: template

# bad
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')

# good
format('%{greeting}', greeting: 'Hello')</greeting>

Example: EnforcedStyle: unannotated

# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', 'Hello')

# good
format('%s', 'Hello')</greeting>

Inconsistent indentation detected.
Open

      def serialize_binary(value)
        raise NotSupportedError, "cannot serialize binary data as JSON"
      end
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Use || instead of or.
Open

        return wrapper % " #{lines.first} " unless lines.size > 1 or  lines.first.include?("\n") or lines.first.size > 50
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cop checks for uses of and and or, and suggests using && and || instead. It can be configured to check only in conditions, or in all contexts.

Example: EnforcedStyle: always (default)

# bad
foo.save and return

# bad
if foo and bar
end

# good
foo.save && return

# good
if foo && bar
end

Example: EnforcedStyle: conditionals

# bad
if foo and bar
end

# good
foo.save && return

# good
foo.save and return

# good
if foo && bar
end

Inconsistent indentation detected.
Open

      def serialize_bool(value)
        value ? "true" : "false"
      end
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Inconsistent indentation detected.
Open

      def serialize_decrypted(value)
        serialize_str(value.decrypted_string)
      end
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Inconsistent indentation detected.
Open

      def serialize_mapping(node)
        lines("{%s}", super.map { |key, value| key_value(key, value) })
      end
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Use || instead of or.
Open

        return wrapper % " #{lines.first} " unless lines.size > 1 or  lines.first.include?("\n") or lines.first.size > 50
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cop checks for uses of and and or, and suggests using && and || instead. It can be configured to check only in conditions, or in all contexts.

Example: EnforcedStyle: always (default)

# bad
foo.save and return

# bad
if foo and bar
end

# good
foo.save && return

# good
if foo && bar
end

Example: EnforcedStyle: conditionals

# bad
if foo and bar
end

# good
foo.save && return

# good
foo.save and return

# good
if foo && bar
end

Inconsistent indentation detected.
Open

      def key_value(key, value, wrapper = "%s")
        space = pretty? ? " " : ""
        wrapper % "#{serialize_str(key)}:#{space}#{value}"
      end
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Inconsistent indentation detected.
Open

      def serialize_encrypted(value)
        key_value("secure", serialize_str(value.encrypted_string), "{%s}")
      end
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cops checks for inconsistent indentation.

Example:

class A
  def test
    puts 'hello'
     puts 'world'
  end
end

Prefer to_s over string interpolation.
Open

        "#{value}"
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cop checks for strings that are just an interpolated expression.

Example:

# bad
"#{@var}"

# good
@var.to_s

# good if @var is already a String
@var

Unused method argument - value. If it's necessary, use _ or _value as an argument name to indicate that it won't be used. You can also write as serialize_binary(*) if you want the method to accept any arguments but don't care about them.
Open

      def serialize_binary(value)
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

This cop checks for unused method arguments.

Example:

# bad

def some_method(used, unused, _unused_but_allowed)
  puts used
end

Example:

# good

def some_method(used, _unused, _unused_but_allowed)
  puts used
end

Prefer annotated tokens (like %<foo>s</foo>) over unannotated tokens (like %s).
Open

        lines("{%s}", super.map { |key, value| key_value(key, value) })
Severity: Minor
Found in lib/cc/yaml/serializer/json.rb by rubocop

Use a consistent style for named format string tokens.

Note: unannotated style cop only works for strings which are passed as arguments to those methods: sprintf, format, %. The reason is that unannotated format is very similar to encoded URLs or Date/Time formatting strings.

Example: EnforcedStyle: annotated (default)

# bad
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')

# good
format('%<greeting>s', greeting: 'Hello')</greeting>

Example: EnforcedStyle: template

# bad
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')

# good
format('%{greeting}', greeting: 'Hello')</greeting>

Example: EnforcedStyle: unannotated

# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', 'Hello')

# good
format('%s', 'Hello')</greeting>

There are no issues that match your filters.

Category
Status