Tools/Summary/Thread.pm

filter: Statementstmt 65.4% 106 / 162 Branchbran 46.3% 25 / 54 Conditioncond 49.1% 29 / 59 MC/DCmcdc 31.4% 11 / 35 Subroutinesub 92.5% 25 / 27 Podpod 0.0% 0 / 17 totaltotal 55.3% 196 / 354 CC 48 SCAR 27.7
CC
48
Cov
58%
from_mailbox_thread
72.0
merge_out_of_date
18.3
earlier_thread
16.1
CRAP 16.0
« Tools/Downloader/NNTP.pm ↑ Summary Tools/CLI/ToHTML.pm »
1 #!/usr/bin/perl
2  
3 package Mail::Summary::Tools::Summary::Thread;
4 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Mail::Summary::Tools::ArchiveLink::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Mail::Summary::Tools::ArchiveLink::Hardcoded;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Mail::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 has subject => (
11 isa => "Str",
12 is => "rw",
13 required => 1,
14 );
15  
16 6 has message_id => (
17 isa => "Str",
18 is => "rw",
19 required => 1,
20 );
21  
22 6 has hidden => (
23 isa => "Bool|Str",
24 is => "rw",
25 required => 0,
26 );
27  
28 has extra => (
29 isa => "HashRef",
30 is => "rw",
31 5 ▶ default => sub { return { } },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 6 required => 0,
33 );
34  
35 6 has summary => (
36 isa => "Str",
37 is => "rw",
38 required => 0,
39 default => "",
40 );
41  
42 6 has default_archive => (
43 isa => "Str",
44 is => "rw",
45 default => "google",
46 );
47  
48 has archive_link => (
49 isa => "Mail::Summary::Tools::ArchiveLink",
50 is => "rw",
51 lazy => 1,
52 14 ▶ default => sub { $_[0]->make_archive_link },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 6 );
54  
55 has archive_link_params => (
56 isa => "HashRef",
57 is => "rw",
58 auto_deref => 1,
59 23 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
60 6 );
61  
62 0 ▶sub _extract_name {
Subroutine _extract_name: not called CC=2 SCAR=17.9
63 # this is a slightly less eager _extract_name from Mail::Address
64 # that version was mean to chromatic, making him turn out as Chromatic
65 # the case changing logic has been removed
66 0 my ( $self, $name ) = @_;
67 0 local $_ = $name;
68  
69 # trim whitespace
70 0 s/^\s+//;
71 0 s/\s+$//;
72 0 s/\s+/ /;
73  
74 # Disregard numeric names (e.g. 123456.1234@compuserve.com)
75 0 ▶ return "" if /^[\d ]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^[\d ]+$/ 0 0 0
76  
77 # remove outermost parenthesis
78 0 s/^\((.*)\)$/$1/;
79  
80 # remove outer quotation marks
81 0 s/^"(.*)"$/$1/;
82  
83 # remove minimal embedded comments
84 0 s/\(.*?\)//g;
85  
86 # remove all escapes
87 0 s/\\//g;
88  
89 # remove internal quotation marks
90 0 s/^"(.*)"$/$1/;
91  
92 # reverse "Last, First M." if applicable
93 0 s/^([^\s]+) ?, ?(.*)$/$2 $1/;
94 0 s/,.*//;
95  
96 # some cleanup
97 0 s/\[[^\]]*\]//g;
98 0 s/(^[\s'"]+|[\s'"]+$)//g;
99 0 s/\s{2,}/ /g;
100  
101 0 return $_;
102 }
103  
104 0 ▶sub from_mailbox_thread {
Subroutine from_mailbox_thread: not called CC=8 SCAR=42.8
Pod undocumented
105 0 my ( $class, $thread, %options ) = @_;
106  
107 0 my @messages = $thread->threadMessages;
108  
109 0 my $root = $messages[0];
110  
111 0 my $subject = $root->subject;
112 0 ▶ $subject = $options{process_subject}->($subject) if $options{process_subject};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"process_subject"} 0 0 0
113  
114 0 my %extra;
115  
116 0 ▶ if ( $options{collect_posters} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"collect_posters"}) 0 0 0
117 0 my @from_fields = map { $_->head->get('From')->study } @messages;
118  
119 0 my %seen_email;
120 0 my @addresses = grep { !$seen_email{$_->address}++ }
121 0 map { Mail::Address->parse($_->decodedBody) } @from_fields;
122  
123 0 my @posters = map {{
124 0 ▶ name => $class->_extract_name($_->phrase) || $class->_extract_name($_->comment) || $_->user,
Condition0%0/6
$class->_extract_name($_->phrase) || $class->_extract_name($_->comment) || $_->user
l!l&&r!l&&!r
000
$class->_extract_name($_->phrase) or $class->_extract_name($_->comment)
l!l&&r!l&&!r
000
MC/DC0%0/3
$class->_extract_name($_->phrase) || $class->_extract_name($_->comment) || $_->user
$class->_extract_name($_->phrase) $class->_extract_name($_->comment) $_->user
Truth table0%0/4
A or B || C
A= $class->_extract_name($_->phrase)B= $class->_extract_name($_->comment)C= $_->user
ABCresult
1XX1
01X1
0011
0000
125 email => $_->address,
126 }} @addresses;
127  
128 0 $extra{posters} = \@posters;
129 }
130  
131 0 ▶ if ( $options{collect_dates} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"collect_dates"}) 0 0 0
132 0 $extra{date_from} = $thread->startTimeEstimate;
133 0 $extra{date_to} = $thread->endTimeEstimate;
134 }
135  
136 0 ▶ if ( $options{collect_rt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"collect_rt"}) 0 0 0
137 0 eval { $extra{rt_ticket} = $root->head->get('RT-Ticket')->unfoldedBody };
138 }
139  
140 0 my @message_ids = grep { $_ ne $root->messageId } map { $_->messageId } @messages;
141 0 ▶ $extra{messages} = \@message_ids if @message_ids;
Branch: 1 branch
BranchTrueFalseTotal
if @message_ids 0 0 0
142  
143 0 $class->new(
144 subject => $subject,
145 message_id => $root->messageId,
146 extra => \%extra,
147 );
148 }
149  
150 1 ▶sub load {
Subroutine load: called CC=3 SCAR=11.5
Pod undocumented
151 1 my ( $class, $hash, %options ) = @_;
152  
153 1 my @good_keys = qw/summary message_id subject hidden/;
154  
155 1 my %hash = %$hash;
156  
157 1 my %good_values;
158 1 @good_values{@good_keys} = delete @hash{@good_keys};
159  
160 1 my ( $thread_uri, $message_uri ) = delete @hash{qw/thread_uri message_uri/};
161  
162 1 ▶ if ( defined($thread_uri) || defined($message_uri) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $thread_uri or defined $message_uri) 0 1 1
Condition33%1/3
defined $thread_uri or defined $message_uri
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $thread_uri or defined $message_uri
defined $thread_uri defined $message_uri
Truth table33%1/3
A or B
A= defined $thread_uriB= defined $message_uri
ABresult
1X1
011
000
163 0 $good_values{archive_link} = Mail::Summary::Tools::ArchiveLink::Hardcoded->new(
164 thread_uri => $thread_uri,
165 message_uri => $message_uri,
166 );
167 }
168  
169 $class->new(
170 1 %{ $options{thread} },
171 %good_values,
172 extra => \%hash,
173 );
174 }
175  
176 1 ▶sub to_hash {
Subroutine to_hash: called CC=3 SCAR=14.8
Pod undocumented
177 1 my $self = shift;
178
179 1 my @link_info;
180 1 ▶ if ( exists $self->{archive_link} ) { # FIXME $self->meta->get_attribute_by_name("archive_link)->is_initialized( $self )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"archive_link"}) 0 1 1
181 0 ▶ if ( (my $link = $self->archive_link)->isa("Mail::Summary::Tools::ArchiveLink::Hardcoded") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $link = $self->archive_link)->isa("Mail::Summary::Tools::ArchiveLink::Hardcoded")) 0 0 0
182 @link_info = (
183 0 eval { thread_uri => $link->thread_uri->as_string },
184 0 eval { message_uri => $link->message_uri->as_string },
185 );
186 }
187 }
188  
189 return {
190 subject => $self->subject,
191 message_id => $self->message_id,
192 summary => $self->summary,
193 hidden => $self->hidden,
194 @link_info,
195 1 %{ $self->extra },
196 };
197 }
198  
199 14 ▶sub make_archive_link {
Subroutine make_archive_link: called CC=1 SCAR=0.0
Pod undocumented
200 14 my $self = shift;
201  
202 14 my $constructor = $self->default_archive;
203 14 Mail::Summary::Tools::ArchiveLink::Easy->$constructor( $self->message_id, $self->archive_link_params );
204 }
205  
206 # FIXME
207 # redo with an attribute grammar
208  
209 7 ▶sub merge {
Subroutine merge: called CC=1 SCAR=0.0
Pod undocumented
210 7 my ( $self, $thread ) = @_;
211  
212 7 $self->merge_extra( $thread );
213 7 $self->merge_summary( $thread );
214 7 $self->merge_subject( $thread );
215 7 $self->merge_hidden( $thread );
216 7 $self->merge_archive_link( $thread );
217 }
218  
219 7 ▶sub merge_hidden {
Subroutine merge_hidden: called CC=2 SCAR=7.1
Pod undocumented
220 7 my ( $self, $thread ) = @_;
221 7 ▶ $self->hidden( $self->hidden || $thread->hidden );
Condition66%2/3
$self->hidden || $thread->hidden
l!l&&r!l&&!r
106
MC/DC50%1/2
$self->hidden || $thread->hidden
$self->hidden $thread->hidden
Truth table66%2/3
A || B
A= $self->hiddenB= $thread->hidden
ABresult
1X1
011
000
222 }
223  
224 7 ▶sub merge_summary {
Subroutine merge_summary: called CC=2 SCAR=6.9
Pod undocumented
225 7 my ( $self, $thread ) = @_;
226 7 ▶ $self->summary( $self->summary || $thread->summary );
Condition100%3/3
$self->summary || $thread->summary
l!l&&r!l&&!r
322
MC/DC100%2/2
$self->summary || $thread->summary
$self->summary $thread->summary
Truth table100%3/3
A || B
A= $self->summaryB= $thread->summary
ABresult
1X1
011
000
227 }
228  
229 7 ▶sub merge_subject {
Subroutine merge_subject: called CC=1 SCAR=0.0
Pod undocumented
230 7 my ( $self, $thread ) = @_;
231 # noop
232 }
233  
234 7 ▶sub merge_archive_link {
Subroutine merge_archive_link: called CC=2 SCAR=7.4
Pod undocumented
235 7 my ( $self, $thread ) = @_;
236  
237 7 my @hard_coded = grep { $_->isa("Mail::Summary::Tools::ArchiveLink::Hardcoded") } $self->archive_link, $thread->archive_link;
238  
239 7 ▶ if ( @hard_coded ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hard_coded) 0 7 7
240 0 $self->archive_link( $hard_coded[0] );
241 }
242 }
243  
244 7 ▶sub merge_extra {
Subroutine merge_extra: called CC=1 SCAR=0.0
Pod undocumented
245 7 my ( $self, $thread ) = @_;
246  
247 $self->extra({
248 7 %{ $thread->extra },
249 7 %{ $self->extra },
250 $self->merge_dates( $thread ),
251 $self->merge_posters( $thread ),
252 });
253 }
254  
255 7 ▶sub merge_dates {
Subroutine merge_dates: called CC=1 SCAR=0.0
Pod undocumented
256 7 my ( $self, $thread ) = @_;
257  
258 return (
259 7 $self->merge_date_from($thread),
260 $self->merge_date_to($thread),
261 $self->merge_out_of_date($thread),
262 );
263 }
264  
265 7 ▶sub merge_out_of_date {
Subroutine merge_out_of_date: called CC=16 SCAR=29.1
Pod undocumented
266 7 my ( $self, $thread ) = @_;
267 # it can't be out of date if there's no summary
268 7 ▶ return unless $self->summary or $thread->summary or $self->hidden or $thread->hidden;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->summary or $thread->summary or $self->hidden or $thread->hidden 1 6 7
Condition88%8/9
$self->summary or $thread->summary or $self->hidden or $thread->hidden
l!l&&r!l&&!r
601
$self->summary or $thread->summary or $self->hidden
l!l&&r!l&&!r
511
$self->summary or $thread->summary
l!l&&r!l&&!r
322
MC/DC75%3/4
$self->summary or $thread->summary or $self->hidden or $thread->hidden
$self->summary $thread->summary $self->hidden $thread->hidden
Truth table80%4/5
A or B or C or D
A= $self->summaryB= $thread->summaryC= $self->hiddenD= $thread->hidden
ABCDresult
1XXX1
01XX1
001X1
00011
00000
269  
270 # if any thread is out of date then this one becomes out of date
271  
272 6 my $prev_out_of_date = $self->extra->{out_of_date};
273 6 ▶ my $out_of_date = $prev_out_of_date || $thread->extra->{out_of_date};
Condition33%1/3
$prev_out_of_date || $thread->extra->{'out_of_date'}
l!l&&r!l&&!r
006
MC/DC0%0/2
$prev_out_of_date || $thread->extra->{'out_of_date'}
$prev_out_of_date $thread->extra->{'out_of_date'}
Truth table33%1/3
A || B
A= $prev_out_of_dateB= $thread->extra->{'out_of_date'}
ABresult
1X1
011
000
274  
275 6 my $earliest = $self->earlier_thread( $thread );
276 6 my $latest = $self->later_thread( $thread );
277  
278 6 ▶ if ( $self->summary || $self->hidden ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->summary or $self->hidden) { } 4 2 6
elsif ($thread->summary or $thread->hidden) { } 2 0 2
Condition100%3/3
$self->summary or $self->hidden
l!l&&r!l&&!r
312
MC/DC100%2/2
$self->summary or $self->hidden
$self->summary $self->hidden
Truth table100%3/3
A or B
A= $self->summaryB= $self->hidden
ABresult
1X1
011
000
279 # we keep the existing summary, and if the other thread extends beyond
280 # our range it's out of date
281 4 ▶ $out_of_date = 1 if $earliest != $self or $latest != $self;
Branch: 1 branch
BranchTrueFalseTotal
if $earliest != $self or $latest != $self 2 2 4
Condition66%2/3
$earliest != $self or $latest != $self
l!l&&r!l&&!r
202
MC/DC50%1/2
$earliest != $self or $latest != $self
$earliest != $self $latest != $self
Truth table66%2/3
A or B
A= $earliest != $selfB= $latest != $self
ABresult
1X1
011
000
282 ▶ } elsif ( $thread->summary || $thread->hidden ) {
Condition33%1/3
$thread->summary or $thread->hidden
l!l&&r!l&&!r
200
MC/DC0%0/2
$thread->summary or $thread->hidden
$thread->summary $thread->hidden
Truth table33%1/3
A or B
A= $thread->summaryB= $thread->hidden
ABresult
1X1
011
000
283 # we take the new summary, and if we extend beyond the other range then
284 # it's out of date
285 2 ▶ $out_of_date = 1 if $earliest != $thread or $latest != $thread;
Branch: 1 branch
BranchTrueFalseTotal
if $earliest != $thread or $latest != $thread 1 1 2
Condition66%2/3
$earliest != $thread or $latest != $thread
l!l&&r!l&&!r
101
MC/DC50%1/2
$earliest != $thread or $latest != $thread
$earliest != $thread $latest != $thread
Truth table66%2/3
A or B
A= $earliest != $threadB= $latest != $thread
ABresult
1X1
011
000
286 } else {
287 0 $out_of_date = 0;
288 }
289  
290 6 ▶ if ( $out_of_date xor $prev_out_of_date ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out_of_date xor $prev_out_of_date) { } 3 3 6
Condition50%2/4
$out_of_date xor $prev_out_of_date
l&&rl&&!r!l&&r!l&&!r
0303
MC/DC50%1/2
$out_of_date xor $prev_out_of_date
$out_of_date $prev_out_of_date
Truth table50%2/4
A xor B
A= $out_of_dateB= $prev_out_of_date
ABresult
110
101
011
000
291 3 return out_of_date => $out_of_date;
292 } else {
293 3 return;
294 }
295 }
296  
297 13 ▶sub earlier_thread {
Subroutine earlier_thread: called CC=7 SCAR=27.8
Pod undocumented
298 13 my ( $self, $thread ) = @_;
299  
300 13 my $date_from = $self->extra->{date_from};
301 13 my $other_date_from = $thread->extra->{date_from};
302  
303 13 ▶ if ( $date_from and $other_date_from ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($date_from and $other_date_from) { } 13 0 13
elsif ($date_from or $other_date_from) { } 0 0 0
Condition33%1/3
$date_from and $other_date_from
!ll&&!rl&&r
0013
MC/DC0%0/2
$date_from and $other_date_from
$date_from $other_date_from
Truth table33%1/3
A and B
A= $date_fromB= $other_date_from
ABresult
0X0
100
111
304 13 ▶ return $date_from <= $other_date_from ? $self : $thread;
Branch: 1 branch
BranchTrueFalseTotal
$date_from <= $other_date_from ? : 6 7 13
305 ▶ } elsif( $date_from || $other_date_from ) {
Condition0%0/3
$date_from or $other_date_from
l!l&&r!l&&!r
000
MC/DC0%0/2
$date_from or $other_date_from
$date_from $other_date_from
Truth table0%0/3
A or B
A= $date_fromB= $other_date_from
ABresult
1X1
011
000
306 0 ▶ return $date_from ? $self : $thread;
Branch: 1 branch
BranchTrueFalseTotal
$date_from ? : 0 0 0
307 } else {
308 0 return $self;
309 }
310 }
311  
312 13 ▶sub later_thread {
Subroutine later_thread: called CC=7 SCAR=27.8
Pod undocumented
313 13 my ( $self, $thread ) = @_;
314  
315 13 my $date_to = $self->extra->{date_to};
316 13 my $other_date_to = $thread->extra->{date_to};
317  
318 13 ▶ if ( $date_to and $other_date_to ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($date_to and $other_date_to) { } 13 0 13
elsif ($date_to or $other_date_to) { } 0 0 0
Condition33%1/3
$date_to and $other_date_to
!ll&&!rl&&r
0013
MC/DC0%0/2
$date_to and $other_date_to
$date_to $other_date_to
Truth table33%1/3
A and B
A= $date_toB= $other_date_to
ABresult
0X0
100
111
319 13 ▶ return $date_to >= $other_date_to ? $self : $thread;
Branch: 1 branch
BranchTrueFalseTotal
$date_to >= $other_date_to ? : 6 7 13
320 ▶ } elsif ( $date_to || $other_date_to ) {
Condition0%0/3
$date_to or $other_date_to
l!l&&r!l&&!r
000
MC/DC0%0/2
$date_to or $other_date_to
$date_to $other_date_to
Truth table0%0/3
A or B
A= $date_toB= $other_date_to
ABresult
1X1
011
000
321 0 ▶ return $date_to ? $self : $thread;
Branch: 1 branch
BranchTrueFalseTotal
$date_to ? : 0 0 0
322 } else {
323 0 return $self;
324 }
325 }
326  
327 7 ▶sub merge_date_from {
Subroutine merge_date_from: called CC=2 SCAR=6.9
Pod undocumented
328 7 my ( $self, $thread ) = @_;
329  
330 # earliest
331  
332 7 my $min = ( $self->earlier_thread( $thread ) )->extra->{date_from};
333 6 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
334 7 ▶ unless ( $min == $self->extra->{date_from} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min == $self->extra->{'date_from'}) { } 3 4 7
335 4 return ( date_from => $min );
336 } else {
337 3 return;
338 }
339 }
340  
341 7 ▶sub merge_date_to {
Subroutine merge_date_to: called CC=2 SCAR=6.9
Pod undocumented
342 7 my ( $self, $thread ) = @_;
343  
344 7 my $max = ( $self->later_thread( $thread ) )->extra->{date_to};
345 6 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
346 7 ▶ unless ( $max == $self->extra->{date_to} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max == $self->extra->{'date_to'}) { } 3 4 7
347 4 return ( date_to => $max );
348 } else {
349 3 return;
350 }
351 }
352  
353 7 ▶sub merge_posters {
Subroutine merge_posters: called CC=4 SCAR=15.0
Pod undocumented
354 7 my ( $self, $thread ) = @_;
355  
356 7 my %seen;
357 0 my @posters = grep { !$seen{$_->{name}}++ }
358 7 ▶ @{ $self->extra->{posters} || [] },
Condition50%1/2
$self->extra->{'posters'} || []
l!l
07
MC/DC0%0/1
$self->extra->{'posters'} || []
$self->extra->{'posters'}
Truth table50%1/2
A
A= $self->extra->{'posters'}
Aresult
11
00
359 7 ▶ @{ $thread->extra->{posters} || [] };
Condition50%1/2
$thread->extra->{'posters'} || []
l!l
07
MC/DC0%0/1
$thread->extra->{'posters'} || []
$thread->extra->{'posters'}
Truth table50%1/2
A
A= $thread->extra->{'posters'}
Aresult
11
00
360  
361 7 ▶ return ( @posters ? (posters => \@posters) : () );
Branch: 1 branch
BranchTrueFalseTotal
@posters ? : 0 7 7
362 }
363  
364 6 __PACKAGE__;
365  
366 __END__
« Tools/Downloader/NNTP.pm ↑ Summary Tools/CLI/ToHTML.pm »