| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return $x if ref $x; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | return '1' if $x && $x eq '1'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$x and $x eq 1
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
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return '0' if !defined $x || $x eq '0' || $x eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not defined $x or $x eq 0 or $x eq ""
not defined $x or $x eq 0
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 31 | my $scheme = $uri->scheme; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 31 | my ($response, $mime_type); | |||||||||||||||||||||||||||||||||||||||||
| 125 | 31 | ▶ | if ($scheme) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 31 | ▶ | if (exists $scheme_handlers->{$scheme}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 77 | ▶ | if ($mime_type =~ m{yaml}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | $schema = eval{ yaml_load($response) }; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | croak "Failed to load resource $resource as $mime_type ( $@ )" if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 77 | ▶ | unless ($schema) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | # try to guess | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $schema = eval { json_decode($response) }; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | $schema = eval { yaml_load($response) } if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | croak "Unsupported mime type $mime_type of resource $resource" unless $schema; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 77 | ▶ | croak "File $path does not have read permission" unless -r _; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$type eq "JSON::PP::Boolean" or $type eq "JSON::XS::Boolean" or $type eq "Cpanel::JSON::XS::Boolean"
$type eq "JSON::PP::Boolean" or $type eq "JSON::XS::Boolean"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 59 | ▶ | my $is_number = looks_like_number($_[0]) && ($_[0] == 1 || $_[0] == 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
&looks_like_number($_[0]) && ($_[0] == 1 || $_[0] == 0)
$_[0] == 1 or $_[0] == 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 59 | ▶ | my $is_string = defined $_[0] && $_[0] eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $_[0] && $_[0] eq ''
MC/DC100%2/2
defined $_[0] && $_[0] eq ''
defined $_[0]
$_[0] eq ''
Truth table100%3/3
A && B
A= defined $_[0]B= $_[0] eq ''
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 59 | my $is_undef = !defined $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 59 | ▶ | return 1 if $is_number || $is_string || $is_undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$is_number or $is_string or $is_undef
$is_number or $is_string
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 203 | ▶ | return 0 if $_[1]; # is strict | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 135 | ▶ | return 0 if ref $_[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 107 | ▶ | return 1 if looks_like_number($_[0]) && int($_[0]) == $_[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
&looks_like_number($_[0]) and int $_[0] == $_[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 1044 | ▶ | return 0 if $_[1]; # is strict | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 81 | ▶ | return 0 if ref $_[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 57 | ▶ | return 1 if looks_like_number($_[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 30 | ▶ | return 0 if $ref eq 'JSON::PP::Boolean' || | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$ref eq "JSON::PP::Boolean" or $ref eq "HASH" or $ref eq "ARRAY"
$ref eq "JSON::PP::Boolean" or $ref eq "HASH"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
!(ref $_[0] || is_number(@_)) && defined $_[0]
ref $_[0] || is_number(@_)
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 202 | ▶ | return !(ref $_[0]) && defined $_[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
!ref($_[0]) && defined $_[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||