mongodb/mongoid

View on GitHub

Showing 236 of 236 total issues

Method destroy has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
Open

      def destroy(options = nil)
        raise Errors::ReadonlyDocument.new(self.class) if readonly?
        self.flagged_for_destroy = true
        result = run_callbacks(:commit, skip_if: -> { in_transaction? }) do
          run_callbacks(:destroy) do
Severity: Minor
Found in lib/mongoid/persistable/destroyable.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 with_session has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
Open

        def with_session(options = {})
          if Threaded.get_session(client: persistence_context.client)
            raise Mongoid::Errors::InvalidSessionNesting.new
          end
          session = persistence_context.client.start_session(options)
Severity: Minor
Found in lib/mongoid/clients/sessions.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 _handle_callbacks_after_instantiation has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
Open

    def _handle_callbacks_after_instantiation(execute_callbacks)
      if execute_callbacks
        apply_defaults
        yield self if block_given?
        run_callbacks(:find) unless _find_callbacks.empty?
Severity: Minor
Found in lib/mongoid/document.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 extract_attribute has 33 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    module_function def extract_attribute(document, key)
      if document.respond_to?(:as_attributes, true)
        # If a document has hash fields, as_attributes would keep those fields
        # as Hash instances which do not offer indifferent access.
        # Convert to BSON::Document to get indifferent access on hash fields.
Severity: Minor
Found in lib/mongoid/matcher.rb - About 1 hr to fix

    Method mul has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

          def mul(factors)
            prepare_atomic_operation do |ops|
              process_atomic_operations(factors) do |field, value|
                factor = value.is_a?(BigDecimal) ? value.to_f : value
                current = attributes[field]
    Severity: Minor
    Found in lib/mongoid/persistable/multipliable.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 demongoize has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

            def demongoize(object)
              return if object.blank?
              time = if object.acts_like?(:time)
                Mongoid::Config.use_utc? ? object : object.getlocal
              elsif object.acts_like?(:date)
    Severity: Minor
    Found in lib/mongoid/extensions/time.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 prepare_arguments has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

          def prepare_arguments(kms_provider_name, client_name)
            client = (client_name || 'default').to_s
            client_options = Mongoid.clients[client]
            unless client_options.is_a?(Hash)
              raise Errors::NoClientConfig.new(client)
    Severity: Minor
    Found in lib/mongoid/tasks/encryption.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 any_of has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

            def any_of(*criteria)
              criteria = _mongoid_flatten_arrays(criteria)
              case criteria.length
              when 0
                clone
    Severity: Minor
    Found in lib/mongoid/criteria/queryable/selectable.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 inc has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

          def inc(increments)
            prepare_atomic_operation do |ops|
              process_atomic_operations(increments) do |field, value|
                increment = value.is_a?(BigDecimal) ? value.to_f : value
                current = attributes[field]
    Severity: Minor
    Found in lib/mongoid/persistable/incrementable.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 create! has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

            def create!(attributes = nil, &block)
              _creating do
                if attributes.is_a?(::Array)
                  attributes.map { |attrs| create!(attrs, &block) }
                else
    Severity: Minor
    Found in lib/mongoid/persistable/creatable.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 add_atomic_changes has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

          def add_atomic_changes(document, name, key, mods, new_elements, old_elements)
            old = (old_elements || [])
            new = (new_elements || [])
            if new.length > old.length
              if new.first(old.length) == old
    Severity: Minor
    Found in lib/mongoid/fields/foreign_key.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

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

      module Persistable
    
        # Defines behavior for $inc operations.
        module Incrementable
          extend ActiveSupport::Concern
    Severity: Major
    Found in lib/mongoid/persistable/incrementable.rb and 1 other location - About 1 hr to fix
    lib/mongoid/persistable/multipliable.rb on lines 5..31

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

    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

      module Persistable
    
        # Defines behavior for $mul operations.
        module Multipliable
          extend ActiveSupport::Concern
    Severity: Major
    Found in lib/mongoid/persistable/multipliable.rb and 1 other location - About 1 hr to fix
    lib/mongoid/persistable/incrementable.rb on lines 5..31

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

    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

    Method write_attribute has 30 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        def write_attribute(name, value)
          validate_writable_field_name!(name.to_s)
    
          field_name = database_field_name(name)
    
    
    Severity: Minor
    Found in lib/mongoid/attributes.rb - About 1 hr to fix

      Method create_collection has 29 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

            def create_collection(force: false)
              if collection_name.empty?
                # This is most probably an anonymous class, we ignore them.
                return
              end
      Severity: Minor
      Found in lib/mongoid/collection_configurable.rb - About 1 hr to fix

        Method create_collection has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

              def create_collection(force: false)
                if collection_name.empty?
                  # This is most probably an anonymous class, we ignore them.
                  return
                end
        Severity: Minor
        Found in lib/mongoid/collection_configurable.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 update_nested_relation has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

                def update_nested_relation(parent, id, attrs)
                  first = existing.first
                  converted = first ? convert_id(first.class, id) : id
        
                  if existing.where(_id: converted).exists?
        Severity: Minor
        Found in lib/mongoid/association/nested/many.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 batch_remove has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

                def batch_remove(docs, method = :delete)
                  # If the _id is nil, we cannot use $pull and delete by searching for
                  # the id. Therefore we have to use pullAll with the documents'
                  # attributes.
                  removals = pre_process_batch_remove(docs, method)
        Severity: Minor
        Found in lib/mongoid/association/embedded/batchable.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 bind_one has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

                  def bind_one
                    binding do
                      check_polymorphic_inverses!(_target)
                      bind_foreign_key(_base, record_id(_target))
                      bind_polymorphic_inverse_type(_base, _target.class.name)
        Severity: Minor
        Found in lib/mongoid/association/referenced/belongs_to/binding.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 define_setter! has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

              def self.define_setter!(association)
                name = association.name
                association.inverse_class.tap do |klass|
                  klass.re_define_method("#{name}=") do |object|
                    without_autobuild do
        Severity: Minor
        Found in lib/mongoid/association/accessors.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

        Severity
        Category
        Status
        Source
        Language