discourse/discourse

View on GitHub
app/models/topic.rb

Summary

Maintainability
F
1 wk
Test Coverage

File topic.rb has 1189 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require_dependency 'slug'
require_dependency 'avatar_lookup'
require_dependency 'topic_view'
require_dependency 'rate_limiter'
require_dependency 'text_sentinel'
Severity: Major
Found in app/models/topic.rb - About 3 days to fix

    Class Topic has 105 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class Topic < ActiveRecord::Base
      class UserExists < StandardError; end
      include ActionView::Helpers::SanitizeHelper
      include RateLimiter::OnCreateRecord
      include HasCustomFields
    Severity: Major
    Found in app/models/topic.rb - About 2 days to fix

      Method set_or_create_timer has a Cognitive Complexity of 36 (exceeds 5 allowed). Consider refactoring.
      Open

        def set_or_create_timer(status_type, time, by_user: nil, based_on_last_post: false, category_id: SiteSetting.uncategorized_category_id)
          return delete_topic_timer(status_type, by_user: by_user) if time.blank?
      
          public_topic_timer = !!TopicTimer.public_types[status_type]
          topic_timer_options = { topic: self, public_type: public_topic_timer }
      Severity: Minor
      Found in app/models/topic.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 changed_to_category has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
      Open

        def changed_to_category(new_category)
          return true if new_category.blank? || Category.exists?(topic_id: id)
          return false if new_category.id == SiteSetting.uncategorized_category_id && !SiteSetting.allow_uncategorized_topics
      
          Topic.transaction do
      Severity: Minor
      Found in app/models/topic.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 set_or_create_timer has 44 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def set_or_create_timer(status_type, time, by_user: nil, based_on_last_post: false, category_id: SiteSetting.uncategorized_category_id)
          return delete_topic_timer(status_type, by_user: by_user) if time.blank?
      
          public_topic_timer = !!TopicTimer.public_types[status_type]
          topic_timer_options = { topic: self, public_type: public_topic_timer }
      Severity: Minor
      Found in app/models/topic.rb - About 1 hr to fix

        Method for_digest has 43 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def self.for_digest(user, since, opts = nil)
            opts = opts || {}
            score = "#{ListController.best_period_for(since)}_score"
        
            topics = Topic
        Severity: Minor
        Found in app/models/topic.rb - About 1 hr to fix

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

            def self.reset_all_highest!
              DB.exec <<~SQL
                WITH
                X as (
                  SELECT topic_id,
          Severity: Minor
          Found in app/models/topic.rb - About 1 hr to fix

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

              def self.reset_highest(topic_id)
                archetype = Topic.where(id: topic_id).pluck(:archetype).first
            
                # ignore small_action replies for private messages
                post_type = archetype == Archetype.private_message ? " AND post_type <> #{Post.types[:small_action]}" : ''
            Severity: Minor
            Found in app/models/topic.rb - About 1 hr to fix

              Method similar_to has 31 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                def self.similar_to(title, raw, user = nil)
                  return [] if title.blank?
                  raw = raw.presence || ""
              
                  search_data = "#{title} #{raw[0...MAX_SIMILAR_BODY_LENGTH]}".strip
              Severity: Minor
              Found in app/models/topic.rb - About 1 hr to fix

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

                  def self.for_digest(user, since, opts = nil)
                    opts = opts || {}
                    score = "#{ListController.best_period_for(since)}_score"
                
                    topics = Topic
                Severity: Minor
                Found in app/models/topic.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 invite has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                Open

                  def invite(invited_by, username_or_email, group_ids = nil, custom_message = nil)
                    target_user = User.find_by_username_or_email(username_or_email)
                    guardian = Guardian.new(invited_by)
                    is_email = username_or_email =~ /^.+@.+$/
                
                
                Severity: Minor
                Found in app/models/topic.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 remove_allowed_user has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                Open

                  def remove_allowed_user(removed_by, username)
                    user = username.is_a?(User) ? username : User.find_by(username: username)
                
                    if user
                      topic_user = topic_allowed_users.find_by(user_id: user.id)
                Severity: Minor
                Found in app/models/topic.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 next_post_number has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                Open

                  def self.next_post_number(topic_id, opts = {})
                    highest = DB.query_single("SELECT coalesce(max(post_number),0) AS max FROM posts WHERE topic_id = ?", topic_id).first.to_i
                
                    if opts[:whisper]
                
                
                Severity: Minor
                Found in app/models/topic.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 slug has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                Open

                  def slug
                    unless slug = read_attribute(:slug)
                      return '' unless title.present?
                      slug = Slug.for(title)
                      if new_record?
                Severity: Minor
                Found in app/models/topic.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 invite_to_topic has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                Open

                  def invite_to_topic(invited_by, target_user, group_ids, guardian)
                    Topic.transaction do
                      rate_limit_topic_invitation(invited_by)
                
                      if group_ids
                Severity: Minor
                Found in app/models/topic.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 add_moderator_post has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                Open

                  def add_moderator_post(user, text, opts = nil)
                    opts ||= {}
                    new_post = nil
                    creator = PostCreator.new(user,
                                              raw: text,
                Severity: Minor
                Found in app/models/topic.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 fancy_title has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                  def fancy_title
                    return ERB::Util.html_escape(title) unless SiteSetting.title_fancy_entities?
                
                    unless fancy_title = read_attribute(:fancy_title)
                      fancy_title = Topic.fancy_title(title)
                Severity: Minor
                Found in app/models/topic.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 recover! has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                  def recover!
                    unless deleted_at.nil?
                      update_category_topic_count_by(1)
                      CategoryTagStat.topic_recovered(self) if self.tags.present?
                    end
                Severity: Minor
                Found in app/models/topic.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