Validator/Util.pm

filter: Statementstmt 66.8% 121 / 181 Branchbran 52.1% 48 / 92 Conditioncond 56.2% 27 / 48 MC/DCmcdc 38.4% 10 / 26 Subroutinesub 78.3% 29 / 37 Podpod 0.0% 0 / 17 totaltotal 58.6% 235 / 401 CC 65 SCAR 24.4
CC
65
Cov
61%
unbool
50.1
decode_content
24.5
BEGIN
19.1
CRAP 11.4
« Validator/Constraints/Draft6.pm ↑ Summary Validator/Constraints/Draft4.pm »
1 package JSONSchema::Validator::Util;
2  
3 # ABSTRACT: Useful functions
4  
5 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use URI 1.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 17 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 17 our @ISA = 'Exporter';
16 17 our @EXPORT_OK = qw(
17 json_encode json_decode user_agent_get serialize unbool
18 round read_file is_type detect_type get_resource decode_content
19 data_section
20 );
21  
22 17 ▶use constant FILE_SUFFIX_TO_MIME_TYPE => {
Subroutine BEGIN: called CC=1 SCAR=0.0
23 'yaml' => 'text/vnd.yaml',
24 'yml' => 'text/vnd.yaml',
25 'json' => 'application/json'
26 17 };
27  
28 17 ▶use constant TYPE_MAP => {
Subroutine BEGIN: called CC=1 SCAR=0.0
29 'array' => \&is_array,
30 'boolean' => \&is_bool,
31 'integer' => \&is_integer,
32 'number' => \&is_number,
33 'object' => \&is_object,
34 'null' => \&is_null, # for OAS30 null is not defined
35 'string' => \&is_string,
36  
37 # it is for some buggy code
38 '_ref' => \&is_ref
39 17 };
40  
41 # such order is required
42 17 ▶use constant TYPE_LIST => ['array', 'object', 'null', '_ref', 'integer', 'number', 'boolean', 'string'];
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 17 ▶BEGIN {
Subroutine BEGIN: called CC=7 SCAR=29.5
45 # YAML
46 0 ▶ if (eval { require YAML::XS; YAML::XS->VERSION(0.67); 1; }) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { require YAML::XS; 'YAML::XS'->VERSION(0.67); 1 } }) { } 0 17 17
elsif (eval { do { require YAML::PP; 1 } }) { } 17 0 17
47 0 ▶ *yaml_load = sub { local $YAML::XS::Boolean = 'JSON::PP'; YAML::XS::Load(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 }
49 17 elsif (eval { require YAML::PP; 1; }) {
50 17 my $pp = YAML::PP->new(boolean => 'JSON::PP');
51 0 ▶ *yaml_load = sub { $pp->load_string(@_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
52 } else {
53 0 ▶ *yaml_load = sub { croak 'No YAML package installed' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 }
55  
56 # JSON
57 17 my $json_class;
58 17 ▶ if (eval { require Cpanel::JSON::XS; 1; }) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { require Cpanel::JSON::XS; 1 } }) { } 17 0 17
elsif (eval { do { require JSON::XS; 'JSON::XS'->VERSION(3.0); 1 } }) { } 0 0 0
59 17 $json_class = 'Cpanel::JSON::XS';
60 0 } elsif (eval { require JSON::XS; JSON::XS->VERSION(3.0); 1; }) {
61 0 $json_class = 'JSON::XS';
62 } else {
63 0 require JSON::PP;
64 0 $json_class = 'JSON::PP';
65 }
66 17 my $json = $json_class->new->canonical(1)->utf8;
67 17 ▶ *json_encode = sub { $json->encode(@_); };
Subroutine __ANON__: called CC=1 SCAR=0.0
68 17 ▶ *json_decode = sub { $json->decode(@_); };
Subroutine __ANON__: called CC=1 SCAR=0.0
69  
70 # UserAgent
71 17 ▶ if (eval { require LWP::UserAgent; 1; }) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { require LWP::UserAgent; 1 } }) { } 17 0 17
elsif (eval { do { require Mojo::UserAgent; 1 } }) { } 0 0 0
72 17 my $ua = LWP::UserAgent->new;
73 0 ▶ *user_agent_get = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
74 0 my $uri = shift;
75 0 my $response = $ua->get($uri);
76 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
77 0 return $response->decoded_content, $response->headers->content_type;
78 }
79 0 croak "Can not get uri $uri";
80 17 };
81 0 } elsif (eval { require Mojo::UserAgent; 1; }) {
82 0 my $ua = Mojo::UserAgent->new;
83 0 ▶ *user_agent_get = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
84 0 my $uri = shift;
85 0 my $response = $ua->get($uri)->result;
86 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
87 0 return $response->body, $response->headers->content_type;
88 }
89 0 croak "Can not get uri $uri";
90 0 };
91 } else {
92 0 ▶ *user_agent_get = sub { croak 'No UserAgent package installed' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 }
94 }
95  
96 8 ▶sub unbool {
Subroutine unbool: called CC=8 SCAR=39.1
Pod undocumented
97 8 my $x = shift;
98 8 ▶ return "$x" if ref $x eq 'JSON::PP::Boolean';
Branch: 1 branch
BranchTrueFalseTotal
if ref $x eq "JSON::PP::Boolean" 8 0 8
99 0 ▶ return $x if ref $x;
Branch: 1 branch
BranchTrueFalseTotal
if ref $x 0 0 0
100 0 ▶ return '1' if $x && $x eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $x and $x eq 1 0 0 0
Condition0%0/3
$x and $x eq 1
!ll&&!rl&&r
000
MC/DC0%0/2
$x and $x eq 1
$x $x eq 1
Truth table0%0/3
A and B
A= $xB= $x eq 1
ABresult
0X0
100
111
101 0 ▶ return '0' if !defined $x || $x eq '0' || $x eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x or $x eq 0 or $x eq "" 0 0 0
Condition0%0/6
not defined $x or $x eq 0 or $x eq ""
l!l&&r!l&&!r
000
not defined $x or $x eq 0
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $x or $x eq 0 or $x eq ""
not defined $x $x eq 0 $x eq ""
Truth table0%0/4
not(A) or B or C
A= not defined $xB= $x eq 0C= $x eq ""
ABCresult
1XX1
01X1
0011
0000
102 0 return $x;
103 }
104  
105 8 ▶sub serialize { json_encode(shift) }
Subroutine serialize: called CC=1 SCAR=0.0
Pod undocumented
106  
107 0 ▶sub round {
Subroutine round: not called CC=2 SCAR=17.9
Pod undocumented
108 0 my $value = shift;
109 0 ▶ return int($value + ($value >= 0 ? 0.5 : -0.5));
Branch: 1 branch
BranchTrueFalseTotal
$value >= 0 ? : 0 0 0
110 }
111  
112 # scheme_handlers - map[scheme -> handler]
113 # uri - string
114 31 ▶sub get_resource {
Subroutine get_resource: called CC=6 SCAR=19.9
Pod undocumented
115 31 my ($scheme_handlers, $resource) = @_;
116 31 my $uri = URI->new($resource);
117  
118 31 for my $s ('http', 'https') {
119 62 ▶ $scheme_handlers->{$s} = \&user_agent_get unless exists $scheme_handlers->{$s};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $scheme_handlers->{$s} 62 0 62
120 }
121  
122 31 my $scheme = $uri->scheme;
123  
124 31 my ($response, $mime_type);
125 31 ▶ if ($scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme) { } 31 0 31
126 31 ▶ if (exists $scheme_handlers->{$scheme}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $scheme_handlers->{$scheme}) { } 0 31 31
elsif ($scheme eq 'file') { } 31 0 31
127 0 ($response, $mime_type) = $scheme_handlers->{$scheme}->($uri->as_string);
128 } elsif ($scheme eq 'file') {
129 31 ($response, $mime_type) = read_file($uri->file);
130 } else {
131 0 croak 'Unsupported scheme of uri ' . $uri->as_string;
132 }
133 } else {
134 # may it is path of local file without scheme?
135 0 ($response, $mime_type) = read_file($resource);
136 }
137 31 return ($response, $mime_type);
138 }
139  
140 77 ▶sub decode_content {
Subroutine decode_content: called CC=9 SCAR=32.0
Pod undocumented
141 77 my ($response, $mime_type, $resource) = @_;
142  
143 77 my $schema;
144 77 ▶ if ($mime_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mime_type) 77 0 77
145 77 ▶ if ($mime_type =~ m{yaml}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mime_type =~ /yaml/) { } 0 77 77
elsif ($mime_type =~ /json/) { } 77 0 77
146 0 $schema = eval{ yaml_load($response) };
147 0 ▶ croak "Failed to load resource $resource as $mime_type ( $@ )" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
148 }
149 elsif ($mime_type =~ m{json}) {
150 77 $schema = eval{ json_decode($response) };
151 77 ▶ croak "Failed to load resource $resource as $mime_type ( $@ )" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 77 77
152 }
153 }
154 77 ▶ unless ($schema) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema) 0 77 77
155 # try to guess
156 0 $schema = eval { json_decode($response) };
157 0 ▶ $schema = eval { yaml_load($response) } if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
158 0 ▶ croak "Unsupported mime type $mime_type of resource $resource" unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 0 0
159 }
160  
161 77 return $schema;
162 }
163  
164 77 ▶sub read_file {
Subroutine read_file: called CC=5 SCAR=16.6
Pod undocumented
165 77 my $path = shift;
166 77 ▶ croak "File $path does not exists" unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 77 77
167 77 ▶ croak "File $path does not have read permission" unless -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -r _ 0 77 77
168 77 my $size = -s _;
169  
170 77 my ($filename, $dir, $suffix) = File::Basename::fileparse($path, 'yml', 'yaml', 'json');
171 77 ▶ croak "Unknown file format of $path" unless $suffix;
Branch: 1 branch
BranchTrueFalseTotal
unless $suffix 0 77 77
172  
173 77 my $mime_type = FILE_SUFFIX_TO_MIME_TYPE->{$suffix};
174  
175 77 ▶ open my $fh, '<', $path or croak "Open file $path error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or croak("Open file $path error: $!") 77 0 77
176 77 read $fh, (my $file_content), $size;
177 77 close $fh;
178  
179 77 return $file_content, $mime_type;
180 }
181  
182 # params: $value, $type, $is_strict
183 7816 ▶sub is_type {
Subroutine is_type: called CC=2 SCAR=7.2
Pod undocumented
184 7816 ▶ return 0 unless exists TYPE_MAP->{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists {"_ref" => sub { package JSONSchema::Validator::Util; use warnings; use strict; my $ref = ref $_[0]; return 0 unless $ref; return 0 if $ref eq 'JSON::PP::Boolean' or $ref eq 'HASH' or $ref eq 'ARRAY'; return 1;},"array" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return ref $_[0] eq 'ARRAY';},"boolean" => sub { package JSONSchema::Validator::Util; use warnings; use strict; my $type = ref $_[0]; return 1 if $type eq 'JSON::PP::Boolean' or $type eq 'JSON::XS::Boolean' or $type eq 'Cpanel::JSON::XS::Boolean'; return 0 if $_[1]; my $is_number = looks_like_number($_[0]) && ($_[0] == 1 || $_[0] == 0); my $is_string = defined $_[0] && $_[0] eq ''; my $is_undef = !defined($_[0]); return 1 if $is_number or $is_string or $is_undef; return 0;},"integer" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return 1 if B::svref_2object(\$_[0])->FLAGS & 256; return 0 if $_[1]; return 0 if ref $_[0]; return 1 if looks_like_number($_[0]) and int $_[0] == $_[0]; return 0;},"null" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return !defined($_[0]);},"number" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return 1 if B::svref_2object(\$_[0])->FLAGS & 768; return 0 if $_[1]; return 0 if ref $_[0]; return 1 if looks_like_number($_[0]); return 0;},"object" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return ref $_[0] eq 'HASH';},"string" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return !(ref $_[0] || is_number(@_)) && defined $_[0] if $_[1]; return !ref($_[0]) && defined $_[0];}}->{$_[1]} 0 7816 7816
185 7816 return TYPE_MAP->{$_[1]}->($_[0], $_[2]);
186 }
187  
188 # params: $value, $is_strict
189 190 ▶sub detect_type {
Subroutine detect_type: called CC=3 SCAR=11.1
Pod undocumented
190 190 for my $type (@{TYPE_LIST()}) {
191 1110 ▶ return $type if TYPE_MAP->{$type}->(@_);
Branch: 1 branch
BranchTrueFalseTotal
if {"_ref" => sub { package JSONSchema::Validator::Util; use warnings; use strict; my $ref = ref $_[0]; return 0 unless $ref; return 0 if $ref eq 'JSON::PP::Boolean' or $ref eq 'HASH' or $ref eq 'ARRAY'; return 1;},"array" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return ref $_[0] eq 'ARRAY';},"boolean" => sub { package JSONSchema::Validator::Util; use warnings; use strict; my $type = ref $_[0]; return 1 if $type eq 'JSON::PP::Boolean' or $type eq 'JSON::XS::Boolean' or $type eq 'Cpanel::JSON::XS::Boolean'; return 0 if $_[1]; my $is_number = looks_like_number($_[0]) && ($_[0] == 1 || $_[0] == 0); my $is_string = defined $_[0] && $_[0] eq ''; my $is_undef = !defined($_[0]); return 1 if $is_number or $is_string or $is_undef; return 0;},"integer" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return 1 if B::svref_2object(\$_[0])->FLAGS & 256; return 0 if $_[1]; return 0 if ref $_[0]; return 1 if looks_like_number($_[0]) and int $_[0] == $_[0]; return 0;},"null" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return !defined($_[0]);},"number" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return 1 if B::svref_2object(\$_[0])->FLAGS & 768; return 0 if $_[1]; return 0 if ref $_[0]; return 1 if looks_like_number($_[0]); return 0;},"object" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return ref $_[0] eq 'HASH';},"string" => sub { package JSONSchema::Validator::Util; use warnings; use strict; return !(ref $_[0] || is_number(@_)) && defined $_[0] if $_[1]; return !ref($_[0]) && defined $_[0];}}->{$type}->(@_) 190 920 1110
192 }
193 # it must be unreachable code
194 0 croak 'Unknown type detected';
195 }
196  
197 # params: $value, $is_strict
198 1058 ▶sub is_array {
Subroutine is_array: called CC=1 SCAR=0.0
Pod undocumented
199 1058 return ref $_[0] eq 'ARRAY';
200 }
201  
202 # params: $value, $is_strict
203 3067 ▶sub is_bool {
Subroutine is_bool: called CC=11 SCAR=24.5
Pod undocumented
204 3067 my $type = ref $_[0];
205 3067 ▶ return 1 if $type eq 'JSON::PP::Boolean' or
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "JSON::PP::Boolean" or $type eq "JSON::XS::Boolean" or $type eq "Cpanel::JSON::XS::Boolean" 85 2982 3067
Condition66%4/6
$type eq "JSON::PP::Boolean" or $type eq "JSON::XS::Boolean" or $type eq "Cpanel::JSON::XS::Boolean"
l!l&&r!l&&!r
8502982
$type eq "JSON::PP::Boolean" or $type eq "JSON::XS::Boolean"
l!l&&r!l&&!r
8502982
MC/DC33%1/3
$type eq "JSON::PP::Boolean" or $type eq "JSON::XS::Boolean" or $type eq "Cpanel::JSON::XS::Boolean"
$type eq "JSON::PP::Boolean" $type eq "JSON::XS::Boolean" $type eq "Cpanel::JSON::XS::Boolean"
Truth table50%2/4
A or B or C
A= $type eq "JSON::PP::Boolean"B= $type eq "JSON::XS::Boolean"C= $type eq "Cpanel::JSON::XS::Boolean"
ABCresult
1XX1
01X1
0011
0000
206 $type eq 'JSON::XS::Boolean' or
207 $type eq 'Cpanel::JSON::XS::Boolean';
208 2982 ▶ return 0 if $_[1]; # is strict
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 2923 59 2982
209 59 ▶ my $is_number = looks_like_number($_[0]) && ($_[0] == 1 || $_[0] == 0);
Condition50%3/6
&looks_like_number($_[0]) && ($_[0] == 1 || $_[0] == 0)
!ll&&!rl&&r
5720
$_[0] == 1 or $_[0] == 0
l!l&&r!l&&!r
002
MC/DC0%0/3
&looks_like_number($_[0]) && ($_[0] == 1 || $_[0] == 0)
&looks_like_number($_[0]) $_[0] == 1 $_[0] == 0
Truth table50%2/4
A && B or C
A= &looks_like_number($_[0])B= $_[0] == 1C= $_[0] == 0
ABCresult
0XX0
1000
11X1
1011
210 59 ▶ my $is_string = defined $_[0] && $_[0] eq '';
Condition100%3/3
defined $_[0] && $_[0] eq ''
!ll&&!rl&&r
14711
MC/DC100%2/2
defined $_[0] && $_[0] eq ''
defined $_[0] $_[0] eq ''
Truth table100%3/3
A && B
A= defined $_[0]B= $_[0] eq ''
ABresult
0X0
100
111
211 59 my $is_undef = !defined $_[0];
212 59 ▶ return 1 if $is_number || $is_string || $is_undef;
Branch: 1 branch
BranchTrueFalseTotal
if $is_number or $is_string or $is_undef 12 47 59
Condition83%5/6
$is_number or $is_string or $is_undef
l!l&&r!l&&!r
11147
$is_number or $is_string
l!l&&r!l&&!r
01148
MC/DC66%2/3
$is_number or $is_string or $is_undef
$is_number $is_string $is_undef
Truth table75%3/4
A or B or C
A= $is_numberB= $is_stringC= $is_undef
ABCresult
1XX1
01X1
0011
0000
213 47 return 0;
214 }
215  
216 # params: $value, $is_strict
217 337 ▶sub is_integer {
Subroutine is_integer: called CC=6 SCAR=17.9
Pod undocumented
218 337 ▶ return 1 if B::svref_2object(\$_[0])->FLAGS & B::SVf_IOK();
Branch: 1 branch
BranchTrueFalseTotal
if B::svref_2object(\$_[0])->FLAGS & 256 134 203 337
219 203 ▶ return 0 if $_[1]; # is strict
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 68 135 203
220 135 ▶ return 0 if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 28 107 135
221 107 ▶ return 1 if looks_like_number($_[0]) && int($_[0]) == $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($_[0]) and int $_[0] == $_[0] 24 83 107
Condition100%3/3
&looks_like_number($_[0]) and int $_[0] == $_[0]
!ll&&!rl&&r
632024
MC/DC100%2/2
&looks_like_number($_[0]) and int $_[0] == $_[0]
&looks_like_number($_[0]) int $_[0] == $_[0]
Truth table100%3/3
A and B
A= &looks_like_number($_[0])B= int $_[0] == $_[0]
ABresult
0X0
100
111
222 83 return 0;
223 }
224  
225 # params: $value, $is_strict
226 1168 ▶sub is_number {
Subroutine is_number: called CC=5 SCAR=16.1
Pod undocumented
227 1168 ▶ return 1 if B::svref_2object(\$_[0])->FLAGS & (B::SVf_IOK() | B::SVf_NOK());
Branch: 1 branch
BranchTrueFalseTotal
if B::svref_2object(\$_[0])->FLAGS & 768 124 1044 1168
228 1044 ▶ return 0 if $_[1]; # is strict
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 963 81 1044
229 81 ▶ return 0 if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 24 57 81
230 57 ▶ return 1 if looks_like_number($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($_[0]) 1 56 57
231 56 return 0;
232 }
233  
234 # params: $value, $is_strict
235 142 ▶sub is_ref {
Subroutine is_ref: called CC=5 SCAR=19.4
Pod undocumented
236 142 my $ref = ref $_[0];
237 142 ▶ return 0 unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 112 30 142
238 30 ▶ return 0 if $ref eq 'JSON::PP::Boolean' ||
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "JSON::PP::Boolean" or $ref eq "HASH" or $ref eq "ARRAY" 30 0 30
Condition33%2/6
$ref eq "JSON::PP::Boolean" or $ref eq "HASH" or $ref eq "ARRAY"
l!l&&r!l&&!r
3000
$ref eq "JSON::PP::Boolean" or $ref eq "HASH"
l!l&&r!l&&!r
3000
MC/DC0%0/3
$ref eq "JSON::PP::Boolean" or $ref eq "HASH" or $ref eq "ARRAY"
$ref eq "JSON::PP::Boolean" $ref eq "HASH" $ref eq "ARRAY"
Truth table25%1/4
A or B or C
A= $ref eq "JSON::PP::Boolean"B= $ref eq "HASH"C= $ref eq "ARRAY"
ABCresult
1XX1
01X1
0011
0000
239 $ref eq 'HASH' ||
240 $ref eq 'ARRAY';
241 0 return 1;
242 }
243  
244 # params: $value, $is_strict
245 2379 ▶sub is_object {
Subroutine is_object: called CC=1 SCAR=0.0
Pod undocumented
246 2379 return ref $_[0] eq 'HASH';
247 }
248  
249 # params: $value, $is_strict
250 567 ▶sub is_null {
Subroutine is_null: called CC=1 SCAR=0.0
Pod undocumented
251 567 return !(defined $_[0]);
252 }
253  
254 # params: $value, $is_strict
255 512 ▶sub is_string {
Subroutine is_string: called CC=5 SCAR=16.3
Pod undocumented
256 512 ▶ return !(ref $_[0]) && !is_number(@_) && defined $_[0] if $_[1]; # is strict
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 310 202 512
Condition100%6/6
!(ref $_[0] || is_number(@_)) && defined $_[0]
!ll&&!rl&&r
111298
ref $_[0] || is_number(@_)
l!l&&r!l&&!r
65299
MC/DC100%3/3
!(ref $_[0] || is_number(@_)) && defined $_[0]
ref $_[0] is_number(@_) defined $_[0]
Truth table100%4/4
not(A || B) && C
A= ref $_[0]B= is_number(@_)C= defined $_[0]
ABCresult
1XX0
01X0
0000
0011
257 202 ▶ return !(ref $_[0]) && defined $_[0];
Condition33%1/3
!ref($_[0]) && defined $_[0]
!ll&&!rl&&r
00202
MC/DC0%0/2
!ref($_[0]) && defined $_[0]
!ref($_[0]) defined $_[0]
Truth table33%1/3
not(A) && B
A= !ref($_[0])B= defined $_[0]
ABresult
0X0
100
111
258 }
259  
260 0 ▶sub data_section {
Subroutine data_section: not called CC=2 SCAR=14.2
Pod undocumented
261 0 my $class = shift;
262 0 ▶ my $handle = do { no strict 'refs'; \*{"${class}::DATA"} };
Subroutine BEGIN: called CC=1 SCAR=1.2
263 0 ▶ return unless fileno $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless fileno $handle 0 0 0
264 0 seek $handle, 0, 0;
265 0 local $/ = undef;
266 0 my $data = <$handle>;
267 0 $data =~ s/^.*\n__DATA__\r?\n//s;
268 0 $data =~ s/\r?\n__END__\r?\n.*$//s;
269 0 return $data;
270 }
271  
272 17 1;
273  
274 __END__
« Validator/Constraints/Draft6.pm ↑ Summary Validator/Constraints/Draft4.pm »