Class has too many lines. [106/100] Open
class Event < ActiveRecord::Base
UNREASONABLY_LONG_DATE_SPAN = 300
has_many :application_letters
has_many :agreement_letters
- Read upRead up
- Exclude checks
This cop checks if the length a class exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Method has too many lines. [19/10] Open
def compare_participants_by_agreement(participant1, participant2)
if participant1.requires_agreement_letter_for_event?(self)
if participant2.requires_agreement_letter_for_event?(self)
return participant1.name <=> participant2.name
end
- 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 compare_participants_by_agreement is too high. [16.16/15] Open
def compare_participants_by_agreement(participant1, participant2)
if participant1.requires_agreement_letter_for_event?(self)
if participant2.requires_agreement_letter_for_event?(self)
return participant1.name <=> participant2.name
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
Cyclomatic complexity for compare_participants_by_agreement is too high. [7/6] Open
def compare_participants_by_agreement(participant1, participant2)
if participant1.requires_agreement_letter_for_event?(self)
if participant2.requires_agreement_letter_for_event?(self)
return participant1.name <=> participant2.name
end
- 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.
Method compare_participants_by_agreement
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
def compare_participants_by_agreement(participant1, participant2)
if participant1.requires_agreement_letter_for_event?(self)
if participant2.requires_agreement_letter_for_event?(self)
return participant1.name <=> participant2.name
end
- 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
Avoid too many return
statements within this method. Open
return 1
Avoid too many return
statements within this method. Open
return -1
Avoid too many return
statements within this method. Open
return participant1.name <=> participant2.name
Use max_by(&:end_date)
instead of max { |a, b| a.end_date <=> b.end_date }
. Open
(date_ranges.max { |a,b| a.end_date <=> b.end_date }).end_date
- Read upRead up
- Exclude checks
This cop identifies places where sort { |a, b| a.foo <=> b.foo }
can be replaced by sort_by(&:foo)
.
This cop also checks max
and min
methods.
Example:
# bad
array.sort { |a, b| a.foo <=> b.foo }
array.max { |a, b| a.foo <=> b.foo }
array.min { |a, b| a.foo <=> b.foo }
array.sort { |a, b| a[:foo] <=> b[:foo] }
# good
array.sort_by(&:foo)
array.sort_by { |v| v.foo }
array.sort_by do |var|
var.foo
end
array.max_by(&:foo)
array.min_by(&:foo)
array.sort_by { |a| a[:foo] }
Use min_by(&:start_date)
instead of min { |a, b| a.start_date <=> b.start_date }
. Open
(date_ranges.min { |a,b| a.start_date <=> b.start_date }).start_date
- Read upRead up
- Exclude checks
This cop identifies places where sort { |a, b| a.foo <=> b.foo }
can be replaced by sort_by(&:foo)
.
This cop also checks max
and min
methods.
Example:
# bad
array.sort { |a, b| a.foo <=> b.foo }
array.max { |a, b| a.foo <=> b.foo }
array.min { |a, b| a.foo <=> b.foo }
array.sort { |a, b| a[:foo] <=> b[:foo] }
# good
array.sort_by(&:foo)
array.sort_by { |v| v.foo }
array.sort_by do |var|
var.foo
end
array.max_by(&:foo)
array.min_by(&:foo)
array.sort_by { |a| a[:foo] }
Line is too long. [100/80] Open
accepted_applications = application_letters.where(status: ApplicationLetter.statuses[:accepted])
- Exclude checks
Favor modifier if
usage when having a single-line body. Another good alternative is the usage of control flow &&
/||
. Open
if participant2.requires_agreement_letter_for_event?(self)
- 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?
Prefer single-quoted strings when you don't need string interpolation or special symbols. Open
scope :draft_is, ->(draft) { where("draft = ?", draft) }
- Read upRead up
- Exclude checks
Checks if uses of quotes match the configured preference.
Example: EnforcedStyle: single_quotes (default)
# bad
"No special symbols"
"No string interpolation"
"Just text"
# good
'No special symbols'
'No string interpolation'
'Just text'
"Wait! What's #{this}!"
Example: EnforcedStyle: double_quotes
# bad
'Just some text'
'No special chars or interpolation'
# good
"Just some text"
"No special chars or interpolation"
"Every string in #{project} uses double_quotes"
Line is too long. [98/80] Open
# 2. All participants that have to submit an letter of agreement and did do so, ordered by name.
- Exclude checks
Line is too long. [98/80] Open
# 2. All participants that have to submit an letter of agreement and did do so, ordered by name.
- Exclude checks
Extra empty line detected at class body end. Open
end
- Read upRead up
- Exclude checks
This cops checks if empty lines around the bodies of classes match the configuration.
Example: EnforcedStyle: empty_lines
# good
class Foo
def bar
# ...
end
end
Example: EnforcedStyle: emptylinesexcept_namespace
# good
class Foo
class Bar
# ...
end
end
Example: EnforcedStyle: emptylinesspecial
# good
class Foo
def bar; end
end
Example: EnforcedStyle: noemptylines (default)
# good
class Foo
def bar
# ...
end
end
Line is too long. [100/80] Open
accepted_applications = application_letters.where(status: ApplicationLetter.statuses[:accepted])
- Exclude checks
Line is too long. [100/80] Open
rejected_applications = application_letters.where(status: ApplicationLetter.statuses[:rejected])
- Exclude checks
Line is too long. [82/80] Open
application_letters.where(status: ApplicationLetter.statuses[:accepted]).count
- Exclude checks
Inconsistent indentation detected. Open
@participants.sort { |x, y| self.compare_participants_by_agreement(x,y) }
- Read upRead up
- Exclude checks
This cops checks for inconsistent indentation.
Example:
class A
def test
puts 'hello'
puts 'world'
end
end
Do not use space inside array brackets. Open
enum kind: [ :workshop, :camp ]
- Read upRead up
- Exclude checks
Checks that brackets used for array literals have or don't have surrounding space depending on configuration.
Example: EnforcedStyle: space
# The `space` style enforces that array literals have
# surrounding space.
# bad
array = [a, b, c, d]
# good
array = [ a, b, c, d ]
Example: EnforcedStyle: no_space
# The `no_space` style enforces that array literals have
# no surrounding space.
# bad
array = [ a, b, c, d ]
# good
array = [a, b, c, d]
Example: EnforcedStyle: compact
# The `compact` style normally requires a space inside
# array brackets, with the exception that successive left
# or right brackets are collapsed together in nested arrays.
# bad
array = [ a, [ b, c ] ]
# good
array = [ a, [ b, c ]]
Line is too long. [81/80] Open
end_date - start_date > Rails.configuration.unreasonably_long_event_time_span
- Exclude checks
Rename has_date_ranges
to date_ranges?
. Open
def has_date_ranges
- Read upRead up
- Exclude checks
This cop makes sure that predicates are named properly.
Example:
# bad
def is_even?(value)
end
# good
def even?(value)
end
# bad
def has_value?
end
# good
def value?
end
Redundant return
detected. Open
return email
- Read upRead up
- Exclude checks
This cop checks for redundant return
expressions.
Example:
def test
return something
end
def test
one
two
three
return something
end
It should be extended to handle methods whose body is if/else or a case expression with a default branch.
Extra blank line detected. Open
# @return the minimum start_date over all date ranges
- Read upRead up
- Exclude checks
This cops checks for two or more consecutive blank lines.
Example:
# bad - It has two empty lines.
some_method
# one empty line
# two empty lines
some_method
# good
some_method
# one empty line
some_method
Space missing after comma. Open
(date_ranges.max { |a,b| a.end_date <=> b.end_date }).end_date
- Read upRead up
- Exclude checks
Checks for comma (,) not followed by some kind of space.
Example:
# bad
[1,2]
{ foo:bar,}
# good
[1, 2]
{ foo:bar, }
Line is too long. [198/80] Open
errors.add(:application_deadline, I18n.t('events.errors.application_deadline_before_start_of_event')) if application_deadline.present? && !date_ranges.blank? && application_deadline > start_date
- Exclude checks
Line is too long. [93/80] Open
accepted_applications.map{ |application_letter| application_letter.user.email }.join(',')
- Exclude checks
Redundant return
detected. Open
return participant1.name <=> participant2.name
- Read upRead up
- Exclude checks
This cop checks for redundant return
expressions.
Example:
def test
return something
end
def test
one
two
three
return something
end
It should be extended to handle methods whose body is if/else or a case expression with a default branch.
Redundant self
detected. Open
@participants = self.participants
- Read upRead up
- Exclude checks
This cop checks for redundant uses of self
.
The usage of self
is only needed when:
Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.
Calling an attribute writer to prevent an local variable assignment.
Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.
Note we allow uses of self
with operators because it would be awkward
otherwise.
Example:
# bad
def foo(bar)
self.baz
end
# good
def foo(bar)
self.bar # Resolves name clash with the argument.
end
def foo
bar = 1
self.bar # Resolves name clash with the local variable.
end
def foo
%w[x y z].select do |bar|
self.bar == bar # Resolves name clash with argument of the block.
end
end
Space missing after comma. Open
@participants.sort { |x, y| self.compare_participants_by_agreement(x,y) }
- Read upRead up
- Exclude checks
Checks for comma (,) not followed by some kind of space.
Example:
# bad
[1,2]
{ foo:bar,}
# good
[1, 2]
{ foo:bar, }
Tab detected. Open
@participants.sort { |x, y| self.compare_participants_by_agreement(x,y) }
- Exclude checks
Line is too long. [106/80] Open
# 1. All participants that have to submit an letter of agreement but did not yet do so, ordered by name.
- Exclude checks
Missing top-level class documentation comment. Open
class Event < ActiveRecord::Base
- Read upRead up
- Exclude checks
This cop checks for missing top-level documentation of classes and modules. Classes with no body are exempt from the check and so are namespace modules - modules that have nothing in their bodies except classes, other modules, or constant definitions.
The documentation requirement is annulled if the class or module has a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the same for all its children.
Example:
# bad
class Person
# ...
end
# good
# Description/Explanation of Person class
class Person
# ...
end
Favor modifier if
usage when having a single-line body. Another good alternative is the usage of control flow &&
/||
. Open
if participant2.agreement_letter_for_event?(self)
- 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?
Space missing after comma. Open
(date_ranges.min { |a,b| a.start_date <=> b.start_date }).start_date
- Read upRead up
- Exclude checks
Checks for comma (,) not followed by some kind of space.
Example:
# bad
[1,2]
{ foo:bar,}
# good
[1, 2]
{ foo:bar, }
Do not use space inside array brackets. Open
enum kind: [ :workshop, :camp ]
- Read upRead up
- Exclude checks
Checks that brackets used for array literals have or don't have surrounding space depending on configuration.
Example: EnforcedStyle: space
# The `space` style enforces that array literals have
# surrounding space.
# bad
array = [a, b, c, d]
# good
array = [ a, b, c, d ]
Example: EnforcedStyle: no_space
# The `no_space` style enforces that array literals have
# no surrounding space.
# bad
array = [ a, b, c, d ]
# good
array = [a, b, c, d]
Example: EnforcedStyle: compact
# The `compact` style normally requires a space inside
# array brackets, with the exception that successive left
# or right brackets are collapsed together in nested arrays.
# bad
array = [ a, [ b, c ] ]
# good
array = [ a, [ b, c ]]
Line is too long. [91/80] Open
# 3. All participants that do not have to submit an letter of agreement, ordered by name.
- Exclude checks
Line is too long. [91/80] Open
errors.add(:date_ranges, I18n.t('date_range.errors.no_timespan')) if date_ranges.blank?
- Exclude checks
Redundant return
detected. Open
return email
- Read upRead up
- Exclude checks
This cop checks for redundant return
expressions.
Example:
def test
return something
end
def test
one
two
three
return something
end
It should be extended to handle methods whose body is if/else or a case expression with a default branch.
Keep a blank line before and after protected
. Open
protected
- Read upRead up
- Exclude checks
Access modifiers should be surrounded by blank lines.
Example:
# bad
class Foo
def bar; end
private
def baz; end
end
# good
class Foo
def bar; end
private
def baz; end
end
Space missing to the left of {. Open
rejected_applications.map{ |applications_letter| applications_letter.user.email }.join(',')
- Read upRead up
- Exclude checks
Checks that block braces have or don't have a space before the opening brace depending on configuration.
Example:
# bad
foo.map{ |a|
a.bar.to_s
}
# good
foo.map { |a|
a.bar.to_s
}
Line is too long. [106/80] Open
# 1. All participants that have to submit an letter of agreement but did not yet do so, ordered by name.
- Exclude checks
Line is too long. [95/80] Open
rejected_applications.map{ |applications_letter| applications_letter.user.email }.join(',')
- Exclude checks
Redundant self
detected. Open
self.agreement_letters.where(user: user).take
- Read upRead up
- Exclude checks
This cop checks for redundant uses of self
.
The usage of self
is only needed when:
Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.
Calling an attribute writer to prevent an local variable assignment.
Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.
Note we allow uses of self
with operators because it would be awkward
otherwise.
Example:
# bad
def foo(bar)
self.baz
end
# good
def foo(bar)
self.bar # Resolves name clash with the argument.
end
def foo
bar = 1
self.bar # Resolves name clash with the local variable.
end
def foo
%w[x y z].select do |bar|
self.bar == bar # Resolves name clash with argument of the block.
end
end
Space missing to the left of {. Open
accepted_applications.map{ |application_letter| application_letter.user.email }.join(',')
- Read upRead up
- Exclude checks
Checks that block braces have or don't have a space before the opening brace depending on configuration.
Example:
# bad
foo.map{ |a|
a.bar.to_s
}
# good
foo.map { |a|
a.bar.to_s
}
Line is too long. [100/80] Open
# @return [String] Concatenation of all email addresses of accepted applications, seperated by ','
- Exclude checks
Use %i
or %I
for an array of symbols. Open
enum kind: [ :workshop, :camp ]
- Read upRead up
- Exclude checks
This cop can check for array literals made up of symbols that are not using the %i() syntax.
Alternatively, it checks for symbol arrays using the %i() syntax on projects which do not want to use that syntax.
Configuration option: MinSize
If set, arrays with fewer elements than this value will not trigger the
cop. For example, a MinSize of
3` will not enforce a style on an array
of 2 or fewer elements.
Example: EnforcedStyle: percent (default)
# good
%i[foo bar baz]
# bad
[:foo, :bar, :baz]
Example: EnforcedStyle: brackets
# good
[:foo, :bar, :baz]
# bad
%i[foo bar baz]
Line is too long. [92/80] Open
application_letters.all? { |application_letter| application_letter.status != 'pending' }
- Exclude checks
Line is too long. [91/80] Open
# 3. All participants that do not have to submit an letter of agreement, ordered by name.
- Exclude checks
Pass &:user
as an argument to collect
instead of a block. Open
accepted_applications.collect { |a| a.user }
- Read upRead up
- Exclude checks
Use symbols as procs when possible.
Example:
# bad
something.map { |s| s.upcase }
# good
something.map(&:upcase)
Line is too long. [100/80] Open
# @return [String] Concatenation of all email addresses of rejected applications, seperated by ','
- Exclude checks
Redundant self
detected. Open
self.date_ranges.clear
- Read upRead up
- Exclude checks
This cop checks for redundant uses of self
.
The usage of self
is only needed when:
Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.
Calling an attribute writer to prevent an local variable assignment.
Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.
Note we allow uses of self
with operators because it would be awkward
otherwise.
Example:
# bad
def foo(bar)
self.baz
end
# good
def foo(bar)
self.bar # Resolves name clash with the argument.
end
def foo
bar = 1
self.bar # Resolves name clash with the local variable.
end
def foo
%w[x y z].select do |bar|
self.bar == bar # Resolves name clash with argument of the block.
end
end
Missing space after #
. Open
#validate that application deadline is before the start of the event
- Read upRead up
- Exclude checks
This cop checks whether comments have a leading space after the
#
denoting the start of the comment. The leading space is not
required for some RDoc special syntax, like #++
, #--
,
#:nodoc
, =begin
- and =end
comments, "shebang" directives,
or rackup options.
Example:
# bad
#Some comment
# good
# Some comment
Line is too long. [84/80] Open
validates :max_participants, numericality: { only_integer: true, greater_than: 0 }
- Exclude checks
Redundant self
detected. Open
@participants.sort { |x, y| self.compare_participants_by_agreement(x,y) }
- Read upRead up
- Exclude checks
This cop checks for redundant uses of self
.
The usage of self
is only needed when:
Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.
Calling an attribute writer to prevent an local variable assignment.
Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.
Note we allow uses of self
with operators because it would be awkward
otherwise.
Example:
# bad
def foo(bar)
self.baz
end
# good
def foo(bar)
self.bar # Resolves name clash with the argument.
end
def foo
bar = 1
self.bar # Resolves name clash with the local variable.
end
def foo
%w[x y z].select do |bar|
self.bar == bar # Resolves name clash with argument of the block.
end
end