blib/lib/JSV/Context.pm

filter: Statementstmt 92.3% 97 / 105 Branchbran 80.7% 42 / 52 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 6 totaltotal 84.7% 155 / 183 CC 34 SCAR 27.4
CC
34
Cov
88%
validate
26.4
log_error
6.0
apply_keyword
3.5
CRAP 15.5
↑ Summary blib/lib/JSV/Reference.pm »
1 package JSV::Context;
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  
6 use Class::Accessor::Lite (
7 47 new => 1,
8 rw => [qw/
9 keywords
10 reference
11 original_schema
12 throw_error
13 throw_immediate
14 enable_history
15 enable_format
16 formats
17 history
18 json
19 loose_type
20 /],
21 ro => [qw/
22 errors
23 current_type
24 current_keyword
25 current_pointer
26 current_instance
27 current_schema
28 current_schema_pointer
29 schema_pointer_history
30 /],
31 47 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 47 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 47 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 47 ▶use JSV::Keyword qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 47 ▶use JSV::Util::Type qw(detect_instance_type detect_instance_type_loose);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 47 ▶use JSV::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 47 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 sub validate {
41 1262 ▶ my ($self, $schema, $instance) = @_;
Subroutine validate: called CC=26 SCAR=32.7
Pod undocumented
42  
43 1262 ▶ local $self->{current_type} = !$self->loose_type ? detect_instance_type($instance)
Branch: 1 branch
BranchTrueFalseTotal
!$self->loose_type ? : 705 557 1262
44 : detect_instance_type_loose($instance);
45  
46 1262 local $self->{cleanup_callbacks} = [];
47  
48 1262 my $rv;
49 1262 eval {
50 1262 for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_ANY()} }) {
51 10096 ▶ next unless exists $schema->{$keyword->keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword->keyword} 9150 946 10096
52 946 $self->apply_keyword($keyword, $schema, $instance);
53 }
54  
55 1262 ▶ if ($self->is_matched_types(qw/integer_or_string number_or_string/)) {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->is_matched_types('integer_or_string', 'number_or_string')) { } 165 1097 1262
elsif ($self->is_matched_types('integer', 'number')) { } 209 888 1097
elsif ($self->is_matched_types('string')) { } 411 477 888
elsif ($self->current_type eq 'array') { } 143 334 477
elsif ($self->current_type eq 'object') { } 292 42 334
56 165 for my $keyword (
57 165 @{ $self->keywords->{INSTANCE_TYPE_NUMERIC()} },
58 165 @{ $self->keywords->{INSTANCE_TYPE_STRING()} }
59 ) {
60 1155 ▶ next unless exists $schema->{$keyword->keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword->keyword} 1101 54 1155
61 54 $self->apply_keyword($keyword, $schema, $instance);
62 }
63 }
64 elsif ($self->is_matched_types(qw/integer number/)) {
65 209 for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_NUMERIC()} }) {
66 627 ▶ next unless exists $schema->{$keyword->keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword->keyword} 571 56 627
67 56 $self->apply_keyword($keyword, $schema, $instance);
68 }
69 }
70 elsif ($self->is_matched_types(qw/string/)) {
71 411 for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_STRING()} }) {
72 1644 ▶ next unless exists $schema->{$keyword->keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword->keyword} 1536 108 1644
73 108 $self->apply_keyword($keyword, $schema, $instance);
74 }
75 }
76 elsif ($self->current_type eq "array") {
77 143 for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_ARRAY()} }) {
78 572 ▶ next unless exists $schema->{$keyword->keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword->keyword} 445 127 572
79 127 $self->apply_keyword($keyword, $schema, $instance);
80 }
81 }
82 elsif ($self->current_type eq "object") {
83 292 for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_OBJECT()} }) {
84 ### for addtionalProperties, patternProperties keyword without properties keyword
85 1460 ▶ next unless ( ( grep { defined $_ && exists $schema->{$_} } ($keyword->keyword, @{$keyword->additional_keywords}) ) > 0 );
Branch: 2 branches
BranchTrueFalseTotal
unless grep({defined $_ and exists $schema->{$_};} $keyword->keyword, @{$keyword->additional_keywords;}) > 0 1082 378 1460
defined $_ and exists $schema->{$_} 2044 0 2044
86 # next unless exists $schema->{$_->keyword};
87 378 $self->apply_keyword($keyword, $schema, $instance);
88 }
89 }
90  
91 1262 ▶ $rv = JSV::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->enable_history ? : 0 1262 1262
92 ($self->enable_history ? (history => $self->history) : ()),
93 );
94 };
95 1262 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 1262 1262
96 0 $self->log_error(sprintf("Unexpected error: %s", $e));
97 }
98  
99 1262 ▶ while (my $cb = pop @{ $self->{cleanup_callbacks} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = pop @{$$self{"cleanup_callbacks"};}) 8 1262 1270
100 8 $cb->();
101 }
102  
103 1262 ▶ if ( scalar @{ $self->errors } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->errors;}) 513 749 1262
104 513 ▶ $rv = JSV::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->enable_history ? : 0 513 513
105 errors => $self->errors,
106 ($self->enable_history ? (history => $self->history) : ()),
107 );
108 513 ▶ if ( $self->throw_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->throw_error) 0 513 513
109 0 croak $rv;
110 }
111 }
112  
113 1262 return $rv;
114 }
115  
116 sub apply_keyword {
117 1669 ▶ my ($self, $keyword, $schema, $instance) = @_;
Subroutine apply_keyword: called CC=3 SCAR=12.6
Pod undocumented
118  
119 1669 local $self->{current_keyword} = $keyword->keyword;
120 1669 local $self->{current_schema} = $schema;
121 1669 local $self->{current_instance} = $instance;
122  
123 1669 $keyword->validate($self, $schema, $instance);
124  
125 1669 ▶ if ( $ENV{JSV_DEBUG} || $self->enable_history ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"JSV_DEBUG"} or $self->enable_history) 0 1669 1669
Condition33%1/3
$ENV{"JSV_DEBUG"} or $self->enable_history
l!l&&r!l&&!r
001669
MC/DC0%0/2
$ENV{"JSV_DEBUG"} or $self->enable_history
$ENV{"JSV_DEBUG"} $self->enable_history
Truth table33%1/3
A or B
A= $ENV{"JSV_DEBUG"}B= $self->enable_history
ABresult
1X1
011
000
126 0 push @{ $self->history }, +{
127 keyword => $self->current_keyword,
128 pointer => $self->current_pointer,
129 schema => $self->current_schema,
130 instance => $self->resolve_current_instance,
131 };
132 }
133 }
134  
135 sub log_error {
136 367 ▶ my ($self, $message) = @_;
Subroutine log_error: called CC=4 SCAR=17.9
Pod undocumented
137  
138 367 my $error = +{
139 keyword => $self->current_keyword,
140 pointer => $self->current_pointer,
141 schema => $self->current_schema,
142 instance => $self->resolve_current_instance,
143 schema_pointer => $self->current_schema_pointer,
144 schema_pointer_history => clone($self->schema_pointer_history),
145 message => $message,
146 };
147  
148 367 ▶ if ( $ENV{JSV_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"JSV_DEBUG"}) 0 367 367
149 0 require Data::Dump;
150 0 warn "history = " . Data::Dump::dump($self->history);
151 0 warn "error = " . Data::Dump::dump($error);
152 }
153  
154 367 ▶ if ( $self->throw_immediate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->throw_immediate) { } 0 367 367
155 0 ▶ croak JSV::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->enable_history ? : 0 0 0
156 error => $error,
157 ($self->enable_history ? (history => $self->history) : ()),
158 );
159 }
160 else {
161 367 push @{ $self->{errors} }, $error;
162 }
163 }
164  
165 sub resolve_current_instance {
166 367 ▶ my $self = shift;
Subroutine resolve_current_instance: called CC=3 SCAR=11.0
Pod undocumented
167  
168 367 my $instance;
169 367 ▶ if ( JSON::is_bool($self->current_instance) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&JSON::is_bool(scalar $self->current_instance)) { } 9 358 367
170 9 ▶ if ( $self->current_instance == JSON::true ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_instance == JSON::true()) { } 7 2 9
171 7 $instance = "true";
172 }
173 else {
174 2 $instance = "false";
175 }
176 }
177 else {
178 358 $instance = $self->current_instance;
179 }
180  
181 367 return $instance;
182 }
183  
184 sub is_matched_types {
185 3247 ▶ my ($self, @types) = @_;
Subroutine is_matched_types: called CC=2 SCAR=6.9
Pod undocumented
186  
187 3247 ▶ return (grep { $self->{current_type} eq $_ } @types) > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$self->{'current_type'} eq $_;} @types) > 0 ? : 785 2462 3247
188 }
189  
190 sub register_cleanup_callback {
191 8 ▶ my ($self, $cb) = @_;
Subroutine register_cleanup_callback: called CC=1 SCAR=0.0
Pod undocumented
192  
193 8 push @{ $self->{cleanup_callbacks} }, $cb;
194 }
195  
196 47 1;
↑ Summary blib/lib/JSV/Reference.pm »