ece517-p3/expertiza

View on GitHub
app/models/questionnaire_node.rb

Summary

Maintainability
A
3 hrs
Test Coverage

Mass assignment is not restricted using attr_accessible
Open

class QuestionnaireNode < Node
Severity: Critical
Found in app/models/questionnaire_node.rb by brakeman

This warning comes up if a model does not limit what attributes can be set through mass assignment.

In particular, this check looks for attr_accessible inside model definitions. If it is not found, this warning will be issued.

Brakeman also warns on use of attr_protected - especially since it was found to be vulnerable to bypass. Warnings for mass assignment on models using attr_protected will be reported, but at a lower confidence level.

Note that disabling mass assignment globally will suppress these warnings.

Assignment Branch Condition size for get is too high. [28.72/15]
Open

  def self.get(sortvar = nil, sortorder = nil, user_id = nil, show = nil, parent_id = nil, _search = nil)
    conditions = if show
                   if User.find(user_id).role.name != "Teaching Assistant"
                     'questionnaires.instructor_id = ?'
                   else
Severity: Minor
Found in app/models/questionnaire_node.rb by rubocop

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

Perceived complexity for get is too high. [15/7]
Open

  def self.get(sortvar = nil, sortorder = nil, user_id = nil, show = nil, parent_id = nil, _search = nil)
    conditions = if show
                   if User.find(user_id).role.name != "Teaching Assistant"
                     'questionnaires.instructor_id = ?'
                   else
Severity: Minor
Found in app/models/questionnaire_node.rb by rubocop

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

Cyclomatic complexity for get is too high. [11/6]
Open

  def self.get(sortvar = nil, sortorder = nil, user_id = nil, show = nil, parent_id = nil, _search = nil)
    conditions = if show
                   if User.find(user_id).role.name != "Teaching Assistant"
                     'questionnaires.instructor_id = ?'
                   else
Severity: Minor
Found in app/models/questionnaire_node.rb by rubocop

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

  def self.get(sortvar = nil, sortorder = nil, user_id = nil, show = nil, parent_id = nil, _search = nil)
    conditions = if show
                   if User.find(user_id).role.name != "Teaching Assistant"
                     'questionnaires.instructor_id = ?'
                   else
Severity: Minor
Found in app/models/questionnaire_node.rb - About 1 hr 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 get has 28 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  def self.get(sortvar = nil, sortorder = nil, user_id = nil, show = nil, parent_id = nil, _search = nil)
    conditions = if show
                   if User.find(user_id).role.name != "Teaching Assistant"
                     'questionnaires.instructor_id = ?'
                   else
Severity: Minor
Found in app/models/questionnaire_node.rb - About 1 hr to fix

    Method get has 6 arguments (exceeds 4 allowed). Consider refactoring.
    Open

      def self.get(sortvar = nil, sortorder = nil, user_id = nil, show = nil, parent_id = nil, _search = nil)
    Severity: Minor
    Found in app/models/questionnaire_node.rb - About 45 mins to fix

      Specify an :inverse_of option.
      Open

        belongs_to :questionnaire, class_name: "Questionnaire", foreign_key: "node_object_id"
      Severity: Minor
      Found in app/models/questionnaire_node.rb by rubocop

      This cop looks for has(one|many) and belongsto associations where ActiveRecord can't automatically determine the inverse association because of a scope or the options used. This can result in unnecessary queries in some circumstances. :inverse_of must be manually specified for associations to work in both ways, or set to false to opt-out.

      Example:

      # good
      class Blog < ApplicationRecord
        has_many :posts
      end
      
      class Post < ApplicationRecord
        belongs_to :blog
      end

      Example:

      # bad
      class Blog < ApplicationRecord
        has_many :posts, -> { order(published_at: :desc) }
      end
      
      class Post < ApplicationRecord
        belongs_to :blog
      end
      
      # good
      class Blog < ApplicationRecord
        has_many(:posts,
          -> { order(published_at: :desc) },
          inverse_of: :blog
        )
      end
      
      class Post < ApplicationRecord
        belongs_to :blog
      end
      
      # good
      class Blog < ApplicationRecord
        with_options inverse_of: :blog do
          has_many :posts, -> { order(published_at: :desc) }
        end
      end
      
      class Post < ApplicationRecord
        belongs_to :blog
      end

      Example:

      # bad
      class Picture < ApplicationRecord
        belongs_to :imageable, polymorphic: true
      end
      
      class Employee < ApplicationRecord
        has_many :pictures, as: :imageable
      end
      
      class Product < ApplicationRecord
        has_many :pictures, as: :imageable
      end
      
      # good
      class Picture < ApplicationRecord
        belongs_to :imageable, polymorphic: true
      end
      
      class Employee < ApplicationRecord
        has_many :pictures, as: :imageable, inverse_of: :imageable
      end
      
      class Product < ApplicationRecord
        has_many :pictures, as: :imageable, inverse_of: :imageable
      end

      Example:

      # bad
      # However, RuboCop can not detect this pattern...
      class Physician < ApplicationRecord
        has_many :appointments
        has_many :patients, through: :appointments
      end
      
      class Appointment < ApplicationRecord
        belongs_to :physician
        belongs_to :patient
      end
      
      class Patient < ApplicationRecord
        has_many :appointments
        has_many :physicians, through: :appointments
      end
      
      # good
      class Physician < ApplicationRecord
        has_many :appointments
        has_many :patients, through: :appointments
      end
      
      class Appointment < ApplicationRecord
        belongs_to :physician, inverse_of: :appointments
        belongs_to :patient, inverse_of: :appointments
      end
      
      class Patient < ApplicationRecord
        has_many :appointments
        has_many :physicians, through: :appointments
      end

      @see http://guides.rubyonrails.org/association_basics.html#bi-directional-associations @see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses

      Specify an :inverse_of option.
      Open

        belongs_to :node_object, class_name: "Questionnaire", foreign_key: "node_object_id"
      Severity: Minor
      Found in app/models/questionnaire_node.rb by rubocop

      This cop looks for has(one|many) and belongsto associations where ActiveRecord can't automatically determine the inverse association because of a scope or the options used. This can result in unnecessary queries in some circumstances. :inverse_of must be manually specified for associations to work in both ways, or set to false to opt-out.

      Example:

      # good
      class Blog < ApplicationRecord
        has_many :posts
      end
      
      class Post < ApplicationRecord
        belongs_to :blog
      end

      Example:

      # bad
      class Blog < ApplicationRecord
        has_many :posts, -> { order(published_at: :desc) }
      end
      
      class Post < ApplicationRecord
        belongs_to :blog
      end
      
      # good
      class Blog < ApplicationRecord
        has_many(:posts,
          -> { order(published_at: :desc) },
          inverse_of: :blog
        )
      end
      
      class Post < ApplicationRecord
        belongs_to :blog
      end
      
      # good
      class Blog < ApplicationRecord
        with_options inverse_of: :blog do
          has_many :posts, -> { order(published_at: :desc) }
        end
      end
      
      class Post < ApplicationRecord
        belongs_to :blog
      end

      Example:

      # bad
      class Picture < ApplicationRecord
        belongs_to :imageable, polymorphic: true
      end
      
      class Employee < ApplicationRecord
        has_many :pictures, as: :imageable
      end
      
      class Product < ApplicationRecord
        has_many :pictures, as: :imageable
      end
      
      # good
      class Picture < ApplicationRecord
        belongs_to :imageable, polymorphic: true
      end
      
      class Employee < ApplicationRecord
        has_many :pictures, as: :imageable, inverse_of: :imageable
      end
      
      class Product < ApplicationRecord
        has_many :pictures, as: :imageable, inverse_of: :imageable
      end

      Example:

      # bad
      # However, RuboCop can not detect this pattern...
      class Physician < ApplicationRecord
        has_many :appointments
        has_many :patients, through: :appointments
      end
      
      class Appointment < ApplicationRecord
        belongs_to :physician
        belongs_to :patient
      end
      
      class Patient < ApplicationRecord
        has_many :appointments
        has_many :physicians, through: :appointments
      end
      
      # good
      class Physician < ApplicationRecord
        has_many :appointments
        has_many :patients, through: :appointments
      end
      
      class Appointment < ApplicationRecord
        belongs_to :physician, inverse_of: :appointments
        belongs_to :patient, inverse_of: :appointments
      end
      
      class Patient < ApplicationRecord
        has_many :appointments
        has_many :physicians, through: :appointments
      end

      @see http://guides.rubyonrails.org/association_basics.html#bi-directional-associations @see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses

      Do not prefix reader method names with get_.
      Open

        def get_creation_date
      Severity: Minor
      Found in app/models/questionnaire_node.rb by rubocop

      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

      Do not prefix reader method names with get_.
      Open

        def get_private
      Severity: Minor
      Found in app/models/questionnaire_node.rb by rubocop

      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

      Do not prefix reader method names with get_.
      Open

        def get_modified_date
      Severity: Minor
      Found in app/models/questionnaire_node.rb by rubocop

      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

      Rename is_leaf to leaf?.
      Open

        def is_leaf
      Severity: Minor
      Found in app/models/questionnaire_node.rb by rubocop

      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

      Do not prefix reader method names with get_.
      Open

        def get_name
      Severity: Minor
      Found in app/models/questionnaire_node.rb by rubocop

      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

      Convert if nested inside else to elsif.
      Open

                         if User.find(user_id).role.name != "Teaching Assistant"
      Severity: Minor
      Found in app/models/questionnaire_node.rb by rubocop

      If the else branch of a conditional consists solely of an if node, it can be combined with the else to become an elsif. This helps to keep the nesting level from getting too deep.

      Example:

      # bad
      if condition_a
        action_a
      else
        if condition_b
          action_b
        else
          action_c
        end
      end
      
      # good
      if condition_a
        action_a
      elsif condition_b
        action_b
      else
        action_c
      end

      Use a guard clause instead of wrapping the code inside a conditional expression.
      Open

          if Questionnaire.column_names.include? sortvar and %w[ASC DESC asc desc].include? sortorder
      Severity: Minor
      Found in app/models/questionnaire_node.rb by rubocop

      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

      end at 15, 32 is not aligned with if at 11, 19.
      Open

                                      end
      Severity: Minor
      Found in app/models/questionnaire_node.rb by rubocop

      This cop checks whether the end keywords are aligned properly.

      Three modes are supported through the EnforcedStyleAlignWith configuration parameter:

      If it's set to keyword (which is the default), the end shall be aligned with the start of the keyword (if, class, etc.).

      If it's set to variable the end shall be aligned with the left-hand-side of the variable assignment, if there is one.

      If it's set to start_of_line, the end shall be aligned with the start of the line where the matching keyword appears.

      Example: EnforcedStyleAlignWith: keyword (default)

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
                 end

      Example: EnforcedStyleAlignWith: variable

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
      end

      Example: EnforcedStyleAlignWith: startofline

      # bad
      
      variable = if true
          end
      
      # good
      
      puts(if true
      end)

      end at 21, 32 is not aligned with if at 17, 19.
      Open

                                      end
      Severity: Minor
      Found in app/models/questionnaire_node.rb by rubocop

      This cop checks whether the end keywords are aligned properly.

      Three modes are supported through the EnforcedStyleAlignWith configuration parameter:

      If it's set to keyword (which is the default), the end shall be aligned with the start of the keyword (if, class, etc.).

      If it's set to variable the end shall be aligned with the left-hand-side of the variable assignment, if there is one.

      If it's set to start_of_line, the end shall be aligned with the start of the line where the matching keyword appears.

      Example: EnforcedStyleAlignWith: keyword (default)

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
                 end

      Example: EnforcedStyleAlignWith: variable

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
      end

      Example: EnforcedStyleAlignWith: startofline

      # bad
      
      variable = if true
          end
      
      # good
      
      puts(if true
      end)

      There are no issues that match your filters.

      Category
      Status