GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Address.format()   F
last analyzed

Complexity

Conditions 11

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
c 2
b 0
f 0
dl 0
loc 23
rs 3.4945

How to fix   Complexity   

Complexity

Complex classes like Address.format() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
module MailAddress
2
3
  class Address
4
5
    attr_accessor :phrase, :address, :original
6
7
    def initialize(phrase, address, original)
8
      @address = address
9
      @phrase = phrase
10
      @original = original
11
12
      ###  validate afterwords
13
      # parse failed or invalid address
14
      unless (@address && @original.include?(@address)) && Address._check_address_with_regex(@address)
15
        @address = nil
16
      end
17
18
      # invalid address
19
      @phrase = original if @address.nil?
20
    end
21
22
    ATEXT = '[\-\w !#$%&\'*+/=?^`{|}~]'
23
24
    def format(enquote = false)
25
      addr = []
26
      return @original.gsub(/[;,]/, '') if @address.nil?
27
28
      email_address = enquote ? quoted_address : @address
29
30
      if [email protected]? && @phrase.length > 0 then
31
        # if @phrase.match(/\A\(/) && @phrase.match(/\)\z/)
32
        #   addr.push(email_address) if [email protected]? && @address.length > 0
33
        #   addr.push(@phrase)
34
        # else
35
          addr.push(
36
            @phrase.match(/^(?:\s*#{ATEXT}\s*)+$/) ? @phrase
37
            : @phrase.match(/(?<!\\)"/)            ? @phrase
38
            : %Q("#{@phrase}")
39
            )
40
          addr.push "<#{email_address}>" if [email protected]? && @address.length > 0
41
        # end
42
      elsif [email protected]? && @address.length > 0 then
43
        addr.push(email_address)
44
      end
45
      addr.join(' ')
46
    end
47
48
    def name
49
      phrase = @phrase.dup
50
      name   = Address._extract_name(phrase)
51
      name.length > 0 ? name : nil
52
    end
53
54
    def host
55
      addr = @address || '';
56
      i = addr.rindex('@')
57
      i.nil? ? nil : addr[i + 1 .. -1]
58
    end
59
60
    def user
61
      addr = @address || '';
62
      i = addr.rindex('@')
63
      i.nil? ? addr : addr[0 ... i]
64
    end
65
66
    def quoted_address
67
      if @address
68
        local_part = self.user.gsub(/(\A"|"\z)/, '')
69
        if local_part.include?('..') || local_part.end_with?('.')
70
          return "\"#{local_part}\"@#{self.host}"
71
        end
72
      end
73
      @address
74
    end
75
76
    private
77
78
    # given a comment, attempt to extract a person's name
79
    def self._extract_name(name)
80
      # This function can be called as method as well
81
      return '' if name.nil? || name == ''
82
83
      # Using encodings, too hard. See Mail::Message::Field::Full.
84
      return '' if name.match(/\=\?.*?\?\=/)
85
86
      # trim whitespace
87
      name.sub!(/^\s+/, '')
88
      name.sub!(/\s+$/, '')
89
      name.sub!(/\s+/, ' ')
90
91
      # Disregard numeric names (e.g. [email protected])
92
      return "" if name.match(/^[\d ]+$/)
93
94
      name.sub!(/^\((.*)\)$/, '\1') # remove outermost parenthesis
95
      name.sub!(/^"(.*)"$/, '\1')   # remove outer quotation marks
96
      name.gsub!(/\\/, '')          # remove all escapes
97
      name.sub!(/^"(.*)"$/, '\1')   # remove internal quotation marks
98
99
      # some cleanup
100
      name.gsub!(/\[[^\]]*\]/, '')
101
      name.gsub!(/(^[\s'"]+|[\s'"]+$)/, '')
102
      name.gsub!(/\s{2,}/, ' ')
103
      name
104
    end
105
106
    def self._check_address_with_regex(email_address)
107
      return nil unless email_address
108
      # check if the address is compliant with RFC2822
109
      # regex check (see  http://blog.livedoor.jp/dankogai/archives/51189905.html)
110
#      email_address.match(/^(?:(?:(?:(?:[a-zA-Z0-9_!#\$\%&'*+\/=?\^`{}~|\-]+)(?:\.(?:[a-zA-Z0-9_!#\$\%&'*+\/=?\^`{}~|\-]+))*)|(?:"(?:\\[^\r\n]|[^\\"])*")))\@(?:(?:(?:(?:[a-zA-Z0-9_!#\$\%&'*+\/=?\^`{}~|\-]+)(?:\.(?:[a-zA-Z0-9_!#\$\%&'*+\/=?\^`{}~|\-]+))*)|(?:\[(?:\\\S|[\x21-\x5a\x5e-\x7e])*\])))$/)
111
112
      # permit Docomo/Au address
113
      EMAIL_ADDRESS_ =~ email_address
114
    end
115
116
  end
117
118
end
119