lib/Business/DK/CPR.pm

filter: Statementstmt 73.7% 146 / 198 Branchbran 43.5% 27 / 62 Conditioncond 33.3% 5 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 34 / 34 Podpod 100% 9 / 9 totaltotal 68.2% 223 / 327 CC 42 SCAR 34.8
CC
42
Cov
65%
generate1968
137.6
generate2007
60.7
generate
22.9
CRAP 32.6
↑ Summary lib/Data/FormValidator/Constraints/Business/DK/CPR.pm »
1 package Business::DK::CPR;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Business::DK::CVR qw(_calculate_sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Date::Calc qw(check_date);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Params::Validate qw( validate_pos SCALAR ARRAYREF );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use 5.012; #5.12.0
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 16 our $VERSION = '0.17';
16 16 our @EXPORT_OK = qw(
17 validate
18 validateCPR
19 generate
20 validate1968
21 generate1968
22 validate2007
23 generate2007
24 calculate
25 );
26  
27 16 ▶use constant MODULUS_OPERAND_1968 => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 16 ▶use constant MODULUS_OPERAND_2007 => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 16 ▶use constant DATE_LENGTH => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 16 ▶use constant CONTROL_CIFER_LENGTH => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 16 ▶use constant VALID => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 16 ▶use constant VALID_MALE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 16 ▶use constant VALID_FEMALE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 16 ▶use constant INVALID => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 16 ▶use constant FEMALE => 'female';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 16 ▶use constant MALE => 'male';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 16 Readonly my @controlcifers => qw(4 3 2 7 6 5 4 3 2 1);
39  
40 16 my %female_seeds;
41 16 tie %female_seeds, 'Tie::IxHash',
42 4 => { max => 9994, min => 10 },
43 2 => { max => 9998, min => 8 },
44 6 => { max => 9996, min => 12 };
45  
46 16 my %male_seeds;
47 16 tie %male_seeds, 'Tie::IxHash',
48 1 => { max => 9997, min => 7 },
49 3 => { max => 9999, min => 9 },
50 5 => { max => 9995, min => 11 };
51  
52 23 ▶sub merge {
Subroutine merge: called CC=2 SCAR=6.9
Pod documented
53 23 my ( $left_hashref, $right_hashref ) = @_;
54  
55 23 my %hash = %{$right_hashref};
56  
57 23 foreach ( keys %{$left_hashref} ) {
58 69 $hash{$_} = $left_hashref->{$_};
59 }
60  
61 23 return \%hash;
62 }
63  
64 4 ▶sub calculate {
Subroutine calculate: called CC=4 SCAR=13.9
Pod documented
65 4 my ($birthdate) = @_;
66  
67 4 validate_pos( @_,
68 { type => SCALAR, callbacks => { 'date' => \&_checkdate } } );
69  
70 2 my @cprs;
71 2 for ( 1 .. 999 ) {
72 1998 my $n = sprintf '%03s', $_;
73  
74 #From DK::Business::CVR
75 1998 my $sum = _calculate_sum( ( $birthdate . $n ), \@controlcifers );
76 1998 my $mod = $sum % MODULUS_OPERAND_1968;
77  
78 1998 my $checkciffer = ( MODULUS_OPERAND_1968 - $mod );
79  
80 1998 ▶ if ( $checkciffer < 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checkciffer < 10) 1636 362 1998
81 1636 push @cprs, ( $birthdate . $n . $checkciffer );
82 }
83 }
84  
85 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 1 2
86 1 return @cprs;
87 }
88 else {
89 1 return scalar @cprs;
90 }
91 }
92  
93 1 ▶sub validateCPR {
Subroutine validateCPR: called CC=1 SCAR=0.0
Pod documented
94  
95 #We postpone parameter validation
96 1 return validate(shift);
97 }
98  
99 861 ▶sub _length {
Subroutine _length: called CC=2 SCAR=6.9
100 861 my ( $number, $length ) = @_;
101  
102 861 ▶ if ( length($number) != $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $number != $length) 2 859 861
103 2 croak "argument: $number has to be $length digits long";
104 }
105 859 return 1;
106 }
107  
108 14 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
109 14 my ($controlnumber) = @_;
110  
111 #We postpone parameter validation
112  
113 14 my $rv;
114 14 ▶ if ( $rv = validate1968($controlnumber) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv = validate1968($controlnumber)) { } 2 7 9
115 2 return $rv;
116 }
117 else {
118 7 return validate2007($controlnumber);
119 }
120 }
121  
122 29 ▶sub validate2007 {
Subroutine validate2007: called CC=11 SCAR=24.5
Pod documented
123 29 my ($controlnumber) = @_;
124 29 validate_pos( @_, { type => SCALAR, regex => qr/^\d+$/ } );
125  
126 27 _checkdate( substr $controlnumber, 0, DATE_LENGTH );
127 23 _assert_controlnumber($controlnumber);
128  
129 23 my $control = substr $controlnumber, DATE_LENGTH, CONTROL_CIFER_LENGTH;
130  
131 23 my $remainder = $control % MODULUS_OPERAND_2007;
132  
133 #Our own merge, could be Hash::Merge's merge
134 23 my %seeds = %{ merge( \%male_seeds, \%female_seeds ) };
135  
136 23 ▶ if ( my $series = $seeds{$remainder} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $series = $seeds{$remainder}) { } 13 10 23
elsif ($control == 0 || $control == 6 and $remainder == 0) { } 4 12 16
137 13 ▶ if ( $control < $seeds{$remainder}->{min} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($control < $seeds{$remainder}{'min'}) { } 11 2 13
elsif ($control > $seeds{$remainder}{'max'}) { } 0 2 2
138 11 return INVALID;
139 }
140 elsif ( $control > $seeds{$remainder}->{max} ) {
141 0 return INVALID;
142 }
143 }
144 ▶ elsif ( ( $control == 0 or $control == 6 ) && $remainder == 0 ) {
Condition83%5/6
$control == 0 || $control == 6 and $remainder == 0
!ll&&!rl&&r
604
$control == 0 or $control == 6
l!l&&r!l&&!r
226
MC/DC66%2/3
$control == 0 || $control == 6 and $remainder == 0
$control == 0 $control == 6 $remainder == 0
Truth table60%3/5
A or B and C
A= $control == 0B= $control == 6C= $remainder == 0
ABCresult
00X0
1X00
0100
1X11
0111
145 4 return INVALID;
146 }
147  
148 8 ▶ if ( $female_seeds{$remainder} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($female_seeds{$remainder}) { } 1 7 8
elsif ($male_seeds{$remainder}) { } 1 6 7
elsif ($remainder == 0) { } 6 0 6
149 1 return VALID_FEMALE;
150 }
151 elsif ( $male_seeds{$remainder} ) {
152 1 return VALID_MALE;
153 }
154 elsif ( $remainder == 0 ) {
155 6 ▶ if ( _is_equal($control) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_equal($control)) { } 6 0 6
156 6 return VALID_FEMALE;
157 }
158 else {
159 0 return VALID_MALE;
160 }
161 }
162 else {
163 0 return INVALID;
164 }
165 }
166  
167 846 ▶sub validate1968 {
Subroutine validate1968: called CC=3 SCAR=11.0
Pod documented
168 846 my ($controlnumber) = @_;
169  
170 846 validate_pos( @_, { type => SCALAR, regex => qr/^\d+$/ } );
171  
172 842 _checkdate( substr $controlnumber, 0, DATE_LENGTH );
173 835 _assert_controlnumber($controlnumber);
174  
175 835 my $sum = _calculate_sum( $controlnumber, \@controlcifers );
176  
177 #Note this might look like it is turned upside down but no rest from the
178 #modulus calculation indicated validity
179 835 ▶ if ( $sum % MODULUS_OPERAND_1968 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sum % 11) { } 9 826 835
180 9 return INVALID;
181 }
182 else {
183 826 ▶ if ( _is_equal($sum) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_equal($sum)) { } 411 415 826
184 411 return VALID_MALE;
185 }
186 else {
187 415 return VALID_FEMALE;
188 }
189 }
190 }
191  
192 832 ▶sub _is_equal {
Subroutine _is_equal: called CC=1 SCAR=0.0
193 832 my ($operand) = @_;
194  
195 832 validate_pos( @_, { type => SCALAR, regex => qr/^\d+$/ } );
196  
197 832 return ( not( $operand % 2 ) );
198 }
199  
200 863 ▶sub _assert_controlnumber {
Subroutine _assert_controlnumber: called CC=1 SCAR=0.0
201 863 my ($controlnumber) = @_;
202  
203 863 validate_pos( @_, { type => SCALAR, regex => qr/^\d+$/ } );
204  
205 861 _length( $controlnumber, scalar @controlcifers );
206  
207 859 return VALID;
208 }
209  
210 879 ▶sub _checkdate {
Subroutine _checkdate: called CC=2 SCAR=6.9
211  
212 879 my $dateregex = qr{
213 \A #beginning of line
214 (\d{2}) #day of month, 2 digit representation, 01-31
215 (\d{2}) #month, 2 digit representation jan 01 - dec 12
216 (\d{2}) #year, 2 digit representation
217 \Z #end of line
218 }xsm;
219  
220 #According to the documentation validate_pos gets two paramters, hence the
221 #second optional argument specification
222 879 validate_pos(
223 @_,
224 { type => SCALAR, regex => $dateregex },
225 { type => ARRAYREF, optional => 1 },
226 );
227  
228 #Params::Validate does not capture for us, so we re-do our regex
229 870 $_[0] =~ m/$dateregex/;
230  
231 870 ▶ if ( not check_date( $3, $2, $1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (check_date($3, $2, $1)) 9 861 870
232 9 croak "argument: $_[0] has to be a valid date in the format: ddmmyy";
233 }
234  
235 861 return VALID;
236 }
237  
238 2 ▶sub generate {
Subroutine generate: called CC=5 SCAR=31.3
Pod documented
239 2 my ( $birthdate, $gender ) = @_;
240  
241 2 validate_pos(
242 @_,
243 { type => SCALAR, callbacks => { 'date' => \&_checkdate }, },
244 { type => SCALAR, optional => 1, default => q{} },
245 );
246  
247 0 my @genders;
248  
249 0 ▶ if ($gender) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gender) { } 0 0 0
250 0 push @genders, $gender;
251 }
252 else {
253 0 @genders = qw(male female);
254 }
255  
256 0 my %cprs;
257 0 foreach my $g (@genders) {
258 0 my @cprs2007 = generate2007( $birthdate, $g );
259  
260 0 my $i = 1;
261 0 foreach my $cpr (@cprs2007) {
262 0 $cprs{$cpr}++;
263 }
264 }
265  
266 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
267 0 return keys %cprs;
268 }
269 else {
270 0 return scalar keys %cprs;
271 }
272 }
273  
274 2 ▶sub generate2007 {
Subroutine generate2007: called CC=8 SCAR=41.1
Pod documented
275 2 my ( $birthdate, $gender ) = @_;
276  
277 #TODO assert gender?
278 2 validate_pos(
279 @_,
280 { type => SCALAR, callbacks => { 'date' => \&_checkdate }, },
281 { type => SCALAR, optional => 1 },
282 );
283  
284 0 my @cprs;
285 my %seeds;
286  
287 0 ▶ if ( defined $gender ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gender) 0 0 0
288 0 ▶ if ( $gender eq MALE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($gender eq 'male') { } 0 0 0
elsif ($gender eq 'female') { } 0 0 0
289 0 %seeds = %male_seeds;
290 }
291 elsif ( $gender eq FEMALE ) {
292 0 %seeds = %female_seeds;
293 }
294 else {
295 0 carp("Unknown gender: $gender, assuming no gender");
296 0 $gender = undef;
297 }
298 }
299  
300 0 ▶ if ( not $gender ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gender) 0 0 0
301 0 %seeds = %{ merge( \%female_seeds, \%male_seeds ) };
302 }
303  
304 0 foreach my $seed ( keys %seeds ) {
305 0 my $s = $seeds{$seed}->{min};
306 0 ▶ while ( $s < $seeds{$seed}->{max} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s < $seeds{$seed}{"max"}) 0 0 0
307 0 $s += MODULUS_OPERAND_2007;
308 0 push @cprs, ( $birthdate . sprintf '%04d', $s );
309 }
310 }
311  
312 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
313 0 return @cprs;
314 }
315 else {
316 0 return scalar @cprs;
317 }
318 }
319  
320 2 ▶sub generate1968 {
Subroutine generate1968: called CC=12 SCAR=49.2
Pod documented
321 2 my ( $birthdate, $gender ) = @_;
322  
323 #TODO assert gender?
324 2 validate_pos(
325 @_,
326 { type => SCALAR, callbacks => { 'date' => \&_checkdate }, },
327 { type => SCALAR, optional => 1, default => q{} },
328 );
329  
330 0 my @cprs;
331 my @malecprs;
332 0 my @femalecprs;
333  
334 0 my $checksum = 0;
335  
336 0 ▶ while ( $checksum < 9999 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checksum < 9999) 0 0 0
337  
338 0 my $cpr = $birthdate . sprintf '%04d', $checksum;
339  
340 0 ▶ if ( my $rv = validate1968($cpr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rv = validate1968($cpr)) 0 0 0
341  
342 0 ▶ if ( defined $gender and $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gender and $rv) { } 0 0 0
Condition0%0/3
defined $gender and $rv
!ll&&!rl&&r
000
MC/DC0%0/2
defined $gender and $rv
defined $gender $rv
Truth table0%0/3
A and B
A= defined $genderB= $rv
ABresult
0X0
100
111
343 0 ▶ if ( $rv == VALID_MALE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv == 1) { } 0 0 0
elsif ($rv == 2) { } 0 0 0
344 0 push @malecprs, $cpr;
345 }
346 elsif ( $rv == VALID_FEMALE ) {
347 0 push @femalecprs, $cpr;
348 }
349  
350 }
351 else {
352 0 push @cprs, $cpr;
353 }
354 }
355 0 $checksum++;
356 }
357  
358 0 ▶ if ( $gender and $gender eq FEMALE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($gender and $gender eq 'female') { } 0 0 0
elsif ($gender and $gender eq 'male') { } 0 0 0
Condition0%0/3
$gender and $gender eq "female"
!ll&&!rl&&r
000
MC/DC0%0/2
$gender and $gender eq "female"
$gender $gender eq "female"
Truth table0%0/3
A and B
A= $genderB= $gender eq "female"
ABresult
0X0
100
111
359 0 @cprs = @femalecprs;
360 }
361 ▶ elsif ( $gender and $gender eq MALE ) {
Condition0%0/3
$gender and $gender eq "male"
!ll&&!rl&&r
000
MC/DC0%0/2
$gender and $gender eq "male"
$gender $gender eq "male"
Truth table0%0/3
A and B
A= $genderB= $gender eq "male"
ABresult
0X0
100
111
362 0 @cprs = @malecprs;
363 }
364  
365 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
366 0 return @cprs;
367 }
368 else {
369 0 return scalar @cprs;
370 }
371 }
372  
373 16 1;
374  
375 __END__
↑ Summary lib/Data/FormValidator/Constraints/Business/DK/CPR.pm »