Eric-Guo/wechat

View on GitHub
lib/wechat/responder.rb

Summary

Maintainability
C
1 day
Test Coverage
A
95%

File responder.rb has 253 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require 'English'
require 'rexml/document'

module Wechat
  module Responder
Severity: Minor
Found in lib/wechat/responder.rb - About 2 hrs to fix

    Method on has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
    Open

          def on(message_type, with: nil, respond: nil, &block)
            config = respond.nil? ? {} : { respond: respond }
            config[:proc] = block if block_given?
    
            if with.present?
    Severity: Minor
    Found in lib/wechat/responder.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 responder_for has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
    Open

          def responder_for(message)
            message_type = message[:MsgType].to_sym
            responders = user_defined_responders(message_type)
    
            case message_type
    Severity: Minor
    Found in lib/wechat/responder.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 match_responders has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

          def match_responders(responders, value)
            matched = responders.each_with_object({}) do |responder, memo|
              condition = responder[:with]
    
              if condition.nil?
    Severity: Minor
    Found in lib/wechat/responder.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 on has 33 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

          def on(message_type, with: nil, respond: nil, &block)
            config = respond.nil? ? {} : { respond: respond }
            config[:proc] = block if block_given?
    
            if with.present?
    Severity: Minor
    Found in lib/wechat/responder.rb - About 1 hr to fix

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

            def responder_for(message)
              message_type = message[:MsgType].to_sym
              responders = user_defined_responders(message_type)
      
              case message_type
      Severity: Minor
      Found in lib/wechat/responder.rb - About 1 hr to fix

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

            def run_responder(request)
              self.class.responder_for(request) do |responder, *args|
                responder ||= self.class.user_defined_responders(:fallback).first
        
                next if responder.nil?
        Severity: Minor
        Found in lib/wechat/responder.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 post_body has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
        Open

            def post_body
              if request.media_type == 'application/json'
                data_hash = params
        
                if @we_encrypt_mode && data['Encrypt'].present?
        Severity: Minor
        Found in lib/wechat/responder.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 verify_signature has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
        Open

            def verify_signature
              if @we_encrypt_mode
                signature = params[:signature] || params[:msg_signature]
                msg_encrypt = params[:echostr] || request_encrypt_content
              else
        Severity: Minor
        Found in lib/wechat/responder.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

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

              def known_scan_with_match_responders(responders, message)
                matched = responders.each_with_object({}) do |responder, memo|
                  if %w[scan subscribe].freeze.include?(message[:Event]) && message[:EventKey] == responder[:with]
                    memo[:scaned] ||= [responder, message[:Ticket]]
                  elsif %w[scancode_push scancode_waitmsg].freeze.include?(message[:Event]) && message[:EventKey] == responder[:with]
        Severity: Minor
        Found in lib/wechat/responder.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 match? instead of =~ when MatchData is not used.
        Open

                    memo[:scoped] ||= [responder] + $LAST_MATCH_INFO.captures if value =~ condition
        Severity: Minor
        Found in lib/wechat/responder.rb by rubocop

        In Ruby 2.4, String#match?, Regexp#match? and Symbol#match? have been added. The methods are faster than match. Because the methods avoid creating a MatchData object or saving backref. So, when MatchData is not used, use match? instead of match.

        Example:

        # bad
        def foo
          if x =~ /re/
            do_something
          end
        end
        
        # bad
        def foo
          if x.match(/re/)
            do_something
          end
        end
        
        # bad
        def foo
          if /re/ === x
            do_something
          end
        end
        
        # good
        def foo
          if x.match?(/re/)
            do_something
          end
        end
        
        # good
        def foo
          if x =~ /re/
            do_something(Regexp.last_match)
          end
        end
        
        # good
        def foo
          if x.match(/re/)
            do_something($~)
          end
        end
        
        # good
        def foo
          if /re/ === x
            do_something($~)
          end
        end

        There are no issues that match your filters.

        Category
        Status