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 SCAR 31.4
CC
23
Cov
93%
as_string
18.0
force_fold_at
6.0
eol
2.0
CRAP 23.1
« lib/Mail/AuthenticationResults/Token/String.pm ↑ Summary blib/lib/Mail/AuthenticationResults/Header/AuthServID.pm »
1 package Mail::AuthenticationResults::FoldableHeader;
2 # ABSTRACT: Class for modelling a foldable header string
3  
4 29 require 5.008;
5 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 our $VERSION = '2.20230112'; # VERSION
8 29 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 29 ▶use Mail::AuthenticationResults::Token::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use Mail::AuthenticationResults::Token::Space;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use Mail::AuthenticationResults::Token::Separator;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 ▶use Mail::AuthenticationResults::Token::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 29 ▶use Mail::AuthenticationResults::Token::Assignment;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 sub new {
18 104 ▶ my ( $class, $args ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
19  
20 104 my $self = {};
21 104 bless $self, $class;
22  
23 104 $self->{ 'string' } = [];
24  
25 104 return $self;
26 }
27  
28  
29 sub eol {
30 104 ▶ my ( $self ) = @_;
Subroutine eol: called CC=2 SCAR=6.9
Pod documented
31 104 ▶ return $self->{ 'eol' } if exists ( $self->{ 'eol' } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"eol"} 30 74 104
32 74 return "\n";
33 }
34  
35  
36 sub set_eol {
37 30 ▶ my ( $self, $eol ) = @_;
Subroutine set_eol: called CC=1 SCAR=0.0
Pod documented
38 30 $self->{ 'eol' } = $eol;
39 30 return $self;
40 }
41  
42  
43 sub indent {
44 104 ▶ my ( $self ) = @_;
Subroutine indent: called CC=2 SCAR=6.9
Pod documented
45 104 ▶ return $self->{ 'indent' } if exists ( $self->{ 'indent' } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"indent"} 30 74 104
46 74 return ' ';
47 }
48  
49  
50 sub set_indent {
51 30 ▶ my ( $self, $indent ) = @_;
Subroutine set_indent: called CC=1 SCAR=0.0
Pod documented
52 30 $self->{ 'indent' } = $indent;
53 30 return $self;
54 }
55  
56  
57 sub sub_indent {
58 104 ▶ my ( $self ) = @_;
Subroutine sub_indent: called CC=2 SCAR=6.9
Pod documented
59 104 ▶ return $self->{ 'sub_indent' } if exists ( $self->{ 'sub_indent' } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"sub_indent"} 30 74 104
60 74 return ' ';
61 }
62  
63  
64 sub set_sub_indent {
65 30 ▶ my ( $self, $indent ) = @_;
Subroutine set_sub_indent: called CC=1 SCAR=0.0
Pod documented
66 30 $self->{ 'sub_indent' } = $indent;
67 30 return $self;
68 }
69  
70  
71 sub try_fold_at {
72 453 ▶ my ( $self ) = @_;
Subroutine try_fold_at: called CC=2 SCAR=6.9
Pod documented
73 453 ▶ return $self->{ 'try_fold_at' } if exists ( $self->{ 'try_fold_at' } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"try_fold_at"} 87 366 453
74 366 return 800;
75 }
76  
77  
78 sub set_try_fold_at {
79 3 ▶ my ( $self, $length ) = @_;
Subroutine set_try_fold_at: called CC=1 SCAR=0.0
Pod documented
80 3 $self->{ 'try_fold_at' } = $length;
81 3 return $self;
82 }
83  
84  
85 sub force_fold_at {
86 0 ▶ my ( $self ) = @_;
Subroutine force_fold_at: not called CC=2 SCAR=17.9
Pod documented
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 sub set_force_fold_at {
93 0 ▶ my ( $self, $length ) = @_;
Subroutine set_force_fold_at: not called CC=1 SCAR=6.9
Pod documented
94 0 $self->{ 'force_fold_at' } = $length;
95 0 return $self;
96 }
97  
98  
99 sub string {
100 534 ▶ my( $self, $string ) = @_;
Subroutine string: called CC=1 SCAR=0.0
Pod documented
101 534 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::String->new_from_value( $string );
102 534 return $self;
103 }
104  
105  
106 sub space {
107 381 ▶ my ( $self, $string ) = @_;
Subroutine space: called CC=1 SCAR=0.0
Pod documented
108 381 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Space->new_from_value( $string );
109 381 return $self;
110 }
111  
112  
113 sub separator {
114 101 ▶ my ( $self, $string ) = @_;
Subroutine separator: called CC=1 SCAR=0.0
Pod documented
115 101 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Separator->new_from_value( $string );
116 101 return $self;
117 }
118  
119  
120 sub comment {
121 83 ▶ my ( $self, $string ) = @_;
Subroutine comment: called CC=1 SCAR=0.0
Pod documented
122 83 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Comment->new_from_value( $string );
123 83 return $self;
124 }
125  
126  
127 sub assignment {
128 233 ▶ my ( $self, $string ) = @_;
Subroutine assignment: called CC=1 SCAR=0.0
Pod documented
129 233 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Assignment->new_from_value( $string );
130 233 return $self;
131 }
132  
133  
134 sub as_string {
135 104 ▶ my ( $self ) = @_;
Subroutine as_string: called CC=18 SCAR=28.9
Pod documented
136  
137 104 my $string = q{};
138 104 my $string_length = 0;
139 104 my $content_added = 0;
140  
141 104 my $sections = [];
142 104 my $stack = [];
143 104 my $last_type;
144  
145 104 foreach my $part ( @{ $self->{ 'string' } } ) {
146 1332 ▶ if ( $part->is() eq 'space' && $last_type ne 'space' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part->is eq "space" and $last_type ne "space") 292 1040 1332
Condition100%3/3
$part->is eq "space" and $last_type ne "space"
!ll&&!rl&&r
95189292
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 292 ▶ push @$sections, $stack if @$stack;
Branch: 1 branch
BranchTrueFalseTotal
if @$stack 292 0 292
149 292 $stack = [];
150 }
151 1332 push @$stack, $part;
152 1332 $last_type = $part->is();
153 }
154 104 ▶ push @$sections, $stack if @$stack;
Branch: 1 branch
BranchTrueFalseTotal
if @$stack 98 6 104
155  
156 104 my $eol = $self->eol();;
157 104 my $indent = $self->indent();
158 104 my $sub_indent = $self->sub_indent();
159  
160 104 my $fold_length = 0;
161 SECTION:
162 104 ▶ while ( my $section = shift @$sections ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $section = shift @$sections) 417 104 521
163 417 ▶ 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) 121 296 417
Condition100%3/3
$section->[0]->is eq "space" and $section->[0]->value eq $eol
!ll&&!rl&&r
98198121
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 121 $fold_length = 0;
166 121 ▶ if ( ! exists( $section->[0]->{ '_folded' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $section->[0]{"_folded"}) 94 27 121
167 94 ▶ if ( $section->[1]->is() eq 'space' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section->[1]->is eq "space") 89 5 94
168 # Take the last indent value for the fold indent
169 89 $indent = $section->[1]->value();
170 }
171 }
172 }
173  
174 417 my $section_string = join( q{}, map { $_->value() } @$section );
175 417 my $section_length = length( $section_string );
176  
177 417 ▶ if ( $fold_length + $section_length > $self->try_fold_at() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fold_length + $section_length > $self->try_fold_at) 21 396 417
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 396 $string .= $section_string;
231 396 $fold_length += $section_length;
232 }
233  
234 104 return $string;
235 }
236  
237 29 1;
238  
239 __END__
« lib/Mail/AuthenticationResults/Token/String.pm ↑ Summary blib/lib/Mail/AuthenticationResults/Header/AuthServID.pm »