assemblymade/coderwall

View on GitHub
app/models/protip.rb

Summary

Maintainability
F
6 days
Test Coverage

Class Protip has 117 methods (exceeds 20 allowed). Consider refactoring.
Open

class Protip < ActiveRecord::Base
  extend FriendlyId
  friendly_id :slug_format, :use => :slugged

  include Featurable
Severity: Major
Found in app/models/protip.rb - About 2 days to fix

    File protip.rb has 778 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    require 'net_validators'
    require 'open-uri'
    require 'cfm'
    require 'scoring'
    require 'search'
    Severity: Major
    Found in app/models/protip.rb - About 1 day to fix

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

          def preprocess_query(query_string)
            query = team = nil
            unless query_string.nil?
              query = query_string.dup
              query.gsub!(/(\d+)\"/, "\\1\\\"") #handle 27" cases
      Severity: Minor
      Found in app/models/protip.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 search has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring.
      Open

          def search(query_string, tags =[], options={})
            query, team, author, bookmarked_by, execution, sorts= preprocess_query(query_string)
            tags = [] if tags.nil?
            tags = preprocess_tags(tags)
            tag_ids = process_tags_for_search(tags)
      Severity: Minor
      Found in app/models/protip.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 to_indexed_json has 32 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def to_indexed_json
          to_public_hash.deep_merge(
            {
              trending_score:        trending_score,
              popular_score:         value_score,
      Severity: Minor
      Found in app/models/protip.rb - About 1 hr to fix

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

          def upvote_velocity(since = Time.at(0))
            Rails.logger.ap since if ENV['DEBUG']
        
            us = upvotes_since(since)
            Rails.logger.ap us  if ENV['DEBUG']
        Severity: Minor
        Found in app/models/protip.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 upvote_by has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

          def upvote_by(voter, tracking_code, ip_address)
            begin
              unless already_voted?(voter, tracking_code, ip_address) or (self.author.id == voter.try(:id))
                self.likes.create(user: voter, value: voter.nil? ? 1 : adjust_like_value(voter, voter.like_value), tracking_code: tracking_code, ip_address: ip_address)
                generate_event(voter: voter.username) unless voter.nil?
        Severity: Minor
        Found in app/models/protip.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 viewed_by has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

          def viewed_by(viewer)
            epoch_now = Time.now.to_i
            Redis.current.incr(impressions_key)
            if viewer.is_a?(User)
              Redis.current.zadd(user_views_key, epoch_now, viewer.id)
        Severity: Minor
        Found in app/models/protip.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 search has 26 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            def search(query_string, tags =[], options={})
              query, team, author, bookmarked_by, execution, sorts= preprocess_query(query_string)
              tags = [] if tags.nil?
              tags = preprocess_tags(tags)
              tag_ids = process_tags_for_search(tags)
        Severity: Minor
        Found in app/models/protip.rb - About 1 hr to fix

          Consider simplifying this complex logical expression.
          Open

                unless query_string.nil?
                  query = query_string.dup
                  query.gsub!(/(\d+)\"/, "\\1\\\"") #handle 27" cases
                  team = query.gsub!(/(team:([0-9A-Z\-]+))/i, "") && $2
                  team = (team =~ /^[a-f0-9]+$/i && team.length == 24 ? team : Team.where(slug: team).first.try(:id))
          Severity: Major
          Found in app/models/protip.rb - About 1 hr to fix

            Method upvote_ancestor has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
            Open

              def upvote_ancestor(link_identifier, link)
                ProtipLink.where(identifier: link_identifier).order('created_at ASC').first.try(:tap) do |ancestor|
                  if (ancestor.protip != self) and (ancestor.protip.author.id != self.author.id) and (ancestor.url == link)
                    ancestor.protip.upvote_by(self.user, self.user.tracking_code, DEFAULT_IP_ADDRESS) unless ancestor.nil? || ancestor.protip.nil?
                    break
            Severity: Minor
            Found in app/models/protip.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 process_links has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
            Open

              def process_links
                if self.body_changed?
                  self.links.each do |link|
                    link_identifier = ProtipLink.generate_identifier(link)
                    existing_link = self.protip_links.find_or_initialize_by_identifier(identifier: link_identifier, url: link.first(254))
            Severity: Minor
            Found in app/models/protip.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 sanitize_tags! has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
            Open

              def sanitize_tags!
                new_topics = self.topic_list.reject { |tag| tag.blank? }.map do |topic|
                  sanitized_topic = self.class.preprocess_tag(topic)
                  invalid_topic = topic.match("^((?!#{VALID_TAG}).)*$") && $1
                  errors[:topics] << "The tag '#{topic}' has invalid characters: #{invalid_topic unless invalid_topic.nil?}" if sanitized_topic.nil?
            Severity: Minor
            Found in app/models/protip.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 event_audience has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
            Open

              def event_audience(event_type)
                audience = {}
                case event_type
                when :protip_view, :protip_upvote
                  audience = Audience.user(self.author.id)
            Severity: Minor
            Found in app/models/protip.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 search_top_trending_since has 5 arguments (exceeds 4 allowed). Consider refactoring.
            Open

                def search_top_trending_since(query, since, tags, page = 1, per_page = 10)
            Severity: Minor
            Found in app/models/protip.rb - About 35 mins to fix

              Method search_trending_by_user has 5 arguments (exceeds 4 allowed). Consider refactoring.
              Open

                  def search_trending_by_user(username, query_string, tags, page, per_page)
              Severity: Minor
              Found in app/models/protip.rb - About 35 mins to fix

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

                    def trending_topics
                      trending_protips = search(nil, [], page: 1, per_page: 100)
                
                      unless trending_protips.respond_to?(:errored?) and trending_protips.errored?
                        static_trending = ENV['FEATURED_TOPICS'].split(",").map(&:strip).map(&:downcase) unless ENV['FEATURED_TOPICS'].blank?
                Severity: Minor
                Found in app/models/protip.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 already_voted? has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                  def already_voted?(voter, tracking, ip_address)
                    existing_upvote = likes.where(user_id: voter.id).first unless voter.nil?
                    existing_upvote = likes.where(tracking_code: tracking).first if existing_upvote.nil? and tracking
                    existing_upvote = likes.where(ip_address: ip_address).first if existing_upvote.nil? and voter.nil? and (tracking.nil? || !User.exists?(tracking_code: tracking))
                
                
                Severity: Minor
                Found in app/models/protip.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 images has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                  def images
                    if self.new_record?
                      self.links.select { |link| ProtipLink.is_image? link }
                    else
                      if protip_links.loaded?
                Severity: Minor
                Found in app/models/protip.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 search_next has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    def search_next(query, tag, index, page)
                      return nil if page.nil? || (tag.blank? && query.blank?) #when your viewing a protip if we don't check this it thinks we came from trending and shows the next trending prootip eventhough we directly landed here
                      page = (index.to_i * page.to_i) + 1
                      tag = [tag] unless tag.is_a?(Array) || tag.nil?
                      search(query, tag, page: page, per_page: 1).results.try(:first)
                Severity: Minor
                Found in app/models/protip.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 search_trending_by_team has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    def search_trending_by_team(team_id, query_string, page, per_page)
                      options = { page: page, per_page: per_page }
                      force_index_commit = Protip.tire.index.refresh if Rails.env.test?
                      query = "team.name:#{team_id.to_s}"
                      query              += " #{query_string}" unless query_string.nil?
                Severity: Minor
                Found in app/models/protip.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

                  def total_views(epoch_since = 0)
                    if epoch_since.to_i == 0
                      Redis.current.get(impressions_key).to_i
                    else
                      epoch_now = Time.now.to_i
                Severity: Minor
                Found in app/models/protip.rb and 1 other location - About 35 mins to fix
                app/models/concerns/user_viewer.rb on lines 23..29

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

                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

                  state_machine initial: :active do
                    event :report_spam do
                      transition active: :reported_as_spam
                    end
                
                
                Severity: Minor
                Found in app/models/protip.rb and 1 other location - About 20 mins to fix
                app/models/comment.rb on lines 44..54

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

                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

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

                      Redis.current.zadd(user_anon_views_key, epoch_now, viewer)
                      count = Redis.current.zcard(user_anon_views_key)
                      Redis.current.zremrangebyrank(user_anon_views_key, -(count - 100), -1) if count > 100
                Severity: Minor
                Found in app/models/protip.rb and 1 other location - About 15 mins to fix
                app/models/concerns/user_viewer.rb on lines 11..13

                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