Module has too many lines. [127/100] Open
module AuthenticatedSystem
protected
def deauthenticate
current_user.forget_me if logged_in?
- Read upRead up
- Exclude checks
This cop checks if the length a module exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Assignment Branch Condition size for current_user is too high. [31.45/15] Open
def current_user
# @current_user ||= (session[:user] && User.find_by_id(session[:user])) || :false
unless @current_user
# maybe_user will be nil if session[:user] does not exist or we fail to find the User in the DB
maybe_user = User.find_by_id(session[:user])
- Read upRead up
- Exclude checks
This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
Method has too many lines. [23/10] Open
def access_denied
respond_to do |accepts|
accepts.html do
store_location
- Read upRead up
- Exclude checks
This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Method has too many lines. [21/10] Open
def login_required
# Walter McGinnis, 2007-12-12
# adding support for rss authentication
# via rails 2.0 http_basic_authentication
user = nil
- Read upRead up
- Exclude checks
This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Assignment Branch Condition size for access_denied is too high. [26.02/15] Open
def access_denied
respond_to do |accepts|
accepts.html do
store_location
- Read upRead up
- Exclude checks
This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
Method has too many lines. [16/10] Open
def current_user
# @current_user ||= (session[:user] && User.find_by_id(session[:user])) || :false
unless @current_user
# maybe_user will be nil if session[:user] does not exist or we fail to find the User in the DB
maybe_user = User.find_by_id(session[:user])
- Read upRead up
- Exclude checks
This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Cyclomatic complexity for login_required is too high. [9/6] Open
def login_required
# Walter McGinnis, 2007-12-12
# adding support for rss authentication
# via rails 2.0 http_basic_authentication
user = nil
- Read upRead up
- Exclude checks
This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.
An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.
Assignment Branch Condition size for login_required is too high. [18.76/15] Open
def login_required
# Walter McGinnis, 2007-12-12
# adding support for rss authentication
# via rails 2.0 http_basic_authentication
user = nil
- Read upRead up
- Exclude checks
This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
Cyclomatic complexity for current_user is too high. [9/6] Open
def current_user
# @current_user ||= (session[:user] && User.find_by_id(session[:user])) || :false
unless @current_user
# maybe_user will be nil if session[:user] does not exist or we fail to find the User in the DB
maybe_user = User.find_by_id(session[:user])
- Read upRead up
- Exclude checks
This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.
An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.
Perceived complexity for login_required is too high. [10/7] Open
def login_required
# Walter McGinnis, 2007-12-12
# adding support for rss authentication
# via rails 2.0 http_basic_authentication
user = nil
- Read upRead up
- Exclude checks
This cop tries to produce a complexity score that's a measure of the
complexity the reader experiences when looking at a method. For that
reason it considers when
nodes as something that doesn't add as much
complexity as an if
or a &&
. Except if it's one of those special
case
/when
constructs where there's no expression after case
. Then
the cop treats it as an if
/elsif
/elsif
... and lets all the when
nodes count. In contrast to the CyclomaticComplexity cop, this cop
considers else
nodes as adding complexity.
Example:
def my_method # 1
if cond # 1
case var # 2 (0.8 + 4 * 0.2, rounded)
when 1 then func_one
when 2 then func_two
when 3 then func_three
when 4..10 then func_other
end
else # 1
do_something until a && b # 2
end # ===
end # 7 complexity points
Perceived complexity for current_user is too high. [9/7] Open
def current_user
# @current_user ||= (session[:user] && User.find_by_id(session[:user])) || :false
unless @current_user
# maybe_user will be nil if session[:user] does not exist or we fail to find the User in the DB
maybe_user = User.find_by_id(session[:user])
- Read upRead up
- Exclude checks
This cop tries to produce a complexity score that's a measure of the
complexity the reader experiences when looking at a method. For that
reason it considers when
nodes as something that doesn't add as much
complexity as an if
or a &&
. Except if it's one of those special
case
/when
constructs where there's no expression after case
. Then
the cop treats it as an if
/elsif
/elsif
... and lets all the when
nodes count. In contrast to the CyclomaticComplexity cop, this cop
considers else
nodes as adding complexity.
Example:
def my_method # 1
if cond # 1
case var # 2 (0.8 + 4 * 0.2, rounded)
when 1 then func_one
when 2 then func_two
when 3 then func_three
when 4..10 then func_other
end
else # 1
do_something until a && b # 2
end # ===
end # 7 complexity points
Assignment Branch Condition size for login_from_cookie is too high. [16.97/15] Open
def login_from_cookie
return unless cookies[:auth_token] && !logged_in?
user = User.find_by_remember_token(cookies[:auth_token])
if user && user.remember_token?
user.remember_me
- Read upRead up
- Exclude checks
This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
Assignment Branch Condition size for get_auth_data is too high. [16.4/15] Open
def get_auth_data
auth_key = @@http_auth_headers.detect { |h| request.env.has_key?(h) }
auth_data = request.env[auth_key].to_s.split unless auth_key.blank?
auth_data && auth_data[0] == 'Basic' ? Base64.decode64(auth_data[1]).split(':')[0..1] : [nil, nil]
end
- Read upRead up
- Exclude checks
This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
Method login_required
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
def login_required
# Walter McGinnis, 2007-12-12
# adding support for rss authentication
# via rails 2.0 http_basic_authentication
user = 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 current_user
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
def current_user
# @current_user ||= (session[:user] && User.find_by_id(session[:user])) || :false
unless @current_user
# maybe_user will be nil if session[:user] does not exist or we fail to find the User in the DB
maybe_user = User.find_by_id(session[:user])
- 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 access_denied
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
def access_denied
respond_to do |accepts|
accepts.html do
store_location
- 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
protected
(on line 2) does not make singleton methods protected. Use protected
inside a class << self
block instead. Open
def self.included(base)
- Read upRead up
- Exclude checks
This cop checks for private
or protected
access modifiers which are
applied to a singleton method. These access modifiers do not make
singleton methods private/protected. private_class_method
can be
used for that.
Example:
# bad
class C
private
def self.method
puts 'hi'
end
end
Example:
# good
class C
def self.method
puts 'hi'
end
private_class_method :method
end
Example:
# good
class C
class << self
private
def method
puts 'hi'
end
end
end
Use Hash#key?
instead of Hash#has_key?
. Open
auth_key = @@http_auth_headers.detect { |h| request.env.has_key?(h) }
- Read upRead up
- Exclude checks
This cop (by default) checks for uses of methods Hash#haskey? and
Hash#hasvalue? where it enforces Hash#key? and Hash#value?
It is configurable to enforce the inverse, using verbose
method
names also.
Example: EnforcedStyle: short (default)
# bad Hash#haskey? Hash#hasvalue?
# good Hash#key? Hash#value?
Example: EnforcedStyle: verbose
# bad Hash#key? Hash#value?
# good Hash#haskey? Hash#hasvalue?
Symbol with a boolean name - you probably meant to use false
. Open
self.current_user ||= :false
- Read upRead up
- Exclude checks
This cop checks for :true
and :false
symbols.
In most cases it would be a typo.
Example:
# bad
:true
# good
true
Example:
# bad
:false
# good
false
Pass &:to_s
as an argument to map
instead of a block. Open
locale_match = %r(^/(#{I18n.available_locales_with_labels.keys.map { |l| l.to_s }.join('|')}))
- Read upRead up
- Exclude checks
Use symbols as procs when possible.
Example:
# bad
something.map { |s| s.upcase }
# good
something.map(&:upcase)
Symbol with a boolean name - you probably meant to use false
. Open
current_user != :false
- Read upRead up
- Exclude checks
This cop checks for :true
and :false
symbols.
In most cases it would be a typo.
Example:
# bad
:true
# good
true
Example:
# bad
:false
# good
false
%w
-literals should be delimited by [
and ]
. Open
@@http_auth_headers = %w(X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION Authorization)
- Read upRead up
- Exclude checks
This cop enforces the consistent usage of %
-literal delimiters.
Specify the 'default' key to set all preferred delimiters at once. You can continue to specify individual preferred delimiters to override the default.
Example:
# Style/PercentLiteralDelimiters:
# PreferredDelimiters:
# default: '[]'
# '%i': '()'
# good
%w[alpha beta] + %i(gamma delta)
# bad
%W(alpha #{beta})
# bad
%I(alpha beta)
Use ==
if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition. Open
if user = authenticate_or_request_with_http_basic { |u, p| User.authenticate(u, p) }
- Read upRead up
- Exclude checks
This cop checks for assignments in the conditions of if/while/until.
Example:
# bad
if some_var = true
do_something
end
Example:
# good
if some_var == true
do_something
end
Use a guard clause instead of wrapping the code inside a conditional expression. Open
if user && user.remember_token?
- Read upRead up
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression
Example:
# bad
def test
if something
work
end
end
# good
def test
return unless something
work
end
# also good
def test
work if something
end
# bad
if something
raise 'exception'
else
ok
end
# good
raise 'exception' if something
ok
Use the return of the conditional for variable assignment and comparison. Open
if user.nil?
self.current_user ||= :false
else
self.current_user ||= user
end
- Exclude checks
Use ==
if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition. Open
if user = authenticate_with_http_basic { |u, p| User.authenticate(u, p) }
- Read upRead up
- Exclude checks
This cop checks for assignments in the conditions of if/while/until.
Example:
# bad
if some_var = true
do_something
end
Example:
# good
if some_var == true
do_something
end
Symbol with a boolean name - you probably meant to use false
. Open
@current_user = (maybe_user.nil? ? :false : maybe_user)
- Read upRead up
- Exclude checks
This cop checks for :true
and :false
symbols.
In most cases it would be a typo.
Example:
# bad
:true
# good
true
Example:
# bad
:false
# good
false
Do not prefix reader method names with get_
. Open
def get_auth_data
- Read upRead up
- Exclude checks
This cop makes sure that accessor methods are named properly.
Example:
# bad
def set_attribute(value)
end
# good
def attribute=(value)
end
# bad
def get_attribute
end
# good
def attribute
end
%r
-literals should be delimited by {
and }
. Open
locale_match = %r(^/(#{I18n.available_locales_with_labels.keys.map { |l| l.to_s }.join('|')}))
- Read upRead up
- Exclude checks
This cop enforces the consistent usage of %
-literal delimiters.
Specify the 'default' key to set all preferred delimiters at once. You can continue to specify individual preferred delimiters to override the default.
Example:
# Style/PercentLiteralDelimiters:
# PreferredDelimiters:
# default: '[]'
# '%i': '()'
# good
%w[alpha beta] + %i(gamma delta)
# bad
%W(alpha #{beta})
# bad
%I(alpha beta)
Replace class var @@http_auth_headers with a class instance var. Open
@@http_auth_headers = %w(X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION Authorization)
- Read upRead up
- Exclude checks
This cop checks for uses of class variables. Offenses are signaled only on assignment to class variables to reduce the number of offenses that would be reported.
Favor modifier if
usage when having a single-line body. Another good alternative is the usage of control flow &&
/||
. Open
if logged_in? && authorized?
- Read upRead up
- Exclude checks
Checks for if and unless statements that would fit on one line
if written as a modifier if/unless. The maximum line length is
configured in the Metrics/LineLength
cop.
Example:
# bad
if condition
do_stuff(bar)
end
unless qux.empty?
Foo.do_something
end
# good
do_stuff(bar) if condition
Foo.do_something unless qux.empty?
Use safe navigation (&.
) instead of checking if an object exists before calling the method. Open
if user && user.remember_token?
- Read upRead up
- Exclude checks
This cop transforms usages of a method call safeguarded by a non nil
check for the variable whose method is being called to
safe navigation (&.
).
Configuration option: ConvertCodeThatCanStartToReturnNil
The default for this is false
. When configured to true
, this will
check for code in the format !foo.nil? && foo.bar
. As it is written,
the return of this code is limited to false
and whatever the return
of the method is. If this is converted to safe navigation,
foo&.bar
can start returning nil
as well as what the method
returns.
Example:
# bad
foo.bar if foo
foo.bar(param1, param2) if foo
foo.bar { |e| e.something } if foo
foo.bar(param) { |e| e.something } if foo
foo.bar if !foo.nil?
foo.bar unless !foo
foo.bar unless foo.nil?
foo && foo.bar
foo && foo.bar(param1, param2)
foo && foo.bar { |e| e.something }
foo && foo.bar(param) { |e| e.something }
# good
foo&.bar
foo&.bar(param1, param2)
foo&.bar { |e| e.something }
foo&.bar(param) { |e| e.something }
foo.nil? || foo.bar
!foo || foo.bar
# Methods that `nil` will `respond_to?` should not be converted to
# use safe navigation
foo.to_i if foo
Symbol with a boolean name - you probably meant to use false
. Open
if @current_user != :false && @current_user.anonymous? && session[:anonymous_user].present?
- Read upRead up
- Exclude checks
This cop checks for :true
and :false
symbols.
In most cases it would be a typo.
Example:
# bad
:true
# good
true
Example:
# bad
:false
# good
false