mysociety/alaveteli

View on GitHub
app/controllers/request_controller.rb

Summary

Maintainability
F
6 days
Test Coverage

File request_controller.rb has 600 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require 'zip'

class RequestController < ApplicationController
  skip_before_action :html_response, only: [:show, :select_authorities]

Severity: Major
Found in app/controllers/request_controller.rb - About 1 day to fix

    Method new has a Cognitive Complexity of 37 (exceeds 5 allowed). Consider refactoring.
    Open

      def new
        # All new requests are of normal_sort
        unless params[:outgoing_message].nil?
          params[:outgoing_message][:what_doing] = 'normal_sort'
        end
    Severity: Minor
    Found in app/controllers/request_controller.rb - About 5 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

    Class RequestController has 33 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class RequestController < ApplicationController
      skip_before_action :html_response, only: [:show, :select_authorities]
    
      before_action :check_read_only, only: [:new, :upload_response]
      before_action :set_render_recaptcha, only: [ :new ]
    Severity: Minor
    Found in app/controllers/request_controller.rb - About 4 hrs to fix

      Method new has 104 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def new
          # All new requests are of normal_sort
          unless params[:outgoing_message].nil?
            params[:outgoing_message][:what_doing] = 'normal_sort'
          end
      Severity: Major
      Found in app/controllers/request_controller.rb - About 4 hrs to fix

        Method render_new_compose has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
        Open

          def render_new_compose
            params[:info_request] = {} unless params[:info_request]
        
            # Reconstruct the params
            # first the public body (by URL name or id)
        Severity: Minor
        Found in app/controllers/request_controller.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 upload_response has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
        Open

          def upload_response
            AlaveteliLocalization.with_locale(locale) do
              @info_request = InfoRequest.not_embargoed.find_by_url_title!(params[:url_title])
        
              @reason_params = {
        Severity: Minor
        Found in app/controllers/request_controller.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 upload_response has 55 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def upload_response
            AlaveteliLocalization.with_locale(locale) do
              @info_request = InfoRequest.not_embargoed.find_by_url_title!(params[:url_title])
        
              @reason_params = {
        Severity: Major
        Found in app/controllers/request_controller.rb - About 2 hrs to fix

          Method render_new_compose has 55 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def render_new_compose
              params[:info_request] = {} unless params[:info_request]
          
              # Reconstruct the params
              # first the public body (by URL name or id)
          Severity: Major
          Found in app/controllers/request_controller.rb - About 2 hrs to fix

            Method download_entire_request has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
            Open

              def download_entire_request
                AlaveteliLocalization.with_locale(locale) do
                  @info_request = InfoRequest.find_by_url_title!(params[:url_title])
                  # Check for access and hide emargoed requests immediately, so that we
                  # don't leak any info to people who can't access them
            Severity: Minor
            Found in app/controllers/request_controller.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 assign_variables_for_show_template has 37 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              def assign_variables_for_show_template(info_request)
                @info_request = info_request
                @status = info_request.calculate_status
                @old_unclassified =
                  info_request.is_old_unclassified? && authenticated?
            Severity: Minor
            Found in app/controllers/request_controller.rb - About 1 hr to fix

              Method show has 36 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                def show
                  medium_cache
                  AlaveteliLocalization.with_locale(locale) do
                    # Test for whole request being hidden
                    return render_hidden if cannot?(:read, @info_request)
              Severity: Minor
              Found in app/controllers/request_controller.rb - About 1 hr to fix

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

                  def make_request_zip(info_request, file_path)
                    Zip::File.open(file_path, create: true) do |zipfile|
                      file_info = make_request_summary_file(info_request)
                      zipfile.get_output_stream(file_info[:filename]) { |f| f.write(file_info[:data]) }
                      message_index = 0
                Severity: Minor
                Found in app/controllers/request_controller.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 make_request_summary_file has 31 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                  def make_request_summary_file(info_request)
                    done = false
                    @render_to_file = true
                    assign_variables_for_show_template(info_request)
                    if HTMLtoPDFConverter.exist?
                Severity: Minor
                Found in app/controllers/request_controller.rb - About 1 hr to fix

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

                    def show
                      medium_cache
                      AlaveteliLocalization.with_locale(locale) do
                        # Test for whole request being hidden
                        return render_hidden if cannot?(:read, @info_request)
                  Severity: Minor
                  Found in app/controllers/request_controller.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 make_request_summary_file has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def make_request_summary_file(info_request)
                      done = false
                      @render_to_file = true
                      assign_variables_for_show_template(info_request)
                      if HTMLtoPDFConverter.exist?
                  Severity: Minor
                  Found in app/controllers/request_controller.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

                        redirect_to frontpage_path and return
                  Severity: Major
                  Found in app/controllers/request_controller.rb - About 30 mins to fix

                    Avoid too many return statements within this method.
                    Open

                          handle_spam_subject(@info_request.user) && return
                    Severity: Major
                    Found in app/controllers/request_controller.rb - About 30 mins to fix

                      Avoid too many return statements within this method.
                      Open

                            return
                      Severity: Major
                      Found in app/controllers/request_controller.rb - About 30 mins to fix

                        Avoid too many return statements within this method.
                        Open

                                return
                        Severity: Major
                        Found in app/controllers/request_controller.rb - About 30 mins to fix

                          Avoid too many return statements within this method.
                          Open

                                return
                          Severity: Major
                          Found in app/controllers/request_controller.rb - About 30 mins to fix

                            Avoid too many return statements within this method.
                            Open

                                  handle_blocked_ip(@info_request) && return
                            Severity: Major
                            Found in app/controllers/request_controller.rb - About 30 mins to fix

                              Avoid too many return statements within this method.
                              Open

                                  return render_new_preview if params[:preview].to_i == 1
                              Severity: Major
                              Found in app/controllers/request_controller.rb - About 30 mins to fix

                                Avoid too many return statements within this method.
                                Open

                                      return
                                Severity: Major
                                Found in app/controllers/request_controller.rb - About 30 mins to fix

                                  Avoid too many return statements within this method.
                                  Open

                                        return
                                  Severity: Major
                                  Found in app/controllers/request_controller.rb - About 30 mins to fix

                                    Avoid too many return statements within this method.
                                    Open

                                            return
                                    Severity: Major
                                    Found in app/controllers/request_controller.rb - About 30 mins to fix

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

                                        def handle_blocked_ip(info_request)
                                          if send_exception_notifications?
                                            msg = "Possible spam request (ip_in_blocklist) from " \
                                                  "User##{info_request.user_id}: #{user_ip} (#{country_from_ip})"
                                            ExceptionNotifier.notify_exception(Exception.new(msg), env: request.env)
                                      Severity: Minor
                                      Found in app/controllers/request_controller.rb and 1 other location - About 45 mins to fix
                                      app/controllers/user_controller.rb on lines 421..432

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

                                      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 3 locations. Consider refactoring.
                                      Open

                                        def handle_spam_subject(user)
                                          if send_exception_notifications?
                                            e = Exception.new("Spam request from user #{ user.id }")
                                            ExceptionNotifier.notify_exception(e, env: request.env)
                                          end
                                      Severity: Minor
                                      Found in app/controllers/request_controller.rb and 2 other locations - About 30 mins to fix
                                      app/controllers/comment_controller.rb on lines 148..158
                                      app/controllers/users/messages_controller.rb on lines 110..120

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

                                      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