mysociety/alaveteli

View on GitHub
app/models/info_request.rb

Summary

Maintainability
F
1 wk
Test Coverage

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

require 'digest/sha1'
require 'fileutils'

class InfoRequest < ApplicationRecord
  OLD_AGE_IN_DAYS = 21.days
Severity: Major
Found in app/models/info_request.rb - About 3 days to fix

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

    class InfoRequest < ApplicationRecord
      OLD_AGE_IN_DAYS = 21.days
    
      include Rails.application.routes.url_helpers
      include AlaveteliPro::RequestSummaries
    Severity: Major
    Found in app/models/info_request.rb - About 2 days to fix

      Method calculate_event_states has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
      Open

        def calculate_event_states
          curr_state = nil
          info_request_events.reverse.each do |event|
            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 who_can_followup_to has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
      Open

        def who_can_followup_to(skip_message = nil)
          ret = []
          done = {}
          if skip_message
            if (email = OutgoingMailer.email_for_followup(self, 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 receive has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
      Open

        def receive(email, raw_email_data, *args)
          return if already_received?(email)
      
          defaults = { override_stop_new_responses: false,
                       rejected_reason: nil,
      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 stop_new_responses_on_old_requests has 36 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def self.stop_new_responses_on_old_requests
          # 'old' months since last change to request, only allow new incoming
          # messages from authority domains
          InfoRequest.
            been_published.
      Severity: Minor
      Found in app/models/info_request.rb - About 1 hr to fix

        Method calculate_last_event_forming_initial_request has a Cognitive Complexity of 12 (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 receive has 32 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def receive(email, raw_email_data, *args)
            return if already_received?(email)
        
            defaults = { override_stop_new_responses: false,
                         rejected_reason: nil,
        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_status_description has 28 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 holding_pen_request has 27 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                def self.holding_pen_request
                  ir = InfoRequest.find_by_url_title("holding_pen")
                  if ir.nil?
                    ir = InfoRequest.new(
                      user: User.internal_admin_user,
              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 get_previous_email_sent_to has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def get_previous_email_sent_to(info_request_event)
                      last_email = nil
                      info_request_events.each do |e|
                        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
                          break if e.id == info_request_event.id
                  Severity: Minor
                  Found in app/models/info_request.rb - About 55 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 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
                          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

                    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

                    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

                    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 35..52

                    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

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

                            batch.each do |info_request|
                              old_allow_new_responses_from = info_request.allow_new_responses_from
                    
                              info_request.
                                update_column(:allow_new_responses_from, 'nobody')
                    Severity: Minor
                    Found in app/models/info_request.rb and 1 other location - About 15 mins to fix
                    app/models/info_request.rb on lines 560..571

                    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 25.

                    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

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

                            batch.each do |info_request|
                              old_allow_new_responses_from = info_request.allow_new_responses_from
                    
                              info_request.
                                update_column(:allow_new_responses_from, 'authority_only')
                    Severity: Minor
                    Found in app/models/info_request.rb and 1 other location - About 15 mins to fix
                    app/models/info_request.rb on lines 584..595

                    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 25.

                    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