blib/lib/Number/Phone/Formatter.pm

filter: Statementstmt 97.4% 38 / 39 Branchbran 100% 8 / 8 Conditioncond 91.6% 11 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 94.5% 69 / 73 CC 10 SCAR 15.6
CC
10
Cov
97%
_format
8.0
_maybe_add_country
2.0
_npl_object
2.0
CRAP 4.8
« blib/lib/Number/Phone/UK.pm ↑ Summary blib/lib/Number/Phone/StubCountry.pm »
1 package Number::Phone::Formatter;
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  
6 10 our $VERSION = '1.0';
7  
8 10 ▶use Number::Phone::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 36 ▶sub _regex_variable {
Subroutine _regex_variable: called CC=1 SCAR=0.0
11 36 my ($var, $qr, $subs) = @_;
12 36 $subs =~ s/"/\\"/;
13 36 $subs = "\"$subs\"";
14 36 $var =~ s/$qr/$subs/xee;
15 36 return $var;
16 }
17  
18 24 ▶sub _maybe_add_country {
Subroutine _maybe_add_country: called CC=2 SCAR=6.9
19 24 my ($object, $number, $national) = @_;
20 24 ▶ $number = '+' . $object->country_code() . ' ' . $number unless $national;
Branch: 1 branch
BranchTrueFalseTotal
unless $national 8 16 24
21 24 return $number;
22 }
23  
24 145 ▶sub _npl_object {
Subroutine _npl_object: called CC=2 SCAR=6.9
25 145 my ( $class, $object ) = @_;
26  
27 145 ▶ unless ( $object->isa('Number::Phone::StubCountry') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object->isa("Number::Phone::StubCountry")) 11 134 145
28 11 $object = Number::Phone::Lib->new( $object->format() );
29 }
30  
31 145 return $object;
32 }
33  
34 # this is used by N::P::Formatter::National(lyPreferredIntl), it wants
35 # something that looks very much like a N::P::StubCountry::XX as those
36 # will contain libphonenumber's formatting data. If we're instead passed
37 # a full-fat object, it will first create the equivalent stub.
38 24 ▶sub _format {
Subroutine _format: called CC=8 SCAR=20.8
39 24 my ($class, $object, $national) = @_;
40  
41 24 $object = $class->_npl_object($object);
42  
43 24 my $number = $object->{number};
44 24 foreach my $formatter (@{$object->{formatters}}) {
45 152 my ($leading_digits, $pattern) = map { $formatter->{$_} } qw(leading_digits pattern);
46 152 ▶ if ((!$leading_digits || $number =~ /^($leading_digits)/x) && $number =~ /^$pattern$/x) {
Branch: 1 branch
BranchTrueFalseTotal
if (!$leading_digits || $number =~ /^($leading_digits)/x and $number =~ /^$pattern$/x) 24 128 152
Condition100%6/6
!$leading_digits || $number =~ /^($leading_digits)/x and $number =~ /^$pattern$/x
!ll&&!rl&&r
973124
not $leading_digits or $number =~ /^($leading_digits)/x
l!l&&r!l&&!r
25397
MC/DC100%3/3
!$leading_digits || $number =~ /^($leading_digits)/x and $number =~ /^$pattern$/x
not $leading_digits $number =~ /^($leading_digits)/x $number =~ /^$pattern$/x
Truth table80%4/5
not(A) or B and C
A= not $leading_digitsB= $number =~ /^($leading_digits)/xC= $number =~ /^$pattern$/x
ABCresult
00X0
1X00
0100
1X11
0111
47 24 my $format;
48 24 ▶ if ($national && $formatter->{national_rule}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($national and $formatter->{'national_rule'}) { } 12 12 24
Condition100%3/3
$national and $formatter->{"national_rule"}
!ll&&!rl&&r
8412
MC/DC100%2/2
$national and $formatter->{"national_rule"}
$national $formatter->{"national_rule"}
Truth table100%3/3
A and B
A= $nationalB= $formatter->{"national_rule"}
ABresult
0X0
100
111
49 12 $format = _regex_variable($formatter->{format}, qr/(\$\d)/, $formatter->{national_rule});
50 } else {
51 12 ▶ $format = $formatter->{intl_format} || $formatter->{format};
Condition66%2/3
$formatter->{'intl_format'} || $formatter->{'format'}
l!l&&r!l&&!r
570
MC/DC0%0/2
$formatter->{'intl_format'} || $formatter->{'format'}
$formatter->{'intl_format'} $formatter->{'format'}
Truth table66%2/3
A || B
A= $formatter->{'intl_format'}B= $formatter->{'format'}
ABresult
1X1
011
000
52 }
53 24 $number = _regex_variable($number, qr/^$pattern$/, $format);
54 24 return _maybe_add_country($object, $number, $national);
55 }
56 }
57 0 return _maybe_add_country($object, $number, $national);
58 }
59  
60 10 1;
61  
62 =head1 NAME
63  
64 Number::Phone::Formatter - base class for other formatters
65  
66 =head1 DESCRIPTION
67  
68 A base class containing utility functions used by the formatters.
69  
70 =head1 AUTHOR, COPYRIGHT and LICENCE
71  
72 Copyright 2018 Matthew Somerville E<lt>F<matthew-github@dracos.co.uk>E<gt>
73  
74 This software is free-as-in-speech software, and may be used,
75 distributed, and modified under the terms of either the GNU
76 General Public Licence version 2 or the Artistic Licence. It's
77 up to you which one you use. The full text of the licences can
78 be found in the files GPL2.txt and ARTISTIC.txt, respectively.
79  
80 =cut
« blib/lib/Number/Phone/UK.pm ↑ Summary blib/lib/Number/Phone/StubCountry.pm »