3scale/porta

View on GitHub
app/models/service.rb

Summary

Maintainability
D
2 days
Test Coverage

attr_accessible is recommended over attr_protected
Open

  attr_protected :account_id, :tenant_id, :audit_ids
Severity: Minor
Found in app/models/service.rb by brakeman

This warning comes up if a model does not limit what attributes can be set through mass assignment.

In particular, this check looks for attr_accessible inside model definitions. If it is not found, this warning will be issued.

Brakeman also warns on use of attr_protected - especially since it was found to be vulnerable to bypass. Warnings for mass assignment on models using attr_protected will be reported, but at a lower confidence level.

Note that disabling mass assignment globally will suppress these warnings.

Class Service has 63 methods (exceeds 20 allowed). Consider refactoring.
Open

class Service < ApplicationRecord # rubocop:disable Metrics/ClassLength
  include Searchable
  include Backend::ModelExtensions::Service
  include Logic::Contracting::Service
  include Logic::PlanChanges::Service
Severity: Major
Found in app/models/service.rb - About 1 day to fix

    File service.rb has 435 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    require 'backend_client'
    
    class Service < ApplicationRecord # rubocop:disable Metrics/ClassLength
      include Searchable
      include Backend::ModelExtensions::Service
    Severity: Minor
    Found in app/models/service.rb - About 6 hrs to fix

      Method to_xml has 30 lines of code (exceeds 25 allowed). Consider refactoring.
      Wontfix

        def to_xml(options = {})
          xml = options[:builder] || ThreeScale::XML::Builder.new
      
          xml.service do |xml|
            unless new_record?
      Severity: Minor
      Found in app/models/service.rb - About 1 hr to fix

        Service#visible_plans_for refers to 'buyer_account' more than self (maybe move it to another class?)
        Open

            if buyer_account && !results.include?(buyer_account.bought_plan)
              results << buyer_account.bought_plan
        Severity: Minor
        Found in app/models/service.rb by reek

        Feature Envy occurs when a code fragment references another object more often than it references itself, or when several clients do the same series of manipulations on a particular type of object.

        Feature Envy reduces the code's ability to communicate intent: code that "belongs" on one class but which is located in another can be hard to find, and may upset the "System of Names" in the host class.

        Feature Envy also affects the design's flexibility: A code fragment that is in the wrong class creates couplings that may not be natural within the application's domain, and creates a loss of cohesion in the unwilling host class.

        Feature Envy often arises because it must manipulate other objects (usually its arguments) to get them into a useful form, and one force preventing them (the arguments) doing this themselves is that the common knowledge lives outside the arguments, or the arguments are of too basic a type to justify extending that type. Therefore there must be something which 'knows' about the contents or purposes of the arguments. That thing would have to be more than just a basic type, because the basic types are either containers which don't know about their contents, or they are single objects which can't capture their relationship with their fellows of the same type. So, this thing with the extra knowledge should be reified into a class, and the utility method will most likely belong there.

        Example

        Running Reek on:

        class Warehouse
          def sale_price(item)
            (item.price - item.rebate) * @vat
          end
        end

        would report:

        Warehouse#total_price refers to item more than self (FeatureEnvy)

        since this:

        (item.price - item.rebate)

        belongs to the Item class, not the Warehouse.

        Service#to_xml contains iterators nested 3 deep
        Wontfix

                  notification_settings.each { |notification, values| xml.tag! notification, values }
        Severity: Minor
        Found in app/models/service.rb by reek

        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)

        Service has at least 62 methods
        Open

        class Service < ApplicationRecord # rubocop:disable Metrics/ClassLength
        Severity: Minor
        Found in app/models/service.rb by reek

        Too Many Methods is a special case of LargeClass.

        Example

        Given this configuration

        TooManyMethods:
          max_methods: 3

        and this code:

        class TooManyMethods
          def one; end
          def two; end
          def three; end
          def four; end
        end

        Reek would emit the following warning:

        test.rb -- 1 warning:
          [1]:TooManyMethods has at least 4 methods (TooManyMethods)

        Service#to_xml has approx 25 statements
        Open

          def to_xml(options = {})
        Severity: Minor
        Found in app/models/service.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.)

        Service#visible_plans_for refers to 'results' more than self (maybe move it to another class?)
        Open

            if buyer_account && !results.include?(buyer_account.bought_plan)
              results << buyer_account.bought_plan
        Severity: Minor
        Found in app/models/service.rb by reek

        Feature Envy occurs when a code fragment references another object more often than it references itself, or when several clients do the same series of manipulations on a particular type of object.

        Feature Envy reduces the code's ability to communicate intent: code that "belongs" on one class but which is located in another can be hard to find, and may upset the "System of Names" in the host class.

        Feature Envy also affects the design's flexibility: A code fragment that is in the wrong class creates couplings that may not be natural within the application's domain, and creates a loss of cohesion in the unwilling host class.

        Feature Envy often arises because it must manipulate other objects (usually its arguments) to get them into a useful form, and one force preventing them (the arguments) doing this themselves is that the common knowledge lives outside the arguments, or the arguments are of too basic a type to justify extending that type. Therefore there must be something which 'knows' about the contents or purposes of the arguments. That thing would have to be more than just a basic type, because the basic types are either containers which don't know about their contents, or they are single objects which can't capture their relationship with their fellows of the same type. So, this thing with the extra knowledge should be reified into a class, and the utility method will most likely belong there.

        Example

        Running Reek on:

        class Warehouse
          def sale_price(item)
            (item.price - item.rebate) * @vat
          end
        end

        would report:

        Warehouse#total_price refers to item more than self (FeatureEnvy)

        since this:

        (item.price - item.rebate)

        belongs to the Item class, not the Warehouse.

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

          def mode_type
            return unless proxy
        
            if proxy.self_managed?
              if oauth?
        Severity: Minor
        Found in app/models/service.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

        Service assumes too much for instance variable '@deleted_by_state_machine'
        Open

        class Service < ApplicationRecord # rubocop:disable Metrics/ClassLength
        Severity: Minor
        Found in app/models/service.rb by reek

        Classes should not assume that instance variables are set or present outside of the current class definition.

        Good:

        class Foo
          def initialize
            @bar = :foo
          end
        
          def foo?
            @bar == :foo
          end
        end

        Good as well:

        class Foo
          def foo?
            bar == :foo
          end
        
          def bar
            @bar ||= :foo
          end
        end

        Bad:

        class Foo
          def go_foo!
            @bar = :foo
          end
        
          def foo?
            @bar == :foo
          end
        end

        Example

        Running Reek on:

        class Dummy
          def test
            @ivar
          end
        end

        would report:

        [1]:InstanceVariableAssumption: Dummy assumes too much for instance variable @ivar

        Note that this example would trigger this smell warning as well:

        class Parent
          def initialize(omg)
            @omg = omg
          end
        end
        
        class Child < Parent
          def foo
            @omg
          end
        end

        The way to address the smell warning is that you should create an attr_reader to use @omg in the subclass and not access @omg directly like this:

        class Parent
          attr_reader :omg
        
          def initialize(omg)
            @omg = omg
          end
        end
        
        class Child < Parent
          def foo
            omg
          end
        end

        Directly accessing instance variables is considered a smell because it breaks encapsulation and makes it harder to reason about code.

        If you don't want to expose those methods as public API just make them private like this:

        class Parent
          def initialize(omg)
            @omg = omg
          end
        
          private
          attr_reader :omg
        end
        
        class Child < Parent
          def foo
            omg
          end
        end

        Current Support in Reek

        An instance variable must:

        • be set in the constructor
        • or be accessed through a method with lazy initialization / memoization.

        If not, Instance Variable Assumption will be reported.

        Service#visible_plans_for calls 'buyer_account.bought_plan' 2 times
        Open

            if buyer_account && !results.include?(buyer_account.bought_plan)
              results << buyer_account.bought_plan
        Severity: Minor
        Found in app/models/service.rb by reek

        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.

        Method to_xml has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
        Wontfix

          def to_xml(options = {})
            xml = options[:builder] || ThreeScale::XML::Builder.new
        
            xml.service do |xml|
              unless new_record?
        Severity: Minor
        Found in app/models/service.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

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

          def default_service_plan_state
            return unless account && account.provider_can_use?(:published_service_plan_signup)
            return if account.should_be_deleted?
            account.settings.service_plans_ui_visible? ? 'hidden' : 'published'
          end
        Severity: Minor
        Found in app/models/service.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

        There are no issues that match your filters.

        Category
        Status