wearefine/fae

View on GitHub
app/controllers/concerns/fae/cloneable.rb

Summary

Maintainability
B
4 hrs
Test Coverage

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

  module Cloneable
    extend ActiveSupport::Concern

    private

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 clone_has_one_relationship is too high. [43.78/15]
Open

    def clone_has_one_relationship(association,type)
      old_record = @item.send(association)

      if old_record.present?
        new_record = old_record.dup

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 clone_has_many_relationships is too high. [23.22/15]
Open

    def clone_has_many_relationships(association)
      if @item.send(association).present?
        @item.send(association).reverse.each do |record|
          new_record = association.to_s.classify.constantize.find_by_id(record.id).dup
          new_record.send("#{@klass_singular}_id" + '=', @cloned_item.id) if new_record.send("#{@klass_singular}_id").present?

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 clone_has_one_relationship has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
Open

    def clone_has_one_relationship(association,type)
      old_record = @item.send(association)

      if old_record.present?
        new_record = old_record.dup
Severity: Minor
Found in app/controllers/concerns/fae/cloneable.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

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

    def clone_has_one_relationship(association,type)
      old_record = @item.send(association)

      if old_record.present?
        new_record = old_record.dup

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.

Perceived complexity for clone_has_one_relationship is too high. [10/7]
Open

    def clone_has_one_relationship(association,type)
      old_record = @item.send(association)

      if old_record.present?
        new_record = old_record.dup

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 clone_has_one_relationship is too high. [9/6]
Open

    def clone_has_one_relationship(association,type)
      old_record = @item.send(association)

      if old_record.present?
        new_record = old_record.dup

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.

Assignment Branch Condition size for clone_join_relationships is too high. [18.06/15]
Open

    def clone_join_relationships(object)
      if @item.send(object.to_sym).present?
        @item.send(object.to_sym).reverse.each do |record|
          copied_join = object.classify.constantize.find_by_id(record.id).dup
          copied_join.send("#{@klass_singular}_id" + '=', @cloned_item.id)

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

    def rename_unique_attribute(item, attribute, value)
      index = 2
      symbol = attribute.to_sym
      value = unique_name(item, attribute, value, index.to_s)

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 create_from_existing is too high. [17.58/15]
Open

    def create_from_existing(id)
      @item = @klass.find(id)
      @cloned_item = @item.dup
      update_cloned_attributes(@cloned_item)
      @cloned_item.on_prod = false if @item.respond_to?(:on_prod)

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

    def create_from_existing(id)
      @item = @klass.find(id)
      @cloned_item = @item.dup
      update_cloned_attributes(@cloned_item)
      @cloned_item.on_prod = false if @item.respond_to?(:on_prod)

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 unique_name is too high. [16.25/15]
Open

    def unique_name(item, attribute, value, suffix)
      item.class.validators_on(attribute.to_sym).each do |validator|
        if validator.class.name.include?('LengthValidator') && validator.options[:maximum].present?
          max_length = validator.options[:maximum] - (suffix.length + 1)
          value = value[0...max_length]

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 update_cloneable_associations has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
Open

    def update_cloneable_associations
      associations_for_cloning.each do |association|
        type = @klass.reflect_on_association(association)
        through_record = type.through_reflection

Severity: Minor
Found in app/controllers/concerns/fae/cloneable.rb - About 55 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 clone_has_many_relationships has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

    def clone_has_many_relationships(association)
      if @item.send(association).present?
        @item.send(association).reverse.each do |record|
          new_record = association.to_s.classify.constantize.find_by_id(record.id).dup
          new_record.send("#{@klass_singular}_id" + '=', @cloned_item.id) if new_record.send("#{@klass_singular}_id").present?
Severity: Minor
Found in app/controllers/concerns/fae/cloneable.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 update_cloned_attributes has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Open

    def update_cloned_attributes(item)
      attribute_names = attributes_for_cloning.map(&:to_s) - ['id']
      item.attributes.each do |attribute, value|
        if attribute_names.include? attribute
          rename_unique_attribute(item, attribute, value) if attr_is_unique?(item, attribute)
Severity: Minor
Found in app/controllers/concerns/fae/cloneable.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

Space missing after comma.
Open

        if ['::Fae::Image','::Fae::File'].include?(type.options[:class_name])

Checks for comma (,) not followed by some kind of space.

Example:

# bad
[1,2]
{ foo:bar,}

# good
[1, 2]
{ foo:bar, }

Line is too long. [99/80]
Open

        if validator.class.name.include?('LengthValidator') && validator.options[:maximum].present?

Use a guard clause instead of wrapping the code inside a conditional expression.
Open

      if @item.send(association).present?

Use a guard clause instead of wrapping the code inside a conditional expression

Example:

# bad
def test
  if something
    work
  end
end

# good
def test
  return unless something
  work
end

# also good
def test
  work if something
end

# bad
if something
  raise 'exception'
else
  ok
end

# good
raise 'exception' if something
ok

Convert if nested inside else to elsif.
Open

          new_record.send("#{@klass_singular}_id" + '=', @cloned_item.id) if new_record.send("#{@klass_singular}_id").present?

If the else branch of a conditional consists solely of an if node, it can be combined with the else to become an elsif. This helps to keep the nesting level from getting too deep.

Example:

# bad
if condition_a
  action_a
else
  if condition_b
    action_b
  else
    action_c
  end
end

# good
if condition_a
  action_a
elsif condition_b
  action_b
else
  action_c
end

Line is too long. [130/80]
Open

          new_record.send("#{type.options[:as]}_id" + '=', @cloned_item.id) if new_record.send("#{type.options[:as]}_id").present?

Line is too long. [126/80]
Open

          new_record.send("#{@klass_singular}_id" + '=', @cloned_item.id) if new_record.send("#{@klass_singular}_id").present?

Use next to skip iteration.
Open

        if validator.class.name.include?('LengthValidator') && validator.options[:maximum].present?

Use next to skip iteration instead of a condition at the end.

Example: EnforcedStyle: skipmodifierifs (default)

# bad
[1, 2].each do |a|
  if a == 1
    puts a
  end
end

# good
[1, 2].each do |a|
  next unless a == 1
  puts a
end

# good
[1, 2].each do |o|
  puts o unless o == 1
end

Example: EnforcedStyle: always

# With `always` all conditions at the end of an iteration needs to be
# replaced by next - with `skip_modifier_ifs` the modifier if like
# this one are ignored: `[1, 2].each { |a| return 'yes' if a == 1 }`

# bad
[1, 2].each do |o|
  puts o unless o == 1
end

# bad
[1, 2].each do |a|
  if a == 1
    puts a
  end
end

# good
[1, 2].each do |a|
  next unless a == 1
  puts a
end

Line is too long. [126/80]
Open

          new_record.send("#{@klass_singular}_id" + '=', @cloned_item.id) if new_record.send("#{@klass_singular}_id").present?

Line is too long. [142/80]
Open

          new_record.send("#{type.options[:as]}_type" + '=', @cloned_item.class.name) if new_record.send("#{type.options[:as]}_type").present?

Use Kernel#loop with break rather than begin/end/until(or while).
Open

      end while record.present?

This cop checks for uses of begin...end while/until something.

Example:

# bad

# using while
begin
  do_something
end while some_condition

Example:

# bad

# using until
begin
  do_something
end until some_condition

Example:

# good

# using while
while some_condition
  do_something
end

Example:

# good

# using until
until some_condition
  do_something
end

Line is too long. [133/80]
Open

          Fae::AssetCloner.new(old_record, new_record, :asset).set_file if old_record.asset.present? && old_record.asset.url.present?

Space missing after comma.
Open

    def clone_has_one_relationship(association,type)

Checks for comma (,) not followed by some kind of space.

Example:

# bad
[1,2]
{ foo:bar,}

# good
[1, 2]
{ foo:bar, }

Line is too long. [93/80]
Open

          rename_unique_attribute(item, attribute, value) if attr_is_unique?(item, attribute)

Line is too long. [86/80]
Open

          new_record = association.to_s.classify.constantize.find_by_id(record.id).dup

Line is too long. [113/80]
Open

            rename_unique_attribute(new_record, attribute, value) if attr_is_unique?(new_record, attribute.first)

Use a guard clause instead of wrapping the code inside a conditional expression.
Open

      if old_record.present?

Use a guard clause instead of wrapping the code inside a conditional expression

Example:

# bad
def test
  if something
    work
  end
end

# good
def test
  return unless something
  work
end

# also good
def test
  work if something
end

# bad
if something
  raise 'exception'
else
  ok
end

# good
raise 'exception' if something
ok

Line is too long. [117/80]
Open

      item.class.validators_on(attribute.to_sym).map(&:class).include? ActiveRecord::Validations::UniquenessValidator

Line is too long. [111/80]
Open

          rename_unique_attribute(new_record, attribute, value) if attr_is_unique?(new_record, attribute.first)

Missing top-level module documentation comment.
Open

  module Cloneable

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 a guard clause instead of wrapping the code inside a conditional expression.
Open

      if @item.send(object.to_sym).present?

Use a guard clause instead of wrapping the code inside a conditional expression

Example:

# bad
def test
  if something
    work
  end
end

# good
def test
  return unless something
  work
end

# also good
def test
  work if something
end

# bad
if something
  raise 'exception'
else
  ok
end

# good
raise 'exception' if something
ok

Space missing after comma.
Open

          clone_has_one_relationship(association,type) if type.macro == :has_one

Checks for comma (,) not followed by some kind of space.

Example:

# bad
[1,2]
{ foo:bar,}

# good
[1, 2]
{ foo:bar, }

There are no issues that match your filters.

Category
Status