discourse/discourse

View on GitHub
app/controllers/posts_controller.rb

Summary

Maintainability
F
5 days
Test Coverage

File posts_controller.rb has 610 lines of code (exceeds 250 allowed). Consider refactoring.
Open

class PostsController < ApplicationController

  requires_login except: [
    :show,
    :replies,
Severity: Major
Found in app/controllers/posts_controller.rb - About 1 day to fix

    Class PostsController has 48 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class PostsController < ApplicationController
    
      requires_login except: [
        :show,
        :replies,
    Severity: Minor
    Found in app/controllers/posts_controller.rb - About 6 hrs to fix

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

        def update
          params.require(:post)
      
          post = Post.where(id: params[:id])
          post = post.with_deleted if guardian.is_staff?
      Severity: Minor
      Found in app/controllers/posts_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 revert has a Cognitive Complexity of 25 (exceeds 5 allowed). Consider refactoring.
      Open

        def revert
          raise Discourse::NotFound unless guardian.is_staff?
      
          post_id = params[:id] || params[:post_id]
          revision = params[:revision].to_i
      Severity: Minor
      Found in app/controllers/posts_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 create_params has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring.
      Open

        def create_params
          permitted = [
            :raw,
            :topic_id,
            :archetype,
      Severity: Minor
      Found in app/controllers/posts_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 create_params has 66 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def create_params
          permitted = [
            :raw,
            :topic_id,
            :archetype,
      Severity: Major
      Found in app/controllers/posts_controller.rb - About 2 hrs to fix

        Method update has 51 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def update
            params.require(:post)
        
            post = Post.where(id: params[:id])
            post = post.with_deleted if guardian.is_staff?
        Severity: Major
        Found in app/controllers/posts_controller.rb - About 2 hrs to fix

          Method latest has 46 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def latest
              params.permit(:before)
              last_post_id = params[:before].to_i
              last_post_id = Post.last.id if last_post_id <= 0
          
          
          Severity: Minor
          Found in app/controllers/posts_controller.rb - About 1 hr to fix

            Method bookmark has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
            Open

              def bookmark
                if params[:bookmarked] == "true"
                  post = find_post_from_params
                  result = PostActionCreator.create(current_user, post, :bookmark)
                  return render_json_error(result) if result.failed?
            Severity: Minor
            Found in app/controllers/posts_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 revert has 35 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              def revert
                raise Discourse::NotFound unless guardian.is_staff?
            
                post_id = params[:id] || params[:post_id]
                revision = params[:revision].to_i
            Severity: Minor
            Found in app/controllers/posts_controller.rb - About 1 hr to fix

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

                def user_posts_feed
                  params.require(:username)
                  user = fetch_user_from_params
              
                  posts = Post.public_posts
              Severity: Minor
              Found in app/controllers/posts_controller.rb - About 1 hr to fix

                Avoid too many return statements within this method.
                Open

                    return render_json_error(topic) if topic.errors.present?
                Severity: Major
                Found in app/controllers/posts_controller.rb - About 30 mins to fix

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

                    def latest
                      params.permit(:before)
                      last_post_id = params[:before].to_i
                      last_post_id = Post.last.id if last_post_id <= 0
                  
                  
                  Severity: Minor
                  Found in app/controllers/posts_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

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

                    def find_post_revision_from_topic_id
                      post = Post.find_by(topic_id: params[:topic_id].to_i, post_number: (params[:post_number] || 1).to_i)
                      raise Discourse::NotFound unless guardian.can_see?(post)
                  
                      revision = params[:revision].to_i
                  Severity: Minor
                  Found in app/controllers/posts_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

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

                    def find_post_using(finder)
                      # Include deleted posts if the user is staff
                      finder = finder.with_deleted if current_user.try(:staff?)
                      post = finder.first
                      raise Discourse::NotFound unless post
                  Severity: Minor
                  Found in app/controllers/posts_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

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

                    def backwards_compatible_json(json_obj, success)
                      json_obj.symbolize_keys!
                      if params[:nested_post].blank? && json_obj[:errors].blank? && json_obj[:action] != :enqueued
                        json_obj = json_obj[:post]
                      end
                  Severity: Minor
                  Found in app/controllers/posts_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

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

                        posts = Post.public_posts
                          .order(created_at: :desc)
                          .where('posts.id <= ?', last_post_id)
                          .where('posts.id > ?', last_post_id - 50)
                          .includes(topic: :category)
                  Severity: Minor
                  Found in app/controllers/posts_controller.rb and 1 other location - About 20 mins to fix
                  app/controllers/posts_controller.rb on lines 59..66

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

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

                        posts = Post.private_posts
                          .order(created_at: :desc)
                          .where('posts.id <= ?', last_post_id)
                          .where('posts.id > ?', last_post_id - 50)
                          .includes(topic: :category)
                  Severity: Minor
                  Found in app/controllers/posts_controller.rb and 1 other location - About 20 mins to fix
                  app/controllers/posts_controller.rb on lines 69..76

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

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

                    def revisions
                      post = find_post_from_params
                      raise Discourse::NotFound if post.hidden && !guardian.can_view_hidden_post_revisions?
                  
                      post_revision = find_post_revision_from_params
                  Severity: Minor
                  Found in app/controllers/posts_controller.rb and 1 other location - About 20 mins to fix
                  app/controllers/posts_controller.rb on lines 383..389

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

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

                    def latest_revision
                      post = find_post_from_params
                      raise Discourse::NotFound if post.hidden && !guardian.can_view_hidden_post_revisions?
                  
                      post_revision = find_latest_post_revision_from_params
                  Severity: Minor
                  Found in app/controllers/posts_controller.rb and 1 other location - About 20 mins to fix
                  app/controllers/posts_controller.rb on lines 374..380

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

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

                      changes[:raw] = post_revision.modifications["raw"][0] if post_revision.modifications["raw"].present? && post_revision.modifications["raw"][0] != post.raw
                      if post.is_first_post?
                  Severity: Minor
                  Found in app/controllers/posts_controller.rb and 1 other location - About 15 mins to fix
                  app/controllers/posts_controller.rb on lines 441..442

                  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

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

                        changes[:title] = post_revision.modifications["title"][0] if post_revision.modifications["title"].present? && post_revision.modifications["title"][0] != topic.title
                        changes[:category_id] = post_revision.modifications["category_id"][0] if post_revision.modifications["category_id"].present? && post_revision.modifications["category_id"][0] != topic.category.id
                  Severity: Minor
                  Found in app/controllers/posts_controller.rb and 1 other location - About 15 mins to fix
                  app/controllers/posts_controller.rb on lines 439..440

                  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