discourse/discourse

View on GitHub
app/controllers/application_controller.rb

Summary

Maintainability
F
5 days
Test Coverage

File application_controller.rb has 637 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require 'current_user'
require_dependency 'canonical_url'
require_dependency 'discourse'
require_dependency 'custom_renderer'
require_dependency 'archetype'
Severity: Major
Found in app/controllers/application_controller.rb - About 1 day to fix

    Class ApplicationController has 61 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class ApplicationController < ActionController::Base
      include CurrentUser
      include CanonicalURL::ControllerExtensions
      include JsonError
      include GlobalPath
    Severity: Major
    Found in app/controllers/application_controller.rb - About 1 day to fix

      Method rescue_discourse_actions has a Cognitive Complexity of 25 (exceeds 5 allowed). Consider refactoring.
      Open

        def rescue_discourse_actions(type, status_code, opts = nil)
          opts ||= {}
          show_json_errors = (request.format && request.format.json?) ||
                             (request.xhr?) ||
                             ((params[:external_id] || '').ends_with? '.json')
      Severity: Minor
      Found in app/controllers/application_controller.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

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

        def handle_theme
          return if request.xhr? || request.format == "json" || request.format == "js"
          return if request.method != "GET"
      
          resolve_safe_mode
      Severity: Minor
      Found in app/controllers/application_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 redirect_to_login_if_required has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
      Open

        def redirect_to_login_if_required
          return if request.format.json? && is_api?
      
          # Used by clients authenticated via user API.
          # Redirects to provided URL scheme if
      Severity: Minor
      Found in app/controllers/application_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 redirect_to_login_if_required has 47 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def redirect_to_login_if_required
          return if request.format.json? && is_api?
      
          # Used by clients authenticated via user API.
          # Redirects to provided URL scheme if
      Severity: Minor
      Found in app/controllers/application_controller.rb - About 1 hr to fix

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

          def clear_notifications
            if current_user && !@readonly_mode
        
              cookie_notifications = cookies['cn']
              notifications = request.headers['Discourse-Clear-Notifications']
        Severity: Minor
        Found in app/controllers/application_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 json_result has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
        Open

          def json_result(obj, opts = {})
            if yield(obj)
              json = success_json
        
              # If we were given a serializer, add the class to the json that comes back
        Severity: Minor
        Found in app/controllers/application_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 rescue_discourse_actions has 31 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def rescue_discourse_actions(type, status_code, opts = nil)
            opts ||= {}
            show_json_errors = (request.format && request.format.json?) ||
                               (request.xhr?) ||
                               ((params[:external_id] || '').ends_with? '.json')
        Severity: Minor
        Found in app/controllers/application_controller.rb - About 1 hr to fix

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

            def fetch_user_from_params(opts = nil, eager_load = [])
              opts ||= {}
              user = if params[:username]
                username_lower = params[:username].downcase.chomp('.json')
                find_opts = { username_lower: username_lower }
          Severity: Minor
          Found in app/controllers/application_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 render_json_dump has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
          Open

            def render_json_dump(obj, opts = nil)
              opts ||= {}
              if opts[:rest_serializer]
                obj['__rest_serializer'] = "1"
                opts.each do |k, v|
          Severity: Minor
          Found in app/controllers/application_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 build_not_found_page has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
          Open

            def build_not_found_page(status = 404, layout = false)
              if SiteSetting.bootstrap_error_pages?
                preload_json
                layout = 'application' if layout == 'no_ember'
              end
          Severity: Minor
          Found in app/controllers/application_controller.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_locale has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
          Open

            def set_locale
              if !current_user
                if SiteSetting.set_locale_from_accept_language_header
                  locale = locale_from_header
                else
          Severity: Minor
          Found in app/controllers/application_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

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

            def custom_html_json
              target = view_context.mobile_view? ? :mobile : :desktop
          
              data =
                if @theme_ids.present?
          Severity: Minor
          Found in app/controllers/application_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

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

            def block_if_requires_login
              if arg = self.class.requires_login_arg
                check =
                  if except = arg[:except]
                    !except.include?(action_name.to_sym)
          Severity: Minor
          Found in app/controllers/application_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

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

            def check_xhr
              # bypass xhr check on PUT / POST / DELETE provided api key is there, otherwise calling api is annoying
              return if !request.get? && (is_api? || is_user_api?)
              raise RenderEmpty.new unless ((request.format && request.format.json?) || request.xhr?)
            end
          Severity: Minor
          Found in app/controllers/application_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

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

            Avoid too many return statements within this method.
            Open

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

              Avoid too many return statements within this method.
              Open

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

                Avoid too many return statements within this method.
                Open

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

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

                      def remember_theme_id
                        if @theme_ids.present? && request.format == "html"
                          Stylesheet::Watcher.theme_id = @theme_ids.first if defined? Stylesheet::Watcher
                        end
                      end
                  Severity: Minor
                  Found in app/controllers/application_controller.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

                  There are no issues that match your filters.

                  Category
                  Status