Class Server
has 36 methods (exceeds 20 allowed). Consider refactoring. Open
class Server < Sinatra::Base
enable :static, :methodoverride
set :public_folder, Geminabox.public_folder
set :views, Geminabox.views
File server.rb
has 310 lines of code (exceeds 250 allowed). Consider refactoring. Confirmed
require 'reentrant_flock'
require 'rubygems/util'
module Geminabox
Method reindex
has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring. Open
def reindex(force_rebuild = false)
fixup_bundler_rubygems!
force_rebuild = true unless Geminabox.incremental_updates
if force_rebuild
indexer.generate_index
- 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
Use text.start_with?('http://', 'https://')
instead of text.start_with?('http://') || text.start_with?('https://')
. Open
if text && (text.start_with?('http://') || text.start_with?('https://'))
- Read upRead up
- Exclude checks
This cop checks for double #start_with?
or #end_with?
calls
separated by ||
. In some cases such calls can be replaced
with an single #start_with?
/#end_with?
call.
Example:
# bad
str.start_with?("a") || str.start_with?(Some::CONST)
str.start_with?("a", "b") || str.start_with?("c")
str.end_with?(var1) || str.end_with?(var2)
# good
str.start_with?("a", Some::CONST)
str.start_with?("a", "b", "c")
str.end_with?(var1, var2)
Use flat_map
instead of map...flatten
. Open
query_gems.map{|query_gem| gem_dependencies(query_gem) }.flatten(1)
- Read upRead up
- Exclude checks
This cop is used to identify usages of
Example:
# bad
[1, 2, 3, 4].map { |e| [e, e] }.flatten(1)
[1, 2, 3, 4].collect { |e| [e, e] }.flatten(1)
# good
[1, 2, 3, 4].flat_map { |e| [e, e] }
[1, 2, 3, 4].map { |e| [e, e] }.flatten
[1, 2, 3, 4].collect { |e| [e, e] }.flatten
Do not suppress exceptions. Open
rescue
- Read upRead up
- Exclude checks
This cop checks for rescue blocks with no body.
Example:
# bad
def some_method
do_something
rescue
# do nothing
end
Example:
# bad
begin
do_something
rescue
# do nothing
end
Example:
# good
def some_method
do_something
rescue
handle_exception
end
Example:
# good
begin
do_something
rescue
handle_exception
end
Do not suppress exceptions. Open
rescue Gem::SystemExitException
- Read upRead up
- Exclude checks
This cop checks for rescue blocks with no body.
Example:
# bad
def some_method
do_something
rescue
# do nothing
end
Example:
# bad
begin
do_something
rescue
# do nothing
end
Example:
# good
def some_method
do_something
rescue
handle_exception
end
Example:
# good
begin
do_something
rescue
handle_exception
end
Use 2 spaces for indentation in a heredoc by using <<~
instead of <<
. Open
<html>
<head><title>Error - #{code}</title></head>
<body>
<h1>Error - #{code}</h1>
<p>#{message}</p>
- Read upRead up
- Exclude checks
This cops checks the indentation of the here document bodies. The bodies
are indented one step.
In Ruby 2.3 or newer, squiggly heredocs (<<~
) should be used. If you
use the older rubies, you should introduce some library to your project
(e.g. ActiveSupport, Powerpack or Unindent).
Note: When Metrics/LineLength
's AllowHeredoc
is false(not default),
this cop does not add any offenses for long here documents to
avoid Metrics/LineLength
's offenses.
Example:
# bad
<<-RUBY
something
RUBY
# good
# When EnforcedStyle is squiggly, bad code is auto-corrected to the
# following code.
<<~RUBY
something
RUBY
# good
# When EnforcedStyle is active_support, bad code is auto-corrected to
# the following code.
<<-RUBY.strip_heredoc
something
RUBY