sanger/sequencescape

View on GitHub
app/controllers/admin/studies_controller.rb

Summary

Maintainability
A
2 hrs
Test Coverage
F
40%

Complex method Admin::StudiesController#filter (72.6)
Open

  def filter # rubocop:todo Metrics/CyclomaticComplexity
    filter_conditions = { approved: false } if params[:filter][:by] == 'not approved' unless params[:filter].nil?

    if params[:filter][:by] == 'not approved' || params[:filter][:by] == 'all'
      @studies = Study.where(filter_conditions).alphabetical.select { |p| p.name.include? params[:q] }

Flog calculates the ABC score for methods. The ABC score is based on assignments, branches (method calls), and conditions.

You can read more about ABC metrics or the flog tool

Complex method Admin::StudiesController#managed_update (48.2)
Open

  def managed_update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
    @study = Study.find(params[:id])

    if params[:study][:uploaded_data].present?
      Document.create!(documentable: @study, uploaded_data: params[:study][:uploaded_data])

Flog calculates the ABC score for methods. The ABC score is based on assignments, branches (method calls), and conditions.

You can read more about ABC metrics or the flog tool

Admin::StudiesController#filter has approx 9 statements
Open

  def filter # rubocop:todo Metrics/CyclomaticComplexity

A method with Too Many Statements is any method that has a large number of lines.

Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

So the following method would score +6 in Reek's statement-counting algorithm:

def parse(arg, argv, &error)
  if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
    return nil, block, nil                                         # +1
  end
  opt = (val = parse_arg(val, &error))[1]                          # +2
  val = conv_arg(*val)                                             # +3
  if opt and !arg
    argv.shift                                                     # +4
  else
    val[0] = nil                                                   # +5
  end
  val                                                              # +6
end

(You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

Admin::StudiesController#managed_update has approx 13 statements
Open

  def managed_update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength

A method with Too Many Statements is any method that has a large number of lines.

Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

So the following method would score +6 in Reek's statement-counting algorithm:

def parse(arg, argv, &error)
  if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
    return nil, block, nil                                         # +1
  end
  opt = (val = parse_arg(val, &error))[1]                          # +2
  val = conv_arg(*val)                                             # +3
  if opt and !arg
    argv.shift                                                     # +4
  else
    val[0] = nil                                                   # +5
  end
  val                                                              # +6
end

(You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

Method filter has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

  def filter # rubocop:todo Metrics/CyclomaticComplexity
    filter_conditions = { approved: false } if params[:filter][:by] == 'not approved' unless params[:filter].nil?

    if params[:filter][:by] == 'not approved' || params[:filter][:by] == 'all'
      @studies = Study.where(filter_conditions).alphabetical.select { |p| p.name.include? params[:q] }
Severity: Minor
Found in app/controllers/admin/studies_controller.rb - About 45 mins to fix

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 managed_update has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

  def managed_update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
    @study = Study.find(params[:id])

    if params[:study][:uploaded_data].present?
      Document.create!(documentable: @study, uploaded_data: params[:study][:uploaded_data])
Severity: Minor
Found in app/controllers/admin/studies_controller.rb - About 45 mins to fix

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

Admin::StudiesController tests '@study.warnings.present?' at least 3 times
Open

    flash.now[:warning] = @study.warnings if @study.warnings.present?
  end

  def edit
    @request_types = RequestType.order(name: :asc)

Repeated Conditional is a special case of Simulated Polymorphism. Basically it means you are checking the same value throughout a single class and take decisions based on this.

Example

Given

class RepeatedConditionals
  attr_accessor :switch

  def repeat_1
    puts "Repeat 1!" if switch
  end

  def repeat_2
    puts "Repeat 2!" if switch
  end

  def repeat_3
    puts "Repeat 3!" if switch
  end
end

Reek would emit the following warning:

test.rb -- 4 warnings:
  [5, 9, 13]:RepeatedConditionals tests switch at least 3 times (RepeatedConditional)

If you get this warning then you are probably not using the right abstraction or even more probable, missing an additional abstraction.

Admin::StudiesController#edit calls 'params[:id]' 2 times
Open

    if params[:id] != '0'
      @study = Study.find(params[:id])

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#show calls '@study.warnings' 2 times
Open

    flash.now[:warning] = @study.warnings if @study.warnings.present?

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#filter calls 'params[:q]' 2 times
Open

      @studies = Study.where(filter_conditions).alphabetical.select { |p| p.name.include? params[:q] }
    end

    unless params[:filter].nil?
      if params[:filter][:by] == 'unallocated manager'

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#filter calls 'params[:filter][:by] == 'not approved'' 2 times
Open

    filter_conditions = { approved: false } if params[:filter][:by] == 'not approved' unless params[:filter].nil?

    if params[:filter][:by] == 'not approved' || params[:filter][:by] == 'all'

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#managed_update calls '@study.id' 2 times
Open

      redirect_to controller: 'admin/studies', action: 'update', id: @study.id
    end
  rescue ActiveRecord::RecordInvalid => e
    errors = @study.errors.full_messages
    logger.warn "Failed to update attributes: #{errors}}"

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#filter calls 'params[:filter]' 7 times
Open

    filter_conditions = { approved: false } if params[:filter][:by] == 'not approved' unless params[:filter].nil?

    if params[:filter][:by] == 'not approved' || params[:filter][:by] == 'all'
      @studies = Study.where(filter_conditions).alphabetical.select { |p| p.name.include? params[:q] }
    end

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#filter calls 'p.name.include? params[:q]' 2 times
Open

      @studies = Study.where(filter_conditions).alphabetical.select { |p| p.name.include? params[:q] }
    end

    unless params[:filter].nil?
      if params[:filter][:by] == 'unallocated manager'

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#filter calls 'params[:filter][:by]' 4 times
Open

    filter_conditions = { approved: false } if params[:filter][:by] == 'not approved' unless params[:filter].nil?

    if params[:filter][:by] == 'not approved' || params[:filter][:by] == 'all'
      @studies = Study.where(filter_conditions).alphabetical.select { |p| p.name.include? params[:q] }
    end

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#filter calls 'params[:filter].nil?' 2 times
Open

    filter_conditions = { approved: false } if params[:filter][:by] == 'not approved' unless params[:filter].nil?

    if params[:filter][:by] == 'not approved' || params[:filter][:by] == 'all'
      @studies = Study.where(filter_conditions).alphabetical.select { |p| p.name.include? params[:q] }
    end

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#filter calls 'p.name' 2 times
Open

      @studies = Study.where(filter_conditions).alphabetical.select { |p| p.name.include? params[:q] }
    end

    unless params[:filter].nil?
      if params[:filter][:by] == 'unallocated manager'

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#edit calls '@study.warnings' 2 times
Open

      flash.now[:warning] = @study.warnings if @study.warnings.present?

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#managed_update calls 'params[:study][:uploaded_data]' 2 times
Open

    if params[:study][:uploaded_data].present?
      Document.create!(documentable: @study, uploaded_data: params[:study][:uploaded_data])

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#update calls 'flash.now' 2 times
Open

    flash.now[:warning] = @study.warnings if @study.warnings.present?
    flash.now[:notice] = 'Your study has been updated'

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#managed_update calls 'params[:study]' 5 times
Open

    if params[:study][:uploaded_data].present?
      Document.create!(documentable: @study, uploaded_data: params[:study][:uploaded_data])
    end
    params[:study].delete(:uploaded_data)

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#update calls '@study.warnings' 2 times
Open

    flash.now[:warning] = @study.warnings if @study.warnings.present?

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Reek implements a check for Duplicate Method Call.

Example

Here's a very much simplified and contrived example. The following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

One quick approach to silence Reek would be to refactor the code thus:

def double_thing()
  thing = @other.thing
  thing + thing
end

A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

class Other
  def double_thing()
    thing + thing
  end
end

The approach you take will depend on balancing other factors in your code.

Admin::StudiesController#filter performs a nil-check
Open

    filter_conditions = { approved: false } if params[:filter][:by] == 'not approved' unless params[:filter].nil?

    if params[:filter][:by] == 'not approved' || params[:filter][:by] == 'all'
      @studies = Study.where(filter_conditions).alphabetical.select { |p| p.name.include? params[:q] }
    end

A NilCheck is a type check. Failures of NilCheck violate the "tell, don't ask" principle.

Additionally, type checks often mask bigger problems in your source code like not using OOP and / or polymorphism when you should.

Example

Given

class Klass
  def nil_checker(argument)
    if argument.nil?
      puts "argument isn't nil!"
    end
  end
end

Reek would emit the following warning:

test.rb -- 1 warning:
  [3]:Klass#nil_checker performs a nil-check. (NilCheck)

Admin::StudiesController#managed_update has the variable name 'e'
Open

  rescue ActiveRecord::RecordInvalid => e

An Uncommunicative Variable Name is a variable name that doesn't communicate its intent well enough.

Poor names make it hard for the reader to build a mental picture of what's going on in the code. They can also be mis-interpreted; and they hurt the flow of reading, because the reader must slow down to interpret the names.

Admin::StudiesController#filter has the variable name 'p'
Open

      @studies = Study.where(filter_conditions).alphabetical.select { |p| p.name.include? params[:q] }
    end

    unless params[:filter].nil?
      if params[:filter][:by] == 'unallocated manager'

An Uncommunicative Variable Name is a variable name that doesn't communicate its intent well enough.

Poor names make it hard for the reader to build a mental picture of what's going on in the code. They can also be mis-interpreted; and they hurt the flow of reading, because the reader must slow down to interpret the names.

TODO found
Open

  # TODO: remove unneeded code

Similar blocks of code found in 2 locations. Consider refactoring.
Open

  def sort
    @studies = Study.all.sort_by(&:name)
    case params[:sort]
    when 'date'
      @studies = @studies.sort_by(&:created_at)
Severity: Minor
Found in app/controllers/admin/studies_controller.rb and 1 other location - About 30 mins to fix
app/controllers/admin/projects_controller.rb on lines 89..98

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 32.

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

Further Reading

There are no issues that match your filters.

Category
Status