Builder/Address.pm

filter: Statementstmt 98.4% 63 / 64 Branchbran 93.7% 30 / 32 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 90.0% 9 / 10 Podpod 50.0% 2 / 4 totaltotal 90.7% 118 / 130 CC 21 SCAR 20.4
CC
21
Cov
94%
__ANON__
13.0
compare
6.1
serialize
4.0
CRAP 7.7
« Builder/Role/File.pm ↑ Summary Builder/TypeConstraints.pm »
1 # ============================================================================
2 package Mail::Builder::Address;
3 # ============================================================================
4  
5 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Mail::Builder::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 8 ▶use Email::Valid;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 our $VERSION = $Mail::Builder::VERSION;
15  
16 8 has 'email' => (
17 is => 'rw',
18 isa => 'Mail::Builder::Type::EmailAddress',
19 required => 1,
20 );
21  
22 8 has 'name' => (
23 is => 'rw',
24 isa => 'Str',
25 predicate => 'has_name',
26 );
27  
28 8 has 'comment' => (
29 is => 'rw',
30 isa => 'Str',
31 predicate => 'has_comment',
32 );
33  
34 =encoding utf8
35  
36 =head1 NAME
37  
38 Mail::Builder::Address - Module for handling e-mail addresses
39  
40 =head1 SYNOPSIS
41  
42 use Mail::Builder;
43
44 my $mail = Mail::Builder::Address->new('mightypirate@meele-island.mq','Gaybrush Thweedwood');
45 # Now correct type in the display name and address
46 $mail->name('Guybrush Threepwood');
47 $mail->email('verymightypirate@meele-island.mq');
48 $mail->comment('Mighty Pirate (TM)');
49
50 # Serialize
51 print $mail->serialize;
52
53 # Use the address as a recipient for Mail::Builder object
54 $mb->to($mail); # Removes all other recipients
55 OR
56 $mb->to->add($mail); # Adds one more recipient (without removing the existing ones)
57  
58 =head1 DESCRIPTION
59  
60 This is a simple module for handling e-mail addresses. It can store the address
61 and an optional display name.
62  
63 =head1 METHODS
64  
65 =head2 Constructor
66  
67 =head3 new
68  
69 Mail::Builder::Address->new(EMAIL[,DISPLAY NAME[,COMMENT]]);
70 OR
71 Mail::Builder::Address->new({
72 email => EMAIL,
73 [ name => DISPLAY NAME, ]
74 [ comment => COMMENT, ]
75 })
76 OR
77 my $email = Email::Address->parse(...);
78 Mail::Builder::Address->new($email);
79  
80 Simple constructor
81  
82 =cut
83  
84  
85 around BUILDARGS => sub {
86 38 ▶ my $orig = shift;
Subroutine __ANON__: called CC=13 SCAR=25.7
87 38 my $class = shift;
88 38 my @args = @_;
89
90 38 my $args_length = scalar @args;
91 38 my %params;
92
93 38 ▶ if ($args_length == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($args_length == 1) { } 23 15 38
elsif ($args_length == 2 and $args[0] ne 'email') { } 7 8 15
elsif ($args_length == 3) { } 1 7 8
94 23 ▶ if (blessed $args[0] && $args[0]->isa('Email::Address')) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($args[0]) and $args[0]->isa('Email::Address')) { } 3 20 23
elsif (ref $args[0] eq 'HASH') { } 5 15 20
elsif (ref $args[0] eq 'ARRAY') { } 2 13 15
Condition66%2/3
builtin::blessed($args[0]) and $args[0]->isa("Email::Address")
!ll&&!rl&&r
2003
MC/DC50%1/2
builtin::blessed($args[0]) and $args[0]->isa("Email::Address")
builtin::blessed($args[0]) $args[0]->isa("Email::Address")
Truth table66%2/3
A and B
A= builtin::blessed($args[0])B= $args[0]->isa("Email::Address")
ABresult
0X0
100
111
95 3 $params{email} = $args[0]->address;
96 3 $params{name} = $args[0]->phrase;
97 3 $params{comment} = $args[0]->comment;
98 } elsif (ref $args[0] eq 'HASH') {
99 5 return $class->$orig($args[0]);
100 } elsif (ref $args[0] eq 'ARRAY') {
101 2 $params{email} = $args[0]->[0];
102 2 $params{name} = $args[0]->[1];
103 2 $params{comment} = $args[0]->[2];
104 } else {
105 13 $params{email} = $args[0];
106 }
107 ▶ } elsif ($args_length == 2
Condition100%3/3
$args_length == 2 and $args[0] ne "email"
!ll&&!rl&&r
177
MC/DC100%2/2
$args_length == 2 and $args[0] ne "email"
$args_length == 2 $args[0] ne "email"
Truth table100%3/3
A and B
A= $args_length == 2B= $args[0] ne "email"
ABresult
0X0
100
111
108 && $args[0] ne 'email') {
109 7 $params{email} = $args[0];
110 7 $params{name} = $args[1];
111 } elsif ($args_length == 3) {
112 1 $params{email} = $args[0];
113 1 $params{name} = $args[1];
114 1 $params{comment} = $args[2];
115 } else {
116 7 return $class->$orig(@args);
117 }
118
119 delete $params{name}
120 26 ▶ unless defined $params{name} && $params{name} !~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"name"} and not $params{"name"} =~ /^\s*$/ 13 13 26
Condition66%2/3
defined $params{"name"} and not $params{"name"} =~ /^\s*$/
!ll&&!rl&&r
13013
MC/DC50%1/2
defined $params{"name"} and not $params{"name"} =~ /^\s*$/
defined $params{"name"} not $params{"name"} =~ /^\s*$/
Truth table66%2/3
A and not(B)
A= defined $params{"name"}B= not $params{"name"} =~ /^\s*$/
ABresult
0X0
100
111
121 delete $params{comment}
122 26 ▶ unless defined $params{comment} && $params{comment} !~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"comment"} and not $params{"comment"} =~ /^\s*$/ 25 1 26
Condition66%2/3
defined $params{"comment"} and not $params{"comment"} =~ /^\s*$/
!ll&&!rl&&r
2501
MC/DC50%1/2
defined $params{"comment"} and not $params{"comment"} =~ /^\s*$/
defined $params{"comment"} not $params{"comment"} =~ /^\s*$/
Truth table66%2/3
A and not(B)
A= defined $params{"comment"}B= not $params{"comment"} =~ /^\s*$/
ABresult
0X0
100
111
123
124 26 return $class->$orig(\%params);
125 8 };
126  
127 sub address {
128 4 ▶ my $self = shift;
Subroutine address: called CC=1 SCAR=0.0
Pod undocumented
129 4 return $self->email(@_);
130 }
131  
132 =head2 Public Methods
133  
134 =head3 serialize
135  
136 Prints the address as required for creating the e-mail header.
137  
138 =cut
139  
140 sub serialize {
141 27 ▶ my ($self) = @_;
Subroutine serialize: called CC=4 SCAR=13.9
Pod documented
142
143 27 ▶ return $self->email
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_name 13 14 27
144 unless $self->has_name;
145
146 14 my $name = $self->name;
147 14 $name =~ s/"/\\"/g;
148
149 14 my $encoded = Mail::Builder::Utils::encode_mime($name);
150 14 ▶ $encoded = qq["$encoded"]
Branch: 1 branch
BranchTrueFalseTotal
unless $encoded =~ /=\?/ 12 2 14
151 unless $encoded =~ /=\?/;
152 14 my $return = sprintf '%s <%s>',$encoded,$self->email;
153 14 ▶ $return .= ' '.Mail::Builder::Utils::encode_mime($self->comment)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_comment 1 13 14
154 if $self->has_comment;
155
156 14 return $return;
157 }
158  
159 =head3 compare
160  
161 $obj->compare(OBJECT);
162 or
163 $obj->compare(E-MAIL);
164  
165 Checks if two address objects contain the same e-mail address. Returns true
166 or false. The compare method does not check if the address names of the
167 two objects are identical.
168  
169 Instead of a C<Mail::Builder::Address> object you can also pass a
170 scalar value representing the e-mail address.
171  
172 =cut
173  
174 sub compare {
175 29 ▶ my ($self,$compare) = @_;
Subroutine compare: called CC=6 SCAR=18.0
Pod documented
176
177 29 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $compare 0 29 29
178 unless (defined $compare);
179
180 29 ▶ if (blessed($compare)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($compare)) { } 17 12 29
181 17 ▶ return 0 unless $compare->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $compare->isa(__PACKAGE__) 0 17 17
182 17 ▶ return (uc($self->email) eq uc($compare->email)) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
uc $self->email eq uc $compare->email ? : 1 16 17
183 } else {
184 12 ▶ return ( uc($compare) eq uc($self->{email}) ) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
uc $compare eq uc $self->{'email'} ? : 2 10 12
185 }
186 }
187  
188 sub empty {
189 0 ▶ croak('DEPRECATED')
Subroutine empty: not called CC=1 SCAR=6.9
Pod undocumented
190 }
191  
192 8 __PACKAGE__->meta->make_immutable;
193  
194 8 1;
195  
196 =head2 Accessors
197  
198 =head3 name
199  
200 Display name
201  
202 =head3 email
203  
204 E-mail address. Will be checked with L<Email::Valid>
205 L<Email::Valid> options may be changed by setting the appropriate values
206 in the %Mail::Builder::TypeConstraints::EMAILVALID hash.
207  
208 Eg. if you want to disable the check for valid TLDs you can set the 'tldcheck'
209 option (without dashes 'tldcheck' and not '-tldcheck'):
210  
211 $Mail::Builder::TypeConstraints::EMAILVALID{tldcheck} = 0;
212  
213 Required
214  
215 =head3 comment
216  
217 Comment
218  
219 =head1 AUTHOR
220  
221 Maroš Kollár
222 CPAN ID: MAROS
223 maros [at] k-1.com
224 http://www.k-1.com
225  
226 =cut
« Builder/Role/File.pm ↑ Summary Builder/TypeConstraints.pm »