GeoIP2/Types.pm

filter: Statementstmt 100% 64 / 64 Branchbran 100% 8 / 8 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 30 / 30 Podpod 0.0% 0 / 20 totaltotal 85.2% 116 / 136 CC 9 SCAR 6.4
CC
9
Cov
100%
object_can_type
5.0
object_isa_type
4.0
_tc_fail
2.0
CRAP 1.9
« GeoIP2.pm ↑ Summary GeoIP2/Role/Record/HasNames.pm »
1 package GeoIP2::Types;
2 7 $GeoIP2::Types::VERSION = '0.050000';
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Data::Validate::IP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use GeoIP2::Error::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use List::MoreUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Sub::Quote qw( quote_sub );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 our @EXPORT_OK = qw(
16 ArrayRef
17 Bool
18 BoolCoercion
19 HTTPStatus
20 HashRef
21 IPAddress
22 JSONObject
23 LocalesArrayRef
24 MaxMindID
25 MaxMindLicenseKey
26 MaybeStr
27 NameHashRef
28 NonNegativeInt
29 Num
30 PositiveInt
31 Str
32 URIObject
33 UserAgentObject
34 object_can_type
35 object_isa_type
36 );
37  
38 7 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
39  
40 sub ArrayRef () {
41 8 ▶ return quote_sub(
Subroutine ArrayRef: called CC=1 SCAR=0.0
Pod undocumented
42 q{ GeoIP2::Types::_tc_fail( $_[0], 'ArrayRef' )
43 unless defined $_[0]
44 && ref $_[0]
45 && Scalar::Util::reftype( $_[0] ) eq 'ARRAY'
46 && ! Scalar::Util::blessed( $_[0] ); }
47 );
48 }
49  
50 sub Bool () {
51 13 ▶ return quote_sub(
Subroutine Bool: called CC=1 SCAR=0.0
Pod undocumented
52 q{ GeoIP2::Types::_tc_fail( $_[0], 'Bool' )
53 unless ( ( defined $_[0] && !ref $_[0] && $_[0] =~ /^(?:0|1|)$/ )
54 || !defined $_[0] ); }
55 );
56 }
57  
58 sub BoolCoercion () {
59 12 ▶ return quote_sub(
Subroutine BoolCoercion: called CC=1 SCAR=0.0
Pod undocumented
60 q{ defined $_[0] && Scalar::Util::blessed($_[0])
61 && ( $_[0]->isa('JSON::Boolean')
62 || $_[0]->isa('JSON::PP::Boolean')
63 || $_[0]->isa('JSON::XS::Boolean')
64 )
65 ? $_[0] + 0 : $_[0] }
66 );
67 }
68  
69 sub HTTPStatus () {
70 2 ▶ return quote_sub(
Subroutine HTTPStatus: called CC=1 SCAR=0.0
Pod undocumented
71 q{ GeoIP2::Types::_tc_fail( $_[0], 'HTTPStatus' )
72 unless defined $_[0]
73 && ! ref $_[0]
74 && $_[0] =~ /^[2345]\d\d$/ }
75 );
76 }
77  
78 sub HashRef () {
79 81 ▶ return quote_sub(
Subroutine HashRef: called CC=1 SCAR=0.0
Pod undocumented
80 q{ GeoIP2::Types::_tc_fail( $_[0], 'HashRef' )
81 unless defined $_[0]
82 && ref $_[0]
83 && Scalar::Util::reftype( $_[0] ) eq 'HASH'
84 && ! Scalar::Util::blessed( $_[0] ); }
85 );
86 }
87  
88 sub IPAddress {
89 7 ▶ return quote_sub(
Subroutine IPAddress: called CC=1 SCAR=0.0
Pod undocumented
90 q{ GeoIP2::Types::_tc_fail( $_[0], 'IPAddress' )
91 unless Data::Validate::IP::is_ipv4( $_[0] )
92 || Data::Validate::IP::is_ipv6( $_[0] ); }
93 );
94 }
95  
96 sub JSONObject () {
97 2 ▶ return quote_sub(q{ GeoIP2::Types::object_can_type( $_[0], 'decode' ) });
Subroutine JSONObject: called CC=1 SCAR=0.0
Pod undocumented
98 }
99  
100 {
101 7 our %_SupportedLangs = map { $_ => 1 } qw(
102 de
103 en
104 es
105 fr
106 ja
107 pt-BR
108 ru
109 zh-CN
110 );
111  
112 sub LocalesArrayRef () {
113 7 ▶ return quote_sub(
Subroutine LocalesArrayRef: called CC=1 SCAR=0.0
Pod undocumented
114 q{ GeoIP2::Types::_tc_fail( $_[0], 'LocalesArrayRef' )
115 unless defined $_[0]
116 && ref $_[0]
117 && Scalar::Util::reftype( $_[0] ) eq 'ARRAY'
118 && !Scalar::Util::blessed( $_[0] )
119 && List::MoreUtils::all(
120 sub { defined $_ && !ref $_ && $GeoIP2::Types::_SupportedLangs{$_} },
121 @{ $_[0] }
122 ); }
123 );
124 }
125 }
126  
127 # Same as PositiveInt
128 7 sub MaxMindID () {
129 2 ▶ return quote_sub(
Subroutine MaxMindID: called CC=1 SCAR=0.0
Pod undocumented
130 q{ GeoIP2::Types::_tc_fail( $_[0], 'MaxMindID' )
131 unless defined $_[0]
132 && ! ref $_[0]
133 && $_[0] =~ /^\d+$/
134 && $_[0] > 0; }
135 );
136 }
137  
138 sub MaxMindLicenseKey () {
139 2 ▶ return quote_sub(
Subroutine MaxMindLicenseKey: called CC=1 SCAR=0.0
Pod undocumented
140 q{ GeoIP2::Types::_tc_fail( $_[0], 'MaxMindLicenseKey' )
141 unless defined $_[0] && $_[0] =~ /^[a-zA-Z0-9]{12}$/; }
142 );
143 }
144  
145 sub MaybeStr () {
146 7 ▶ return quote_sub(
Subroutine MaybeStr: called CC=1 SCAR=0.0
Pod undocumented
147 q{ GeoIP2::Types::_tc_fail( $_[0], 'StrOrUndef' )
148 unless !defined $_[0]
149 || ( defined $_[0]
150 && !ref $_[0] ); }
151 );
152 }
153  
154 sub NameHashRef () {
155 7 ▶ return quote_sub(
Subroutine NameHashRef: called CC=1 SCAR=0.0
Pod undocumented
156 q{ GeoIP2::Types::_tc_fail( $_[0], 'NameHashRef' )
157 unless defined $_[0]
158 && ref $_[0]
159 && Scalar::Util::reftype( $_[0] ) eq 'HASH'
160 && ! Scalar::Util::blessed( $_[0] )
161 && &List::MoreUtils::all( sub { defined $_ && ! ref $_ }, values %{ $_[0] } ); }
162 );
163 }
164  
165 sub NonNegativeInt () {
166 36 ▶ return quote_sub(
Subroutine NonNegativeInt: called CC=1 SCAR=0.0
Pod undocumented
167 q{ GeoIP2::Types::_tc_fail( $_[0], 'NonNegativeInt' )
168 unless defined $_[0]
169 && ! ref $_[0]
170 && $_[0] =~ /^\d+$/
171 && $_[0] >= 0; }
172 );
173 }
174  
175 sub Num () {
176 13 ▶ return quote_sub(
Subroutine Num: called CC=1 SCAR=0.0
Pod undocumented
177 q{ GeoIP2::Types::_tc_fail( $_[0], 'Num' )
178 unless defined $_[0]
179 && ! ref $_[0]
180 && $_[0] =~ /^-?\d+(\.\d+)?$/; }
181 );
182 }
183  
184 sub PositiveInt () {
185 35 ▶ return quote_sub(
Subroutine PositiveInt: called CC=1 SCAR=0.0
Pod undocumented
186 q{ GeoIP2::Types::_tc_fail( $_[0], 'PositiveInt' )
187 unless defined $_[0]
188 && ! ref $_[0]
189 && $_[0] =~ /^\d+$/
190 && $_[0] > 0; }
191 );
192 }
193  
194 sub Str () {
195 75 ▶ return quote_sub(
Subroutine Str: called CC=1 SCAR=0.0
Pod undocumented
196 q{ GeoIP2::Types::_tc_fail( $_[0], 'Str' )
197 unless defined $_[0]
198 && ! ref $_[0]; }
199 );
200 }
201  
202 sub URIObject () {
203 3 ▶ return quote_sub(q{ GeoIP2::Types::object_isa_type( $_[0], 'URI' ) });
Subroutine URIObject: called CC=1 SCAR=0.0
Pod undocumented
204 }
205  
206 sub UserAgentObject () {
207 2 ▶ return quote_sub(
Subroutine UserAgentObject: called CC=1 SCAR=0.0
Pod undocumented
208 q{ GeoIP2::Types::object_can_type( $_[0], 'agent', 'request' ) });
209 }
210  
211 sub object_can_type {
212 53 ▶ my $thing = shift;
Subroutine object_can_type: called CC=5 SCAR=16.1
Pod undocumented
213 53 my @methods = @_;
214  
215 53 ▶ _tc_fail( $thing, 'Object' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thing and builtin::blessed($thing) 21 32 53
Condition100%3/3
defined $thing and builtin::blessed($thing)
!ll&&!rl&&r
21932
MC/DC100%2/2
defined $thing and builtin::blessed($thing)
defined $thing builtin::blessed($thing)
Truth table100%3/3
A and B
A= defined $thingB= builtin::blessed($thing)
ABresult
0X0
100
111
216 unless defined $thing
217 && Scalar::Util::blessed($thing);
218  
219 32 for my $method (@methods) {
220 47 ▶ _tc_fail( $thing, "Object which ->can($method)" )
Branch: 1 branch
BranchTrueFalseTotal
unless $thing->can($method) 2 45 47
221 unless $thing->can($method);
222 }
223 }
224  
225 sub object_isa_type {
226 100 ▶ my $thing = shift;
Subroutine object_isa_type: called CC=4 SCAR=13.9
Pod undocumented
227 100 my $class = shift;
228  
229 100 ▶ _tc_fail( $thing, "$class Object" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thing and builtin::blessed($thing) and $thing->isa($class) 12 88 100
Condition100%6/6
defined $thing and builtin::blessed($thing) and $thing->isa($class)
!ll&&!rl&&r
11188
defined $thing and builtin::blessed($thing)
!ll&&!rl&&r
11089
MC/DC100%3/3
defined $thing and builtin::blessed($thing) and $thing->isa($class)
defined $thing builtin::blessed($thing) $thing->isa($class)
Truth table100%4/4
A and B and C
A= defined $thingB= builtin::blessed($thing)C= $thing->isa($class)
ABCresult
0XX0
10X0
1100
1111
230 unless defined $thing
231 && Scalar::Util::blessed($thing)
232 && $thing->isa($class);
233 }
234  
235 sub _tc_fail {
236 152 ▶ my $value = shift;
Subroutine _tc_fail: called CC=2 SCAR=6.9
237 152 my $type = shift;
238  
239 152 ▶ $value
Branch: 1 branch
BranchTrueFalseTotal
!defined($value) ? : 14 138 152
240 = !defined $value
241 ? 'undef'
242 : $value;
243  
244 152 GeoIP2::Error::Type->throw(
245 message => "$value is not a valid $type",
246 type => $type,
247 value => $value
248 );
249 }
250  
251 7 1;
« GeoIP2.pm ↑ Summary GeoIP2/Role/Record/HasNames.pm »