Showing 107 of 107 total issues
Omit the hash value. Open
get '/api/v1/image', params: params
- Read upRead up
- Exclude checks
Checks hash literal syntax.
It can enforce either the use of the class hash rocket syntax or the use of the newer Ruby 1.9 syntax (when applicable).
A separate offense is registered for each problematic pair.
The supported styles are:
- ruby19 - forces use of the 1.9 syntax (e.g.
{a: 1}
) when hashes have all symbols for keys - hash_rockets - forces use of hash rockets for all hashes
- nomixedkeys - simply checks for hashes with mixed syntaxes
- ruby19nomixed_keys - forces use of ruby 1.9 syntax and forbids mixed syntax hashes
This cop has EnforcedShorthandSyntax
option.
It can enforce either the use of the explicit hash value syntax or
the use of Ruby 3.1's hash value shorthand syntax.
The supported styles are:
- always - forces use of the 3.1 syntax (e.g. {foo:})
- never - forces use of explicit hash literal value
- either - accepts both shorthand and explicit use of hash literal value
- consistent - forces use of the 3.1 syntax only if all values can be omitted in the hash
Example: EnforcedStyle: ruby19 (default)
# bad
{:a => 2}
{b: 1, :c => 2}
# good
{a: 2, b: 1}
{:c => 2, 'd' => 2} # acceptable since 'd' isn't a symbol
{d: 1, 'e' => 2} # technically not forbidden
Example: EnforcedStyle: hash_rockets
# bad
{a: 1, b: 2}
{c: 1, 'd' => 5}
# good
{:a => 1, :b => 2}
Example: EnforcedStyle: nomixedkeys
# bad
{:a => 1, b: 2}
{c: 1, 'd' => 2}
# good
{:a => 1, :b => 2}
{c: 1, d: 2}
Example: EnforcedStyle: ruby19nomixed_keys
# bad
{:a => 1, :b => 2}
{c: 2, 'd' => 3} # should just use hash rockets
# good
{a: 1, b: 2}
{:c => 3, 'd' => 4}
Example: EnforcedShorthandSyntax: always (default)
# bad
{foo: foo, bar: bar}
# good
{foo:, bar:}
Example: EnforcedShorthandSyntax: never
# bad
{foo:, bar:}
# good
{foo: foo, bar: bar}
Example: EnforcedShorthandSyntax: either
# good
{foo: foo, bar: bar}
# good
{foo:, bar:}
Example: EnforcedShorthandSyntax: consistent
# bad - `foo` and `bar` values can be omitted
{foo: foo, bar: bar}
# bad - `bar` value can be omitted
{foo:, bar: bar}
# bad - mixed syntaxes
{foo:, bar: baz}
# good
{foo:, bar:}
# good - can't omit `baz`
{foo: foo, bar: baz}
Prefer response.parsed_body
to JSON.parse(response.body)
. Open
expect(JSON.parse(response.body)).to match(hash_including('total' => 1, 'offset' => 0, 'results' => [{ 'type' => 'MrssPhoto', 'title' => 'title', 'url' => 'https://www.flickr.com/photos/41555360@N03/14610842557/', 'thumbnail_url' => 'https://farm4.staticflickr.com/3841/14610842557_ed0ff5879a_q.jpg', 'taken_at' => '2013-09-20' }], 'suggestion' => { 'text' => 'cindy', 'highlighted' => '<strong>cindy</strong>' }))
- Exclude checks
Prefer have_received
for setting message expectations. Setup ImageSearch
as a spy using allow
or instance_spy
. Open
expect(ImageSearch).to receive(:new).and_raise
- Exclude checks
Prefer response.parsed_body
to JSON.parse(response.body)
. Open
expect(JSON.parse(response.body)).to match(hash_including('id' => 'http://some.mrss.url/already.xml',
- Exclude checks
Do not use Time.now
without zone. Use one of Time.zone.now
, Time.current
, Time.now.in_time_zone
, Time.now.utc
, Time.now.getlocal
, Time.now.xmlschema
, Time.now.iso8601
, Time.now.jisx0301
, Time.now.rfc3339
, Time.now.httpdate
, Time.now.to_i
, Time.now.to_f
instead. Open
photo2 = Hashie::Mash.new(id: 'photo2', owner: 'owner2', tags: '', title: 'title2', description: 'desc 2', datetaken: Time.now.strftime('%Y-%m-%d %H:%M:%S'), views: 200, url_o: 'http://photo2', url_q: 'http://photo_thumbnail2', dateupload: Time.now.to_i)
- Read upRead up
- Exclude checks
This cop checks for the use of Time methods without zone.
Built on top of Ruby on Rails style guide (https://github.com/rubocop-hq/rails-style-guide#time) and the article http://danilenko.org/2012/7/6/rails_timezones/
Two styles are supported for this cop. When EnforcedStyle is 'strict' then only use of Time.zone is allowed.
When EnforcedStyle is 'flexible' then it's also allowed to use Time.intimezone.
Example: EnforcedStyle: strict
# `strict` means that `Time` should be used with `zone`.
# bad
Time.now
Time.parse('2015-03-02 19:05:37')
# bad
Time.current
Time.at(timestamp).in_time_zone
# good
Time.zone.now
Time.zone.parse('2015-03-02 19:05:37')
Example: EnforcedStyle: flexible (default)
# `flexible` allows usage of `in_time_zone` instead of `zone`.
# bad
Time.now
Time.parse('2015-03-02 19:05:37')
# good
Time.zone.now
Time.zone.parse('2015-03-02 19:05:37')
# good
Time.current
Time.at(timestamp).in_time_zone
Prefer allow
over expect
when configuring a response. Open
expect(flickr_user_client).to receive_message_chain('people.getPublicPhotos').and_return(no_results)
- Exclude checks
Prefer using verifying doubles over normal doubles. Open
and_yield(double(FlickrProfile, id: 'abc', profile_type: 'user')).
- Exclude checks
Assignment Branch Condition size for detect_albums! is too high. [<6, 19, 1> 19.95/17] Open
def self.detect_albums!(photo)
photo_klass = photo.class
photo_source = photo_klass.name.split(/(?=[A-Z])/).first
album_detector_klass = "#{photo_source}AlbumDetector".constantize
assign_default_album(photo)
- Read upRead up
- Exclude checks
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 and https://en.wikipedia.org/wiki/ABC_Software_Metric.
Interpreting ABC size:
- <= 17 satisfactory
- 18..30 unsatisfactory
- > 30 dangerous
You can have repeated "attributes" calls count as a single "branch".
For this purpose, attributes are any method with no argument; no attempt
is meant to distinguish actual attr_reader
from other methods.
Example: CountRepeatedAttributes: false (default is true)
# `model` and `current_user`, referenced 3 times each,
# are each counted as only 1 branch each if
# `CountRepeatedAttributes` is set to 'false'
def search
@posts = model.active.visible_by(current_user)
.search(params[:q])
@posts = model.some_process(@posts, current_user)
@posts = model.another_process(@posts, current_user)
render 'pages/search/page'
end
This cop also takes into account AllowedMethods
(defaults to []
)
And AllowedPatterns
(defaults to []
)
Prefer using verifying doubles over normal doubles. Open
and_yield(double(MrssProfile, name: '4', id: 'http://some/mrss.url/feed.xml2'))
- Exclude checks
max
expects at least 2 positional arguments, got 0. Open
json.max do
# https://www.elastic.co/guide/en/elasticsearch/reference/2.0/breaking_20_scripting_changes.html#_scripting_syntax
json.script do
json.source '_score'
json.lang 'painless'
- Read upRead up
- Exclude checks
Checks for a block that is known to need more positional
block arguments than are given (by default this is configured for
Enumerable
methods needing 2 arguments). Optional arguments are allowed,
although they don't generally make sense as the default value will
be used. Blocks that have no receiver, or take splatted arguments
(ie. *args
) are always accepted.
Keyword arguments (including **kwargs
) do not get counted towards
this, as they are not used by the methods in question.
Method names and their expected arity can be configured like this:
Methods:
inject: 2
reduce: 2
Safety:
This cop matches for method names only and hence cannot tell apart methods with same name in different classes, which may lead to a false positive.
Example:
# bad
values.reduce {}
values.min { |a| a }
values.sort { |a; b| a + b }
# good
values.reduce { |memo, obj| memo << obj }
values.min { |a, b| a <=> b }
values.sort { |*x| x[0] <=> x[1] }
Assignment Branch Condition size for perform is too high. [<11, 17, 9> 22.16/17] Open
def perform(id, profile_type, days_ago = nil)
page = 1
pages = 1
min_ts = days_ago.present? ? days_ago.days.ago.to_i : 0
oldest_retrieved_ts = Time.now.to_i
- Read upRead up
- Exclude checks
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 and https://en.wikipedia.org/wiki/ABC_Software_Metric.
Interpreting ABC size:
- <= 17 satisfactory
- 18..30 unsatisfactory
- > 30 dangerous
You can have repeated "attributes" calls count as a single "branch".
For this purpose, attributes are any method with no argument; no attempt
is meant to distinguish actual attr_reader
from other methods.
Example: CountRepeatedAttributes: false (default is true)
# `model` and `current_user`, referenced 3 times each,
# are each counted as only 1 branch each if
# `CountRepeatedAttributes` is set to 'false'
def search
@posts = model.active.visible_by(current_user)
.search(params[:q])
@posts = model.some_process(@posts, current_user)
@posts = model.another_process(@posts, current_user)
render 'pages/search/page'
end
This cop also takes into account AllowedMethods
(defaults to []
)
And AllowedPatterns
(defaults to []
)
Prefer response.parsed_body
to JSON.parse(response.body)
. Open
expect(JSON.parse(response.body)).to match(hash_including('name' => 'commercegov', 'id' => '61913304@N07', 'profile_type' => 'user'))
- Exclude checks
Prefer allow
over expect
when configuring a response. Open
expect(AlbumDetectionPhotoIterator).to receive(:new).
- Exclude checks
rescue
at 17, 24 is not aligned with first_bucket_size = begin
at 15, 4. Open
rescue StandardError
- Read upRead up
- Exclude checks
Checks whether the rescue and ensure keywords are aligned properly.
Example:
# bad
begin
something
rescue
puts 'error'
end
# good
begin
something
rescue
puts 'error'
end
Prefer Rails.root.join('path/to')
. Open
CSV.foreach("#{Rails.root}/config/mrss_profiles.csv") do |row|
- Read upRead up
- Exclude checks
This cop is used to identify usages of file path joining process
to use Rails.root.join
clause. It is used to add uniformity when
joining paths.
Example: EnforcedStyle: arguments (default)
# bad
Rails.root.join('app/models/goober')
File.join(Rails.root, 'app/models/goober')
"#{Rails.root}/app/models/goober"
# good
Rails.root.join('app', 'models', 'goober')
Example: EnforcedStyle: slashes
# bad
Rails.root.join('app', 'models', 'goober')
File.join(Rails.root, 'app/models/goober')
"#{Rails.root}/app/models/goober"
# good
Rails.root.join('app/models/goober')
Do not use expect
in before
hook Open
expect(Elasticsearch::Persistence.client.indices).to receive(:get_alias).with(name: described_class.alias_name).and_raise(Elasticsearch::Transport::Transport::Errors::NotFound)
- Exclude checks
Omit the hash value. Open
settings: settings
- Read upRead up
- Exclude checks
Checks hash literal syntax.
It can enforce either the use of the class hash rocket syntax or the use of the newer Ruby 1.9 syntax (when applicable).
A separate offense is registered for each problematic pair.
The supported styles are:
- ruby19 - forces use of the 1.9 syntax (e.g.
{a: 1}
) when hashes have all symbols for keys - hash_rockets - forces use of hash rockets for all hashes
- nomixedkeys - simply checks for hashes with mixed syntaxes
- ruby19nomixed_keys - forces use of ruby 1.9 syntax and forbids mixed syntax hashes
This cop has EnforcedShorthandSyntax
option.
It can enforce either the use of the explicit hash value syntax or
the use of Ruby 3.1's hash value shorthand syntax.
The supported styles are:
- always - forces use of the 3.1 syntax (e.g. {foo:})
- never - forces use of explicit hash literal value
- either - accepts both shorthand and explicit use of hash literal value
- consistent - forces use of the 3.1 syntax only if all values can be omitted in the hash
Example: EnforcedStyle: ruby19 (default)
# bad
{:a => 2}
{b: 1, :c => 2}
# good
{a: 2, b: 1}
{:c => 2, 'd' => 2} # acceptable since 'd' isn't a symbol
{d: 1, 'e' => 2} # technically not forbidden
Example: EnforcedStyle: hash_rockets
# bad
{a: 1, b: 2}
{c: 1, 'd' => 5}
# good
{:a => 1, :b => 2}
Example: EnforcedStyle: nomixedkeys
# bad
{:a => 1, b: 2}
{c: 1, 'd' => 2}
# good
{:a => 1, :b => 2}
{c: 1, d: 2}
Example: EnforcedStyle: ruby19nomixed_keys
# bad
{:a => 1, :b => 2}
{c: 2, 'd' => 3} # should just use hash rockets
# good
{a: 1, b: 2}
{:c => 3, 'd' => 4}
Example: EnforcedShorthandSyntax: always (default)
# bad
{foo: foo, bar: bar}
# good
{foo:, bar:}
Example: EnforcedShorthandSyntax: never
# bad
{foo:, bar:}
# good
{foo: foo, bar: bar}
Example: EnforcedShorthandSyntax: either
# good
{foo: foo, bar: bar}
# good
{foo:, bar:}
Example: EnforcedShorthandSyntax: consistent
# bad - `foo` and `bar` values can be omitted
{foo: foo, bar: bar}
# bad - `bar` value can be omitted
{foo:, bar: bar}
# bad - mixed syntaxes
{foo:, bar: baz}
# good
{foo:, bar:}
# good - can't omit `baz`
{foo: foo, bar: baz}
Prefer have_received
for setting message expectations. Setup Elasticsearch::Persistence.client
as a spy using allow
or instance_spy
. Open
expect(Elasticsearch::Persistence.client).to receive(:search).and_return(result)
- Exclude checks
Prefer using be_able_to_parse
matcher over able_to_parse?
. Open
expect(described_class.able_to_parse?(mrss_xml)).to be_truthy
- Exclude checks
Prefer have_received
for setting message expectations. Setup ImageSearch
as a spy using allow
or instance_spy
. Open
expect(ImageSearch).to receive(:new).and_raise
- Exclude checks