discourse/discourse

View on GitHub
lib/post_creator.rb

Summary

Maintainability
F
3 days
Test Coverage

File post_creator.rb has 410 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require_dependency 'rate_limiter'
require_dependency 'topic_creator'
require_dependency 'post_jobs_enqueuer'
require_dependency 'distributed_mutex'
require_dependency 'has_errors'
Severity: Minor
Found in lib/post_creator.rb - About 5 hrs to fix

    Class PostCreator has 37 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class PostCreator
      include HasErrors
    
      attr_reader :opts
    
    
    Severity: Minor
    Found in lib/post_creator.rb - About 4 hrs to fix

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

        def valid?
          @topic = nil
          @post = nil
      
          if @user.suspended? && !skip_validations?
      Severity: Minor
      Found in lib/post_creator.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 create has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
      Open

        def create
          if valid?
            transaction do
              build_post_stats
              create_topic
      Severity: Minor
      Found in lib/post_creator.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 valid? has 56 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def valid?
          @topic = nil
          @post = nil
      
          if @user.suspended? && !skip_validations?
      Severity: Major
      Found in lib/post_creator.rb - About 2 hrs to fix

        Method create has 32 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def create
            if valid?
              transaction do
                build_post_stats
                create_topic
        Severity: Minor
        Found in lib/post_creator.rb - About 1 hr to fix

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

            def update_user_counts
              return if @opts[:import_mode]
          
              @user.create_user_stat if @user.user_stat.nil?
          
          
          Severity: Minor
          Found in lib/post_creator.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 auto_close has 26 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def auto_close
              topic = @post.topic
              is_private_message = topic.private_message?
              topic_posts_count = @post.topic.posts_count
          
          
          Severity: Minor
          Found in lib/post_creator.rb - About 1 hr to fix

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

              def initialize(user, opts)
                # TODO: we should reload user in case it is tainted, should take in a user_id as opposed to user
                # If we don't do this we introduce a rather risky dependency
                @user = user
                @opts = opts || {}
            Severity: Minor
            Found in lib/post_creator.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 update_topic_stats has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
            Open

              def update_topic_stats
                attrs = { updated_at: Time.now }
            
                if @post.post_type != Post.types[:whisper]
                  attrs[:last_posted_at] = @post.created_at
            Severity: Minor
            Found in lib/post_creator.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 false
            Severity: Major
            Found in lib/post_creator.rb - About 30 mins to fix

              Avoid too many return statements within this method.
              Open

                  return true if skip_validations?
              Severity: Major
              Found in lib/post_creator.rb - About 30 mins to fix

                Avoid too many return statements within this method.
                Open

                      return false
                Severity: Major
                Found in lib/post_creator.rb - About 30 mins to fix

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

                    def self.set_reply_info(post)
                      return unless post.reply_to_post_number.present?
                  
                      reply_info = Post.where(topic_id: post.topic_id, post_number: post.reply_to_post_number)
                        .select(:user_id, :post_type)
                  Severity: Minor
                  Found in lib/post_creator.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 setup_post has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def setup_post
                      @opts[:raw] = TextCleaner.normalize_whitespaces(@opts[:raw] || '').gsub(/\s+\z/, "")
                  
                      post = Post.new(raw: @opts[:raw],
                                      topic_id: @topic.try(:id),
                  Severity: Minor
                  Found in lib/post_creator.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 ensure_in_allowed_users has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def ensure_in_allowed_users
                      return unless @topic.private_message? && @topic.id
                  
                      unless @topic.topic_allowed_users.where(user_id: @user.id).exists?
                        unless @topic.topic_allowed_groups.where('group_id IN (
                  Severity: Minor
                  Found in lib/post_creator.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 update_topic_auto_close has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def update_topic_auto_close
                      return if @opts[:import_mode]
                  
                      if @topic.closed?
                        @topic.delete_topic_timer(TopicTimer.types[:close])
                  Severity: Minor
                  Found in lib/post_creator.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