GeoIP2/Types.pm

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