blib/lib/JSON/RPC2/Client.pm

filter: Statementstmt 54.4% 67 / 123 Branchbran 31.9% 23 / 72 Conditioncond 29.4% 10 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 61.1% 11 / 18 Podpod 100% 10 / 10 totaltotal 43.8% 121 / 276 CC 48 SCAR 37.9
CC
48
Cov
44%
batch_response
90.0
response
70.2
call_named
30.0
CRAP 44.4
↑ Summary blib/lib/JSON/RPC2/Server.pm »
1 package JSON::RPC2::Client;
2 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = 'v2.1.1';
9  
10 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Scalar::Util qw( weaken refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 sub new {
15 2 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
16 2 my $self = {
17 next_id => 0,
18 free_id => [],
19 call => {},
20 id => {},
21 };
22 2 return bless $self, $class;
23 }
24  
25 sub batch {
26 0 ▶ my ($self, @requests) = @_;
Subroutine batch: not called CC=2 SCAR=17.9
Pod documented
27 0 my @call = grep {ref} @requests;
28 0 @requests = grep {!ref} @requests;
29 0 ▶ croak 'at least one request required' if !@requests;
Branch: 1 branch
BranchTrueFalseTotal
unless @requests 0 0 0
30 0 my $request = '['.join(q{,}, @requests).']';
31 0 return ($request, @call);
32 }
33  
34 sub notify {
35 0 ▶ my ($self, $method, @params) = @_;
Subroutine notify: not called CC=3 SCAR=24.8
Pod documented
36 0 ▶ croak 'method required' if !defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 0 0
37 0 ▶ return encode_json({
Branch: 1 branch
BranchTrueFalseTotal
!@params ? : 0 0 0
38 jsonrpc => '2.0',
39 method => $method,
40 (!@params ? () : (
41 params => \@params,
42 )),
43 });
44 }
45  
46 sub notify_named {
47 0 ▶ my ($self, $method, @params) = @_;
Subroutine notify_named: not called CC=4 SCAR=30.0
Pod documented
48 0 ▶ croak 'method required' if !defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 0 0
49 0 ▶ croak 'odd number of elements in %params' if @params % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @params % 2 0 0 0
50 0 my %params = @params;
51 0 ▶ return encode_json({
Branch: 1 branch
BranchTrueFalseTotal
!@params ? : 0 0 0
52 jsonrpc => '2.0',
53 method => $method,
54 (!@params ? () : (
55 params => \%params,
56 )),
57 });
58 }
59  
60 sub call {
61 4 ▶ my ($self, $method, @params) = @_;
Subroutine call: called CC=4 SCAR=14.6
Pod documented
62 4 ▶ croak 'method required' if !defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 4 4
63 4 my ($id, $call) = $self->_get_id();
64 4 ▶ my $request = encode_json({
Branch: 1 branch
BranchTrueFalseTotal
!@params ? : 0 4 4
65 jsonrpc => '2.0',
66 method => $method,
67 (!@params ? () : (
68 params => \@params,
69 )),
70 id => $id,
71 });
72 4 ▶ return wantarray ? ($request, $call) : $request;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
73 }
74  
75 sub call_named {
76 0 ▶ my ($self, $method, @params) = @_;
Subroutine call_named: not called CC=5 SCAR=34.0
Pod documented
77 0 ▶ croak 'method required' if !defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 0 0
78 0 ▶ croak 'odd number of elements in %params' if @params % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @params % 2 0 0 0
79 0 my %params = @params;
80 0 my ($id, $call) = $self->_get_id();
81 0 ▶ my $request = encode_json({
Branch: 1 branch
BranchTrueFalseTotal
!@params ? : 0 0 0
82 jsonrpc => '2.0',
83 method => $method,
84 (!@params ? () : (
85 params => \%params,
86 )),
87 id => $id,
88 });
89 0 ▶ return wantarray ? ($request, $call) : $request;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
90 }
91  
92 sub _get_id {
93 4 ▶ my $self = shift;
Subroutine _get_id: called CC=2 SCAR=6.9
94 4 ▶ my $id = @{$self->{free_id}} ? pop @{$self->{free_id}} : $self->{next_id}++;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'free_id'};} ? : 2 2 4
95 4 my $call = {};
96 4 $self->{call}{ refaddr($call) } = $call;
97 4 $self->{id}{ $id } = $call;
98 4 weaken($self->{id}{ $id });
99 4 return ($id, $call);
100 }
101  
102 sub pending {
103 0 ▶ my ($self) = @_;
Subroutine pending: not called CC=1 SCAR=6.9
Pod documented
104 0 return values %{ $self->{call} };
105 }
106  
107 sub cancel {
108 0 ▶ my ($self, $call) = @_;
Subroutine cancel: not called CC=2 SCAR=17.9
Pod documented
109 0 ▶ croak 'no such request' if !delete $self->{call}{ refaddr($call) };
Branch: 1 branch
BranchTrueFalseTotal
unless delete $self->{"call"}{builtin::refaddr($call)} 0 0 0
110 0 return;
111 }
112  
113 sub batch_response {
114 0 ▶ my ($self, $json) = @_;
Subroutine batch_response: not called CC=9 SCAR=45.0
Pod documented
115 0 ▶ croak 'require 1 param' if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
116  
117 0 undef $@;
118 0 ▶ my $response = ref $json ? $json : eval { decode_json($json) };
Branch: 1 branch
BranchTrueFalseTotal
ref $json ? : 0 0 0
119 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
120 0 return [ 'Parse error' ];
121 }
122 0 ▶ if ($response && ref $response eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response and ref $response eq "HASH") 0 0 0
Condition0%0/3
$response and ref $response eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$response and ref $response eq "HASH"
$response ref $response eq "HASH"
Truth table0%0/3
A and B
A= $responseB= ref $response eq "HASH"
ABresult
0X0
100
111
123 0 return [ $self->response($response) ];
124 }
125 0 ▶ if (!$response || ref $response ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response or ref $response ne "ARRAY") 0 0 0
Condition0%0/3
not $response or ref $response ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $response or ref $response ne "ARRAY"
not $response ref $response ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not $responseB= ref $response ne "ARRAY"
ABresult
1X1
011
000
126 0 return [ 'expect Array or Object' ];
127 }
128 0 ▶ if (!@{$response}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$response;}) 0 0 0
129 0 return [ 'empty Array' ];
130 }
131  
132 0 return map {[ $self->response($_) ]} @{$response};
133 }
134  
135 sub response { ## no critic (ProhibitExcessComplexity RequireArgUnpacking)
136 6 ▶ my ($self, $json) = @_;
Subroutine response: called CC=25 SCAR=42.5
Pod documented
137 6 ▶ croak 'require 1 param' if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 6 6
138  
139 6 undef $@;
140 6 ▶ my $response = ref $json ? $json : eval { decode_json($json) };
Branch: 1 branch
BranchTrueFalseTotal
ref $json ? : 3 3 6
141 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
142 0 return 'Parse error';
143 }
144 6 ▶ if (ref $response ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response ne "HASH") 0 6 6
145 0 return 'expect Object';
146 }
147 6 ▶ if (!defined $response->{jsonrpc} || $response->{jsonrpc} ne '2.0') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $response->{"jsonrpc"} or $response->{"jsonrpc"} ne "2.0") 0 6 6
Condition33%1/3
not defined $response->{"jsonrpc"} or $response->{"jsonrpc"} ne "2.0"
l!l&&r!l&&!r
006
MC/DC0%0/2
not defined $response->{"jsonrpc"} or $response->{"jsonrpc"} ne "2.0"
not defined $response->{"jsonrpc"} $response->{"jsonrpc"} ne "2.0"
Truth table33%1/3
not(A) or B
A= not defined $response->{"jsonrpc"}B= $response->{"jsonrpc"} ne "2.0"
ABresult
1X1
011
000
148 0 return 'expect {jsonrpc}="2.0"';
149 }
150 6 ▶ if (!exists $response->{id} || ref $response->{id} || !defined $response->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $response->{"id"} or ref $response->{"id"} or not defined $response->{"id"}) 0 6 6
Condition33%2/6
not exists $response->{"id"} or ref $response->{"id"} or not defined $response->{"id"}
l!l&&r!l&&!r
006
not exists $response->{"id"} or ref $response->{"id"}
l!l&&r!l&&!r
006
MC/DC0%0/3
not exists $response->{"id"} or ref $response->{"id"} or not defined $response->{"id"}
not exists $response->{"id"} ref $response->{"id"} not defined $response->{"id"}
Truth table25%1/4
not(A) or B or not(C)
A= not exists $response->{"id"}B= ref $response->{"id"}C= not defined $response->{"id"}
ABCresult
1XX1
01X1
0011
0000
151 0 return 'expect {id} is scalar';
152 }
153 6 ▶ if (!exists $self->{id}{ $response->{id} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"id"}{$response->{"id"}}) 2 4 6
154 2 return 'unknown {id}';
155 }
156 4 ▶ if (!(exists $response->{result} xor exists $response->{error})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $response->{"result"} xor exists $response->{"error"}) 0 4 4
Condition50%2/4
exists $response->{"result"} xor exists $response->{"error"}
l&&rl&&!r!l&&r!l&&!r
0220
MC/DC0%0/2
exists $response->{"result"} xor exists $response->{"error"}
exists $response->{"result"} exists $response->{"error"}
Truth table50%2/4
A xor B
A= exists $response->{"result"}B= exists $response->{"error"}
ABresult
110
101
011
000
157 0 return 'expect {result} or {error}';
158 }
159 4 ▶ if (exists $response->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $response->{"error"}) 2 2 4
160 2 my $e = $response->{error};
161 2 ▶ if (ref $e ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e ne "HASH") 0 2 2
162 0 return 'expect {error} is Object';
163 }
164 2 ▶ if (!defined $e->{code} || ref $e->{code} || $e->{code} !~ /\A-?\d+\z/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $e->{"code"} or ref $e->{"code"} or not $e->{"code"} =~ /\A-?\d+\z/msx) 0 2 2
Condition33%2/6
not defined $e->{"code"} or ref $e->{"code"} or not $e->{"code"} =~ /\A-?\d+\z/msx
l!l&&r!l&&!r
002
not defined $e->{"code"} or ref $e->{"code"}
l!l&&r!l&&!r
002
MC/DC0%0/3
not defined $e->{"code"} or ref $e->{"code"} or not $e->{"code"} =~ /\A-?\d+\z/msx
not defined $e->{"code"} ref $e->{"code"} not $e->{"code"} =~ /\A-?\d+\z/msx
Truth table25%1/4
not(A) or B or not(C)
A= not defined $e->{"code"}B= ref $e->{"code"}C= not $e->{"code"} =~ /\A-?\d+\z/msx
ABCresult
1XX1
01X1
0011
0000
165 0 return 'expect {error}{code} is Integer';
166 }
167 2 ▶ if (!defined $e->{message} || ref $e->{message}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $e->{"message"} or ref $e->{"message"}) 0 2 2
Condition33%1/3
not defined $e->{"message"} or ref $e->{"message"}
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $e->{"message"} or ref $e->{"message"}
not defined $e->{"message"} ref $e->{"message"}
Truth table33%1/3
not(A) or B
A= not defined $e->{"message"}B= ref $e->{"message"}
ABresult
1X1
011
000
168 0 return 'expect {error}{message} is String';
169 }
170 ## no critic (ProhibitMagicNumbers)
171 2 ▶ if ((3 == keys %{$e} && !exists $e->{data}) || 3 < keys %{$e}) {
Branch: 1 branch
BranchTrueFalseTotal
if (3 == keys %{$e;} and not exists $e->{"data"} or 3 < keys %{$e;}) 0 2 2
Condition33%2/6
3 == keys %{$e;} and not exists $e->{"data"} or 3 < keys %{$e;}
l!l&&r!l&&!r
002
3 == keys %{$e;} and not exists $e->{"data"}
!ll&&!rl&&r
200
MC/DC0%0/3
3 == keys %{$e;} and not exists $e->{"data"} or 3 < keys %{$e;}
3 == keys %{$e;} not exists $e->{"data"} 3 < keys %{$e;}
Truth table20%1/5
A and not(B) or C
A= 3 == keys %{$e;}B= not exists $e->{"data"}C= 3 < keys %{$e;}
ABCresult
11X1
0X11
1011
0X00
1000
172 0 return 'only optional key must be {error}{data}';
173 }
174 }
175  
176 4 my $id = $response->{id};
177 4 push @{ $self->{free_id} }, $id;
178 4 my $call = delete $self->{id}{ $id };
179 4 ▶ if ($call) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call) 4 0 4
180 4 $call = delete $self->{call}{ refaddr($call) };
181 }
182 4 ▶ if (!$call) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($call) 0 4 4
183 0 return; # call was canceled
184 }
185 4 return (undef, $response->{result}, $response->{error}, $call);
186 }
187  
188  
189 3 1; # Magic true value required at end of module
190 __END__
↑ Summary blib/lib/JSON/RPC2/Server.pm »