MIME/Header.pm

filter: Statementstmt 90.6% 68 / 75 Branchbran 65.0% 13 / 20 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.7% 15 / 16 Podpod 14.2% 1 / 7 totaltotal 79.6% 98 / 123 CC 15 SCAR 14.1
CC
15
Cov
84%
header_as_obj
7.2
header_str
7.0
set_class_for_header
6.0
CRAP 4.1
« MIME/Encode.pm ↑ Summary MIME/Modifier.pm »
1 20 ▶use v5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::MIME::Header 1.954;
4 # ABSTRACT: the header of a MIME message
5  
6 20 ▶use parent 'Email::Simple::Header';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use Email::MIME::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use Encode 1.9801;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 20 our @CARP_NOT;
14  
15 20 our %header_to_class_map;
16  
17 20 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
18 20 my @address_list_headers = qw(from sender reply-to to cc bcc);
19 20 push @address_list_headers, map { "resent-$_" } @address_list_headers;
20 20 push @address_list_headers, map { "downgraded-$_" } @address_list_headers; # RFC 5504
21 20 push @address_list_headers, qw(original-from disposition-notification-to); # RFC 5703 and RFC 3798
22 20 $header_to_class_map{$_} = 'Email::MIME::Header::AddressList' foreach @address_list_headers;
23 }
24  
25 #pod =head1 DESCRIPTION
26 #pod
27 #pod This object behaves like a standard Email::Simple header, with the following
28 #pod changes:
29 #pod
30 #pod =for :list
31 #pod * the C<header> method automatically decodes encoded headers if possible
32 #pod * the C<header_as_obj> method returns an object representation of the header value
33 #pod * the C<header_raw> method returns the raw header; (read only for now)
34 #pod * stringification uses C<header_raw> rather than C<header>
35 #pod
36 #pod Note that C<header_set> does not do encoding for you, and expects an
37 #pod encoded header. Thus, C<header_set> round-trips with C<header_raw>,
38 #pod not C<header>! Be sure to properly encode your headers with
39 #pod C<Encode::encode('MIME-Header', $value)> before passing them to
40 #pod C<header_set>. And be sure to use minimal version 2.83 of Encode
41 #pod module due to L<bugs in MIME-Header|Encode::MIME::Header/BUGS>.
42 #pod
43 #pod Alternately, if you have Unicode (character) strings to set in headers, use the
44 #pod C<header_str_set> method.
45 #pod
46 #pod =cut
47  
48 723 ▶sub header_str {
Subroutine header_str: called CC=7 SCAR=19.5
Pod undocumented
49 723 my $self = shift;
50 723 my $wanta = wantarray;
51  
52 723 ▶ return unless defined $wanta; # ??
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wanta 0 723 723
53  
54 723 ▶ my @header = $wanta ? $self->header_raw(@_)
Branch: 1 branch
BranchTrueFalseTotal
$wanta ? : 157 566 723
55 : scalar $self->header_raw(@_);
56  
57 723 foreach my $header (@header) {
58 711 ▶ next unless defined $header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 228 483 711
59 483 ▶ next unless $header =~ /=\?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $header =~ /=\?/u 451 32 483
60  
61 32 _maybe_decode($_[0], \$header);
62 }
63 723 ▶ return $wanta ? @header : $header[0];
Branch: 1 branch
BranchTrueFalseTotal
$wanta ? : 157 566 723
64 }
65  
66 697 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
Pod documented
67 697 my $self = shift;
68 697 return $self->header_str(@_);
69 }
70  
71 20 ▶sub header_str_set {
Subroutine header_str_set: called CC=1 SCAR=0.0
Pod undocumented
72 20 my ($self, $name, @vals) = @_;
73  
74 my @values = map {
75 20 Email::MIME::Encode::maybe_mime_encode_header($name, $_, 'UTF-8')
76 } @vals;
77  
78 20 $self->header_raw_set($name => @values);
79 }
80  
81 1 ▶sub header_str_pairs {
Subroutine header_str_pairs: called CC=2 SCAR=6.9
Pod undocumented
82 1 my ($self) = @_;
83  
84 1 my @pairs = $self->header_pairs;
85 1 for (grep { $_ % 2 } (1 .. $#pairs)) {
86 5 _maybe_decode($pairs[$_-1], \$pairs[$_]);
87 }
88  
89 1 return @pairs;
90 }
91  
92 14 ▶sub header_as_obj {
Subroutine header_as_obj: called CC=6 SCAR=19.7
Pod undocumented
93 14 my ($self, $name, $index, $class) = @_;
94  
95 14 ▶ $class //= $self->get_class_for_header($name);
Condition33%1/3
$class //= $self->get_class_for_header($name)
l!l&&r!l&&!r
0140
MC/DC0%0/2
$class //= $self->get_class_for_header($name)
$class $self->get_class_for_header($name)
Truth table33%1/3
A //= B
A= $classB= $self->get_class_for_header($name)
ABresult
1X1
011
000
96  
97 {
98 14 local @CARP_NOT = qw(Email::MIME);
99 14 local $@;
100 14 ▶ Carp::croak("No class for header '$name' was specified") unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 14 14
101 14 ▶ Carp::croak("Cannot load package '$class' for header '$name': $@") unless eval { Module::Runtime::require_module($class) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { &Module::Runtime::require_module($class) } } 0 14 14
102 14 ▶ Carp::croak("Class '$class' does not have method 'from_mime_string'") unless $class->can('from_mime_string');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("from_mime_string") 0 14 14
103 }
104  
105 14 my @values = $self->header_raw($name, $index);
106 14 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 14 14
107 0 return map { $class->from_mime_string($_) } @values;
108 } else {
109 14 return $class->from_mime_string(@values);
110 }
111 }
112  
113 37 ▶sub _maybe_decode {
Subroutine _maybe_decode: called CC=1 SCAR=0.0
114 37 my ($name, $str_ref) = @_;
115 37 $$str_ref = Email::MIME::Encode::maybe_mime_decode_header($name, $$str_ref);
116 37 return;
117 }
118  
119 14 ▶sub get_class_for_header {
Subroutine get_class_for_header: called CC=1 SCAR=0.0
Pod undocumented
120 14 my ($self, $name) = @_;
121 14 return $header_to_class_map{lc $name};
122 }
123  
124 0 ▶sub set_class_for_header {
Subroutine set_class_for_header: not called CC=2 SCAR=17.9
Pod undocumented
125 0 my ($self, $class, $header) = @_;
126 0 $header = lc $header;
127 0 ▶ Carp::croak("Class for header '$header' is already set") if defined $header_to_class_map{$header};
Branch: 1 branch
BranchTrueFalseTotal
if defined $Email::MIME::Header::header_to_class_map{$header} 0 0 0
128 0 $header_to_class_map{$header} = $class;
129 0 return;
130 }
131  
132 20 1;
133  
134 __END__
« MIME/Encode.pm ↑ Summary MIME/Modifier.pm »