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
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'
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
- Read upRead up
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)
- Read upRead up
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,
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']
- Read upRead up
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?
- Read upRead up
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)
- Read upRead up
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)
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))
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
- Read upRead up
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))
- Read upRead up
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?
- Read upRead up
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)
- Read upRead up
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)
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)
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?
- Read upRead up
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))
- Read upRead up
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?
- Read upRead up
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)
- Read upRead up
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?
- Read upRead up
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
- Read upRead up
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
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
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
- Read upRead up
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
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
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
- Read upRead up
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
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76