reactrb/reactive-record

View on GitHub
lib/reactive_record/server_data_cache.rb

Summary

Maintainability
D
2 days
Test Coverage

Method load_from_json has a Cognitive Complexity of 48 (exceeds 5 allowed). Consider refactoring.
Open

      def self.load_from_json(tree, target = nil)
        ignore_all = nil

        # have to process *all before any other items
        # we leave the "*all" key in just for debugging purposes, and then skip it below
Severity: Minor
Found in lib/reactive_record/server_data_cache.rb - About 7 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 apply_method_to_cache has a Cognitive Complexity of 32 (exceeds 5 allowed). Consider refactoring.
Open

          def apply_method_to_cache(method)
            @db_cache.inject(nil) do | representative, cache_item |
              if cache_item.vector == vector
                if @ar_object.class < ActiveRecord::Base and @ar_object.attributes.has_key?(method)
                  @ar_object.check_permission_with_acting_user(acting_user, :view_permitted?, method)
Severity: Minor
Found in lib/reactive_record/server_data_cache.rb - About 4 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 as_hash has a Cognitive Complexity of 30 (exceeds 5 allowed). Consider refactoring.
Open

          def as_hash(children = nil)
            unless children
              return {} if @ar_object.is_a?(Class) and (@ar_object < ActiveRecord::Base)
              children = [@ar_object.is_a?(BigDecimal) ? @ar_object.to_f : @ar_object]
            end
Severity: Minor
Found in lib/reactive_record/server_data_cache.rb - About 4 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

Cyclomatic complexity for load_from_json is too high. [29/6]
Open

      def self.load_from_json(tree, target = nil)
        ignore_all = nil

        # have to process *all before any other items
        # we leave the "*all" key in just for debugging purposes, and then skip it below

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 has too many lines. [46/30]
Open

      def self.load_from_json(tree, target = nil)
        ignore_all = nil

        # have to process *all before any other items
        # we leave the "*all" key in just for debugging purposes, and then skip it below

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

          def apply_method_to_cache(method)
            @db_cache.inject(nil) do | representative, cache_item |
              if cache_item.vector == vector
                if @ar_object.class < ActiveRecord::Base and @ar_object.attributes.has_key?(method)
                  @ar_object.check_permission_with_acting_user(acting_user, :view_permitted?, method)

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.

Cyclomatic complexity for as_hash is too high. [13/6]
Open

          def as_hash(children = nil)
            unless children
              return {} if @ar_object.is_a?(Class) and (@ar_object < ActiveRecord::Base)
              children = [@ar_object.is_a?(BigDecimal) ? @ar_object.to_f : @ar_object]
            end

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

      def self.load_from_json(tree, target = nil)
        ignore_all = nil

        # have to process *all before any other items
        # we leave the "*all" key in just for debugging purposes, and then skip it below
Severity: Minor
Found in lib/reactive_record/server_data_cache.rb - About 1 hr to fix

    Method has too many lines. [31/30]
    Open

              def apply_method_to_cache(method)
                @db_cache.inject(nil) do | representative, cache_item |
                  if cache_item.vector == vector
                    if @ar_object.class < ActiveRecord::Base and @ar_object.attributes.has_key?(method)
                      @ar_object.check_permission_with_acting_user(acting_user, :view_permitted?, method)

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

              def apply_method_to_cache(method)
                @db_cache.inject(nil) do | representative, cache_item |
                  if cache_item.vector == vector
                    if @ar_object.class < ActiveRecord::Base and @ar_object.attributes.has_key?(method)
                      @ar_object.check_permission_with_acting_user(acting_user, :view_permitted?, method)
    Severity: Minor
    Found in lib/reactive_record/server_data_cache.rb - About 1 hr to fix

      Method apply_star has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
      Open

                def apply_star
                  if value && value.length > 0
                    i = -1
                    value.inject(nil) do | representative, ar_object |
                      i += 1
      Severity: Minor
      Found in lib/reactive_record/server_data_cache.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

      Avoid deeply nested control flow statements.
      Open

                          if cache_item.value and cache_item.value != []
                            ReactiveRecord::Pry::rescued(e)
                            raise e, "ReactiveRecord exception caught when applying #{method} to db object #{cache_item.value}: #{e}", e.backtrace
                          else
                            representative
      Severity: Major
      Found in lib/reactive_record/server_data_cache.rb - About 45 mins to fix

        Avoid deeply nested control flow statements.
        Open

                      elsif method == "*all"
                        @parent.as_hash({jsonize(method) => children.first})
                      else
                        @parent.as_hash({jsonize(method) => children})
        Severity: Major
        Found in lib/reactive_record/server_data_cache.rb - About 45 mins to fix

          Avoid deeply nested control flow statements.
          Open

                            :id => (method.is_a?(Array) && method.first == "new") ? [nil] : [@ar_object.id],
          Severity: Major
          Found in lib/reactive_record/server_data_cache.rb - About 45 mins to fix

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

                      def aggregation?(method)
                        if method.is_a?(String) && value.class.respond_to?(:reflect_on_aggregation)
                          aggregation = value.class.reflect_on_aggregation(method.to_sym)
                          if aggregation && !(aggregation.klass < ActiveRecord::Base) && value.send(method)
                            aggregation
            Severity: Minor
            Found in lib/reactive_record/server_data_cache.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

            Avoid more than 4 levels of block nesting.
            Open

                                if cache_item.value and cache_item.value != []
                                  ReactiveRecord::Pry::rescued(e)
                                  raise e, "ReactiveRecord exception caught when applying #{method} to db object #{cache_item.value}: #{e}", e.backtrace
                                else
                                  representative

            This cop checks for excessive nesting of conditional and looping constructs.

            You can configure if blocks are considered using the CountBlocks option. When set to false (the default) blocks are not counted towards the nesting level. Set to true to count blocks as well.

            The maximum level of nesting allowed is configurable.

            end at 354, 2 is not aligned with module at 1, 0.
            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)

            Useless assignment to variable - ignore_all.
            Open

                    ignore_all = nil

            This cop checks for every useless assignment to local variable in every scope. The basic idea for this cop was from the warning of ruby -cw:

            assigned but unused variable - foo

            Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.

            Example:

            # bad
            
            def some_method
              some_var = 1
              do_something
            end

            Example:

            # good
            
            def some_method
              some_var = 1
              do_something(some_var)
            end

            Use == if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.
            Open

                            elsif preloaded_value = @preloaded_records[cache_item.absolute_vector + [method]]

            This cop checks for assignments in the conditions of if/while/until.

            Example:

            # bad
            
            if some_var = true
              do_something
            end

            Example:

            # good
            
            if some_var == true
              do_something
            end

            Use == if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.
            Open

                    if id_value = tree["id"] and id_value.is_a? Array

            This cop checks for assignments in the conditions of if/while/until.

            Example:

            # bad
            
            if some_var = true
              do_something
            end

            Example:

            # good
            
            if some_var == true
              do_something
            end

            Ambiguous block operator. Parenthesize the method arguments if it's surely a block operator, or add a whitespace to the right of the & if it should be a binary AND.
            Open

                    def detect(&block); @cache.detect &block; end

            This cop checks for ambiguous operators in the first argument of a method invocation without parentheses.

            Example:

            # bad
            
            # The `*` is interpreted as a splat operator but it could possibly be
            # a `*` method invocation (i.e. `do_something.*(some_array)`).
            do_something *some_array

            Example:

            # good
            
            # With parentheses, there's no ambiguity.
            do_something(*some_array)

            Avoid rescuing the Exception class. Perhaps you meant to rescue StandardError?
            Open

                              rescue Exception => e
                                if cache_item.value and cache_item.value != []
                                  ReactiveRecord::Pry::rescued(e)
                                  raise e, "ReactiveRecord exception caught when applying #{method} to db object #{cache_item.value}: #{e}", e.backtrace
                                else

            This cop checks for rescue blocks targeting the Exception class.

            Example:

            # bad
            
            begin
              do_something
            rescue Exception
              handle_exception
            end

            Example:

            # good
            
            begin
              do_something
            rescue ArgumentError
              handle_exception
            end

            Avoid rescuing the Exception class. Perhaps you meant to rescue StandardError?
            Open

            rescue Exception => e
              `debugger`
              nil

            This cop checks for rescue blocks targeting the Exception class.

            Example:

            # bad
            
            begin
              do_something
            rescue Exception
              handle_exception
            end

            Example:

            # good
            
            begin
              do_something
            rescue ArgumentError
              handle_exception
            end

            Ambiguous splat operator. Parenthesize the method arguments if it's surely a splat operator, or add a whitespace to the right of the * if it should be a multiplication.
            Open

                        new_target = target.send *method

            This cop checks for ambiguous operators in the first argument of a method invocation without parentheses.

            Example:

            # bad
            
            # The `*` is interpreted as a splat operator but it could possibly be
            # a `*` method invocation (i.e. `do_something.*(some_array)`).
            do_something *some_array

            Example:

            # good
            
            # With parentheses, there's no ambiguity.
            do_something(*some_array)

            Use == if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.
            Open

                            elsif aggregation = cache_item.aggregation?(method)

            This cop checks for assignments in the conditions of if/while/until.

            Example:

            # bad
            
            if some_var = true
              do_something
            end

            Example:

            # good
            
            if some_var == true
              do_something
            end

            Use == if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.
            Open

                            if preloaded_value = @preloaded_records[absolute_vector + ["*#{i}"]]

            This cop checks for assignments in the conditions of if/while/until.

            Example:

            # bad
            
            if some_var = true
              do_something
            end

            Example:

            # good
            
            if some_var == true
              do_something
            end

            Ambiguous splat operator. Parenthesize the method arguments if it's surely a splat operator, or add a whitespace to the right of the * if it should be a multiplication.
            Open

                          new_target = target.send *method

            This cop checks for ambiguous operators in the first argument of a method invocation without parentheses.

            Example:

            # bad
            
            # The `*` is interpreted as a splat operator but it could possibly be
            # a `*` method invocation (i.e. `do_something.*(some_array)`).
            do_something *some_array

            Example:

            # good
            
            # With parentheses, there's no ambiguity.
            do_something(*some_array)

            Use == if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.
            Open

                      elsif target.class.respond_to?(:reflect_on_aggregation) and aggregation = target.class.reflect_on_aggregation(method) and

            This cop checks for assignments in the conditions of if/while/until.

            Example:

            # bad
            
            if some_var = true
              do_something
            end

            Example:

            # good
            
            if some_var == true
              do_something
            end

            Use == if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.
            Open

                      elsif value.is_a? Hash and value[:id] and value[:id].first and association = target.class.reflect_on_association(method)

            This cop checks for assignments in the conditions of if/while/until.

            Example:

            # bad
            
            if some_var = true
              do_something
            end

            Example:

            # good
            
            if some_var == true
              do_something
            end

            Use == if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.
            Open

                    if sorted_collection = tree["*all"]

            This cop checks for assignments in the conditions of if/while/until.

            Example:

            # bad
            
            if some_var = true
              do_something
            end

            Example:

            # good
            
            if some_var == true
              do_something
            end

            Unused block argument - representative. If it's necessary, use _ or _representative as an argument name to indicate that it won't be used.
            Open

                          value.inject(nil) do | representative, ar_object |

            This cop checks for unused block arguments.

            Example:

            # bad
            
            do_something do |used, unused|
              puts used
            end
            
            do_something do |bar|
              puts :foo
            end
            
            define_method(:foo) do |bar|
              puts :baz
            end

            Example:

            #good
            
            do_something do |used, _unused|
              puts used
            end
            
            do_something do
              puts :foo
            end
            
            define_method(:foo) do |_bar|
              puts :baz
            end

            Useless assignment to variable - e.
            Open

            rescue Exception => e

            This cop checks for every useless assignment to local variable in every scope. The basic idea for this cop was from the warning of ruby -cw:

            assigned but unused variable - foo

            Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.

            Example:

            # bad
            
            def some_method
              some_var = 1
              do_something
            end

            Example:

            # good
            
            def some_method
              some_var = 1
              do_something(some_var)
            end

            Ambiguous block operator. Parenthesize the method arguments if it's surely a block operator, or add a whitespace to the right of the & if it should be a binary AND.
            Open

                    def select(&block); @cache.select &block; end

            This cop checks for ambiguous operators in the first argument of a method invocation without parentheses.

            Example:

            # bad
            
            # The `*` is interpreted as a splat operator but it could possibly be
            # a `*` method invocation (i.e. `do_something.*(some_array)`).
            do_something *some_array

            Example:

            # good
            
            # With parentheses, there's no ambiguity.
            do_something(*some_array)

            Use == if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.
            Open

                        if existing = db_cache.detect { |cached_item| cached_item.vector == [klass_constant] }

            This cop checks for assignments in the conditions of if/while/until.

            Example:

            # bad
            
            if some_var = true
              do_something
            end

            Example:

            # good
            
            if some_var == true
              do_something
            end

            Unused block argument - accessor. If it's necessary, use _ or _accessor as an argument name to indicate that it won't be used.
            Open

                              cache_item.build_new_cache_item(aggregation.mapping.collect { |attribute, accessor| cache_item.value[attribute] }, method, method)

            This cop checks for unused block arguments.

            Example:

            # bad
            
            do_something do |used, unused|
              puts used
            end
            
            do_something do |bar|
              puts :foo
            end
            
            define_method(:foo) do |bar|
              puts :baz
            end

            Example:

            #good
            
            do_something do |used, _unused|
              puts used
            end
            
            do_something do
              puts :foo
            end
            
            define_method(:foo) do |_bar|
              puts :baz
            end

            There are no issues that match your filters.

            Category
            Status