lib/Mail/AuthenticationResults/FoldableHeader.pm

filter: Statementstmt 95.7% 135 / 141 Branchbran 88.2% 30 / 34 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 92.0% 23 / 25 Podpod 100% 17 / 17 totaltotal 93.1% 216 / 232 CC 23 SCAR 20.7
CC
23
Cov
93%
as_string
18.0
force_fold_at
6.0
eol
2.0
CRAP 7.9
« blib/lib/Mail/AuthenticationResults/Header/AuthServID.pm ↑ Summary blib/lib/Mail/AuthenticationResults/Header/Entry.pm »
1 package Mail::AuthenticationResults::FoldableHeader;
2 # ABSTRACT: Class for modelling a foldable header string
3  
4 30 require 5.008;
5 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 our $VERSION = '2.20260216'; # VERSION
8 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 30 ▶use Mail::AuthenticationResults::Token::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 ▶use Mail::AuthenticationResults::Token::Space;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use Mail::AuthenticationResults::Token::Separator;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 30 ▶use Mail::AuthenticationResults::Token::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 30 ▶use Mail::AuthenticationResults::Token::Assignment;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 109 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 109 my ( $class, $args ) = @_;
19  
20 109 my $self = {};
21 109 bless $self, $class;
22  
23 109 $self->{ 'string' } = [];
24  
25 109 return $self;
26 }
27  
28  
29 109 ▶sub eol {
Subroutine eol: called CC=2 SCAR=6.9
Pod documented
30 109 my ( $self ) = @_;
31 109 ▶ return $self->{ 'eol' } if exists ( $self->{ 'eol' } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"eol"} 33 76 109
32 76 return "\n";
33 }
34  
35  
36 33 ▶sub set_eol {
Subroutine set_eol: called CC=1 SCAR=0.0
Pod documented
37 33 my ( $self, $eol ) = @_;
38 33 $self->{ 'eol' } = $eol;
39 33 return $self;
40 }
41  
42  
43 109 ▶sub indent {
Subroutine indent: called CC=2 SCAR=6.9
Pod documented
44 109 my ( $self ) = @_;
45 109 ▶ return $self->{ 'indent' } if exists ( $self->{ 'indent' } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"indent"} 33 76 109
46 76 return ' ';
47 }
48  
49  
50 33 ▶sub set_indent {
Subroutine set_indent: called CC=1 SCAR=0.0
Pod documented
51 33 my ( $self, $indent ) = @_;
52 33 $self->{ 'indent' } = $indent;
53 33 return $self;
54 }
55  
56  
57 109 ▶sub sub_indent {
Subroutine sub_indent: called CC=2 SCAR=6.9
Pod documented
58 109 my ( $self ) = @_;
59 109 ▶ return $self->{ 'sub_indent' } if exists ( $self->{ 'sub_indent' } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"sub_indent"} 33 76 109
60 76 return ' ';
61 }
62  
63  
64 33 ▶sub set_sub_indent {
Subroutine set_sub_indent: called CC=1 SCAR=0.0
Pod documented
65 33 my ( $self, $indent ) = @_;
66 33 $self->{ 'sub_indent' } = $indent;
67 33 return $self;
68 }
69  
70  
71 490 ▶sub try_fold_at {
Subroutine try_fold_at: called CC=2 SCAR=6.9
Pod documented
72 490 my ( $self ) = @_;
73 490 ▶ return $self->{ 'try_fold_at' } if exists ( $self->{ 'try_fold_at' } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"try_fold_at"} 87 403 490
74 403 return 800;
75 }
76  
77  
78 3 ▶sub set_try_fold_at {
Subroutine set_try_fold_at: called CC=1 SCAR=0.0
Pod documented
79 3 my ( $self, $length ) = @_;
80 3 $self->{ 'try_fold_at' } = $length;
81 3 return $self;
82 }
83  
84  
85 0 ▶sub force_fold_at {
Subroutine force_fold_at: not called CC=2 SCAR=17.9
Pod documented
86 0 my ( $self ) = @_;
87 0 ▶ return $self->{ 'force_fold_at' } if exists ( $self->{ 'force_fold_at' } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"force_fold_at"} 0 0 0
88 0 return 900;
89 }
90  
91  
92 0 ▶sub set_force_fold_at {
Subroutine set_force_fold_at: not called CC=1 SCAR=6.9
Pod documented
93 0 my ( $self, $length ) = @_;
94 0 $self->{ 'force_fold_at' } = $length;
95 0 return $self;
96 }
97  
98  
99 589 ▶sub string {
Subroutine string: called CC=1 SCAR=0.0
Pod documented
100 589 my( $self, $string ) = @_;
101 589 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::String->new_from_value( $string );
102 589 return $self;
103 }
104  
105  
106 441 ▶sub space {
Subroutine space: called CC=1 SCAR=0.0
Pod documented
107 441 my ( $self, $string ) = @_;
108 441 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Space->new_from_value( $string );
109 441 return $self;
110 }
111  
112  
113 117 ▶sub separator {
Subroutine separator: called CC=1 SCAR=0.0
Pod documented
114 117 my ( $self, $string ) = @_;
115 117 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Separator->new_from_value( $string );
116 117 return $self;
117 }
118  
119  
120 91 ▶sub comment {
Subroutine comment: called CC=1 SCAR=0.0
Pod documented
121 91 my ( $self, $string ) = @_;
122 91 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Comment->new_from_value( $string );
123 91 return $self;
124 }
125  
126  
127 259 ▶sub assignment {
Subroutine assignment: called CC=1 SCAR=0.0
Pod documented
128 259 my ( $self, $string ) = @_;
129 259 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Assignment->new_from_value( $string );
130 259 return $self;
131 }
132  
133  
134 109 ▶sub as_string {
Subroutine as_string: called CC=18 SCAR=28.9
Pod documented
135 109 my ( $self ) = @_;
136  
137 109 my $string = q{};
138 109 my $string_length = 0;
139 109 my $content_added = 0;
140  
141 109 my $sections = [];
142 109 my $stack = [];
143 109 my $last_type;
144  
145 109 foreach my $part ( @{ $self->{ 'string' } } ) {
146 1497 ▶ if ( $part->is() eq 'space' && $last_type ne 'space' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part->is eq "space" and $last_type ne "space") 324 1173 1497
Condition100%3/3
$part->is eq "space" and $last_type ne "space"
!ll&&!rl&&r
1056117324
MC/DC100%2/2
$part->is eq "space" and $last_type ne "space"
$part->is eq "space" $last_type ne "space"
Truth table100%3/3
A and B
A= $part->is eq "space"B= $last_type ne "space"
ABresult
0X0
100
111
147 # We have a folding space
148 324 ▶ push @$sections, $stack if @$stack;
Branch: 1 branch
BranchTrueFalseTotal
if @$stack 324 0 324
149 324 $stack = [];
150 }
151 1497 push @$stack, $part;
152 1497 $last_type = $part->is();
153 }
154 109 ▶ push @$sections, $stack if @$stack;
Branch: 1 branch
BranchTrueFalseTotal
if @$stack 103 6 109
155  
156 109 my $eol = $self->eol();;
157 109 my $indent = $self->indent();
158 109 my $sub_indent = $self->sub_indent();
159  
160 109 my $fold_length = 0;
161 SECTION:
162 109 ▶ while ( my $section = shift @$sections ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $section = shift @$sections) 454 109 563
163 454 ▶ if ( $section->[0]->is() eq 'space' && $section->[0]->value() eq $eol ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section->[0]->is eq "space" and $section->[0]->value eq $eol) 149 305 454
Condition100%3/3
$section->[0]->is eq "space" and $section->[0]->value eq $eol
!ll&&!rl&&r
103202149
MC/DC100%2/2
$section->[0]->is eq "space" and $section->[0]->value eq $eol
$section->[0]->is eq "space" $section->[0]->value eq $eol
Truth table100%3/3
A and B
A= $section->[0]->is eq "space"B= $section->[0]->value eq $eol
ABresult
0X0
100
111
164 # This section starts a new line
165 149 $fold_length = 0;
166 149 ▶ if ( ! exists( $section->[0]->{ '_folded' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $section->[0]{"_folded"}) 122 27 149
167 122 ▶ if ( $section->[1]->is() eq 'space' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section->[1]->is eq "space") 117 5 122
168 # Take the last indent value for the fold indent
169 117 $indent = $section->[1]->value();
170 }
171 }
172 }
173  
174 454 my $section_string = join( q{}, map { $_->value() } @$section );
175 454 my $section_length = length( $section_string );
176  
177 454 ▶ if ( $fold_length + $section_length > $self->try_fold_at() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fold_length + $section_length > $self->try_fold_at) 21 433 454
178 21 ▶if ( $fold_length > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fold_length > 0) { } 15 6 21
179 # Remove whitespace tokens at beginning of section
180 15 ▶ while ( $section->[0]->is() eq 'space' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section->[0]->is eq "space") 15 15 30
181 15 shift @$section;
182 }
183 # Insert new folding whitespace at beginning of section
184 15 unshift @$section, Mail::AuthenticationResults::Token::Space->new_from_value( $indent . $sub_indent );
185 15 unshift @$section, Mail::AuthenticationResults::Token::Space->new_from_value( $eol );
186 15 $section->[0]->{ '_folded' } = 1;
187 15 unshift @$sections, $section;
188 15 next SECTION;
189 }
190 else {
191 # ToDo:
192 # This section alone is over the line limit
193 # It already starts with a fold, so we need to remove
194 # some of it to a new line if we can.
195  
196 # Strategy 1: Fold at a relevant token boundary
197 6 my $first_section = [];
198 6 my $second_section = [];
199 6 push @$second_section, Mail::AuthenticationResults::Token::Space->new_from_value( $eol );
200 6 push @$second_section, Mail::AuthenticationResults::Token::Space->new_from_value( $indent . $sub_indent );
201 6 $second_section->[0]->{ '_folded' } = 1;
202 6 my $first_section_length = 0;
203 6 foreach my $part ( @$section ) {
204 30 my $part_length = length $part->value();
205 30 ▶ if ( $part_length + $first_section_length < $self->try_fold_at() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part_length + $first_section_length < $self->try_fold_at) { } 24 6 30
206 24 push @$first_section, $part;
207 24 $first_section_length += $part_length;
208 }
209 else {
210 6 push @$second_section, $part;
211 6 $first_section_length = $self->try_fold_at() + 1; # everything from this point goes onto second
212 }
213 }
214 # Do we have a first and second section with actual content?
215 6 ▶ if ( ( grep { $_->is() ne 'space' } @$first_section ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->is ne "space";} @$first_section and grep {$_->is ne "space";} @$second_section) 6 0 6
Condition33%1/3
grep {$_->is ne "space";} @$first_section and grep {$_->is ne "space";} @$second_section
!ll&&!rl&&r
006
MC/DC0%0/2
grep {$_->is ne "space";} @$first_section and grep {$_->is ne "space";} @$second_section
grep {$_->is ne "space";} @$first_section grep {$_->is ne "space";} @$second_section
Truth table33%1/3
A and B
A= grep {$_->is ne "space";} @$first_sectionB= grep {$_->is ne "space";} @$second_section
ABresult
0X0
100
111
216 18 ( grep { $_->is() ne 'space' } @$second_section ) ) {
217 6 unshift @$sections, $second_section;
218 6 unshift @$sections, $first_section;
219 6 next SECTION;
220 }
221  
222 # We MUST fold at $self->force_fold_at();
223 # Strategy 2: Force fold at a space within a string
224 # Strategy 3: Force fold anywhere
225  
226 # We assume that force fold is greater than try fold
227 }
228 }
229  
230 433 $string .= $section_string;
231 433 $fold_length += $section_length;
232 }
233  
234 109 return $string;
235 }
236  
237 30 1;
238  
239 __END__
« blib/lib/Mail/AuthenticationResults/Header/AuthServID.pm ↑ Summary blib/lib/Mail/AuthenticationResults/Header/Entry.pm »