blib/lib/JSV/Util/Type.pm

filter: Statementstmt 96.1% 50 / 52 Branchbran 88.4% 23 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 91.2% 83 / 91 SCAR 26.4
CC
14
Cov
94%
detect_instance_type
8.2
detect_instance_type_loose
6.0
escape_json_pointer
2.0
CRAP 14.1
« blib/lib/JSV/Context.pm ↑ Summary blib/lib/JSV/Keyword/Draft4/Type.pm »
1 package JSV::Util::Type;
2  
3 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 45 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 45 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 45 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 45 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 45 our @EXPORT_OK = (qw/
13 detect_instance_type
14 detect_instance_type_loose
15 escape_json_pointer
16 /);
17  
18 45 our %REF_TYPE_MAP = (
19 HASH => "object",
20 ARRAY => "array",
21 );
22  
23 sub detect_instance_type {
24 1540 ▶ my $instance = shift;
Subroutine detect_instance_type: called CC=8 SCAR=21.0
Pod undocumented
25  
26 1540 my $ref_type;
27  
28 1540 ▶ if (!defined $instance) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $instance) { } 211 1329 1540
elsif ($ref_type = ref $instance) { } 544 785 1329
29 211 return "null";
30 }
31 elsif ($ref_type = ref $instance) {
32 544 ▶ if (!blessed $instance) {
Branch: 2 branches
BranchTrueFalseTotal
if (not builtin::blessed($instance)) { } 512 32 544
elsif (&JSON::is_bool($instance)) { } 32 0 32
33 512 return $REF_TYPE_MAP{$ref_type};
34 }
35 elsif (JSON::is_bool($instance)) {
36 32 return "boolean";
37 }
38 else {
39 0 croak(sprintf("Unknown reference type (ref_type: %s)", $ref_type));
40 }
41 }
42 else {
43 785 my $flags = B::svref_2object(\$instance)->FLAGS;
44  
45 785 ▶ if (( $flags & B::SVp_IOK ) == B::SVp_IOK) {
Branch: 3 branches
BranchTrueFalseTotal
if (($flags & 4096) == 4096) { } 303 482 785
elsif (($flags & 8192) == 8192) { } 60 422 482
elsif (($flags & 16384) == 16384) { } 422 0 422
46 303 return "integer";
47 }
48 elsif (( $flags & B::SVp_NOK ) == B::SVp_NOK ) {
49 60 return "number";
50 }
51 elsif (( $flags & B::SVp_POK ) == B::SVp_POK) {
52 422 return "string";
53 }
54 else {
55 0 croak(sprintf("Unknown type (flags: %s)", $flags));
56 }
57 }
58 }
59  
60 sub detect_instance_type_loose {
61 557 ▶ my ($instance) = @_;
Subroutine detect_instance_type_loose: called CC=6 SCAR=17.9
Pod undocumented
62  
63 557 my $type_strict = detect_instance_type($instance);
64  
65 557 ▶ if ( $type_strict eq "integer" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type_strict eq 'integer') { } 134 423 557
elsif ($type_strict eq 'number') { } 20 403 423
elsif ($type_strict eq 'string') { } 188 215 403
66 134 return "integer_or_string";
67 }
68 elsif ( $type_strict eq "number" ) {
69 20 return "number_or_string";
70 }
71 elsif ( $type_strict eq "string" ) {
72 188 ▶ if ( looks_like_number($instance) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($instance)) 11 177 188
73 11 ▶ return "integer_or_string" if $instance =~ m/^(?:[+-])?[1-9]?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $instance =~ /^(?:[+-])?[1-9]?\d+$/ 9 2 11
74 2 return "number_or_string";
75 }
76 }
77 392 return $type_strict;
78 }
79  
80 sub escape_json_pointer {
81 705 ▶ my $property = shift;
Subroutine escape_json_pointer: called CC=2 SCAR=7.0
Pod undocumented
82 705 ▶ return unless defined $property;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $property 0 705 705
83  
84 # according to http://tools.ietf.org/html/rfc6901#section-4
85 705 $property =~ s!~!~0!g; # replace tilde first
86 705 $property =~ s!/!~1!g;
87  
88 705 return $property;
89 }
90 45 1;
« blib/lib/JSV/Context.pm ↑ Summary blib/lib/JSV/Keyword/Draft4/Type.pm »