zammad/zammad

View on GitHub
app/models/channel/email_parser.rb

Summary

Maintainability
F
1 wk
Test Coverage

File email_parser.rb has 762 lines of code (exceeds 250 allowed). Consider refactoring.
Open

class Channel::EmailParser
  include Channel::EmailHelper

  PROCESS_TIME_MAX = 180
  EMAIL_REGEX = %r{.+@.+}
Severity: Major
Found in app/models/channel/email_parser.rb - About 1 day to fix

    Method get_attachments has a Cognitive Complexity of 62 (exceeds 5 allowed). Consider refactoring.
    Open

      def get_attachments(file, attachments, mail)
        return file.parts.map { |p| get_attachments(p, attachments, mail) } if file.parts.any?
        return [] if [mail.text_part&.body&.encoded, mail.html_part&.body&.encoded].include?(file.body.encoded)
        return [] if file.content_type&.start_with?('text/plain') && !file.attachment?
    
    
    Severity: Minor
    Found in app/models/channel/email_parser.rb - About 1 day 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 _process has a Cognitive Complexity of 59 (exceeds 5 allowed). Consider refactoring.
    Open

      def _process(channel, msg)
    
        # parse email
        mail = parse(msg)
    
    
    Severity: Minor
    Found in app/models/channel/email_parser.rb - About 1 day 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 set_attributes_by_x_headers has a Cognitive Complexity of 47 (exceeds 5 allowed). Consider refactoring.
    Open

      def set_attributes_by_x_headers(item_object, header_name, mail, suffix = false)
    
        # loop all x-zammad-header-* headers
        item_object.attributes.each_key do |key|
    
    
    Severity: Minor
    Found in app/models/channel/email_parser.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

    Method _process has 146 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

      def _process(channel, msg)
    
        # parse email
        mail = parse(msg)
    
    
    Severity: Major
    Found in app/models/channel/email_parser.rb - About 5 hrs to fix

      Method get_attachments has 141 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def get_attachments(file, attachments, mail)
          return file.parts.map { |p| get_attachments(p, attachments, mail) } if file.parts.any?
          return [] if [mail.text_part&.body&.encoded, mail.html_part&.body&.encoded].include?(file.body.encoded)
          return [] if file.content_type&.start_with?('text/plain') && !file.attachment?
      
      
      Severity: Major
      Found in app/models/channel/email_parser.rb - About 5 hrs to fix

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

        class Channel::EmailParser
          include Channel::EmailHelper
        
          PROCESS_TIME_MAX = 180
          EMAIL_REGEX = %r{.+@.+}
        Severity: Minor
        Found in app/models/channel/email_parser.rb - About 3 hrs to fix

          Method message_header_hash has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
          Open

            def message_header_hash(mail)
              imported_fields = mail.header.fields.to_h do |f|
                begin
                  value = if f.value.match?(ISO2022JP_REGEXP)
                            value = header_field_unpack_japanese(f)
          Severity: Minor
          Found in app/models/channel/email_parser.rb - About 2 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

          Method message_body_hash has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
          Open

            def message_body_hash(mail)
              if mail.html_part&.body.present?
                content_type = mail.html_part.mime_type || 'text/plain'
                body = body_text(mail.html_part, strict_html: true)
              elsif mail.text_part.present? && mail.all_parts.any? { |elem| elem.inline? && elem.content_type&.start_with?('image') }
          Severity: Minor
          Found in app/models/channel/email_parser.rb - About 2 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

          Method set_attributes_by_x_headers has 45 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def set_attributes_by_x_headers(item_object, header_name, mail, suffix = false)
          
              # loop all x-zammad-header-* headers
              item_object.attributes.each_key do |key|
          
          
          Severity: Minor
          Found in app/models/channel/email_parser.rb - About 1 hr to fix

            Method sender_attributes has 40 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              def self.sender_attributes(from)
                if from.is_a?(ActiveSupport::HashWithIndifferentAccess)
                  from = SENDER_FIELDS.filter_map { |f| from[f] }
                                      .map(&:to_utf8).compact_blank
                                      .partition { |address| address.match?(EMAIL_REGEX) }
            Severity: Minor
            Found in app/models/channel/email_parser.rb - About 1 hr to fix

              Method message_header_hash has 36 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                def message_header_hash(mail)
                  imported_fields = mail.header.fields.to_h do |f|
                    begin
                      value = if f.value.match?(ISO2022JP_REGEXP)
                                value = header_field_unpack_japanese(f)
              Severity: Minor
              Found in app/models/channel/email_parser.rb - About 1 hr to fix

                Method message_body_hash has 35 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                  def message_body_hash(mail)
                    if mail.html_part&.body.present?
                      content_type = mail.html_part.mime_type || 'text/plain'
                      body = body_text(mail.html_part, strict_html: true)
                    elsif mail.text_part.present? && mail.all_parts.any? { |elem| elem.inline? && elem.content_type&.start_with?('image') }
                Severity: Minor
                Found in app/models/channel/email_parser.rb - About 1 hr to fix

                  Method get_nonplaintext_body_as_attachment has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def get_nonplaintext_body_as_attachment(mail)
                      if !(mail.html_part&.body.present? || (!mail.multipart? && mail.mime_type.present? && mail.mime_type != 'text/plain'))
                        return
                      end
                  
                  
                  Severity: Minor
                  Found in app/models/channel/email_parser.rb - About 55 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 check_attributes_by_x_headers has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def self.check_attributes_by_x_headers(header_name, value)
                      class_name = nil
                      attribute = nil
                      # skip check attributes if it is tags
                      return true if header_name == 'x-zammad-ticket-tags'
                  Severity: Minor
                  Found in app/models/channel/email_parser.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 sender_attributes has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def self.sender_attributes(from)
                      if from.is_a?(ActiveSupport::HashWithIndifferentAccess)
                        from = SENDER_FIELDS.filter_map { |f| from[f] }
                                            .map(&:to_utf8).compact_blank
                                            .partition { |address| address.match?(EMAIL_REGEX) }
                  Severity: Minor
                  Found in app/models/channel/email_parser.rb - About 35 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 reprocess_failed_articles has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def self.reprocess_failed_articles
                      articles = Ticket::Article.where(body: ::HtmlSanitizer::UNPROCESSABLE_HTML_MSG)
                      articles.reorder(id: :desc).as_batches do |article|
                        if !article.as_raw&.content
                          puts "No raw content for article id #{article.id}! Please verify manually via command: Ticket::Article.find(#{article.id}).as_raw" # rubocop:disable Rails/Output
                  Severity: Minor
                  Found in app/models/channel/email_parser.rb - About 35 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

                  Avoid too many return statements within this method.
                  Open

                      return false if !class_instance.association_id_validation(attribute, value)
                  Severity: Major
                  Found in app/models/channel/email_parser.rb - About 30 mins to fix

                    Avoid too many return statements within this method.
                    Open

                        return if !class_object
                    Severity: Major
                    Found in app/models/channel/email_parser.rb - About 30 mins to fix

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

                        def gracefully_get_attachments(part, attachments, mail)
                          get_attachments(part, attachments, mail).flatten.compact
                        rescue => e # Protect process to work with spam emails (see test/fixtures/mail15.box)
                          raise e if (fail_count ||= 0).positive?
                      
                      
                      Severity: Minor
                      Found in app/models/channel/email_parser.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

                      There are no issues that match your filters.

                      Category
                      Status