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 CC 14 SCAR 15.9
CC
14
Cov
94%
detect_instance_type
8.2
detect_instance_type_loose
6.0
escape_json_pointer
2.0
CRAP 4.9
« JSV/Keyword/Draft4/Items.pm ↑ Summary JSV/Keyword/Draft4/Dependencies.pm »
1 package JSV::Util::Type;
2  
3 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 47 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 47 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 47 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 47 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 47 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 47 our @EXPORT_OK = (qw/
13 detect_instance_type
14 detect_instance_type_loose
15 escape_json_pointer
16 /);
17  
18 47 our %REF_TYPE_MAP = (
19 HASH => "object",
20 ARRAY => "array",
21 );
22  
23 1761 ▶sub detect_instance_type {
Subroutine detect_instance_type: called CC=8 SCAR=21.0
Pod undocumented
24 1761 my $instance = shift;
25  
26 1761 my $ref_type;
27  
28 1761 ▶ if (!defined $instance) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $instance) { } 247 1514 1761
elsif ($ref_type = ref $instance) { } 626 888 1514
29 247 return "null";
30 }
31 elsif ($ref_type = ref $instance) {
32 626 ▶ if (!blessed $instance) {
Branch: 2 branches
BranchTrueFalseTotal
if (not builtin::blessed($instance)) { } 576 50 626
elsif (&JSON::is_bool($instance)) { } 50 0 50
33 576 return $REF_TYPE_MAP{$ref_type};
34 }
35 elsif (JSON::is_bool($instance)) {
36 50 return "boolean";
37 }
38 else {
39 0 croak(sprintf("Unknown reference type (ref_type: %s)", $ref_type));
40 }
41 }
42 else {
43 888 my $flags = B::svref_2object(\$instance)->FLAGS;
44  
45 888 ▶ if (( $flags & B::SVp_IOK ) == B::SVp_IOK) {
Branch: 3 branches
BranchTrueFalseTotal
if (($flags & 4096) == 4096) { } 349 539 888
elsif (($flags & 8192) == 8192) { } 60 479 539
elsif (($flags & 16384) == 16384) { } 479 0 479
46 349 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 479 return "string";
53 }
54 else {
55 0 croak(sprintf("Unknown type (flags: %s)", $flags));
56 }
57 }
58 }
59  
60 638 ▶sub detect_instance_type_loose {
Subroutine detect_instance_type_loose: called CC=6 SCAR=17.9
Pod undocumented
61 638 my ($instance) = @_;
62  
63 638 my $type_strict = detect_instance_type($instance);
64  
65 638 ▶ if ( $type_strict eq "integer" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type_strict eq 'integer') { } 152 486 638
elsif ($type_strict eq 'number') { } 20 466 486
elsif ($type_strict eq 'string') { } 226 240 466
66 152 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 226 ▶ if ( looks_like_number($instance) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($instance)) 40 186 226
73 40 ▶ return "integer_or_string" if $instance =~ m/^(?:[+-])?[1-9]?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $instance =~ /^(?:[+-])?[1-9]?\d+$/ 21 19 40
74 19 return "number_or_string";
75 }
76 }
77 426 return $type_strict;
78 }
79  
80 773 ▶sub escape_json_pointer {
Subroutine escape_json_pointer: called CC=2 SCAR=7.0
Pod undocumented
81 773 my $property = shift;
82 773 ▶ return unless defined $property;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $property 0 773 773
83  
84 # according to http://tools.ietf.org/html/rfc6901#section-4
85 773 $property =~ s!~!~0!g; # replace tilde first
86 773 $property =~ s!/!~1!g;
87  
88 773 return $property;
89 }
90 47 1;
« JSV/Keyword/Draft4/Items.pm ↑ Summary JSV/Keyword/Draft4/Dependencies.pm »