mysociety/alaveteli

View on GitHub
app/models/public_body.rb

Summary

Maintainability
F
5 days
Test Coverage

File public_body.rb has 666 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require 'csv'
require 'securerandom'
require 'set'
require 'confidence_intervals'

Severity: Major
Found in app/models/public_body.rb - About 1 day to fix

    Class PublicBody has 47 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class PublicBody < ActiveRecord::Base
      include AdminColumn
    
      class ImportCSVDryRun < StandardError ; end
    
    
    Severity: Minor
    Found in app/models/public_body.rb - About 6 hrs to fix

      Method import_csv_from_file has a Cognitive Complexity of 39 (exceeds 5 allowed). Consider refactoring.
      Open

        def self.import_csv_from_file(csv_filename, tag, tag_behaviour, dry_run, editor, available_locales = [])
          errors = []
          notes = []
          begin
            ActiveRecord::Base.transaction do
      Severity: Minor
      Found in app/models/public_body.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

      Method import_csv_from_file has 62 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def self.import_csv_from_file(csv_filename, tag, tag_behaviour, dry_run, editor, available_locales = [])
          errors = []
          notes = []
          begin
            ActiveRecord::Base.transaction do
      Severity: Major
      Found in app/models/public_body.rb - About 2 hrs to fix

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

          def set_locale_fields_from_csv_row(is_new, locale, row, options)
            changed = ActiveSupport::OrderedHash.new
            csv_field_names = options[:field_names]
            csv_import_fields.each do |field_name, field_notes|
              localized_field_name = self.class.localized_csv_field_name(locale, field_name)
        Severity: Minor
        Found in app/models/public_body.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 import_values_from_csv_row has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
        Open

          def import_values_from_csv_row(row, line, name, options)
            is_new = new_record?
            edit_info = if is_new
              { :action => "creating new authority",
                :comment => 'Created from spreadsheet' }
        Severity: Minor
        Found in app/models/public_body.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 with_query has 34 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def self.with_query(query, tag)
            like_query = "%#{query}%"
            has_first_letter = tag.size == 1
        
            underscore_locale = AlaveteliLocalization.locale
        Severity: Minor
        Found in app/models/public_body.rb - About 1 hr to fix

          Method internal_admin_body has 33 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def self.internal_admin_body
              matching_pbs = PublicBody.where(:url_name => 'internal_admin_authority')
              case
              when matching_pbs.empty? then
                # "internal admin" exists but has the wrong default locale - fix & return
          Severity: Minor
          Found in app/models/public_body.rb - About 1 hr to fix

            Method get_request_percentages has 33 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              def self.get_request_percentages(column, n, highest, minimum_requests)
                total_column = "info_requests_visible_classified_count"
                ordering = "y_value"
                ordering += " DESC" if highest
                y_value_column = "(cast(#{column} as float) / #{total_column})"
            Severity: Minor
            Found in app/models/public_body.rb - About 1 hr to fix

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

                  def compare(previous = nil, &block)
                    if previous.nil?
                      changes = []
                    else
                      v = self
              Severity: Minor
              Found in app/models/public_body.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 import_values_from_csv_row has 29 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                def import_values_from_csv_row(row, line, name, options)
                  is_new = new_record?
                  edit_info = if is_new
                    { :action => "creating new authority",
                      :comment => 'Created from spreadsheet' }
              Severity: Minor
              Found in app/models/public_body.rb - About 1 hr to fix

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

                  def set_locale_fields_from_csv_row(is_new, locale, row, options)
                    changed = ActiveSupport::OrderedHash.new
                    csv_field_names = options[:field_names]
                    csv_import_fields.each do |field_name, field_notes|
                      localized_field_name = self.class.localized_csv_field_name(locale, field_name)
                Severity: Minor
                Found in app/models/public_body.rb - About 1 hr to fix

                  Method import_csv_from_file has 6 arguments (exceeds 4 allowed). Consider refactoring.
                  Open

                    def self.import_csv_from_file(csv_filename, tag, tag_behaviour, dry_run, editor, available_locales = [])
                  Severity: Minor
                  Found in app/models/public_body.rb - About 45 mins to fix

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

                      def self.with_query(query, tag)
                        like_query = "%#{query}%"
                        has_first_letter = tag.size == 1
                    
                        underscore_locale = AlaveteliLocalization.locale
                    Severity: Minor
                    Found in app/models/public_body.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

                    Method import_csv has 6 arguments (exceeds 4 allowed). Consider refactoring.
                    Open

                      def self.import_csv(csv, tag, tag_behaviour, dry_run, editor, available_locales = [])
                    Severity: Minor
                    Found in app/models/public_body.rb - About 45 mins to fix

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

                        def calculated_home_page
                          if home_page && !home_page.empty?
                            home_page[URI::regexp(%w(http https))] ? home_page : "http://#{home_page}"
                          elsif request_email_domain
                            "http://www.#{request_email_domain}"
                      Severity: Minor
                      Found in app/models/public_body.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 with_tag has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                      Open

                        def self.with_tag(tag)
                          return all if tag.size == 1
                      
                          where_condition = ''
                          where_parameters = []
                      Severity: Minor
                      Found in app/models/public_body.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

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

                            ['name', '(i18n)<strong>Existing records cannot be renamed</strong>'],
                            ['short_name', '(i18n)'],
                            ['request_email', '(i18n)'],
                            ['notes', '(i18n)'],
                            ['publication_scheme', '(i18n)'],
                      Severity: Minor
                      Found in app/models/public_body.rb and 1 other location - About 15 mins to fix
                      spec/models/public_body_spec.rb on lines 1668..1675

                      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