coreSegmentFault/view_delegates

View on GitHub

Showing 78 of 78 total issues

Class has too many lines. [151/100]
Open

  class ViewDelegate
    class << self
      attr_accessor :polymorph_function
      attr_accessor :delegate_cache
    end

This cop checks if the length a class exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Assignment Branch Condition size for render is too high. [40.55/15]
Open

    def render(view, local_params: {}, &block)
      locals = {}
      self.class.view_locals&.each do |method|
        locals[method] = send(method)
      end

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/15]
Open

    def render(view, local_params: {}, &block)
      locals = {}
      self.class.view_locals&.each do |method|
        locals[method] = send(method)
      end

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.

Complex method ViewDelegates::ViewDelegate#render (58.7)
Open

    def render(view, local_params: {}, &block)
      locals = {}
      self.class.view_locals&.each do |method|
        locals[method] = send(method)
      end

Flog calculates the ABC score for methods. The ABC score is based on assignments, branches (method calls), and conditions.

You can read more about ABC metrics or the flog tool

Complex method ViewDelegates::ViewDelegate::cache (42.8)
Open

      def cache(option, size: 50)
        if option
          render_method = instance_method :render
          @delegate_cache = ViewDelegates::Cache.new(max_size: size)
          define_method(:render) do |view, local_params: {}, &block|

Flog calculates the ABC score for methods. The ABC score is based on assignments, branches (method calls), and conditions.

You can read more about ABC metrics or the flog tool

Assignment Branch Condition size for cache is too high. [17.03/15]
Open

      def cache(option, size: 50)
        if option
          render_method = instance_method :render
          @delegate_cache = ViewDelegates::Cache.new(max_size: size)
          define_method(:render) do |view, local_params: {}, &block|

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. [17/15]
Open

      def cache(option, size: 50)
        if option
          render_method = instance_method :render
          @delegate_cache = ViewDelegates::Cache.new(max_size: size)
          define_method(:render) do |view, local_params: {}, &block|

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

    def render(view, local_params: {}, &block)
      locals = {}
      self.class.view_locals&.each do |method|
        locals[method] = send(method)
      end
Severity: Minor
Found in lib/view_delegates/poros/view_delegate.rb - About 1 hr to fix

    Complex method ViewDelegates::ViewDelegate::model_array (26.8)
    Open

          def model_array(method, properties: [])
            attr_accessor method
            # Add the method name to the array of delegate models
            self.ar_models += [method]
            # Define a setter for the model

    Flog calculates the ABC score for methods. The ABC score is based on assignments, branches (method calls), and conditions.

    You can read more about ABC metrics or the flog tool

    ViewDelegates::ViewDelegate#render has approx 21 statements
    Open

        def render(view, local_params: {}, &block)

    A method with Too Many Statements is any method that has a large number of lines.

    Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

    So the following method would score +6 in Reek's statement-counting algorithm:

    def parse(arg, argv, &error)
      if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
        return nil, block, nil                                         # +1
      end
      opt = (val = parse_arg(val, &error))[1]                          # +2
      val = conv_arg(*val)                                             # +3
      if opt and !arg
        argv.shift                                                     # +4
      else
        val[0] = nil                                                   # +5
      end
      val                                                              # +6
    end

    (You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

    ViewDelegates::Cache#get has approx 7 statements
    Open

        def get(key)
    Severity: Minor
    Found in lib/view_delegates/poros/cache.rb by reek

    A method with Too Many Statements is any method that has a large number of lines.

    Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

    So the following method would score +6 in Reek's statement-counting algorithm:

    def parse(arg, argv, &error)
      if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
        return nil, block, nil                                         # +1
      end
      opt = (val = parse_arg(val, &error))[1]                          # +2
      val = conv_arg(*val)                                             # +3
      if opt and !arg
        argv.shift                                                     # +4
      else
        val[0] = nil                                                   # +5
      end
      val                                                              # +6
    end

    (You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

    ViewDelegates::ViewDelegate#cache is controlled by argument 'option'
    Open

            if option

    Control Parameter is a special case of Control Couple

    Example

    A simple example would be the "quoted" parameter in the following method:

    def write(quoted)
      if quoted
        write_quoted @value
      else
        write_unquoted @value
      end
    end

    Fixing those problems is out of the scope of this document but an easy solution could be to remove the "write" method alltogether and to move the calls to "writequoted" / "writeunquoted" in the initial caller of "write".

    ViewDelegates::ViewDelegate#render contains iterators nested 2 deep
    Open

              define_method view_helper do |*args|

    A Nested Iterator occurs when a block contains another block.

    Example

    Given

    class Duck
      class << self
        def duck_names
          %i!tick trick track!.each do |surname|
            %i!duck!.each do |last_name|
              puts "full name is #{surname} #{last_name}"
            end
          end
        end
      end
    end

    Reek would report the following warning:

    test.rb -- 1 warning:
      [5]:Duck#duck_names contains iterators nested 2 deep (NestedIterators)

    ViewDelegates::ViewDelegate#model_array contains iterators nested 2 deep
    Open

              model_array = model_array.map {|e| model_to_struct(e, model_delegate)}

    A Nested Iterator occurs when a block contains another block.

    Example

    Given

    class Duck
      class << self
        def duck_names
          %i!tick trick track!.each do |surname|
            %i!duck!.each do |last_name|
              puts "full name is #{surname} #{last_name}"
            end
          end
        end
      end
    end

    Reek would report the following warning:

    test.rb -- 1 warning:
      [5]:Duck#duck_names contains iterators nested 2 deep (NestedIterators)

    ViewDelegates::ViewDelegate#cache has approx 9 statements
    Open

          def cache(option, size: 50)

    A method with Too Many Statements is any method that has a large number of lines.

    Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

    So the following method would score +6 in Reek's statement-counting algorithm:

    def parse(arg, argv, &error)
      if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
        return nil, block, nil                                         # +1
      end
      opt = (val = parse_arg(val, &error))[1]                          # +2
      val = conv_arg(*val)                                             # +3
      if opt and !arg
        argv.shift                                                     # +4
      else
        val[0] = nil                                                   # +5
      end
      val                                                              # +6
    end

    (You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

    ViewDelegates::ViewDelegate#model has approx 7 statements
    Open

          def model(method, properties: [])

    A method with Too Many Statements is any method that has a large number of lines.

    Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

    So the following method would score +6 in Reek's statement-counting algorithm:

    def parse(arg, argv, &error)
      if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
        return nil, block, nil                                         # +1
      end
      opt = (val = parse_arg(val, &error))[1]                          # +2
      val = conv_arg(*val)                                             # +3
      if opt and !arg
        argv.shift                                                     # +4
      else
        val[0] = nil                                                   # +5
      end
      val                                                              # +6
    end

    (You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

    ViewDelegates::ViewDelegate#model_array has approx 8 statements
    Open

          def model_array(method, properties: [])

    A method with Too Many Statements is any method that has a large number of lines.

    Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

    So the following method would score +6 in Reek's statement-counting algorithm:

    def parse(arg, argv, &error)
      if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
        return nil, block, nil                                         # +1
      end
      opt = (val = parse_arg(val, &error))[1]                          # +2
      val = conv_arg(*val)                                             # +3
      if opt and !arg
        argv.shift                                                     # +4
      else
        val[0] = nil                                                   # +5
      end
      val                                                              # +6
    end

    (You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

    Method cache has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
    Open

          def cache(option, size: 50)
            if option
              render_method = instance_method :render
              @delegate_cache = ViewDelegates::Cache.new(max_size: size)
              define_method(:render) do |view, local_params: {}, &block|
    Severity: Minor
    Found in lib/view_delegates/poros/view_delegate.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

    ViewDelegates::ViewDelegate#initialize calls 'send("#{t}=", view_data[t])' 2 times
    Open

            send("#{t}=", view_data[t]) if view_data[t]
          end
          self.class.properties&.each do |t|
            send("#{t}=", view_data[t]) if view_data[t]

    Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

    Reek implements a check for Duplicate Method Call.

    Example

    Here's a very much simplified and contrived example. The following method will report a warning:

    def double_thing()
      @other.thing + @other.thing
    end

    One quick approach to silence Reek would be to refactor the code thus:

    def double_thing()
      thing = @other.thing
      thing + thing
    end

    A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

    class Other
      def double_thing()
        thing + thing
      end
    end

    The approach you take will depend on balancing other factors in your code.

    ViewDelegates::ViewDelegate#render calls 'self.class.helpers_name' 2 times
    Open

          helper_obj = Struct.new(self.class.helpers_name.camelcase) do
    
            module_helpers.each do |view_helper|
              define_method view_helper do |*args|
                module_methods[view_helper].call(*args)

    Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

    Reek implements a check for Duplicate Method Call.

    Example

    Here's a very much simplified and contrived example. The following method will report a warning:

    def double_thing()
      @other.thing + @other.thing
    end

    One quick approach to silence Reek would be to refactor the code thus:

    def double_thing()
      thing = @other.thing
      thing + thing
    end

    A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

    class Other
      def double_thing()
        thing + thing
      end
    end

    The approach you take will depend on balancing other factors in your code.

    Severity
    Category
    Status
    Source
    Language