blib/lib/Algorithm/CheckDigits/M11_004.pm

filter: Statementstmt 93.7% 60 / 64 Branchbran 61.5% 16 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod 80.0% 4 / 5 totaltotal 83.0% 93 / 112 CC 15 SCAR 11.0
CC
15
Cov
83%
_compute_checkdigit
5.0
basenumber
3.5
checkdigit
3.5
CRAP 3.0
« blib/lib/Algorithm/CheckDigits/M11_003.pm ↑ Summary blib/lib/Algorithm/CheckDigits/M11_011.pm »
1 package Algorithm::CheckDigits::M11_004;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use version; our $VERSION = 'v1.3.6';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our @ISA = qw(Algorithm::CheckDigits);
11  
12 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
13 2 my $proto = shift;
14 2 my $type = shift;
15 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
16 2 my $self = bless({}, $class);
17 2 $self->{type} = lc($type);
18 2 return $self;
19 } # new()
20  
21 4 ▶sub is_valid {
Subroutine is_valid: called CC=2 SCAR=7.6
Pod documented
22 4 my ($self,$number) = @_;
23 4 ▶ if ($number =~ /^([-\d.]+)(\d\d)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ /^([-\d.]+)(\d\d)$/) 4 0 4
24 4 return $2 eq $self->_compute_checkdigit($1);
25 }
26 0 return ''
27 } # is_valid()
28  
29 2 ▶sub complete {
Subroutine complete: called CC=3 SCAR=12.0
Pod documented
30 2 my ($self,$number) = @_;
31 2 ▶ if ($number =~ /^[-\d.]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ /^[-\d.]+$/) 2 0 2
32 2 my $cd = $self->_compute_checkdigit($number);
33 2 ▶ return $number . $cd unless 0 > $cd;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 > $cd 2 0 2
34 }
35 0 return '';
36 } # complete()
37  
38 2 ▶sub basenumber {
Subroutine basenumber: called CC=3 SCAR=12.5
Pod documented
39 2 my ($self,$number) = @_;
40 2 ▶ if ($number =~ /^([-\d.]+)(\d\d)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ /^([-\d.]+)(\d\d)$/) 2 0 2
41 2 ▶ return $1 if ($2 eq $self->_compute_checkdigit($1));
Branch: 1 branch
BranchTrueFalseTotal
if $2 eq $self->_compute_checkdigit($1) 2 0 2
42 }
43 0 return '';
44 } # basenumber()
45  
46 2 ▶sub checkdigit {
Subroutine checkdigit: called CC=3 SCAR=12.5
Pod documented
47 2 my ($self,$number) = @_;
48 2 ▶ if ($number =~ /^([-\d.]+)(\d\d)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ /^([-\d.]+)(\d\d)$/) 2 0 2
49 2 ▶ return $2 if ($2 eq $self->_compute_checkdigit($1));
Branch: 1 branch
BranchTrueFalseTotal
if $2 eq $self->_compute_checkdigit($1) 2 0 2
50 }
51 0 return '';
52 } # checkdigit()
53  
54 10 ▶sub _compute_checkdigit {
Subroutine _compute_checkdigit: called CC=5 SCAR=16.1
55 10 my $self = shift;
56 10 my $number = shift;
57 10 my ($cd1,$cd2) = ('','');
58  
59 20 ▶ my $calc_cd = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
60 20 my $number = shift;
61 20 my $weight = shift;
62 20 my @digits = split(//,$number);
63 20 my $sum = 0;
64 20 ▶ for (my $i = 0; $i <= $#digits; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#digits) 150 20 170
65 150 $sum += $weight * $digits[$i];
66 150 --$weight;
67 };
68 20 $sum %= 11;
69 20 ▶ return 0 if (2 > $sum);
Branch: 1 branch
BranchTrueFalseTotal
if 2 > $sum 10 10 20
70 10 return 11 - $sum;
71 10 };
72  
73 10 ▶ return -1 unless ($number =~ /^[-\d.]+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $number =~ /^[-\d.]+$/ 0 10 10
74  
75 10 $number =~ s/[-.]//g;
76 10 ▶ if ('cpf' eq $self->{type}) {
Branch: 2 branches
BranchTrueFalseTotal
if ('cpf' eq $self->{'type'}) { } 5 5 10
elsif ('titulo_eleitor' eq $self->{'type'}) { } 5 0 5
77 5 ▶ return -1 unless length($number) == 9;
Branch: 1 branch
BranchTrueFalseTotal
unless length $number == 9 0 5 5
78 5 $cd1 = $calc_cd->($number,10);
79 5 $cd2 = $calc_cd->($number . $cd1,11);
80 } elsif ('titulo_eleitor' eq $self->{type}) {
81 5 $number = substr("00000000000" . $number, -10);
82 5 $cd1 = $calc_cd->(substr($number,0,8),9);
83 5 $cd2 = $calc_cd->(substr($number,-2) . $cd1,4);
84 }
85 10 return $cd1 . $cd2;
86 } # _compute_checkdigit()
87  
88 # Preloaded methods go here.
89  
90 2 1;
91 __END__
« blib/lib/Algorithm/CheckDigits/M11_003.pm ↑ Summary blib/lib/Algorithm/CheckDigits/M11_011.pm »