blib/lib/String/CodiceFiscale.pm

filter: Statementstmt 90.7% 373 / 411 Branchbran 83.5% 167 / 200 Conditioncond 72.8% 51 / 70 MC/DCmcdc 56.1% 23 / 41 Subroutinesub 97.5% 39 / 40 Podpod 61.5% 16 / 26 totaltotal 84.9% 669 / 788 CC 129 SCAR 21.4
CC
129
Cov
87%
_n_re
26.5
fn_c
13.0
day_c
11.5
CRAP 8.5
↑ Summary
1 package String::CodiceFiscale;
2  
3 1 $String::CodiceFiscale::VERSION = '0.02';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use base qw(Class::Data::Inheritable);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our %CRC = (
14 A => [0, 1], B => [1, 0], C => [2, 5],
15 D => [3, 7], E => [4, 9], F => [5, 13],
16 G => [6, 15], H => [7, 17], I => [8, 19],
17 J => [9, 21], K => [10, 2], L => [11, 4],
18 M => [12, 18], N => [13, 20], O => [14, 11],
19 P => [15, 3], Q => [16, 6], R => [17, 8],
20 S => [18, 12], T => [19, 14], U => [20, 16],
21 V => [21, 10], W => [22, 22], X => [23, 25],
22 Y => [24, 24], Z => [25, 23], 0 => [0, 1],
23 1 => [1, 0], 2 => [2, 5], 3 => [3, 7],
24 4 => [4, 9], 5 => [5, 13], 6 => [6, 15],
25 7 => [7, 17], 8 => [8, 19], 9 => [9, 21],
26 );
27  
28 1 __PACKAGE__->mk_classdata('ERROR');
29  
30 1 our ($MONTHS, @MONTHS, %MONTHS); #code to/from month
31 1 @MONTHS[1..12] = qw(A B C D E H L M P R S T);
32 1 @MONTHS{@MONTHS[1..12]} = 1..12;
33 1 $MONTHS = join '', @MONTHS[1..12];
34  
35 1 our ($XNUMS, @XNUMS, %XNUMS); #coded numbers for rare collision cases
36 1 @XNUMS = qw(L M N P Q R S T U V);
37 1 @XNUMS{@XNUMS} = 0..9; #not used anymore, but here "just in case"
38 1 $XNUMS = join '', @XNUMS;
39  
40 1 our $CONSONANTS = 'BCDFGHJKLMNPQRSTVWXYZ';
41 1 our $VOWELS = 'AEIOU';
42  
43 1 our $RE_cf = qr/
44 ^ #start
45 ([A-Z]{3}) #surname coded
46 ([A-Z]{3}) #firstname coded
47 ([\d$XNUMS]{2}) #year
48 ([$MONTHS]) #month coded
49 ([\d$XNUMS]{2}) #day and sex
50 ([A-Z][\d$XNUMS]{3}) #birthplace coded
51 ([A-Z]) #crc
52 $ #end
53 /xo;
54  
55 1 our $RE_nc = qr/^[$CONSONANTS]*[$VOWELS]*X*$/xo;
56  
57 1 our %OPTS = map {$_ => 1} qw(
58 sn sn_c fn fn_c date year year_c
59 month month_c day day_c sex bp bp_c
60 );
61  
62 12 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
63 12 my $class = shift;
64 12 my $self = bless {}, $class;
65 12 ▶ while (my ($k, $v) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 57 12 69
66 57 ▶ $self->_croak(qq(Not such an options "$k")) unless $OPTS{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $String::CodiceFiscale::OPTS{$k} 0 57 57
67 57 $self->$k($v);
68 }
69 12 return $self;
70 }
71  
72 9 ▶sub sn {
Subroutine sn: called CC=2 SCAR=6.9
Pod documented
73 9 my $self = shift;
74 9 my ($sn) = @_;
75 9 ▶ if (defined $sn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sn) 7 2 9
76 7 $sn = uc($sn);
77 7 $self->{sn} = $sn;
78 7 $self->{sn_c} = undef;
79 7 $self->{sn_re} = undef;
80 }
81 9 return $sn;
82 }
83  
84 38 ▶sub sn_c {
Subroutine sn_c: called CC=11 SCAR=24.0
Pod undocumented
85 38 my $self = shift;
86 38 my ($sn_c) = @_;
87 38 ▶ if (defined $sn_c) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sn_c) 17 21 38
88 17 $sn_c = uc($sn_c);
89 17 ▶ unless ($sn_c =~ /$RE_nc/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sn_c =~ /$String::CodiceFiscale::RE_nc/x) 1 16 17
90 1 $self->error('Coded surname cannot contain ' .
91 'vowels followed by consonants');
92 1 return;
93 }
94 16 ▶ unless (length($sn_c) == 3) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $sn_c == 3) 2 14 16
95 2 $self->error('Coded surname must be 3 chars in length');
96 2 return;
97 }
98 14 $self->{sn_c} = $sn_c;
99 14 $self->{sn} = undef;
100 14 $self->{sn_re} = undef;
101 }
102 35 ▶ if (defined $self->{sn} and not defined $self->{sn_c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"sn"} and not defined $self->{"sn_c"}) 5 30 35
Condition100%3/3
defined $self->{"sn"} and not defined $self->{"sn_c"}
!ll&&!rl&&r
2735
MC/DC100%2/2
defined $self->{"sn"} and not defined $self->{"sn_c"}
defined $self->{"sn"} not defined $self->{"sn_c"}
Truth table100%3/3
A and not(B)
A= defined $self->{"sn"}B= not defined $self->{"sn_c"}
ABresult
0X0
100
111
103 5 my $temp = '';
104 OUTER: {
105 5 ▶ while ($self->{sn} =~ /([$CONSONANTS])/go) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sn"} =~ /([$String::CodiceFiscale::CONSONANTS])/go) 11 3 14
106 11 $temp .= $1;
107 11 ▶ last OUTER if length $temp >= 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $temp >= 3 2 9 11
108 }
109 3 ▶ while ($self->{sn} =~ /([$VOWELS])/go) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sn"} =~ /([$String::CodiceFiscale::VOWELS])/go) 3 1 4
110 3 $temp .= $1;
111 3 ▶ last OUTER if length $temp >= 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $temp >= 3 2 1 3
112 }
113 1 ▶ while (length $temp < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $temp < 3) 1 1 2
114 1 $temp .= 'X';
115 }
116 }
117 5 $self->{sn_c} = $temp;
118 }
119 35 return $self->{sn_c};
120 }
121  
122 3 ▶sub sn_re {
Subroutine sn_re: called CC=1 SCAR=0.0
Pod undocumented
123 3 my $self = shift;
124 3 return $self->_n_re('sn_c');
125 }
126  
127  
128 2 ▶sub sn_match {
Subroutine sn_match: called CC=4 SCAR=15.5
Pod documented
129 2 my $self = shift;
130 2 my ($tm) = @_;
131 2 ▶ return unless defined $tm;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tm 0 2 2
132 2 $tm = uc $tm;
133 2 $self->_fix_name($tm);
134 2 ▶ if (defined(my $sn = $self->sn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $sn = $self->sn)) 0 2 2
135 0 $self->_fix_name($sn);
136 0 return $tm eq $self->sn;
137 }
138 2 ▶ if (defined $self->sn_c) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->sn_c) 2 0 2
139 2 return $tm =~ $self->sn_re;
140 }
141 0 return;
142 }
143  
144 13 ▶sub fn {
Subroutine fn: called CC=2 SCAR=6.9
Pod documented
145 13 my $self = shift;
146 13 my ($fn) = @_;
147 13 ▶ if (defined $fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fn) 9 4 13
148 9 $fn = uc($fn);
149 9 $self->{fn} = $fn;
150 9 $self->{fn_c} = undef;
151 9 $self->{fn_re} = undef;
152 }
153 13 return $fn;
154 }
155  
156 46 ▶sub fn_c {
Subroutine fn_c: called CC=13 SCAR=25.6
Pod undocumented
157 46 my $self = shift;
158 46 my ($fn_c) = @_;
159 46 ▶ if (defined $fn_c) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fn_c) 19 27 46
160 19 $fn_c = uc($fn_c);
161 19 ▶ unless ($fn_c =~ /$RE_nc/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fn_c =~ /$String::CodiceFiscale::RE_nc/x) 1 18 19
162 1 $self->error('Coded name cannot contain ' .
163 'vowels followed by consonants');
164 1 return;
165 }
166 18 ▶ unless (length($fn_c) == 3) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $fn_c == 3) 2 16 18
167 2 $self->error('Coded name must be 3 chars in length');
168 2 return;
169 }
170 16 $self->{fn_c} = $fn_c;
171 16 $self->{fn} = undef;
172 16 $self->{fn_re} = undef;
173 }
174 43 ▶ if (defined $self->{fn} and not defined $self->{fn_c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"fn"} and not defined $self->{"fn_c"}) 7 36 43
Condition100%3/3
defined $self->{"fn"} and not defined $self->{"fn_c"}
!ll&&!rl&&r
3337
MC/DC100%2/2
defined $self->{"fn"} and not defined $self->{"fn_c"}
defined $self->{"fn"} not defined $self->{"fn_c"}
Truth table100%3/3
A and not(B)
A= defined $self->{"fn"}B= not defined $self->{"fn_c"}
ABresult
0X0
100
111
175 7 my $temp = '';
176 7 my $skip = $self->_count_consonants($self->{fn}) > 3;
177 OUTER: {
178 7 ▶ while ($self->{fn} =~ /([$CONSONANTS])/go) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fn"} =~ /([$String::CodiceFiscale::CONSONANTS])/go) 19 3 22
179 19 ▶ if ($skip and length($temp) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip and length $temp == 1) 3 16 19
Condition100%3/3
$skip and length $temp == 1
!ll&&!rl&&r
1333
MC/DC100%2/2
$skip and length $temp == 1
$skip length $temp == 1
Truth table100%3/3
A and B
A= $skipB= length $temp == 1
ABresult
0X0
100
111
180 3 $skip = 0;
181 3 next;
182 }
183 16 $temp .= $1;
184 16 ▶ last OUTER if length $temp >= 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $temp >= 3 4 12 16
185 }
186 3 ▶ while ($self->{fn} =~ /([$VOWELS])/go) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fn"} =~ /([$String::CodiceFiscale::VOWELS])/go) 4 1 5
187 4 $temp .= $1;
188 4 ▶ last OUTER if length $temp >= 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $temp >= 3 2 2 4
189 }
190 1 ▶ while (length $temp < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $temp < 3) 1 1 2
191 1 $temp .= 'X';
192 }
193 }
194 7 $self->{fn_c} = $temp;
195 }
196 43 return $self->{fn_c};
197 }
198  
199 5 ▶sub fn_re {
Subroutine fn_re: called CC=1 SCAR=0.0
Pod undocumented
200 5 my $self = shift;
201 5 return $self->_n_re('fn_c');
202 }
203  
204 4 ▶sub fn_match {
Subroutine fn_match: called CC=4 SCAR=15.5
Pod documented
205 4 my $self = shift;
206 4 my ($tm) = @_;
207 4 ▶ return unless defined $tm;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tm 0 4 4
208 4 $tm = uc $tm;
209 4 $self->_fix_name($tm);
210 4 ▶ if (defined(my $fn = $self->fn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fn = $self->fn)) 0 4 4
211 0 $self->_fix_name($fn);
212 0 return $tm eq $fn;
213 }
214 4 ▶ if (defined $self->fn_c) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->fn_c) 4 0 4
215 4 return $tm =~ $self->fn_re;
216 }
217 0 return;
218 }
219  
220  
221 7 ▶sub date {
Subroutine date: called CC=7 SCAR=20.0
Pod documented
222 7 my $self = shift;
223 7 my ($date) = @_;
224 7 ▶ if (defined $date) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $date) { } 6 1 7
225 6 my $t;
226 6 eval { $t = Time::Piece->strptime($date, '%Y-%m-%d') };
227 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 5 6
228 1 $self->error("Invalid date");
229 1 return;
230 }
231 5 my %date = (
232 year => $t->year,
233 month => $t->mon,
234 day => $t->mday,
235 );
236 5 for (qw(year month day)) {
237 15 ▶ unless ( $self->$_($date{$_}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->$_($date{$_})) 0 15 15
238 0 $self->error("Couldn't parse $_");
239 0 return;
240 }
241 }
242 } else {
243 1 my %date;
244 1 for (qw(year month day)) {
245 3 $date{$_} = $self->$_;
246 3 ▶ unless (defined $date{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $date{$_}) 0 3 3
247 0 $self->error("Couldn't retrieve $_");
248 0 return;
249 }
250 }
251 1 return sprintf("%04d-%02d-%02d", @date{qw(year month day)});
252 }
253 5 return $date;
254 }
255  
256 9 ▶sub year {
Subroutine year: called CC=6 SCAR=17.9
Pod documented
257 9 my $self = shift;
258 9 my ($y) = @_;
259 9 ▶ if (defined $y) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $y) 7 2 9
260 7 ▶ unless ($y =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y =~ /^\d+$/) 1 6 7
261 1 $self->error('A year should be an unsigned integer');
262 1 return;
263 }
264 6 $self->{year} = $y;
265 6 $self->{year_c} = undef;
266 }
267 8 ▶ if (not defined $self->{year} and defined $self->{year_c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"year"} and defined $self->{"year_c"}) 2 6 8
Condition66%2/3
not defined $self->{"year"} and defined $self->{"year_c"}
!ll&&!rl&&r
602
MC/DC50%1/2
not defined $self->{"year"} and defined $self->{"year_c"}
not defined $self->{"year"} defined $self->{"year_c"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"year"}B= defined $self->{"year_c"}
ABresult
0X0
100
111
268 2 my $year = $self->_xnums($self->{year_c});
269 2 my $this_year = (localtime(time))[5] + 1900;
270 2 my $twodigits_year = $this_year % 100; # this is making a guess
271 2 my $century = floor($this_year/100);
272 2 ▶ $self->{year} = sprintf "%d%02d",
Branch: 1 branch
BranchTrueFalseTotal
$year > $twodigits_year ? : 2 0 2
273 $century - ($year > $twodigits_year ? 1 : 0),
274 $year;
275 }
276 8 return $self->{year};
277 }
278  
279 22 ▶sub year_c {
Subroutine year_c: called CC=5 SCAR=16.1
Pod undocumented
280 22 my $self = shift;
281 22 my ($ycx) = @_;
282 22 ▶ if (defined $ycx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ycx) 9 13 22
283 9 my $yc = $self->_xnums($ycx);
284 9 ▶ unless ($yc =~ /^\d\d$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($yc =~ /^\d\d$/) 1 8 9
285 1 $self->error('A year in Codice Fiscale is 2 digit long');
286 1 return;
287 }
288 8 $self->{year_c} = $ycx;
289 8 $self->{year} = undef;
290 }
291 21 ▶ if (not defined $self->{year_c} and defined $self->{year}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"year_c"} and defined $self->{"year"}) 1 20 21
Condition66%2/3
not defined $self->{"year_c"} and defined $self->{"year"}
!ll&&!rl&&r
2001
MC/DC50%1/2
not defined $self->{"year_c"} and defined $self->{"year"}
not defined $self->{"year_c"} defined $self->{"year"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"year_c"}B= defined $self->{"year"}
ABresult
0X0
100
111
292 1 $self->{year_c} = sprintf("%02d", $self->{year} % 100);
293 }
294 21 return $self->{year_c};
295 }
296  
297 11 ▶sub month {
Subroutine month: called CC=7 SCAR=19.5
Pod documented
298 11 my $self = shift;
299 11 my ($m) = @_;
300 11 ▶ if (defined $m) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m) 8 3 11
301 8 ▶ unless ($m =~ /^\d+$/ and $m >= 1 and $m <= 12) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($m =~ /^\d+$/ and $m >= 1 and $m <= 12) 2 6 8
Condition83%5/6
$m =~ /^\d+$/ and $m >= 1 and $m <= 12
!ll&&!rl&&r
116
$m =~ /^\d+$/ and $m >= 1
!ll&&!rl&&r
107
MC/DC66%2/3
$m =~ /^\d+$/ and $m >= 1 and $m <= 12
$m =~ /^\d+$/ $m >= 1 $m <= 12
Truth table75%3/4
A and B and C
A= $m =~ /^\d+$/B= $m >= 1C= $m <= 12
ABCresult
0XX0
10X0
1100
1111
302 2 $self->error('Month must be numeric and between 1 and 12');
303 2 return;
304 }
305 6 $self->{month} = $m;
306 6 $self->{month_c} = undef;
307 }
308 9 ▶ if (not defined $self->{month} and defined $self->{month_c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"month"} and defined $self->{"month_c"}) 2 7 9
Condition66%2/3
not defined $self->{"month"} and defined $self->{"month_c"}
!ll&&!rl&&r
702
MC/DC50%1/2
not defined $self->{"month"} and defined $self->{"month_c"}
not defined $self->{"month"} defined $self->{"month_c"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"month"}B= defined $self->{"month_c"}
ABresult
0X0
100
111
309 2 $self->{month} = $MONTHS{$self->{month_c}};
310 }
311 9 return $self->{month};
312 }
313  
314 22 ▶sub month_c {
Subroutine month_c: called CC=5 SCAR=16.5
Pod undocumented
315 22 my $self = shift;
316 22 my ($mc) = @_;
317 22 ▶ if (defined $mc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mc) 8 14 22
318 8 ▶ unless ($mc =~ /^[$MONTHS]$/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mc =~ /^[$String::CodiceFiscale::MONTHS]$/o) 0 8 8
319 0 $self->error('Month not correctly encoded');
320 0 return;
321 }
322 8 $self->{month_c} = $mc;
323 8 $self->{month} = undef;
324 }
325 22 ▶ if (not defined $self->{month_c} and defined $self->{month}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"month_c"} and defined $self->{"month"}) 2 20 22
Condition66%2/3
not defined $self->{"month_c"} and defined $self->{"month"}
!ll&&!rl&&r
2002
MC/DC50%1/2
not defined $self->{"month_c"} and defined $self->{"month"}
not defined $self->{"month_c"} defined $self->{"month"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"month_c"}B= defined $self->{"month"}
ABresult
0X0
100
111
326 2 $self->{month_c} = $MONTHS[$self->{month}];
327 }
328 22 return $self->{month_c};
329 }
330  
331 10 ▶sub day {
Subroutine day: called CC=8 SCAR=21.0
Pod documented
332 10 my $self = shift;
333 10 my ($d) = @_;
334 10 ▶ if (defined $d) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d) 7 3 10
335 7 ▶ unless ($d =~ /^\d+$/ and 1 <= $d and $d <= 31) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($d =~ /^\d+$/ and 1 <= $d and $d <= 31) 1 6 7
Condition50%3/6
$d =~ /^\d+$/ and 1 <= $d and $d <= 31
!ll&&!rl&&r
016
$d =~ /^\d+$/ and 1 <= $d
!ll&&!rl&&r
007
MC/DC33%1/3
$d =~ /^\d+$/ and 1 <= $d and $d <= 31
$d =~ /^\d+$/ 1 <= $d $d <= 31
Truth table50%2/4
A and B and C
A= $d =~ /^\d+$/B= 1 <= $dC= $d <= 31
ABCresult
0XX0
10X0
1100
1111
336 1 $self->error('Day is out of range');
337 1 return;
338 }
339 6 $self->{day} = $d;
340 6 $self->{day_c} = undef;
341 }
342 9 ▶ if (not defined $self->{day} and defined $self->{day_c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"day"} and defined $self->{"day_c"}) 2 7 9
Condition66%2/3
not defined $self->{"day"} and defined $self->{"day_c"}
!ll&&!rl&&r
702
MC/DC50%1/2
not defined $self->{"day"} and defined $self->{"day_c"}
not defined $self->{"day"} defined $self->{"day_c"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"day"}B= defined $self->{"day_c"}
ABresult
0X0
100
111
343 2 my $dayx = $self->_xnums($self->{day_c});
344 2 ▶ $self->{day} = $dayx > 40 ? $dayx - 40 : $dayx;
Branch: 1 branch
BranchTrueFalseTotal
$dayx > 40 ? : 1 1 2
345 }
346 9 return $self->{day};
347 }
348  
349 24 ▶sub day_c {
Subroutine day_c: called CC=11 SCAR=24.4
Pod undocumented
350 24 my $self = shift;
351 24 my ($dcx) = @_;
352 24 ▶ if (defined $dcx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dcx) 9 15 24
353 9 my $dc = $self->_xnums($dcx);
354 9 ▶ unless ($dc =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dc =~ /^\d+$/) 0 9 9
355 0 $self->error('Invalid coding of day');
356 0 return;
357 }
358 9 ▶ unless ($dc > 0 and not ($dc > 31 and $dc < 41) and $dc <= 71) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dc > 0 and not $dc > 31 && $dc < 41 and $dc <= 71) 1 8 9
Condition77%7/9
$dc > 0 and not $dc > 31 && $dc < 41 and $dc <= 71
!ll&&!rl&&r
108
$dc > 0 and not $dc > 31 && $dc < 41
!ll&&!rl&&r
018
$dc > 31 && $dc < 41
!ll&&!rl&&r
621
MC/DC50%2/4
$dc > 0 and not $dc > 31 && $dc < 41 and $dc <= 71
$dc > 0 $dc > 31 $dc < 41 $dc <= 71
Truth table50%3/6
A and not(B && C) and D
A= $dc > 0B= $dc > 31C= $dc < 41D= $dc <= 71
ABCDresult
0XXX0
111X0
10X00
11000
10X11
11011
359 1 $self->error('Day out of range');
360 1 return;
361 }
362 8 $self->{day_c} = $dcx;
363 8 $self->{day} = undef;
364 8 $self->{sex} = undef;
365 }
366 23 ▶ if (not defined $self->{day_c} and defined $self->{day}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"day_c"} and defined $self->{"day"} and defined $self->{"sex"}) 3 20 23
Condition66%4/6
not defined $self->{"day_c"} and defined $self->{"day"} and defined $self->{"sex"}
!ll&&!rl&&r
2003
not defined $self->{"day_c"} and defined $self->{"day"}
!ll&&!rl&&r
2003
MC/DC33%1/3
not defined $self->{"day_c"} and defined $self->{"day"} and defined $self->{"sex"}
not defined $self->{"day_c"} defined $self->{"day"} defined $self->{"sex"}
Truth table50%2/4
not(A) and B and C
A= not defined $self->{"day_c"}B= defined $self->{"day"}C= defined $self->{"sex"}
ABCresult
0XX0
10X0
1100
1111
367 and defined $self->{sex}) {
368 3 $self->{day_c} = $self->{day};
369 3 ▶ $self->{day_c} += 40 if $self->{sex} eq 'F';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sex"} eq "F" 1 2 3
370 3 $self->{day_c} = sprintf("%02d", $self->{day_c});
371 }
372 23 return $self->{day_c};
373 }
374  
375 7 ▶sub sex {
Subroutine sex: called CC=6 SCAR=18.2
Pod documented
376 7 my $self = shift;
377 7 my ($sex) = @_;
378 7 ▶ if (defined $sex) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sex) 5 2 7
379 5 ▶ unless ($sex =~ /^[MF]$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sex =~ /^[MF]$/i) 0 5 5
380 0 $self->error('Sex can be either "M" or "F"');
381 0 return;
382 }
383 5 $self->{sex} = $sex;
384 5 $self->{day_c} = undef;
385 }
386 7 ▶ if (not defined $self->{sex} and defined $self->{day_c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"sex"} and defined $self->{"day_c"}) 2 5 7
Condition66%2/3
not defined $self->{"sex"} and defined $self->{"day_c"}
!ll&&!rl&&r
502
MC/DC50%1/2
not defined $self->{"sex"} and defined $self->{"day_c"}
not defined $self->{"sex"} defined $self->{"day_c"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"sex"}B= defined $self->{"day_c"}
ABresult
0X0
100
111
387 2 my $dayx = $self->_xnums($self->{day_c});
388 2 ▶ $self->{sex} = $dayx > 40 ? 'F' : 'M';
Branch: 1 branch
BranchTrueFalseTotal
$dayx > 40 ? : 1 1 2
389 }
390 7 return $self->{sex};
391 }
392  
393 9 ▶sub bp {
Subroutine bp: called CC=5 SCAR=16.1
Pod documented
394 9 my $self = shift;
395 9 my ($bp) = @_;
396 9 ▶ if (defined $bp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bp) 6 3 9
397 6 ▶ unless ($bp =~ /^[A-Z]\d\d\d$/) { # to improve further?
Branch: 1 branch
BranchTrueFalseTotal
unless ($bp =~ /^[A-Z]\d\d\d$/) 2 4 6
398 2 $self->error('Invalid birthplace code');
399 2 return;
400 }
401 4 $self->{bp} = $bp;
402 4 $self->{bp_c} = undef;
403 }
404 7 ▶ if (not defined $self->{bp} and defined $self->{bp_c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"bp"} and defined $self->{"bp_c"}) 2 5 7
Condition66%2/3
not defined $self->{"bp"} and defined $self->{"bp_c"}
!ll&&!rl&&r
502
MC/DC50%1/2
not defined $self->{"bp"} and defined $self->{"bp_c"}
not defined $self->{"bp"} defined $self->{"bp_c"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"bp"}B= defined $self->{"bp_c"}
ABresult
0X0
100
111
405 2 my $bpc = $self->{bp_c};
406 2 substr($bpc, 1) = $self->_xnums(substr($bpc, 1));
407 2 $self->{bp} = $bpc;
408 }
409 7 return $self->{bp};
410 }
411  
412 23 ▶sub bp_c {
Subroutine bp_c: called CC=5 SCAR=16.1
Pod undocumented
413 23 my $self = shift;
414 23 my ($bpcx) = @_;
415 23 ▶ if (defined $bpcx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bpcx) 10 13 23
416 10 my $bpc = $bpcx;
417 10 substr($bpc, 1) = $self->_xnums(substr($bpc, 1));
418 10 ▶ unless ($bpc =~ /^[A-Z]\d\d\d$/) { # to improve further?
Branch: 1 branch
BranchTrueFalseTotal
unless ($bpc =~ /^[A-Z]\d\d\d$/) 1 9 10
419 1 $self->error('Invalid birthplace code');
420 1 return;
421 }
422 9 $self->{bp_c} = $bpcx;
423 9 $self->{bp} = undef;
424 }
425 22 ▶ if (not defined $self->{bp_c} and defined $self->{bp}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"bp_c"} and defined $self->{"bp"}) 1 21 22
Condition66%2/3
not defined $self->{"bp_c"} and defined $self->{"bp"}
!ll&&!rl&&r
2101
MC/DC50%1/2
not defined $self->{"bp_c"} and defined $self->{"bp"}
not defined $self->{"bp_c"} defined $self->{"bp"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"bp_c"}B= defined $self->{"bp"}
ABresult
0X0
100
111
426 1 $self->{bp_c} = $self->{bp};
427 }
428 22 return $self->{bp_c};
429 }
430  
431 13 ▶sub bd_c {
Subroutine bd_c: called CC=3 SCAR=11.6
Pod undocumented
432 13 my $self = shift;
433 13 my $bdc = '';
434 13 for (qw(year_c month_c day_c)) {
435 39 my $t = $self->$_;
436 39 ▶ unless (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $t) 0 39 39
437 0 $self->error("Could not produce $_: some data is missing");
438 0 return;
439 }
440 39 $bdc .= $t;
441 }
442 13 return $bdc;
443 }
444  
445 5 ▶sub cf {
Subroutine cf: called CC=1 SCAR=0.0
Pod documented
446 5 my ($self, $dupe) = (@_);
447 5 return $self->_crc(1, $dupe);
448 }
449  
450 8 ▶sub crc {
Subroutine crc: called CC=1 SCAR=0.0
Pod documented
451 8 my $self = shift;
452 8 return $self->_crc(0);
453 }
454  
455 13 ▶sub cf_nocrc {
Subroutine cf_nocrc: called CC=3 SCAR=11.6
Pod undocumented
456 13 my $self = shift;
457 13 my $cf = '';
458 13 for (qw(sn_c fn_c bd_c bp_c)) {
459 52 my $t = $self->$_;
460 52 ▶ unless (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $t) 0 52 52
461 0 $self->error("Could not produce $_: some data is missing");
462 0 return;
463 }
464 52 $cf .= $t;
465 }
466
467 13 return $cf;
468 }
469  
470 13 ▶sub _crc {
Subroutine _crc: called CC=7 SCAR=19.5
471 13 my $self = shift;
472 13 my ($cf_out, $dupe) = @_;
473 13 my $cf = $self->cf_nocrc;
474 13 ▶ unless ($cf) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cf) 0 13 13
475 0 $self->error("Cannot produce a Codice Fiscale: missing data");
476 0 return;
477 }
478 13 ▶ if ($dupe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dupe) 3 10 13
479 3 $dupe %= 128;
480 3 my @bitmap = split('', sprintf("%07b", _bmaker($dupe)));
481 3 my ($tcf, $i) = ($cf, 0);
482 3 ▶ while ($cf =~ /(\d)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cf =~ /(\d)/g) 21 3 24
483 21 ▶ substr($tcf, pos($cf) - 1, 1, $XNUMS[$1]) if $bitmap[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $bitmap[$i] 9 12 21
484 21 $i++
485 }
486 3 $cf = $tcf;
487 }
488 13 my $count = 0;
489 13 ▶ for (my $i = 0; $i <= 14; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 14) 195 13 208
490 195 $count += $CRC{substr($cf, $i, 1)}[($i + 1) % 2];
491 }
492 13 $count %= 26;
493 13 ▶ return ($cf_out ? $cf : '') . chr(65 + $count);
Branch: 1 branch
BranchTrueFalseTotal
$cf_out ? : 5 8 13
494 }
495  
496  
497 7 ▶sub parse {
Subroutine parse: called CC=4 SCAR=14.8
Pod documented
498 7 my $proto = shift;
499 7 my ($cf) = @_;
500 7 $cf = uc $cf;
501 7 ▶ unless (length($cf) == 16) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $cf == 16) 0 7 7
502 0 $proto->error('A valid Codice Fiscale must be exactly 16 chars long');
503 0 return;
504 }
505 7 my ($sn, $fn, $year, $month, $dayx, $born, $crc) = $cf =~ /$RE_cf/;
506 7 ▶ unless ($crc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($crc) 0 7 7
507 0 $proto->error('Cannot parse: invalid format');
508 0 return;
509 }
510  
511 7 my $obj = $proto->new(
512 sn_c => $sn,
513 fn_c => $fn,
514 year_c => $year,
515 month_c => $month,
516 day_c => $dayx,
517 bp_c => $born,
518 );
519  
520 7 ▶ unless ($crc eq $obj->crc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($crc eq $obj->crc) 1 6 7
521 1 $proto->error('Invalid control character');
522 1 return;
523 }
524 6 return $obj;
525 }
526  
527 2 ▶sub validate {
Subroutine validate: called CC=2 SCAR=7.4
Pod documented
528 2 my $proto = shift;
529 2 my ($cf) = @_;
530 2 my $obj = $proto->parse($cf);
531 2 ▶ return 1 if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 2 0 2
532 0 return;
533 }
534  
535  
536 17 ▶sub error {
Subroutine error: called CC=4 SCAR=14.0
Pod documented
537 17 my $proto = shift;
538 17 my ($err) = @_;
539 17 ▶ if (ref $proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 16 1 17
540 16 ▶ $proto->{_err} = $err if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 16 0 16
541 16 return $proto->{_err};
542 }
543
544 1 ▶ $proto->ERROR($err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 1 0 1
545 1 return $proto->ERROR;
546 }
547  
548 {
549  
550 1 my $tr_xnums = eval "sub {\$_[0] =~ tr/$XNUMS/0123456789/}";
551  
552 36 ▶sub _xnums {
Subroutine _xnums: called CC=2 SCAR=7.0
553 36 my $self = shift;
554 36 my ($nums) = @_;
555 36 ▶ return unless $nums =~ /^[\d$XNUMS]+$/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $nums =~ /^[\d$String::CodiceFiscale::XNUMS]+$/o 0 36 36
556 36 $tr_xnums->($nums);
557 36 return $nums;
558 }
559  
560 }
561  
562 1 ▶sub _n_re {
Subroutine _n_re: called CC=14 SCAR=32.8
563 8 my $self = shift;
564 8 my ($method) = @_;
565 8 (my $attr = $method) =~ s/_c$/_re/;
566 8 ▶ return $self->{$attr} if defined $self->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$attr} 2 6 8
567 6 my $nc = $self->$method;
568 6 ▶ unless ($nc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nc) 0 6 6
569 0 ▶ $self->error('There is no coded ' .
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'sn_c' ? : 0 0 0
570 ($method eq 'sn_c' ? 'sur' : '') . 'name set');
571 0 return;
572 }
573
574 6 my ($c, $v, $x) = $nc =~ /^([$CONSONANTS]*)([$VOWELS]*)(X*)$/o;
575 6 my $pat;
576  
577 6 ▶ if (3 == length $c) {
Branch: 6 branches
BranchTrueFalseTotal
if (3 == length $c) { } 4 2 6
elsif (2 == length $c and 1 == length $v) { } 1 1 2
elsif (1 == length $c and 2 == length $v) { } 0 1 1
elsif (3 == length $v) { } 0 1 1
elsif (1 == length $x) { } 1 0 1
elsif (2 == length $x) { } 0 0 0
578 4 my @c = split('', $c);
579 4 ▶ if ($method eq 'fn_c') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'fn_c') { } 4 0 4
580 4 $pat = qr/^(?:
581 [$VOWELS]* $c[0] [$VOWELS]*
582 [$CONSONANTS] [$VOWELS]*
583 $c[1] [$VOWELS]*
584 $c[2] [A-Z]*
585 |
586 [$VOWELS]* $c[0] [$VOWELS]*
587 $c[1] [$VOWELS]*
588 $c[2] [$VOWELS]*
589 )$/xi;
590 } else {
591 0 $pat = qr/^
592 [$VOWELS]* $c[0] [$VOWELS]*
593 $c[1] [$VOWELS]*
594 $c[2] [A-Z]*
595 $/xi;
596 }
597 ▶ } elsif (2 == length($c) and 1 == length($v)) {
Condition66%2/3
2 == length $c and 1 == length $v
!ll&&!rl&&r
101
MC/DC50%1/2
2 == length $c and 1 == length $v
2 == length $c 1 == length $v
Truth table66%2/3
A and B
A= 2 == length $cB= 1 == length $v
ABresult
0X0
100
111
598 1 my @c = split('', $c);
599 1 $pat = qr/^(?:
600 $v [$VOWELS]* $c[0] [$VOWELS]* $c[1] [$VOWELS]*
601 |
602 $c[0] $v [$VOWELS]* $c[1] [$VOWELS]*
603 |
604 $c[0] $c[1] $v [$VOWELS]*
605 )$/xi;
606 ▶ } elsif (1 == length($c) and 2 == length($v)) {
Condition33%1/3
1 == length $c and 2 == length $v
!ll&&!rl&&r
010
MC/DC0%0/2
1 == length $c and 2 == length $v
1 == length $c 2 == length $v
Truth table33%1/3
A and B
A= 1 == length $cB= 2 == length $v
ABresult
0X0
100
111
607 0 my @v = split('', $v);
608 0 $pat = qr/^(?:
609 $c $v[0] $v[1] [$VOWELS]*
610 |
611 $v[0] $c $v[1] [$VOWELS]*
612 |
613 $v[0] $v[1] [$VOWELS]* $c [$VOWELS]*
614 )$/xi;
615 } elsif (3 == length $v) {
616 0 $pat = qr/^ $v [$VOWELS]* $/xi;
617 } elsif (1 == length $x) {
618 1 ▶ if (1 == length($c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == length $c) { } 1 0 1
619 1 $pat = qr/^(?: $c $v | $v $c )$/xi;
620 } else {
621 0 $pat = qr/^ $v $/xi;
622 }
623 } elsif (2 == length $x) {
624 0 $pat = qr/^ $v $/xi;
625 } else {
626 0 $pat = qr/^ .* $/xi;
627 }
628
629 6 return $self->{$attr} = $pat;
630 }
631  
632 6 ▶sub _fix_name {
Subroutine _fix_name: called CC=1 SCAR=0.0
633 6 $_[1] =~ tr/àÀèéÈÉìÌòÒùÙ/AAEEEEIIOOUU/;
634 6 $_[1] =~ tr/a-zA-Z//cd;
635 }
636  
637 132 ▶sub _bmaker {
Subroutine _bmaker: called CC=11 SCAR=24.0
638 132 my ($value, $bit_length, $bits2use, $root, $c) = @_;
639 132 ▶ unless (defined $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 3 129 132
640 3 my $cc = 0;
641 3 $c = \$cc;
642 }
643 132 ▶ $bits2use ||= 1;
Condition100%2/2
$bits2use ||= 1
l!l
1293
MC/DC100%1/1
$bits2use ||= 1
$bits2use
Truth table100%2/2
A
A= $bits2use
Aresult
11
00
644 132 ▶ $root = 1 unless defined $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 3 129 132
645 132 ▶ $bit_length ||= 7;
Condition100%2/2
$bit_length ||= 7
l!l
1293
MC/DC100%1/1
$bit_length ||= 7
$bit_length
Truth table100%2/2
A
A= $bit_length
Aresult
11
00
646  
647 132 my $b = 0; my $sum = 0;
648  
649 132 ▶ while ($root) { # the root function increases the number of encoded chars
Branch: 1 branch
BranchTrueFalseTotal
if ($root) 9 129 138
650 9 $sum = _bmaker($value, $bit_length, $bits2use, 0, $c);
651 9 ▶ return $sum if $$c == $value;
Branch: 1 branch
BranchTrueFalseTotal
if $$c == $value 3 6 9
652 6 $bits2use++;
653 6 ▶ croak("Something went terribly wrong") if $bits2use > $bit_length;
Branch: 1 branch
BranchTrueFalseTotal
if $bits2use > $bit_length 0 6 6
654 }
655
656 129 ▶ while ($b <= ($bit_length - $bits2use)) { #we recursively move the
Branch: 1 branch
BranchTrueFalseTotal
if ($b <= $bit_length - $bits2use) 250 120 370
657 #encoded chars to the left
658 250 ▶ if ($bits2use > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits2use > 1) { } 120 130 250
659 120 $sum = _bmaker($value, $bit_length - 1 - $b, $bits2use - 1, 0, $c);
660 } else {
661 130 $$c++;
662 }
663 #when we reach the desired value, we sum the "bits" in a number
664 #to be used as a binary bitmap for which chars to substitute
665 250 ▶ return 2 ** ($b+1) * $sum + 2 ** $b if $$c == $value;
Branch: 1 branch
BranchTrueFalseTotal
if $$c == $value 9 241 250
666 241 $b++;
667 }
668
669 120 return $sum;
670 }
671  
672  
673  
674 {
675  
676 1 my $count_consonants = eval "sub {\$_[0] =~ tr/$CONSONANTS/$CONSONANTS/}";
677  
678 7 ▶sub _count_consonants { return $count_consonants->($_[1]) }
Subroutine _count_consonants: called CC=1 SCAR=0.0
679  
680 }
681  
682  
683 1 ▶sub _croak {
Subroutine _croak: not called CC=1 SCAR=2.6
684 0 my $self = shift;
685 0 confess @_;
686 }
687  
688 1 1;
689 __END__
↑ Summary