blib/lib/Discord/Client/Model.pm

filter: Statementstmt 100% 56 / 56 Branchbran 80.0% 8 / 10 Conditioncond 53.3% 8 / 15 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 18 / 18 Podpod 100% 6 / 6 totaltotal 86.6% 97 / 112 CC 11 SCAR 10.3
CC
11
Cov
89%
new
6.3
wrap
4.6
_call
2.0
CRAP 2.8
« blib/lib/Discord/Client/Intents.pm ↑ Summary blib/lib/Discord/Client/Model/Message.pm »
1 package Discord::Client::Model;
2  
3 6 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Discord::Client::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 our $VERSION = '0.003';
12  
13 34 ▶sub new {
Subroutine new: called CC=6 SCAR=18.4
Pod documented
14 34 my ( $class, %args ) = @_;
15  
16 # Require a resource identity before constructing a detached JSON snapshot.
17 croak 'Model data requires an ID'
18 unless ref( $args{data} ) eq 'HASH'
19 && defined( $args{data}{id} )
20 && !ref( $args{data}{id} )
21 34 ▶ && $args{data}{id} =~ /\A\d{1,20}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"data"} eq "HASH" and defined $args{"data"}{"id"} and not ref $args{"data"}{"id"} and $args{"data"}{"id"} =~ /\A\d{1,20}\z/u 1 33 34
Condition66%6/9
ref $args{"data"} eq "HASH" and defined $args{"data"}{"id"} and not ref $args{"data"}{"id"} and $args{"data"}{"id"} =~ /\A\d{1,20}\z/u
!ll&&!rl&&r
1033
ref $args{"data"} eq "HASH" and defined $args{"data"}{"id"} and not ref $args{"data"}{"id"}
!ll&&!rl&&r
1033
ref $args{"data"} eq "HASH" and defined $args{"data"}{"id"}
!ll&&!rl&&r
0133
MC/DC25%1/4
ref $args{"data"} eq "HASH" and defined $args{"data"}{"id"} and not ref $args{"data"}{"id"} and $args{"data"}{"id"} =~ /\A\d{1,20}\z/u
ref $args{"data"} eq "HASH" defined $args{"data"}{"id"} not ref $args{"data"}{"id"} $args{"data"}{"id"} =~ /\A\d{1,20}\z/u
Truth table40%2/5
A and B and not(C) and D
A= ref $args{"data"} eq "HASH"B= defined $args{"data"}{"id"}C= not ref $args{"data"}{"id"}D= $args{"data"}{"id"} =~ /\A\d{1,20}\z/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
22 33 my $data = eval { _clone( $args{data} ) };
23 33 ▶ croak 'Model data must be JSON encodable' if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 33 33
24 33 $data->{id} = "$data->{id}";
25 33 return bless { data => $data, rest => $args{rest} }, $class;
26 }
27  
28 67 ▶sub _clone {
Subroutine _clone: called CC=1 SCAR=0.0
29 67 my ($data) = @_;
30  
31 # Support scalar field values as well as nested resource data.
32 67 my $json = JSON::PP->new->allow_nonref;
33 67 return $json->decode( $json->encode($data) );
34 }
35 20 ▶sub id { $_[0]->{data}{id} }
Subroutine id: called CC=1 SCAR=0.0
Pod documented
36 1 ▶sub raw { _clone( $_[0]->{data} ) }
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
37 33 ▶sub get { _clone( $_[0]->{data}{ $_[1] } ) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
38 3 ▶sub rest { $_[0]->{rest} }
Subroutine rest: called CC=1 SCAR=0.0
Pod documented
39  
40 34 ▶sub wrap {
Subroutine wrap: called CC=4 SCAR=15.2
Pod documented
41 34 my ( $class, $type, $data, %args ) = @_;
42  
43 # Load only known model classes; caller input never becomes an arbitrary module path.
44 34 my %classes = ( message => 'Message', channel => 'Channel', guild => 'Guild', user => 'User' );
45 34 ▶ croak 'Unknown model type' unless defined($type) && !ref($type) && $classes{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and not ref $type and $classes{$type} 0 34 34
Condition33%2/6
defined $type and not ref $type and $classes{$type}
!ll&&!rl&&r
0034
defined $type and not ref $type
!ll&&!rl&&r
0034
MC/DC0%0/3
defined $type and not ref $type and $classes{$type}
defined $type not ref $type $classes{$type}
Truth table25%1/4
A and not(B) and C
A= defined $typeB= not ref $typeC= $classes{$type}
ABCresult
0XX0
10X0
1100
1111
46 34 my $package = 'Discord::Client::Model::' . $classes{$type};
47 34 ( my $file = "$package.pm" ) =~ s{::}{/}g;
48 34 require $file;
49 34 return $package->new( data => $data, %args );
50 }
51  
52 12 ▶sub _call {
Subroutine _call: called CC=2 SCAR=6.9
53 12 my ( $self, $method, @args ) = @_;
54  
55 # Read-only snapshots fail asynchronous operations without constructing a hidden client.
56 12 ▶ return $self->_failed( 'Model operation requires a REST client', 'state' ) unless $self->{rest};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"rest"} 1 11 12
57 11 return $self->{rest}->$method(@args);
58 }
59  
60 9 ▶sub _model_future {
Subroutine _model_future: called CC=1 SCAR=0.0
61 9 my ( $self, $type, $future ) = @_;
62  
63 # Wrap successful responses without mutating the original resource snapshot.
64 return $future->then(
65 7 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
66 7 my ($data) = @_;
67 7 my $model = eval { __PACKAGE__->wrap( $type, $data, rest => $self->{rest} ) };
68 7 ▶ return $self->_failed( 'Discord returned invalid model data', 'protocol' ) unless $model;
Branch: 1 branch
BranchTrueFalseTotal
unless $model 1 6 7
69 6 return Future->done($model);
70 }
71 9 );
72 }
73  
74 2 ▶sub _failed {
Subroutine _failed: called CC=1 SCAR=0.0
75 2 my ( $self, $message, $category ) = @_;
76 2 my $error = Discord::Client::Error->new( message => $message, category => $category );
77 2 return Future->fail( $message, $category, $error );
78 }
79  
80 6 1;
81  
82 __END__
« blib/lib/Discord/Client/Intents.pm ↑ Summary blib/lib/Discord/Client/Model/Message.pm »