Business/DK/Phonenumber.pm

filter: Statementstmt 96.8% 92 / 95 Branchbran 80.7% 21 / 26 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 93.3% 140 / 150 CC 18 SCAR 14.2
CC
18
Cov
93%
render
9.1
generate
5.0
validate
3.0
CRAP 4.1
« Class/Business/DK/Phonenumber.pm ↑ Summary Data/FormValidator/Constraints/Business/DK/Phonenumber.pm »
1 package Business::DK::Phonenumber;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use vars qw($VERSION @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 14 $VERSION = '0.10';
11 @EXPORT_OK
12 14 = qw(validate render generate validate_template TRUE FALSE DK_PREFIX DEFAULT_TEMPLATE);
13  
14 14 ▶use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use constant DK_PREFIX => '+45';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use constant DIGITS => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 14 ▶use constant DEFAULT_TEMPLATE => DK_PREFIX . ' %0' . DIGITS . 'd';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 14 ▶use constant SEED => 99999999;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 81 ▶sub validate {
Subroutine validate: called CC=3 SCAR=11.0
Pod documented
22 81 my ( $self, $phonenumber ) = @_;
23  
24 81 ▶ if ( not ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 81 0 81
25 81 $phonenumber = $self;
26 }
27  
28 81 $phonenumber =~ s/\s//sxmg;
29  
30 81 ▶ if ($phonenumber =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($phonenumber =~ / \A( (?:[+])(?:45)(?:\d{8})| #+4512345678 (?:45)(?:\d{8})| #4512345678 (?:\d{8}) #12345678 )\z/gmsx) { } 37 44 81
31 \A(
32 (?:[+])(?:45)(?:\d{8})| #+4512345678
33 (?:45)(?:\d{8})| #4512345678
34 (?:\d{8}) #12345678
35 )\z}sgmx
36 )
37 {
38 37 return TRUE;
39 } else {
40 44 return FALSE;
41 }
42 }
43  
44 116 ▶sub render {
Subroutine render: called CC=9 SCAR=22.1
Pod documented
45 116 my ( $self, $phonenumber, $template ) = @_;
46  
47 116 ▶ if ( $self =~ m/\A[\D]+\b/smx and not ref $self ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self =~ /\A[\D]+\b/msx and not ref $self) { } 2 114 116
elsif (not ref $self) { } 110 4 114
Condition100%3/3
$self =~ /\A[\D]+\b/msx and not ref $self
!ll&&!rl&&r
11042
MC/DC100%2/2
$self =~ /\A[\D]+\b/msx and not ref $self
$self =~ /\A[\D]+\b/msx not ref $self
Truth table100%3/3
A and not(B)
A= $self =~ /\A[\D]+\b/msxB= not ref $self
ABresult
0X0
100
111
48 2 $self = undef;
49  
50 } elsif ( not ref $self ) {
51 110 $template = $phonenumber;
52 110 $phonenumber = $self;
53 110 $self = undef;
54  
55 } else {
56  
57 4 ▶ if ($template) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template) { } 1 3 4
58 1 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 1 0 1
59 1 $self->validate_template($template);
60 } else {
61 0 Class::Business::DK::Phonenumber::validate_template(
62 $template);
63 }
64 } else {
65 3 ▶ $template = $self->{template} || DEFAULT_TEMPLATE;
Condition50%1/2
$self->{'template'} || '+45 %08d'
l!l
30
MC/DC0%0/1
$self->{'template'} || '+45 %08d'
$self->{'template'}
Truth table50%1/2
A
A= $self->{'template'}
Aresult
11
00
66 }
67  
68 4 ▶ if ( not $phonenumber ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($phonenumber) 4 0 4
69 4 $phonenumber = $self->{phonenumber};
70 }
71 }
72  
73 116 $phonenumber =~ s/\s//sxmg;
74  
75 116 my @subs = $template =~ m/%(\d)+d/sxmg;
76  
77 116 my $sum = 0;
78 116 my @phonesplit = ();
79 116 my $phonetmp = $phonenumber;
80  
81 116 foreach my $sub (@subs) {
82 443 $sum += $sub;
83 443 push @phonesplit, substr $phonetmp, 0, $sub, q{};
84 }
85  
86 116 my $output = sprintf $template, @phonesplit;
87  
88 116 return $output;
89 }
90  
91 4 ▶sub generate {
Subroutine generate: called CC=5 SCAR=16.1
Pod documented
92 4 my ( $self, $amount, $template ) = @_;
93  
94 4 ▶ if ( not $amount ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($amount) 1 3 4
95 1 $amount = 1;
96 }
97  
98 4 ▶ if ( not $template ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($template) 2 2 4
99 2 $template = DEFAULT_TEMPLATE;
100 }
101  
102 4 my %phonenumbers;
103 4 ▶ while ( keys %phonenumbers < $amount ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %phonenumbers < $amount) 105 4 109
104 105 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 105 105
105 0 $phonenumbers{ $self->_generate($template) }++;
106 } else {
107 105 $phonenumbers{ _generate($template) }++;
108 }
109 }
110  
111 4 my @phonenumbers = keys %phonenumbers;
112  
113 4 return @phonenumbers;
114 }
115  
116 105 ▶sub _generate {
Subroutine _generate: called CC=2 SCAR=7.1
117 105 my ( $self, $template ) = @_;
118  
119 105 my $random_phone = int rand SEED;
120 105 my $phonenumber = sprintf '%.08d', $random_phone;
121  
122 105 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 105 105
123 0 return $self->render( $phonenumber, $template );
124 } else {
125 105 $template = $self;
126  
127 105 return render( $phonenumber, $template );
128 }
129 }
130  
131 16 ▶sub validate_template {
Subroutine validate_template: called CC=3 SCAR=11.0
Pod documented
132 16 my ( $self, $template ) = @_;
133  
134 16 my @digits = $template =~ m/%(\d)+d/sxmg;
135  
136 16 my $sum = 0;
137 16 foreach my $digit (@digits) {
138 25 $sum += $digit;
139 }
140  
141 16 ▶ if ( $sum == DIGITS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sum == 8) { } 8 8 16
142 8 return TRUE;
143 } else {
144 8 return FALSE;
145 }
146 }
147  
148 14 1;
149  
150 __END__
« Class/Business/DK/Phonenumber.pm ↑ Summary Data/FormValidator/Constraints/Business/DK/Phonenumber.pm »