Sisimai/Mail/Mbox.pm

filter: Statementstmt 100% 48 / 48 Branchbran 68.1% 15 / 22 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 90.1% 82 / 91 CC 15 SCAR 20.5
CC
15
Cov
90%
read
12.4
new
4.0
BEGIN
1.0
CRAP 7.8
« Sisimai/String.pm ↑ Summary Sisimai/Rhost/Mimecast.pm »
1 package Sisimai::Mail::Mbox;
2 88 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 88 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 88 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 88 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 88 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 88 ▶use Class::Accessor::Lite (
Subroutine BEGIN: called CC=1 SCAR=0.0
8 88 'new' => 0,
9 'ro' => [
10 'dir', # [String] Directory name of the mbox
11 'file', # [String] File name of the mbox
12 'path', # [String] Path to mbox
13 'size', # [Integer] File size of the mbox
14 ],
15 'rw' => [
16 'offset', # [Integer] Offset position for seeking
17 'handle', # [IO::File] File handle
18 ]
19 88 );
20  
21 746 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
22 # Constructor of Sisimai::Mail::Mbox
23 # @param [String] argv1 Path to mbox
24 # @return [Sisimai::Mail::Mbox] Object
25 # [Undef] is not a file or does not exist
26 746 my $class = shift;
27 746 ▶ my $argv1 = shift // return undef; return undef unless -f $argv1;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $argv1 0 745 745
Condition100%2/2
shift() // (return undef)
l!l
7451
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
28 745 my $param = {'offset' => 0};
29  
30 745 $param->{'dir'} = File::Basename::dirname $argv1;
31 745 $param->{'path'} = $argv1;
32 745 $param->{'size'} = -s $argv1;
33 745 $param->{'file'} = File::Basename::basename $argv1;
34 745 ▶ $param->{'handle'} = ref $argv1 ? $argv1 : IO::File->new($argv1, 'r');
Branch: 1 branch
BranchTrueFalseTotal
ref $argv1 ? : 0 745 745
35 745 binmode $param->{'handle'};
36  
37 745 return bless($param, __PACKAGE__);
38 }
39  
40 1747 ▶sub read {
Subroutine read: called CC=12 SCAR=25.2
Pod documented
41 # Mbox reader, works as an iterator.
42 # @return [String] Contents of mbox
43 1747 ▶ my $self = shift; return "" unless defined $self->{'path'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"path"} 0 1747 1747
44 1747 ▶ my $seekoffset = $self->{'offset'} // 0;
Condition50%1/2
$self->{"offset"} // 0
l!l
17470
MC/DC0%0/1
$self->{"offset"} // 0
$self->{"offset"}
Truth table50%1/2
A
A= $self->{"offset"}
Aresult
11
00
45 1747 my $filehandle = $self->{'handle'};
46 1747 my $readbuffer = '';
47  
48 1747 ▶ unless( ref $self->{'path'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"path"}) 1747 0 1747
49 # "path" is not IO::File object
50 1747 ▶ return "" unless -f $self->{'path'};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->{"path"} 0 1747 1747
51 1747 ▶ return "" unless -T $self->{'path'};
Branch: 1 branch
BranchTrueFalseTotal
unless -T $self->{"path"} 0 1747 1747
52 }
53 1747 ▶ return "" unless $self->{'offset'} < $self->{'size'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"offset"} < $self->{"size"} 743 1004 1747
54  
55 1004 eval {
56 1004 ▶ $seekoffset = 0 if $seekoffset < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $seekoffset < 0 0 1004 1004
57 1004 seek($filehandle, $seekoffset, 0);
58  
59 1004 ▶ while( my $r = <$filehandle> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $r = readline $filehandle)) 113847 744 114591
60 # Read the UNIX mbox file from 'From ' to the next 'From '
61 113847 ▶ last if( $readbuffer && substr($r, 0, 5) eq 'From ' );
Branch: 1 branch
BranchTrueFalseTotal
if $readbuffer and substr($r, 0, 5) eq "From " 260 113587 113847
Condition100%3/3
$readbuffer and substr($r, 0, 5) eq "From "
!ll&&!rl&&r
1004112583260
MC/DC100%2/2
$readbuffer and substr($r, 0, 5) eq "From "
$readbuffer substr($r, 0, 5) eq "From "
Truth table100%3/3
A and B
A= $readbufferB= substr($r, 0, 5) eq "From "
ABresult
0X0
100
111
62 113587 $readbuffer .= $r;
63 }
64 1004 $seekoffset += length $readbuffer;
65 1004 $self->{'offset'} = $seekoffset;
66 1004 ▶ $filehandle->close unless $seekoffset < $self->{'size'};
Branch: 1 branch
BranchTrueFalseTotal
unless $seekoffset < $self->{"size"} 744 260 1004
67 };
68 1004 return $readbuffer;
69 }
70  
71 88 1;
72 __END__
« Sisimai/String.pm ↑ Summary Sisimai/Rhost/Mimecast.pm »