mysociety/alaveteli

View on GitHub
app/models/info_request.rb

Summary

Maintainability
F
1 wk
Test Coverage

File info_request.rb has 1335 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require 'digest/sha1'
require 'fileutils'

class InfoRequest < ActiveRecord::Base
  include AdminColumn
Severity: Major
Found in app/models/info_request.rb - About 3 days to fix

    Class InfoRequest has 137 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class InfoRequest < ActiveRecord::Base
      include AdminColumn
      include Rails.application.routes.url_helpers
      include AlaveteliPro::RequestSummaries
      include AlaveteliFeatures::Helpers
    Severity: Major
    Found in app/models/info_request.rb - About 2 days to fix

      Method receive has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
      Open

        def receive(email, raw_email_data, *args)
          defaults = { :override_stop_new_responses => false,
                       :rejected_reason => nil,
                       :source => :internal }
      
      
      Severity: Minor
      Found in app/models/info_request.rb - About 2 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 calculate_event_states has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
      Open

        def calculate_event_states
          curr_state = nil
          for event in info_request_events.reverse
            event.xapian_mark_needs_index  # we need to reindex all events in order to update their latest_* terms
            if curr_state.nil?
      Severity: Minor
      Found in app/models/info_request.rb - About 2 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 receive has 40 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def receive(email, raw_email_data, *args)
          defaults = { :override_stop_new_responses => false,
                       :rejected_reason => nil,
                       :source => :internal }
      
      
      Severity: Minor
      Found in app/models/info_request.rb - About 1 hr to fix

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

          def accept_incoming?(email, raw_email_data)
            # See if new responses are prevented
            gatekeeper = ResponseGatekeeper.for(allow_new_responses_from, self)
            # Take action if the message looks like spam
            spam_checker = ResponseGatekeeper::SpamChecker.new
        Severity: Minor
        Found in app/models/info_request.rb - About 1 hr 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 log_overdue_event_type has 31 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def self.log_overdue_event_type(event_type)
            date_field = case event_type
            when 'overdue'
              'date_response_required_by'
            when 'very_overdue'
        Severity: Minor
        Found in app/models/info_request.rb - About 1 hr to fix

          Method get_previous_email_sent_to has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
          Open

            def get_previous_email_sent_to(info_request_event)
              last_email = nil
              for e in info_request_events
                if ((info_request_event.is_sent_sort? && e.is_sent_sort?) || (info_request_event.is_followup_sort? && e.is_followup_sort?)) && e.outgoing_message_id == info_request_event.outgoing_message_id
                  if e.id == info_request_event.id
          Severity: Minor
          Found in app/models/info_request.rb - About 1 hr 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 who_can_followup_to has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
          Open

            def who_can_followup_to(skip_message = nil)
              ret = []
              done = {}
              for incoming_message in incoming_messages.reverse
                if incoming_message == skip_message
          Severity: Minor
          Found in app/models/info_request.rb - About 1 hr 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 calculate_last_event_forming_initial_request has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
          Open

            def calculate_last_event_forming_initial_request
              # TODO: This can be removed when last_event_forming_initial_request_id has
              # been populated for all requests
          
              expecting_clarification = false
          Severity: Minor
          Found in app/models/info_request.rb - About 1 hr 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 get_status_description has 26 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def self.get_status_description(status)
              descriptions = {
                'waiting_classification'        => _("Awaiting classification."),
                'waiting_response'              => _("Awaiting response."),
                'waiting_response_overdue'      => _("Delayed."),
          Severity: Minor
          Found in app/models/info_request.rb - About 1 hr to fix

            Method recent_requests has 26 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              def self.recent_requests
                request_events = []
                request_events_all_successful = false
                # Get some successful requests
                begin
            Severity: Minor
            Found in app/models/info_request.rb - About 1 hr to fix

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

                def set_described_state(new_state, set_by = nil, message = "")
                  old_described_state = described_state
                  ActiveRecord::Base.transaction do
                    self.awaiting_description = false
                    last_event = info_request_events.last
              Severity: Minor
              Found in app/models/info_request.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

              Consider simplifying this complex logical expression.
              Open

                    elsif curr_state && (event.event_type == 'followup_sent' || event.event_type == 'sent') && event.described_state && (event.described_state == 'waiting_response' || event.described_state == 'internal_review')
                      # Followups can set the status to waiting response / internal
                      # review. Initial requests ('sent') set the status to waiting response.
              
                      # We want to store that in calculated_state state so it gets
              Severity: Major
              Found in app/models/info_request.rb - About 40 mins to fix

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

                  def must_be_internal_or_external
                    # We must permit user_id and external_user_name both to be nil, because the system
                    # allows a request to be created by a non-logged-in user.
                    if user_id
                      errors.add(:external_user_name, "must be null for an internal request") unless external_user_name.nil?
                Severity: Minor
                Found in app/models/info_request.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

                Avoid too many return statements within this method.
                Open

                    return 'waiting_response'
                Severity: Major
                Found in app/models/info_request.rb - About 30 mins to fix

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

                    def title_formatting
                      return unless title
                      unless MySociety::Validate.uses_mixed_capitals(title, 1) ||
                        title_starts_with_number || title_is_acronym(6)
                        errors.add(:title, _('Please write the summary using a mixture of capital and lower case letters. This makes it easier for others to read.'))
                  Severity: Minor
                  Found in app/models/info_request.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 user_name_slug has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def user_name_slug
                      if is_external?
                        if external_user_name.nil?
                          fake_slug = "anonymous"
                        else
                  Severity: Minor
                  Found in app/models/info_request.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 is_followupable? has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def is_followupable?(incoming_message)
                      if is_external?
                        @followup_bad_reason = "external"
                        false
                      elsif !OutgoingMailer.is_followupable?(self, incoming_message)
                  Severity: Minor
                  Found in app/models/info_request.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

                  Similar blocks of code found in 2 locations. Consider refactoring.
                  Open

                      descriptions = {
                        'waiting_classification'        => _("Awaiting classification."),
                        'waiting_response'              => _("Awaiting response."),
                        'waiting_response_overdue'      => _("Delayed."),
                        'waiting_response_very_overdue' => _("Long overdue."),
                  Severity: Major
                  Found in app/models/info_request.rb and 1 other location - About 1 hr to fix
                  app/models/info_request/state.rb on lines 29..46

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 53.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  There are no issues that match your filters.

                  Category
                  Status