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