3scale/porta

View on GitHub
app/models/cinstance.rb

Summary

Maintainability
D
2 days
Test Coverage

Mass assignment is not restricted using attr_accessible
Open

class Cinstance < Contract
Severity: Critical
Found in app/models/cinstance.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 Cinstance has 52 methods (exceeds 20 allowed). Consider refactoring.
Open

class Cinstance < Contract
  include SaveDestroyForServiceAssociation
  # Maximum number of cinstances permitted between provider and buyer
  MAX = 10

Severity: Major
Found in app/models/cinstance.rb - About 7 hrs to fix

    File cinstance.rb has 339 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    class Cinstance < Contract
      include SaveDestroyForServiceAssociation
      # Maximum number of cinstances permitted between provider and buyer
      MAX = 10
    
    
    Severity: Minor
    Found in app/models/cinstance.rb - About 4 hrs to fix

      Method to_xml has a Cognitive Complexity of 28 (exceeds 5 allowed). Consider refactoring.
      Open

        def to_xml(options = {})
          result = options[:builder] || ThreeScale::XML::Builder.new
      
          result.application do |xml|
            unless new_record?
      Severity: Minor
      Found in app/models/cinstance.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 to_xml has 45 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

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

        Cinstance has at least 48 methods
        Open

        class Cinstance < Contract
        Severity: Minor
        Found in app/models/cinstance.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)

        Cinstance#to_xml contains iterators nested 3 deep
        Open

                    keys.each do |k|
                      keys_element.key k
                    end
                  end
                end
        Severity: Minor
        Found in app/models/cinstance.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)

        Cinstance has at least 5 instance variables
        Open

        class Cinstance < Contract
        Severity: Minor
        Found in app/models/cinstance.rb by reek

        Too Many Instance Variables is a special case of LargeClass.

        Example

        Given this configuration

        TooManyInstanceVariables:
          max_instance_variables: 3

        and this code:

        class TooManyInstanceVariables
          def initialize
            @arg_1 = :dummy
            @arg_2 = :dummy
            @arg_3 = :dummy
            @arg_4 = :dummy
          end
        end

        Reek would emit the following warning:

        test.rb -- 5 warnings:
          [1]:TooManyInstanceVariables has at least 4 instance variables (TooManyInstanceVariables)

        Cinstance#to_xml has approx 25 statements
        Open

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

        Cinstance#select_users refers to 'c' more than self (maybe move it to another class?)
        Open

            service.cinstances.collect {|c| [ c.user_name, c.id ] }
        Severity: Minor
        Found in app/models/cinstance.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 plan_is_unique has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
        Open

          def plan_is_unique
            if plan && user_account && !multiple_applications_allowed?
              # All non-deleted cinstance with the same user_account as this one...
              others = plan.cinstances.bought_by(user_account)
        
        
        Severity: Minor
        Found in app/models/cinstance.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

        Cinstance tests 'others.empty?' at least 3 times
        Open

              errors.add(:plan_id, 'is already bought') unless others.empty?
            end
          end
        
          def application_id_is_unique
        Severity: Minor
        Found in app/models/cinstance.rb by reek

        Repeated Conditional is a special case of Simulated Polymorphism. Basically it means you are checking the same value throughout a single class and take decisions based on this.

        Example

        Given

        class RepeatedConditionals
          attr_accessor :switch
        
          def repeat_1
            puts "Repeat 1!" if switch
          end
        
          def repeat_2
            puts "Repeat 2!" if switch
          end
        
          def repeat_3
            puts "Repeat 3!" if switch
          end
        end

        Reek would emit the following warning:

        test.rb -- 4 warnings:
          [5, 9, 13]:RepeatedConditionals tests switch at least 3 times (RepeatedConditional)

        If you get this warning then you are probably not using the right abstraction or even more probable, missing an additional abstraction.

        Cinstance tests 'new_record?' at least 4 times
        Open

              unless new_record?
                xml.id_ id
                xml.created_at created_at.xmlschema
                xml.updated_at updated_at.xmlschema
              end
        Severity: Minor
        Found in app/models/cinstance.rb by reek

        Repeated Conditional is a special case of Simulated Polymorphism. Basically it means you are checking the same value throughout a single class and take decisions based on this.

        Example

        Given

        class RepeatedConditionals
          attr_accessor :switch
        
          def repeat_1
            puts "Repeat 1!" if switch
          end
        
          def repeat_2
            puts "Repeat 2!" if switch
          end
        
          def repeat_3
            puts "Repeat 3!" if switch
          end
        end

        Reek would emit the following warning:

        test.rb -- 4 warnings:
          [5, 9, 13]:RepeatedConditionals tests switch at least 3 times (RepeatedConditional)

        If you get this warning then you are probably not using the right abstraction or even more probable, missing an additional abstraction.

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

          def application_id_is_unique
            if provider_account
              others = provider_account.provided_cinstances.by_application_id(application_id)
              others = others.without_ids(self.id) unless new_record?
              errors.add(:application_id, :taken) unless others.empty?
        Severity: Minor
        Found in app/models/cinstance.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

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

          def user_key_is_unique
            if provider_account
              others = provider_account.provided_cinstances.by_user_key(user_key)
              others = others.without_ids(self.id) unless new_record?
              errors.add(:user_key, :taken) unless others.empty?
        Severity: Minor
        Found in app/models/cinstance.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

        Cinstance#push_webhook_key_updated calls 'User.current' 2 times
        Open

            self.web_hook_event!({user: User.current, event: "key_updated"}) if User.current
        Severity: Minor
        Found in app/models/cinstance.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.

        Cinstance assumes too much for instance variable '@validate_plan_is_unique'
        Open

        class Cinstance < Contract
        Severity: Minor
        Found in app/models/cinstance.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.

        Cinstance#to_xml calls 'service.backend_version' 2 times
        Open

              if service.backend_version.v1?
                xml.user_key( user_key )
                xml.provider_verification_key( provider_public_key )
        
              else #v2, oauth on enterprise
        Severity: Minor
        Found in app/models/cinstance.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.

        Cinstance assumes too much for instance variable '@validate_human_edition'
        Open

        class Cinstance < Contract
        Severity: Minor
        Found in app/models/cinstance.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.

        Cinstance has missing safe method 'validate_plan_is_unique!'
        Open

          def validate_plan_is_unique!
        Severity: Minor
        Found in app/models/cinstance.rb by reek

        A candidate method for the Missing Safe Method smell are methods whose names end with an exclamation mark.

        An exclamation mark in method names means (the explanation below is taken from here ):

        The ! in method names that end with ! means, “This method is dangerous”—or, more precisely, this method is the “dangerous” version of an otherwise equivalent method, with the same name minus the !. “Danger” is relative; the ! doesn’t mean anything at all unless the method name it’s in corresponds to a similar but bang-less method name. So, for example, gsub! is the dangerous version of gsub. exit! is the dangerous version of exit. flatten! is the dangerous version of flatten. And so forth.

        Such a method is called Missing Safe Method if and only if her non-bang version does not exist and this method is reported as a smell.

        Example

        Given

        class C
          def foo; end
          def foo!; end
          def bar!; end
        end

        Reek would report bar! as Missing Safe Method smell but not foo!.

        Reek reports this smell only in a class context, not in a module context in order to allow perfectly legit code like this:

        class Parent
          def foo; end
        end
        
        module Dangerous
          def foo!; end
        end
        
        class Son < Parent
          include Dangerous
        end
        
        class Daughter < Parent
        end

        In this example, Reek would not report the Missing Safe Method smell for the method foo of the Dangerous module.

        Cinstance has missing safe method 'change_user_key!'
        Open

          def change_user_key!
        Severity: Minor
        Found in app/models/cinstance.rb by reek

        A candidate method for the Missing Safe Method smell are methods whose names end with an exclamation mark.

        An exclamation mark in method names means (the explanation below is taken from here ):

        The ! in method names that end with ! means, “This method is dangerous”—or, more precisely, this method is the “dangerous” version of an otherwise equivalent method, with the same name minus the !. “Danger” is relative; the ! doesn’t mean anything at all unless the method name it’s in corresponds to a similar but bang-less method name. So, for example, gsub! is the dangerous version of gsub. exit! is the dangerous version of exit. flatten! is the dangerous version of flatten. And so forth.

        Such a method is called Missing Safe Method if and only if her non-bang version does not exist and this method is reported as a smell.

        Example

        Given

        class C
          def foo; end
          def foo!; end
          def bar!; end
        end

        Reek would report bar! as Missing Safe Method smell but not foo!.

        Reek reports this smell only in a class context, not in a module context in order to allow perfectly legit code like this:

        class Parent
          def foo; end
        end
        
        module Dangerous
          def foo!; end
        end
        
        class Son < Parent
          include Dangerous
        end
        
        class Daughter < Parent
        end

        In this example, Reek would not report the Missing Safe Method smell for the method foo of the Dangerous module.

        Cinstance has missing safe method 'reject!'
        Open

          def reject!(reason)
        Severity: Minor
        Found in app/models/cinstance.rb by reek

        A candidate method for the Missing Safe Method smell are methods whose names end with an exclamation mark.

        An exclamation mark in method names means (the explanation below is taken from here ):

        The ! in method names that end with ! means, “This method is dangerous”—or, more precisely, this method is the “dangerous” version of an otherwise equivalent method, with the same name minus the !. “Danger” is relative; the ! doesn’t mean anything at all unless the method name it’s in corresponds to a similar but bang-less method name. So, for example, gsub! is the dangerous version of gsub. exit! is the dangerous version of exit. flatten! is the dangerous version of flatten. And so forth.

        Such a method is called Missing Safe Method if and only if her non-bang version does not exist and this method is reported as a smell.

        Example

        Given

        class C
          def foo; end
          def foo!; end
          def bar!; end
        end

        Reek would report bar! as Missing Safe Method smell but not foo!.

        Reek reports this smell only in a class context, not in a module context in order to allow perfectly legit code like this:

        class Parent
          def foo; end
        end
        
        module Dangerous
          def foo!; end
        end
        
        class Son < Parent
          include Dangerous
        end
        
        class Daughter < Parent
        end

        In this example, Reek would not report the Missing Safe Method smell for the method foo of the Dangerous module.

        Cinstance has missing safe method 'change_provider_public_key!'
        Open

          def change_provider_public_key!
        Severity: Minor
        Found in app/models/cinstance.rb by reek

        A candidate method for the Missing Safe Method smell are methods whose names end with an exclamation mark.

        An exclamation mark in method names means (the explanation below is taken from here ):

        The ! in method names that end with ! means, “This method is dangerous”—or, more precisely, this method is the “dangerous” version of an otherwise equivalent method, with the same name minus the !. “Danger” is relative; the ! doesn’t mean anything at all unless the method name it’s in corresponds to a similar but bang-less method name. So, for example, gsub! is the dangerous version of gsub. exit! is the dangerous version of exit. flatten! is the dangerous version of flatten. And so forth.

        Such a method is called Missing Safe Method if and only if her non-bang version does not exist and this method is reported as a smell.

        Example

        Given

        class C
          def foo; end
          def foo!; end
          def bar!; end
        end

        Reek would report bar! as Missing Safe Method smell but not foo!.

        Reek reports this smell only in a class context, not in a module context in order to allow perfectly legit code like this:

        class Parent
          def foo; end
        end
        
        module Dangerous
          def foo!; end
        end
        
        class Son < Parent
          include Dangerous
        end
        
        class Daughter < Parent
        end

        In this example, Reek would not report the Missing Safe Method smell for the method foo of the Dangerous module.

        Cinstance has missing safe method 'validate_human_edition!'
        Open

          def validate_human_edition!
        Severity: Minor
        Found in app/models/cinstance.rb by reek

        A candidate method for the Missing Safe Method smell are methods whose names end with an exclamation mark.

        An exclamation mark in method names means (the explanation below is taken from here ):

        The ! in method names that end with ! means, “This method is dangerous”—or, more precisely, this method is the “dangerous” version of an otherwise equivalent method, with the same name minus the !. “Danger” is relative; the ! doesn’t mean anything at all unless the method name it’s in corresponds to a similar but bang-less method name. So, for example, gsub! is the dangerous version of gsub. exit! is the dangerous version of exit. flatten! is the dangerous version of flatten. And so forth.

        Such a method is called Missing Safe Method if and only if her non-bang version does not exist and this method is reported as a smell.

        Example

        Given

        class C
          def foo; end
          def foo!; end
          def bar!; end
        end

        Reek would report bar! as Missing Safe Method smell but not foo!.

        Reek reports this smell only in a class context, not in a module context in order to allow perfectly legit code like this:

        class Parent
          def foo; end
        end
        
        module Dangerous
          def foo!; end
        end
        
        class Son < Parent
          include Dangerous
        end
        
        class Daughter < Parent
        end

        In this example, Reek would not report the Missing Safe Method smell for the method foo of the Dangerous module.

        Cinstance#select_users has the variable name 'c'
        Open

            service.cinstances.collect {|c| [ c.user_name, c.id ] }
        Severity: Minor
        Found in app/models/cinstance.rb by reek

        An Uncommunicative Variable Name is a variable name that doesn't communicate its intent well enough.

        Poor names make it hard for the reader to build a mental picture of what's going on in the code. They can also be mis-interpreted; and they hurt the flow of reading, because the reader must slow down to interpret the names.

        Cinstance#to_xml has the variable name 'k'
        Open

                    keys.each do |k|
        Severity: Minor
        Found in app/models/cinstance.rb by reek

        An Uncommunicative Variable Name is a variable name that doesn't communicate its intent well enough.

        Poor names make it hard for the reader to build a mental picture of what's going on in the code. They can also be mis-interpreted; and they hurt the flow of reading, because the reader must slow down to interpret the names.

        There are no issues that match your filters.

        Category
        Status