Business/DK/FI.pm

filter: Statementstmt 100% 76 / 76 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 20 / 20 Podpod 100% 3 / 3 totaltotal 100% 105 / 105 CC 4 SCAR 3.0
CC
4
Cov
100%
_calculate_sum
3.0
validate
2.0
BEGIN
1.0
CRAP 1.3
« Class/Business/DK/FI.pm ↑ Summary
1 package Business::DK::FI;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use vars qw($VERSION @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Params::Validate qw(validate_pos SCALAR ARRAYREF);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 $VERSION = '0.09';
13 10 @EXPORT_OK = qw(validate validateFI generate);
14  
15 10 ▶use constant MODULUS_OPERAND => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use constant THRESHOLD => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use constant DEDUCTION => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use constant INVALID => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use constant VALID => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 10 Readonly::Array my @CONTROLCIFERS => qw(1 2 1 2 1 2 1 2 1 2 1 2 1 2);
22 10 Readonly::Scalar my $CONTROL_LENGTH => scalar @CONTROLCIFERS;
23  
24 ## no critic (NamingConventions::Capitalization)
25  
26 8 ▶sub validateFI {
Subroutine validateFI: called CC=1 SCAR=0.0
Pod documented
27 8 return validate(shift);
28 }
29  
30 18 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
31 18 my ($fi_number) = @ARG;
32  
33 18 validate_pos( @ARG, { type => SCALAR, regex => qr/^\d{15}$/xsm } );
34  
35 11 my ($last_digit);
36 11 ( $fi_number, $last_digit )
37 = $fi_number =~ m/^(\d{$CONTROL_LENGTH})(\d{1})$/xsm;
38  
39 11 my $sum = _calculate_sum( $fi_number, \@CONTROLCIFERS );
40 11 my $checksum = _calculate_checksum($sum);
41  
42 11 ▶ if ( $checksum == $last_digit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checksum == $last_digit) { } 10 1 11
43 10 return VALID;
44 }
45 else {
46 1 return INVALID;
47 }
48 }
49  
50 14 ▶sub _calculate_checksum {
Subroutine _calculate_checksum: called CC=1 SCAR=0.0
51 14 my ($sum) = @ARG;
52  
53 14 validate_pos( @ARG, { type => SCALAR, regex => qr/^\d+$/xsm }, );
54  
55 14 return ( THRESHOLD - ( $sum % MODULUS_OPERAND ) );
56 }
57  
58 14 ▶sub _calculate_sum {
Subroutine _calculate_sum: called CC=3 SCAR=11.0
59 14 my ( $number, $CONTROLCIFERS ) = @ARG;
60  
61 14 validate_pos(
62 @ARG,
63 { type => SCALAR, regex => qr/^\d+$/xsm },
64 { type => ARRAYREF },
65 );
66  
67 14 my $sum = 0;
68 14 my @numbers = split //smx, $number;
69  
70 ## no critic (ControlStructures::ProhibitCStyleForLoops)
71 14 ▶ for ( my $i = 0; $i < scalar @numbers; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @numbers) 196 14 210
72 196 my $tmp_sum = $numbers[$i] * $CONTROLCIFERS->[$i];
73  
74 196 ▶ if ( $tmp_sum >= THRESHOLD ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_sum >= 10) { } 41 155 196
75 41 $sum += ( $tmp_sum - DEDUCTION );
76 }
77 else {
78 155 $sum += $tmp_sum;
79 }
80 }
81 14 return $sum;
82 }
83  
84 7 ▶sub generate {
Subroutine generate: called CC=1 SCAR=0.0
Pod documented
85 7 my ($number) = @ARG;
86  
87 #number has to be a positive number between 1 and 99999999999999
88 validate_pos(
89 @ARG,
90 { type => SCALAR,
91 regex => qr/^\d+$/,
92 callbacks => {
93 5 ▶ 'higher than 0' => sub { shift() >= 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
94 'lower than 99999999999999' =>
95 6 ▶ sub { shift() <= 99999999999999 },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 },
97 },
98 7 );
99  
100 #padding with zeroes up to our maximum length
101 3 my $pattern = '%0' . $CONTROL_LENGTH . 's';
102 3 my $reformatted_number = sprintf $pattern, $number;
103  
104 #this call takes care of the check of the product of the above statement
105 3 my $sum = _calculate_sum( $reformatted_number, \@CONTROLCIFERS );
106 3 my $checksum = _calculate_checksum($sum);
107  
108 3 my $finalized_number = $reformatted_number . $checksum;
109  
110 3 return $finalized_number;
111 }
112  
113 10 1;
114  
115 __END__
« Class/Business/DK/FI.pm ↑ Summary