blib/lib/Date/Pregnancy.pm

filter: Statementstmt 98.1% 105 / 107 Branchbran 85.7% 24 / 28 Conditioncond 66.6% 18 / 27 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 85.3% 169 / 198 CC 26 SCAR 15.3
CC
26
Cov
91%
calculate_birthday
8.0
calculate_week
6.3
calculate_month
6.3
CRAP 4.6
↑ Summary
1 package Date::Pregnancy;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 require Exporter;
11 7 ▶use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use 5.008; #5.8.0
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 $VERSION = '0.06';
15 7 @ISA = qw(Exporter);
16  
17 7 @EXPORT_OK = qw(
18 calculate_birthday calculate_week calculate_month
19 _countback _266days _40weeks
20 );
21  
22 7 ▶use constant AVG_CYCLE => 28;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use constant DAY => ( 60 * 60 * 24 );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 67 ▶sub _40weeks {
Subroutine _40weeks: called CC=3 SCAR=11.0
26 67 ▶ my $dt = shift
Condition66%2/3
shift() || carp('first_day_of_last_period parameter is mandatory')
l!l&&r!l&&!r
6610
MC/DC0%0/2
shift() || carp('first_day_of_last_period parameter is mandatory')
shift() carp('first_day_of_last_period parameter is mandatory')
Truth table66%2/3
A || B
A= shift()B= carp('first_day_of_last_period parameter is mandatory')
ABresult
1X1
011
000
27 || carp "first_day_of_last_period parameter is mandatory";
28 67 ▶ return undef unless ( ref $dt );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dt 1 66 67
29  
30 66 my $birthday = clone($dt);
31 66 $birthday->add( weeks => 40 );
32  
33 66 return $birthday;
34 }
35  
36 9 ▶sub _266days {
Subroutine _266days: called CC=5 SCAR=16.1
37 9 my ( $dt, $period_cycle_length ) = @_;
38  
39 9 ▶ unless ( ref $dt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $dt) 1 8 9
40 1 carp "first_day_of_last_period parameter is mandatory";
41 1 return undef;
42 }
43  
44 8 ▶ if ( !$period_cycle_length ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($period_cycle_length) 1 7 8
45 1 carp "period_cycle_length parameter is mandatory";
46 1 return undef;
47 }
48  
49 7 my $birthday = clone($dt);
50 7 ▶ if ( $period_cycle_length > 28 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($period_cycle_length > 28) { } 1 6 7
elsif ($period_cycle_length < 29) { } 6 0 6
51 1 $birthday->add( seconds =>
52 ( DAY * floor( $period_cycle_length * 0.85 * ( 2 / 3 ) ) ) );
53  
54 } elsif ( $period_cycle_length < 29 ) {
55 6 $birthday->add( seconds => ( DAY * ( $period_cycle_length / 2 ) ) );
56 }
57 7 $birthday->add( days => 266 );
58  
59 7 return $birthday;
60 }
61  
62 6 ▶sub _countback {
Subroutine _countback: called CC=3 SCAR=11.0
63 6 ▶ my $dt = shift
Condition66%2/3
shift() || carp('first_day_of_last_period parameter is mandatory')
l!l&&r!l&&!r
510
MC/DC0%0/2
shift() || carp('first_day_of_last_period parameter is mandatory')
shift() carp('first_day_of_last_period parameter is mandatory')
Truth table66%2/3
A || B
A= shift()B= carp('first_day_of_last_period parameter is mandatory')
ABresult
1X1
011
000
64 || carp "first_day_of_last_period parameter is mandatory";
65 6 ▶ return undef unless ( ref $dt );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dt 1 5 6
66  
67 5 my $birthday = clone($dt);
68  
69 5 $birthday->add( days => 7 );
70 5 $birthday->subtract( months => 3 );
71 5 $birthday->add( years => 1 );
72  
73 #if ($dt->month < 3) {
74 #}
75  
76 5 return $birthday;
77 }
78  
79 84 ▶sub calculate_birthday {
Subroutine calculate_birthday: called CC=8 SCAR=20.8
Pod documented
80 84 my %params = @_;
81  
82 84 ▶ my $method = $params{'method'} || '266days';
Condition100%2/2
$params{'method'} || '266days'
l!l
795
MC/DC100%1/1
$params{'method'} || '266days'
$params{'method'}
Truth table100%2/2
A
A= $params{'method'}
Aresult
11
00
83  
84 84 ▶ my $period_cycle_length = $params{'period_cycle_length'} || AVG_CYCLE;
Condition100%2/2
$params{'period_cycle_length'} || 28
l!l
7212
MC/DC100%1/1
$params{'period_cycle_length'} || 28
$params{'period_cycle_length'}
Truth table100%2/2
A
A= $params{'period_cycle_length'}
Aresult
11
00
85  
86 84 ▶ my $first_day_of_last_period = $params{'first_day_of_last_period'}
Condition66%2/3
$params{'first_day_of_last_period'} || carp('first_day_of_last_period parameter is mandatory')
l!l&&r!l&&!r
7680
MC/DC0%0/2
$params{'first_day_of_last_period'} || carp('first_day_of_last_period parameter is mandatory')
$params{'first_day_of_last_period'} carp('first_day_of_last_period parameter is mandatory')
Truth table66%2/3
A || B
A= $params{'first_day_of_last_period'}B= carp('first_day_of_last_period parameter is mandatory')
ABresult
1X1
011
000
87 || carp "first_day_of_last_period parameter is mandatory";
88 84 ▶ return undef unless ( ref $first_day_of_last_period );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $first_day_of_last_period 8 76 84
89  
90 76 my $calculation = "_$method";
91 76 my @methods = qw(_countback _266days _40weeks);
92  
93 76 ▶ unless ( grep {/$method/} @methods ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {/$method/;} @methods) 1 75 76
94 1 croak "Unknown method: $params{'method'}";
95 }
96  
97 75 $calculation .= '($first_day_of_last_period';
98  
99 75 ▶ if ( $method eq '266days' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "266days") 6 69 75
100 6 $calculation .= ', $period_cycle_length';
101 }
102 75 $calculation .= ');';
103  
104 75 my $birthday = eval("$calculation");
105 75 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 75 75
106  
107 75 return $birthday;
108 }
109  
110 55 ▶sub calculate_week {
Subroutine calculate_week: called CC=6 SCAR=18.4
Pod documented
111 55 my %params = @_;
112  
113 55 ▶ my $period_cycle_length = $params{'period_cycle_length'} || AVG_CYCLE;
Condition50%1/2
$params{'period_cycle_length'} || 28
l!l
055
MC/DC0%0/1
$params{'period_cycle_length'} || 28
$params{'period_cycle_length'}
Truth table50%1/2
A
A= $params{'period_cycle_length'}
Aresult
11
00
114  
115 55 ▶ my $now = $params{'date'} || DateTime->now;
Condition66%2/3
$params{'date'} || 'DateTime'->now
l!l&&r!l&&!r
5230
MC/DC0%0/2
$params{'date'} || 'DateTime'->now
$params{'date'} 'DateTime'->now
Truth table66%2/3
A || B
A= $params{'date'}B= 'DateTime'->now
ABresult
1X1
011
000
116 55 $now->set_time_zone('UTC');
117  
118 55 ▶ my $method = $params{'method'} || '40weeks';
Condition50%1/2
$params{'method'} || '40weeks'
l!l
055
MC/DC0%0/1
$params{'method'} || '40weeks'
$params{'method'}
Truth table50%1/2
A
A= $params{'method'}
Aresult
11
00
119  
120 55 my $birthday;
121 55 ▶ if ( $params{'birthday'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'birthday'}) { } 0 55 55
122 0 $birthday = $params{'birthday'};
123  
124 } else {
125 $birthday = calculate_birthday(
126 55 first_day_of_last_period => $params{'first_day_of_last_period'},
127 period_cycle_length => $period_cycle_length,
128 method => $method,
129 );
130 55 ▶ return undef unless ( ref $birthday );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $birthday 3 52 55
131 }
132 52 $birthday->set_time_zone('UTC');
133  
134 52 $birthday->subtract( months => 9 );
135  
136 52 my $duration = $birthday->delta_days($now);
137  
138 52 return ( $duration->weeks + 1 );
139 }
140  
141 14 ▶sub calculate_month {
Subroutine calculate_month: called CC=6 SCAR=18.4
Pod documented
142 14 my %params = @_;
143  
144 14 ▶ my $period_cycle_length = $params{'period_cycle_length'} || AVG_CYCLE;
Condition50%1/2
$params{'period_cycle_length'} || 28
l!l
014
MC/DC0%0/1
$params{'period_cycle_length'} || 28
$params{'period_cycle_length'}
Truth table50%1/2
A
A= $params{'period_cycle_length'}
Aresult
11
00
145  
146 14 ▶ my $now = $params{'date'} || DateTime->now;
Condition66%2/3
$params{'date'} || 'DateTime'->now
l!l&&r!l&&!r
1040
MC/DC0%0/2
$params{'date'} || 'DateTime'->now
$params{'date'} 'DateTime'->now
Truth table66%2/3
A || B
A= $params{'date'}B= 'DateTime'->now
ABresult
1X1
011
000
147 14 $now->set_time_zone('UTC');
148  
149 14 ▶ my $method = $params{'method'} || '40weeks';
Condition50%1/2
$params{'method'} || '40weeks'
l!l
014
MC/DC0%0/1
$params{'method'} || '40weeks'
$params{'method'}
Truth table50%1/2
A
A= $params{'method'}
Aresult
11
00
150  
151 14 my $birthday;
152 14 ▶ if ( $params{'birthday'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'birthday'}) { } 0 14 14
153 0 $birthday = $params{'birthday'};
154  
155 } else {
156 $birthday = calculate_birthday(
157 14 first_day_of_last_period => $params{'first_day_of_last_period'},
158 period_cycle_length => $period_cycle_length,
159 method => $method,
160 );
161 14 ▶ return undef unless ( ref $birthday );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $birthday 3 11 14
162 }
163 11 $birthday->set_time_zone('UTC');
164  
165 11 $birthday->subtract( months => 9 );
166  
167 11 my $duration = $birthday->delta_md($now);
168  
169 11 return ( $duration->months + 1 );
170 }
171  
172 7 1;
173  
174 __END__
↑ Summary