3scale/porta

View on GitHub
app/models/cms/email_template.rb

Summary

Maintainability
C
1 day
Test Coverage

Mass assignment is not restricted using attr_accessible
Open

class CMS::EmailTemplate < CMS::Template
Severity: Critical
Found in app/models/cms/email_template.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 EmailTemplate has 28 methods (exceeds 20 allowed). Consider refactoring.
Open

class CMS::EmailTemplate < CMS::Template

  validates :system_name, presence: true
  validates :current, presence: true
  validates :system_name, uniqueness: { scope: %i[provider_id], allow_blank: true, case_sensitive: true }
Severity: Minor
Found in app/models/cms/email_template.rb - About 3 hrs to fix

    Method headers_formats has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring.
    Open

      def headers_formats
        headers.to_hash.dup.each do |name, value|
    
          field = "headers.#{name}"
          next if value.blank?
    Severity: Minor
    Found in app/models/cms/email_template.rb - About 3 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

    File email_template.rb has 257 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    class CMS::EmailTemplate < CMS::Template
    
      validates :system_name, presence: true
      validates :current, presence: true
      validates :system_name, uniqueness: { scope: %i[provider_id], allow_blank: true, case_sensitive: true }
    Severity: Minor
    Found in app/models/cms/email_template.rb - About 2 hrs to fix

      CMS::EmailTemplate::HashOrParameters#load refers to 'obj' more than self (maybe move it to another class?)
      Open

            obj.respond_to?(:to_unsafe_h) ? obj.to_unsafe_h : obj.to_h if obj
      Severity: Minor
      Found in app/models/cms/email_template.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.

      CMS::EmailTemplate#headers_formats has approx 8 statements
      Open

        def headers_formats
      Severity: Minor
      Found in app/models/cms/email_template.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.)

      CMS::EmailTemplate has 6 constants
      Open

      class CMS::EmailTemplate < CMS::Template
      Severity: Minor
      Found in app/models/cms/email_template.rb by reek

      Too Many Constants is a special case of LargeClass.

      Example

      Given this configuration

      TooManyConstants:
        max_constants: 3

      and this code:

      class TooManyConstants
        CONST_1 = :dummy
        CONST_2 = :dummy
        CONST_3 = :dummy
        CONST_4 = :dummy
      end

      Reek would emit the following warning:

      test.rb -- 1 warnings:
        [1]:TooManyConstants has 4 constants (TooManyConstants)

      CMS::EmailTemplate::ProviderAssociationExtension#all_new_and_overridden has approx 8 statements
      Open

          def all_new_and_overridden
      Severity: Minor
      Found in app/models/cms/email_template.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.)

      CMS::EmailTemplate::HashOrParameters#dump refers to 'object' more than self (maybe move it to another class?)
      Open

            return object if object.is_a?(String) && assert_valid_value(load(object), action: "dump")
            obj = object.respond_to?(:to_unsafe_h) ? object.to_unsafe_h : object.to_h if object
      Severity: Minor
      Found in app/models/cms/email_template.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.

      CMS::EmailTemplate::HashOrParameters#assert_valid_value refers to 'obj' more than self (maybe move it to another class?)
      Open

            obj.is_a?(Hash) || obj.is_a?(ActionController::Parameters) || super
      Severity: Minor
      Found in app/models/cms/email_template.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.

      CMS::EmailTemplate::MailerExtension#render manually dispatches method call
      Open

                        apply_headers!(template.record) if template.respond_to?(:record)
      Severity: Minor
      Found in app/models/cms/email_template.rb by reek

      Reek reports a Manual Dispatch smell if it finds source code that manually checks whether an object responds to a method before that method is called. Manual dispatch is a type of Simulated Polymorphism which leads to code that is harder to reason about, debug, and refactor.

      Example

      class MyManualDispatcher
        attr_reader :foo
      
        def initialize(foo)
          @foo = foo
        end
      
        def call
          foo.bar if foo.respond_to?(:bar)
        end
      end

      Reek would emit the following warning:

      test.rb -- 1 warning:
        [9]: MyManualDispatcher manually dispatches method call (ManualDispatch)

      CMS::EmailTemplate::Headers assumes too much for instance variable '@table'
      Open

        class Headers < OpenStruct
      Severity: Minor
      Found in app/models/cms/email_template.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.

      CMS::EmailTemplate#headers= manually dispatches method call
      Open

          self[:options] = val.respond_to?(:to_unsafe_h) ? val.to_unsafe_h : val
      Severity: Minor
      Found in app/models/cms/email_template.rb by reek

      Reek reports a Manual Dispatch smell if it finds source code that manually checks whether an object responds to a method before that method is called. Manual dispatch is a type of Simulated Polymorphism which leads to code that is harder to reason about, debug, and refactor.

      Example

      class MyManualDispatcher
        attr_reader :foo
      
        def initialize(foo)
          @foo = foo
        end
      
        def call
          foo.bar if foo.respond_to?(:bar)
        end
      end

      Reek would emit the following warning:

      test.rb -- 1 warning:
        [9]: MyManualDispatcher manually dispatches method call (ManualDispatch)

      CMS::EmailTemplate::ExtensionCore#template_headers manually dispatches method call
      Open

            return if !template.respond_to?(:headers) || !template.headers
      Severity: Minor
      Found in app/models/cms/email_template.rb by reek

      Reek reports a Manual Dispatch smell if it finds source code that manually checks whether an object responds to a method before that method is called. Manual dispatch is a type of Simulated Polymorphism which leads to code that is harder to reason about, debug, and refactor.

      Example

      class MyManualDispatcher
        attr_reader :foo
      
        def initialize(foo)
          @foo = foo
        end
      
        def call
          foo.bar if foo.respond_to?(:bar)
        end
      end

      Reek would emit the following warning:

      test.rb -- 1 warning:
        [9]: MyManualDispatcher manually dispatches method call (ManualDispatch)

      CMS::EmailTemplate#headers_formats calls 'errors.add(field, :invalid_email)' 2 times
      Open

              errors.add(field, :invalid_email) if value !~ EMAILS_FORMAT
      
            when :from
              # extract all email addresses from field
              next unless email = value.scan(EMAIL_FORMAT).flatten.compact.presence
      Severity: Minor
      Found in app/models/cms/email_template.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.

      CMS::EmailTemplate::HashOrParameters#load manually dispatches method call
      Open

            obj.respond_to?(:to_unsafe_h) ? obj.to_unsafe_h : obj.to_h if obj
      Severity: Minor
      Found in app/models/cms/email_template.rb by reek

      Reek reports a Manual Dispatch smell if it finds source code that manually checks whether an object responds to a method before that method is called. Manual dispatch is a type of Simulated Polymorphism which leads to code that is harder to reason about, debug, and refactor.

      Example

      class MyManualDispatcher
        attr_reader :foo
      
        def initialize(foo)
          @foo = foo
        end
      
        def call
          foo.bar if foo.respond_to?(:bar)
        end
      end

      Reek would emit the following warning:

      test.rb -- 1 warning:
        [9]: MyManualDispatcher manually dispatches method call (ManualDispatch)

      CMS::EmailTemplate::ProviderAssociationExtension#find_default_by_name manually dispatches method call
      Open

                  :provider => respond_to?(:proxy_association) ? proxy_association.owner : nil)
      Severity: Minor
      Found in app/models/cms/email_template.rb by reek

      Reek reports a Manual Dispatch smell if it finds source code that manually checks whether an object responds to a method before that method is called. Manual dispatch is a type of Simulated Polymorphism which leads to code that is harder to reason about, debug, and refactor.

      Example

      class MyManualDispatcher
        attr_reader :foo
      
        def initialize(foo)
          @foo = foo
        end
      
        def call
          foo.bar if foo.respond_to?(:bar)
        end
      end

      Reek would emit the following warning:

      test.rb -- 1 warning:
        [9]: MyManualDispatcher manually dispatches method call (ManualDispatch)

      CMS::EmailTemplate::ExtensionCore#template_headers calls 'template.headers' 2 times
      Open

            return if !template.respond_to?(:headers) || !template.headers
      
            template.headers.to_email_headers(template.provider.from_email)
      Severity: Minor
      Found in app/models/cms/email_template.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.

      CMS::EmailTemplate::HashOrParameters#dump manually dispatches method call
      Open

            obj = object.respond_to?(:to_unsafe_h) ? object.to_unsafe_h : object.to_h if object
      Severity: Minor
      Found in app/models/cms/email_template.rb by reek

      Reek reports a Manual Dispatch smell if it finds source code that manually checks whether an object responds to a method before that method is called. Manual dispatch is a type of Simulated Polymorphism which leads to code that is harder to reason about, debug, and refactor.

      Example

      class MyManualDispatcher
        attr_reader :foo
      
        def initialize(foo)
          @foo = foo
        end
      
        def call
          foo.bar if foo.respond_to?(:bar)
        end
      end

      Reek would emit the following warning:

      test.rb -- 1 warning:
        [9]: MyManualDispatcher manually dispatches method call (ManualDispatch)

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

          def dump(object)
            return object if object.is_a?(String) && assert_valid_value(load(object), action: "dump")
            obj = object.respond_to?(:to_unsafe_h) ? object.to_unsafe_h : object.to_h if object
            super(obj)
          end
      Severity: Minor
      Found in app/models/cms/email_template.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

      CMS::EmailTemplate::ProviderAssociationExtension#find_file_template doesn't depend on instance state (maybe move it to another class?)
      Open

          def find_file_template(name)
      Severity: Minor
      Found in app/models/cms/email_template.rb by reek

      A Utility Function is any instance method that has no dependency on the state of the instance.

      CMS::EmailTemplate#file is a writable attribute
      Open

        attr_accessor :file
      Severity: Minor
      Found in app/models/cms/email_template.rb by reek

      A class that publishes a setter for an instance variable invites client classes to become too intimate with its inner workings, and in particular with its representation of state.

      The same holds to a lesser extent for getters, but Reek doesn't flag those.

      Example

      Given:

      class Klass
        attr_accessor :dummy
      end

      Reek would emit the following warning:

      reek test.rb
      
      test.rb -- 1 warning:
        [2]:Klass declares the writable attribute dummy (Attribute)

      CMS::EmailTemplate::ExtensionCore#template_headers doesn't depend on instance state (maybe move it to another class?)
      Open

          def template_headers(template)
      Severity: Minor
      Found in app/models/cms/email_template.rb by reek

      A Utility Function is any instance method that has no dependency on the state of the instance.

      CMS::EmailTemplate#new_by_system_name has unused parameter 'file'
      Open

          def new_by_system_name(system_name, file = nil)
      Severity: Minor
      Found in app/models/cms/email_template.rb by reek

      Unused Parameter refers to methods with parameters that are unused in scope of the method.

      Having unused parameters in a method is code smell because leaving dead code in a method can never improve the method and it makes the code confusing to read.

      Example

      Given:

      class Klass
        def unused_parameters(x,y,z)
          puts x,y # but not z
        end
      end

      Reek would emit the following warning:

      [2]:Klass#unused_parameters has unused parameter 'z' (UnusedParameters)

      CMS::EmailTemplate::Headers#to_yaml has the variable name 'k'
      Open

            @table.reject {|k,v| v.blank?}
      Severity: Minor
      Found in app/models/cms/email_template.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.

      CMS::EmailTemplate::Headers#to_email_headers has the variable name 'k'
      Open

            hash = to_hash.symbolize_keys.reject { |k,v| v.blank? }
      Severity: Minor
      Found in app/models/cms/email_template.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.

      CMS::EmailTemplate::Headers#to_email_headers has the variable name 'v'
      Open

            hash = to_hash.symbolize_keys.reject { |k,v| v.blank? }
      Severity: Minor
      Found in app/models/cms/email_template.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.

      CMS::EmailTemplate::Headers#to_yaml has the variable name 'v'
      Open

            @table.reject {|k,v| v.blank?}
      Severity: Minor
      Found in app/models/cms/email_template.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