SpeciesFileGroup/taxonworks

View on GitHub
lib/queries/source/filter.rb

Summary

Maintainability
D
2 days
Test Coverage

Possible SQL injection
Open

        ::Source.from("( #{union} ) as sources")
Severity: Minor
Found in lib/queries/source/filter.rb by brakeman

Injection is #1 on the 2013 OWASP Top Ten web security risks. SQL injection is when a user is able to manipulate a value which is used unsafely inside a SQL query. This can lead to data leaks, data loss, elevation of privilege, and other unpleasant outcomes.

Brakeman focuses on ActiveRecord methods dealing with building SQL statements.

A basic (Rails 2.x) example looks like this:

User.first(:conditions => "username = '#{params[:username]}'")

Brakeman would produce a warning like this:

Possible SQL injection near line 30: User.first(:conditions => ("username = '#{params[:username]}'"))

The safe way to do this query is to use a parameterized query:

User.first(:conditions => ["username = ?", params[:username]])

Brakeman also understands the new Rails 3.x way of doing things (and local variables and concatenation):

username = params[:user][:name].downcase
password = params[:user][:password]

User.first.where("username = '" + username + "' AND password = '" + password + "'")

This results in this kind of warning:

Possible SQL injection near line 37:
User.first.where((((("username = '" + params[:user][:name].downcase) + "' AND password = '") + params[:user][:password]) + "'"))

See the Ruby Security Guide for more information and Rails-SQLi.org for many examples of SQL injection in Rails.

Method initialize has a Cognitive Complexity of 50 (exceeds 5 allowed). Consider refactoring.
Open

      def initialize(params)
        @query_string = params[:query_term]

        @author = params[:author]
        @author_ids = params[:author_ids] || []
Severity: Minor
Found in lib/queries/source/filter.rb - About 7 hrs 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

File filter.rb has 321 lines of code (exceeds 250 allowed). Consider refactoring.
Open

module Queries
  module Source
    class Filter < Queries::Query

      # TODO: likely move to model (replicated in Source too)
Severity: Minor
Found in lib/queries/source/filter.rb - About 3 hrs to fix

    Class Filter has 27 methods (exceeds 20 allowed). Consider refactoring.
    Open

        class Filter < Queries::Query
    
          # TODO: likely move to model (replicated in Source too)
          # Params exists for all CollectingEvent attributes except these
          ATTRIBUTES = (::Source.column_names - %w{id project_id created_by_id updated_by_id created_at updated_at cached})
    Severity: Minor
    Found in lib/queries/source/filter.rb - About 3 hrs to fix

      Method has too many lines. [30/25]
      Open

            def initialize(params)
              @query_string = params[:query_term]
      
              @author = params[:author]
              @author_ids = params[:author_ids] || []
      Severity: Minor
      Found in lib/queries/source/filter.rb by rubocop

      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. [27/25]
      Open

            def merge_clauses
              clauses = [
                ancestors_facet,
                author_ids_facet,
                topic_ids_facet,
      Severity: Minor
      Found in lib/queries/source/filter.rb by rubocop

      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 initialize has 30 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

            def initialize(params)
              @query_string = params[:query_term]
      
              @author = params[:author]
              @author_ids = params[:author_ids] || []
      Severity: Minor
      Found in lib/queries/source/filter.rb - About 1 hr to fix

        Method merge_clauses has 27 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

              def merge_clauses
                clauses = [
                  ancestors_facet,
                  author_ids_facet,
                  topic_ids_facet,
        Severity: Minor
        Found in lib/queries/source/filter.rb - About 1 hr to fix

          Method all has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
          Open

                def all
                  a = and_clauses
                  b = merge_clauses
          
                  q = nil
          Severity: Minor
          Found in lib/queries/source/filter.rb - About 25 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

          Use year_end.present? instead of !year_end.blank?.
          Open

                  if year_start && !year_end.blank?
          Severity: Minor
          Found in lib/queries/source/filter.rb by rubocop

          This cop checks for code that can be written with simpler conditionals using Object#present? defined by Active Support.

          Interaction with Style/UnlessElse: The configuration of NotBlank will not produce an offense in the context of unless else if Style/UnlessElse is inabled. This is to prevent interference between the auto-correction of the two cops.

          Example: NotNilAndNotEmpty: true (default)

          # Converts usages of `!nil? && !empty?` to `present?`
          
          # bad
          !foo.nil? && !foo.empty?
          
          # bad
          foo != nil && !foo.empty?
          
          # good
          foo.present?

          Example: NotBlank: true (default)

          # Converts usages of `!blank?` to `present?`
          
          # bad
          !foo.blank?
          
          # bad
          not foo.blank?
          
          # good
          foo.present?

          Example: UnlessBlank: true (default)

          # Converts usages of `unless blank?` to `if present?`
          
          # bad
          something unless foo.blank?
          
          # good
          something if foo.present?

          TODO found
          Open

                # TODO: move to generalized code in identifiers concern
          Severity: Minor
          Found in lib/queries/source/filter.rb by fixme

          TODO found
          Open

                # TODO: move to a concern
          Severity: Minor
          Found in lib/queries/source/filter.rb by fixme

          TODO found
          Open

                # TODO: move to a concern
          Severity: Minor
          Found in lib/queries/source/filter.rb by fixme

          TODO found
          Open

                # TODO: move to a concern
          Severity: Minor
          Found in lib/queries/source/filter.rb by fixme

          TODO found
          Open

                # TODO: likely move to model (replicated in Source too)
          Severity: Minor
          Found in lib/queries/source/filter.rb by fixme

          TODO found
          Open

                  @project_id = params[:project_id] # TODO: also in Queries::Query
          Severity: Minor
          Found in lib/queries/source/filter.rb by fixme

          TODO found
          Open

                # TODO: move tc citations concern
          Severity: Minor
          Found in lib/queries/source/filter.rb by fixme

          TODO found
          Open

                # TODO: move to citation concern
          Severity: Minor
          Found in lib/queries/source/filter.rb by fixme

          TODO found
          Open

                # TODO: move to a concern
          Severity: Minor
          Found in lib/queries/source/filter.rb by fixme

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

                def author_ids_facet
                  return nil if author_ids.empty?
                  o = table
                  r = ::Role.arel_table
          
          
          Severity: Major
          Found in lib/queries/source/filter.rb and 2 other locations - About 3 hrs to fix
          lib/queries/collecting_event/filter.rb on lines 140..165
          lib/queries/taxon_name/filter.rb on lines 325..350

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

          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

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

                def ancestor_taxon_names_join
                  h = Arel::Table.new(:taxon_name_hierarchies)
                  c = ::Citation.arel_table
                  t = ::TaxonName.arel_table
          
          
          Severity: Major
          Found in lib/queries/source/filter.rb and 1 other location - About 1 hr to fix
          lib/queries/source/filter.rb on lines 427..435

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

          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

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

                def ancestor_otus_join
                  h = Arel::Table.new(:taxon_name_hierarchies)
                  c = ::Citation.arel_table
                  o = ::Otu.arel_table
          
          
          Severity: Major
          Found in lib/queries/source/filter.rb and 1 other location - About 1 hr to fix
          lib/queries/source/filter.rb on lines 439..447

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

          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

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

                def fragment_year_matches
                  if fragments.any?
                    s = table[:cached].matches_any(fragments)
                    s = s.and(table[:year].eq_any(years)) if !years.empty?
                    s
          Severity: Minor
          Found in lib/queries/source/filter.rb and 1 other location - About 15 mins to fix
          lib/queries/source/autocomplete.rb on lines 156..162

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

          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

          Prefer single-quoted strings when you don't need string interpolation or special symbols.
          Open

                  a = o.alias("a_") 
          Severity: Minor
          Found in lib/queries/source/filter.rb by rubocop

          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"

          Prefer single-quoted strings when you don't need string interpolation or special symbols.
          Open

                      .where("project_sources.project_id != ? OR project_sources.id IS NULL", Current.project_id)
          Severity: Minor
          Found in lib/queries/source/filter.rb by rubocop

          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"

          There are no issues that match your filters.

          Category
          Status