blib/lib/Discord/Client/Commands.pm

filter: Statementstmt 87.2% 288 / 330 Branchbran 57.8% 140 / 242 Conditioncond 42.9% 145 / 338 MC/DCmcdc 13.0% 23 / 176 Subroutinesub 93.1% 41 / 44 Podpod 100% 8 / 8 totaltotal 56.6% 645 / 1138 CC 250 SCAR 39.4
CC
250
Cov
63%
_validate_option_value
251.2
_normalize_option_filters
154.9
define
74.6
CRAP 51.5
↑ Summary blib/lib/Discord/Client/Interaction.pm »
1 package Discord::Client::Commands;
2  
3 10 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Discord::Client::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 our $VERSION = '0.003';
13 10 my %OPTION_TYPES = (
14 subcommand => 1,
15 group => 2,
16 string => 3,
17 integer => 4,
18 boolean => 5,
19 user => 6,
20 channel => 7,
21 role => 8,
22 mentionable => 9,
23 number => 10,
24 attachment => 11
25 );
26 10 my %COMMAND_TYPES = ( slash => 1, chat_input => 1, user => 2, message => 3 );
27  
28 68 ▶sub new {
Subroutine new: called CC=4 SCAR=14.6
Pod documented
29 68 my ( $class, %args ) = @_;
30  
31 # The registry stores declarations locally; construction performs no discovery.
32 68 ▶ croak 'Invalid application ID' if defined( $args{application_id} ) && !_id( $args{application_id} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"application_id"} and not _id($args{"application_id"}) 0 68 68
Condition66%2/3
defined $args{"application_id"} and not _id($args{"application_id"})
!ll&&!rl&&r
52160
MC/DC0%0/2
defined $args{"application_id"} and not _id($args{"application_id"})
defined $args{"application_id"} not _id($args{"application_id"})
Truth table66%2/3
A and not(B)
A= defined $args{"application_id"}B= not _id($args{"application_id"})
ABresult
0X0
100
111
33 return bless {
34 loop => $args{loop} // IO::Async::Loop->new,
35 rest => $args{rest},
36 application_id => $args{application_id},
37 68 ▶ commands => {},
Condition66%2/3
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
6530
MC/DC0%0/2
$args{"loop"} // "IO::Async::Loop"->new
$args{"loop"} "IO::Async::Loop"->new
Truth table66%2/3
A // B
A= $args{"loop"}B= "IO::Async::Loop"->new
ABresult
1X1
011
000
38 order => [],
39 components => {}
40 }, $class;
41 }
42 26 ▶sub _id { defined( $_[0] ) && !ref( $_[0] ) && $_[0] =~ /\A\d+\z/ }
Subroutine _id: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] and $_[0] =~ /\A\d+\z/u 24 2 26
Condition66%2/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
2024
MC/DC50%1/2
defined $_[0] and not ref $_[0]
defined $_[0] not ref $_[0]
Truth table66%2/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
ABresult
0X0
100
111
43  
44 86 ▶sub _name {
Subroutine _name: called CC=4 SCAR=14.6
45 86 my ($value) = @_;
46  
47 # Validate Unicode characters without silently changing the registered spelling.
48 return
49 86 ▶ defined($value)
Condition66%6/9
defined $value && !ref($value) && $value eq lc $value && $value =~ /\A[-_\x{02BC}\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}\z/u
!ll&&!rl&&r
1283
defined $value and not ref $value and $value eq lc $value
!ll&&!rl&&r
0185
defined $value and not ref $value
!ll&&!rl&&r
0086
MC/DC50%2/4
defined $value && !ref($value) && $value eq lc $value && $value =~ /\A[-_\x{02BC}\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}\z/u
defined $value not ref $value $value eq lc $value $value =~ /\A[-_\x{02BC}\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}\z/u
Truth table60%3/5
A and not(B) and C && D
A= defined $valueB= not ref $valueC= $value eq lc $valueD= $value =~ /\A[-_\x{02BC}\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}\z/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
50 && !ref($value)
51 && $value eq lc($value)
52 && $value =~ /\A[-_\x{02BC}\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}\z/;
53 }
54 86 ▶sub _description { defined( $_[0] ) && !ref( $_[0] ) && length( $_[0] ) >= 1 && length( $_[0] ) <= 100 }
Subroutine _description: called CC=4 SCAR=15.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] and length $_[0] >= 1 and length $_[0] <= 100 85 1 86
Condition50%3/6
defined $_[0] and not ref $_[0] and length $_[0] >= 1
!ll&&!rl&&r
0185
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
0086
MC/DC33%1/3
defined $_[0] and not ref $_[0] and length $_[0] >= 1
defined $_[0] not ref $_[0] length $_[0] >= 1
Truth table50%2/4
A and not(B) and C
A= defined $_[0]B= not ref $_[0]C= length $_[0] >= 1
ABCresult
0XX0
10X0
1100
1111
55 5 ▶sub _number { defined( $_[0] ) && !ref( $_[0] ) && looks_like_number( $_[0] ) && abs( $_[0] ) <= 9007199254740992 }
Subroutine _number: called CC=4 SCAR=19.1
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] and &looks_like_number($_[0]) and abs $_[0] <= '9007199254740992' 5 0 5
Condition33%2/6
defined $_[0] and not ref $_[0] and &looks_like_number($_[0])
!ll&&!rl&&r
005
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
005
MC/DC0%0/3
defined $_[0] and not ref $_[0] and &looks_like_number($_[0])
defined $_[0] not ref $_[0] &looks_like_number($_[0])
Truth table25%1/4
A and not(B) and C
A= defined $_[0]B= not ref $_[0]C= &looks_like_number($_[0])
ABCresult
0XX0
10X0
1100
1111
56 100 ▶sub _clone { JSON::PP->new->decode( JSON::PP->new->encode( $_[0] ) ) }
Subroutine _clone: called CC=1 SCAR=0.0
57  
58 68 ▶sub define {
Subroutine define: called CC=37 SCAR=43.1
Pod documented
59 68 my ( $self, $name, %args ) = @_;
60  
61 # Validate the complete declaration before mutating the command registry.
62 68 ▶ croak 'Command registry is closed' if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 68 68
63 68 ▶ my $type = $args{type} // 1;
Condition100%2/2
$args{"type"} // 1
l!l
266
MC/DC100%1/1
$args{"type"} // 1
$args{"type"}
Truth table100%2/2
A
A= $args{"type"}
Aresult
11
00
64 68 ▶ $type = $COMMAND_TYPES{$type} if !ref($type) && exists $COMMAND_TYPES{$type};
Branch: 1 branch
BranchTrueFalseTotal
if not ref $type and exists $COMMAND_TYPES{$type} 2 66 68
Condition66%2/3
not ref $type and exists $COMMAND_TYPES{$type}
!ll&&!rl&&r
0662
MC/DC50%1/2
not ref $type and exists $COMMAND_TYPES{$type}
not ref $type exists $COMMAND_TYPES{$type}
Truth table66%2/3
not(A) and B
A= not ref $typeB= exists $COMMAND_TYPES{$type}
ABresult
0X0
100
111
65 68 ▶ croak 'Invalid command type' unless defined($type) && !ref($type) && $type =~ /\A[1-3]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and not ref $type and $type =~ /\A[1-3]\z/u 0 68 68
Condition33%2/6
defined $type and not ref $type and $type =~ /\A[1-3]\z/u
!ll&&!rl&&r
0068
defined $type and not ref $type
!ll&&!rl&&r
0068
MC/DC0%0/3
defined $type and not ref $type and $type =~ /\A[1-3]\z/u
defined $type not ref $type $type =~ /\A[1-3]\z/u
Truth table25%1/4
A and not(B) and C
A= defined $typeB= not ref $typeC= $type =~ /\A[1-3]\z/u
ABCresult
0XX0
10X0
1100
1111
66 68 ▶ my $valid_name =
Branch: 1 branch
BranchTrueFalseTotal
$type == 1 ? : 66 2 68
Condition33%3/9
defined $name and not ref $name and length $name >= 1 and length $name <= 32
!ll&&!rl&&r
002
defined $name and not ref $name and length $name >= 1
!ll&&!rl&&r
002
defined $name and not ref $name
!ll&&!rl&&r
002
MC/DC0%0/4
defined $name and not ref $name and length $name >= 1 and length $name <= 32
defined $name not ref $name length $name >= 1 length $name <= 32
Truth table20%1/5
A and not(B) and C and D
A= defined $nameB= not ref $nameC= length $name >= 1D= length $name <= 32
ABCDresult
0XXX0
10XX0
110X0
11100
11111
67 $type == 1
68 ? _name($name)
69 : defined($name) && !ref($name) && length($name) >= 1 && length($name) <= 32;
70 68 ▶ croak 'Invalid command name' unless $valid_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid_name 2 66 68
71 66 ▶ croak 'Invalid command description' if $type == 1 && !_description( $args{description} );
Branch: 1 branch
BranchTrueFalseTotal
if $type == 1 and not _description($args{"description"}) 1 65 66
Condition100%3/3
$type == 1 and not _description($args{"description"})
!ll&&!rl&&r
2631
MC/DC100%2/2
$type == 1 and not _description($args{"description"})
$type == 1 not _description($args{"description"})
Truth table100%3/3
A and not(B)
A= $type == 1B= not _description($args{"description"})
ABresult
0X0
100
111
72 croak 'Context menu commands cannot have descriptions or options'
73 if $type != 1
74 65 ▶ && ( ( defined( $args{description} ) && length( $args{description} ) ) || exists( $args{options} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $type != 1 and defined $args{"description"} && length $args{"description"} || exists $args{"options"} 0 65 65
Condition44%4/9
$type != 1 and defined $args{"description"} && length $args{"description"} || exists $args{"options"}
!ll&&!rl&&r
6320
defined $args{"description"} and length $args{"description"} or exists $args{"options"}
l!l&&r!l&&!r
002
defined $args{"description"} and length $args{"description"}
!ll&&!rl&&r
200
MC/DC0%0/4
$type != 1 and defined $args{"description"} && length $args{"description"} || exists $args{"options"}
$type != 1 defined $args{"description"} length $args{"description"} exists $args{"options"}
Truth table33%2/6
A and B and C or D
A= $type != 1B= defined $args{"description"}C= length $args{"description"}D= exists $args{"options"}
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
75 65 ▶ croak 'handler must be a code reference' if defined( $args{handler} ) && ref( $args{handler} ) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"handler"} and ref $args{"handler"} ne "CODE" 0 65 65
Condition66%2/3
defined $args{"handler"} and ref $args{"handler"} ne "CODE"
!ll&&!rl&&r
2630
MC/DC0%0/2
defined $args{"handler"} and ref $args{"handler"} ne "CODE"
defined $args{"handler"} ref $args{"handler"} ne "CODE"
Truth table66%2/3
A and B
A= defined $args{"handler"}B= ref $args{"handler"} ne "CODE"
ABresult
0X0
100
111
76  
77 # Keep executable handlers separate from the JSON sent to Discord.
78 65 my $node = { name => $name, type => 0 + $type, handler => $args{handler} };
79 65 ▶ my ( $wire_options, $nodes ) = $self->_options( $args{options} // [], 'root' );
Condition100%2/2
$args{"options"} // []
l!l
1451
MC/DC100%1/1
$args{"options"} // []
$args{"options"}
Truth table100%2/2
A
A= $args{"options"}
Aresult
11
00
80 58 $node->{options} = $nodes;
81 croak 'A command requires a handler or subcommand handlers'
82 58 ▶ unless $node->{handler} || _has_handlers($nodes);
Branch: 1 branch
BranchTrueFalseTotal
unless $node->{"handler"} or _has_handlers($nodes) 1 57 58
Condition100%3/3
$node->{"handler"} or _has_handlers($nodes)
l!l&&r!l&&!r
5611
MC/DC100%2/2
$node->{"handler"} or _has_handlers($nodes)
$node->{"handler"} _has_handlers($nodes)
Truth table100%3/3
A or B
A= $node->{"handler"}B= _has_handlers($nodes)
ABresult
1X1
011
000
83 57 my %wire = ( %args, name => $name, type => 0 + $type );
84 57 delete $wire{handler};
85 57 ▶ if ( $type == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1) { } 55 2 57
86 55 ▶ $wire{options} = $wire_options if exists $args{options};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"options"} 7 48 55
87 }
88 else {
89 2 delete @wire{qw(description options)};
90 }
91  
92 # Normalize wire types and validate localized names before taking a snapshot.
93 57 ▶ if ( exists( $wire{default_member_permissions} ) && defined( $wire{default_member_permissions} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $wire{"default_member_permissions"} and defined $wire{"default_member_permissions"}) 0 57 57
Condition33%1/3
exists $wire{"default_member_permissions"} and defined $wire{"default_member_permissions"}
!ll&&!rl&&r
5700
MC/DC0%0/2
exists $wire{"default_member_permissions"} and defined $wire{"default_member_permissions"}
exists $wire{"default_member_permissions"} defined $wire{"default_member_permissions"}
Truth table33%1/3
A and B
A= exists $wire{"default_member_permissions"}B= defined $wire{"default_member_permissions"}
ABresult
0X0
100
111
94 0 ▶ croak 'Invalid default member permissions' unless _id( $wire{default_member_permissions} );
Branch: 1 branch
BranchTrueFalseTotal
unless _id($wire{"default_member_permissions"}) 0 0 0
95 0 $wire{default_member_permissions} = "$wire{default_member_permissions}";
96 }
97 57 ▶ $wire{nsfw} = $wire{nsfw} ? JSON::PP::true : JSON::PP::false if exists $wire{nsfw};
Branch: 2 branches
BranchTrueFalseTotal
if exists $wire{"nsfw"} 0 57 57
$wire{'nsfw'} ? : 0 0 0
98 0 ▶ _localizations( $wire{name_localizations}, sub { $type == 1 ? _name( $_[0] ) : _description( $_[0] ) }, 32 )
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$type == 1 ? : 0 0 0
99 57 ▶ if exists $wire{name_localizations};
Branch: 1 branch
BranchTrueFalseTotal
if exists $wire{"name_localizations"} 0 57 57
100 _localizations( $wire{description_localizations}, \&_description, 100 )
101 57 ▶ if exists $wire{description_localizations};
Branch: 1 branch
BranchTrueFalseTotal
if exists $wire{"description_localizations"} 0 57 57
102 57 ▶ croak 'Command declaration is not JSON encodable' unless eval {
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $node->{"wire"} = _clone(\%wire); 1 } } 0 57 57
103 57 $node->{wire} = _clone( \%wire );
104 57 1;
105 };
106  
107 # Commit a valid declaration atomically, preserving its registration order.
108 57 my $key = $type . ':' . $name;
109 57 ▶ unless ( exists $self->{commands}{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"commands"}{$key}) 56 1 57
110 56 my $count = grep { $self->{commands}{$_}{type} == $type } @{ $self->{order} };
111 56 ▶ croak 'Command count exceeds Discord limits' if $count >= ( $type == 1 ? 100 : 15 );
Branch: 2 branches
BranchTrueFalseTotal
if $count >= ($type == 1 ? 100 : 15) 0 56 56
$type == 1 ? : 54 2 56
112 56 push @{ $self->{order} }, $key;
113 }
114 57 $self->{commands}{$key} = $node;
115 57 return $self;
116 }
117  
118 2 ▶sub _has_handlers {
Subroutine _has_handlers: called CC=5 SCAR=19.9
119 2 my ($nodes) = @_;
120  
121 # A parent command can delegate execution to a nested subcommand.
122 2 for my $node (@$nodes) {
123 1 ▶ return 1 if $node->{handler} || _has_handlers( $node->{options} // [] );
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"handler"} or _has_handlers($node->{"options"} // []) 1 0 1
Condition20%1/5
$node->{"handler"} or _has_handlers($node->{"options"} // [])
l!l&&r!l&&!r
100
$node->{"options"} // []
l!l
00
MC/DC0%0/2
$node->{"handler"} or _has_handlers($node->{"options"} // [])
$node->{"handler"} _has_handlers($node->{"options"} // [])
MC/DC0%0/1
$node->{"options"} // []
$node->{"options"}
Truth table33%1/3
A or B
A= $node->{"handler"}B= _has_handlers($node->{"options"} // [])
ABresult
1X1
011
000
Truth table0%0/2
A
A= $node->{"options"}
Aresult
11
00
124 }
125 1 return 0;
126 }
127  
128 0 ▶sub _localizations {
Subroutine _localizations: not called CC=6 SCAR=37.4
129 0 my ( $map, $validator, $max ) = @_;
130  
131 # A null localization map is allowed; supplied translations share the base limits.
132 0 ▶ return if !defined $map;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $map 0 0 0
133 0 ▶ croak 'Invalid command localizations' unless ref($map) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $map eq "HASH" 0 0 0
134 0 for my $value ( values %$map ) {
135 0 ▶ croak 'Invalid localized command value' unless $validator->($value) && length($value) <= $max;
Branch: 1 branch
BranchTrueFalseTotal
unless &$validator($value) and length $value <= $max 0 0 0
Condition0%0/3
&$validator($value) and length $value <= $max
!ll&&!rl&&r
000
MC/DC0%0/2
&$validator($value) and length $value <= $max
&$validator($value) length $value <= $max
Truth table0%0/3
A and B
A= &$validator($value)B= length $value <= $max
ABresult
0X0
100
111
136 }
137 }
138  
139 66 ▶sub _options {
Subroutine _options: called CC=21 SCAR=32.6
140 66 my ( $self, $options, $level ) = @_;
141 66 ▶ croak 'Options must be an array with at most 25 entries'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options eq "ARRAY" and @$options <= 25 0 66 66
Condition33%1/3
ref $options eq "ARRAY" and @$options <= 25
!ll&&!rl&&r
0066
MC/DC0%0/2
ref $options eq "ARRAY" and @$options <= 25
ref $options eq "ARRAY" @$options <= 25
Truth table33%1/3
A and B
A= ref $options eq "ARRAY"B= @$options <= 25
ABresult
0X0
100
111
142 unless ref($options) eq 'ARRAY' && @$options <= 25;
143  
144 # Validate sibling names, required-option order and nesting at each level.
145 66 my ( @wire, @nodes, %names );
146 66 my ( $optional_seen, $has_subcommands );
147 66 for my $option (@$options) {
148 croak 'Invalid command option'
149 unless ref($option) eq 'HASH'
150 && _name( $option->{name} )
151 20 ▶ && _description( $option->{description} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $option eq "HASH" and _name($option->{"name"}) and _description($option->{"description"}) 1 19 20
Condition66%4/6
ref $option eq "HASH" and _name($option->{"name"}) and _description($option->{"description"})
!ll&&!rl&&r
1019
ref $option eq "HASH" and _name($option->{"name"})
!ll&&!rl&&r
0119
MC/DC33%1/3
ref $option eq "HASH" and _name($option->{"name"}) and _description($option->{"description"})
ref $option eq "HASH" _name($option->{"name"}) _description($option->{"description"})
Truth table50%2/4
A and B and C
A= ref $option eq "HASH"B= _name($option->{"name"})C= _description($option->{"description"})
ABCresult
0XX0
10X0
1100
1111
152 19 ▶ croak 'Invalid command option' if $names{ $option->{name} };
Branch: 1 branch
BranchTrueFalseTotal
if $names{$option->{"name"}} 1 18 19
153 18 $names{ $option->{name} } = 1;
154  
155 18 my %declaration = %$option;
156 18 my $type = _option_type( $declaration{type} );
157 18 $declaration{type} = $type;
158 18 my $node = { name => $declaration{name}, type => $type };
159 18 my $is_subcommand = $type <= 2;
160 18 ▶ $has_subcommands //= $is_subcommand;
Condition66%2/3
$has_subcommands //= $is_subcommand
l!l&&r!l&&!r
4140
MC/DC0%0/2
$has_subcommands //= $is_subcommand
$has_subcommands $is_subcommand
Truth table66%2/3
A //= B
A= $has_subcommandsB= $is_subcommand
ABresult
1X1
011
000
161 18 ▶ croak 'Subcommands cannot be mixed with value options'
Branch: 1 branch
BranchTrueFalseTotal
if $is_subcommand != $has_subcommands 0 18 18
162 if $is_subcommand != $has_subcommands;
163 18 ▶ croak 'Groups contain only subcommands' if $level eq 'group' && $type != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $level eq "group" and $type != 1 0 18 18
Condition33%1/3
$level eq "group" and $type != 1
!ll&&!rl&&r
1800
MC/DC0%0/2
$level eq "group" and $type != 1
$level eq "group" $type != 1
Truth table33%1/3
A and B
A= $level eq "group"B= $type != 1
ABresult
0X0
100
111
164 18 ▶ croak 'Subcommands cannot be nested inside subcommands'
Branch: 1 branch
BranchTrueFalseTotal
if $level eq "subcommand" and $is_subcommand 0 18 18
Condition66%2/3
$level eq "subcommand" and $is_subcommand
!ll&&!rl&&r
1710
MC/DC0%0/2
$level eq "subcommand" and $is_subcommand
$level eq "subcommand" $is_subcommand
Truth table66%2/3
A and B
A= $level eq "subcommand"B= $is_subcommand
ABresult
0X0
100
111
165 if $level eq 'subcommand' && $is_subcommand;
166  
167 # Subcommands carry child handlers; value options carry wire constraints.
168 18 ▶ if ($is_subcommand) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_subcommand) { } 2 16 18
169 2 $self->_configure_subcommand( \%declaration, $node );
170 }
171 else {
172 croak 'Value options cannot contain options or handlers'
173 16 ▶ if exists( $declaration{options} ) || exists( $declaration{handler} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $declaration{"options"} or exists $declaration{"handler"} 0 16 16
Condition33%1/3
exists $declaration{"options"} or exists $declaration{"handler"}
l!l&&r!l&&!r
0016
MC/DC0%0/2
exists $declaration{"options"} or exists $declaration{"handler"}
exists $declaration{"options"} exists $declaration{"handler"}
Truth table33%1/3
A or B
A= exists $declaration{"options"}B= exists $declaration{"handler"}
ABresult
1X1
011
000
174 16 ▶ if ( $declaration{required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($declaration{'required'}) { } 4 12 16
175 4 ▶ croak 'Required options must precede optional options' if $optional_seen;
Branch: 1 branch
BranchTrueFalseTotal
if $optional_seen 1 3 4
176 }
177 else {
178 12 $optional_seen = 1;
179 }
180 15 _configure_value_option( \%declaration, $node );
181 }
182  
183 # Snapshot each validated option so later caller edits cannot change deployment.
184 _localizations( $declaration{name_localizations}, \&_name, 32 )
185 13 ▶ if exists $declaration{name_localizations};
Branch: 1 branch
BranchTrueFalseTotal
if exists $declaration{"name_localizations"} 0 13 13
186 _localizations( $declaration{description_localizations}, \&_description, 100 )
187 13 ▶ if exists $declaration{description_localizations};
Branch: 1 branch
BranchTrueFalseTotal
if exists $declaration{"description_localizations"} 0 13 13
188 13 $node->{wire} = _clone( \%declaration );
189 13 push @wire, $node->{wire};
190 13 push @nodes, $node;
191 }
192 59 return ( \@wire, \@nodes );
193 }
194  
195 15 ▶sub _configure_value_option {
Subroutine _configure_value_option: called CC=5 SCAR=16.1
196 15 my ( $declaration, $node ) = @_;
197  
198 # Normalize scalar flags without adding omitted fields to the wire declaration.
199 $declaration->{required} = $declaration->{required} ? JSON::PP::true : JSON::PP::false
200 15 ▶ if exists $declaration->{required};
Branch: 2 branches
BranchTrueFalseTotal
if exists $declaration->{"required"} 3 12 15
$declaration->{'required'} ? : 3 0 3
201  
202 # Keep suggestions, bounds and resource filters independent of sibling ordering.
203 15 ▶ _configure_autocomplete( $declaration, $node ) if exists $declaration->{autocomplete};
Branch: 1 branch
BranchTrueFalseTotal
if exists $declaration->{"autocomplete"} 4 11 15
204 14 ▶ _normalize_choices($declaration) if exists $declaration->{choices};
Branch: 1 branch
BranchTrueFalseTotal
if exists $declaration->{"choices"} 3 11 14
205 14 _normalize_option_bounds($declaration);
206 12 _normalize_option_filters($declaration);
207 }
208  
209 18 ▶sub _option_type {
Subroutine _option_type: called CC=7 SCAR=25.7
210 18 my ($type) = @_;
211  
212 # Accept a name or a wire value, then return one numeric representation.
213 $type = $OPTION_TYPES{$type}
214 18 ▶ if defined($type) && !ref($type) && exists $OPTION_TYPES{$type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and not ref $type and exists $OPTION_TYPES{$type} 18 0 18
Condition33%2/6
defined $type and not ref $type and exists $OPTION_TYPES{$type}
!ll&&!rl&&r
0018
defined $type and not ref $type
!ll&&!rl&&r
0018
MC/DC0%0/3
defined $type and not ref $type and exists $OPTION_TYPES{$type}
defined $type not ref $type exists $OPTION_TYPES{$type}
Truth table25%1/4
A and not(B) and C
A= defined $typeB= not ref $typeC= exists $OPTION_TYPES{$type}
ABCresult
0XX0
10X0
1100
1111
215 18 ▶ croak 'Invalid option type'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and not ref $type and $type =~ /\A(?:[1-9]|10|11)\z/u 0 18 18
Condition33%2/6
defined $type and not ref $type and $type =~ /\A(?:[1-9]|10|11)\z/u
!ll&&!rl&&r
0018
defined $type and not ref $type
!ll&&!rl&&r
0018
MC/DC0%0/3
defined $type and not ref $type and $type =~ /\A(?:[1-9]|10|11)\z/u
defined $type not ref $type $type =~ /\A(?:[1-9]|10|11)\z/u
Truth table25%1/4
A and not(B) and C
A= defined $typeB= not ref $typeC= $type =~ /\A(?:[1-9]|10|11)\z/u
ABCresult
0XX0
10X0
1100
1111
216 unless defined($type) && !ref($type) && $type =~ /\A(?:[1-9]|10|11)\z/;
217 18 return 0 + $type;
218 }
219  
220 2 ▶sub _configure_subcommand {
Subroutine _configure_subcommand: called CC=13 SCAR=31.1
221 2 my ( $self, $declaration, $node ) = @_;
222 2 my $type = $declaration->{type};
223  
224 # Reject value-only settings before descending into the command tree.
225 croak 'Subcommands cannot have required, choices or autocomplete fields'
226 if exists( $declaration->{required} )
227 || exists( $declaration->{choices} )
228 2 ▶ || exists( $declaration->{autocomplete} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $declaration->{"required"} or exists $declaration->{"choices"} or exists $declaration->{"autocomplete"} 0 2 2
Condition33%2/6
exists $declaration->{"required"} or exists $declaration->{"choices"} or exists $declaration->{"autocomplete"}
l!l&&r!l&&!r
002
exists $declaration->{"required"} or exists $declaration->{"choices"}
l!l&&r!l&&!r
002
MC/DC0%0/3
exists $declaration->{"required"} or exists $declaration->{"choices"} or exists $declaration->{"autocomplete"}
exists $declaration->{"required"} exists $declaration->{"choices"} exists $declaration->{"autocomplete"}
Truth table25%1/4
A or B or C
A= exists $declaration->{"required"}B= exists $declaration->{"choices"}C= exists $declaration->{"autocomplete"}
ABCresult
1XX1
01X1
0011
0000
229 croak 'Groups require at least one subcommand'
230 2 ▶ if $type == 2 && ( !ref( $declaration->{options} ) || !@{ $declaration->{options} } );
Branch: 1 branch
BranchTrueFalseTotal
if $type == 2 and not ref $declaration->{"options"} && @{$declaration->{"options"};} 1 1 2
Condition50%3/6
$type == 2 and not ref $declaration->{"options"} && @{$declaration->{"options"};}
!ll&&!rl&&r
101
ref $declaration->{'options'} && @{$declaration->{'options'};}
!ll&&!rl&&r
010
MC/DC33%1/3
$type == 2 and not ref $declaration->{"options"} && @{$declaration->{"options"};}
$type == 2 ref $declaration->{'options'} @{$declaration->{'options'};}
Truth table50%2/4
A and not(B && C)
A= $type == 2B= ref $declaration->{'options'}C= @{$declaration->{'options'};}
ABCresult
0XX0
1110
10X1
1101
231  
232 # Groups select a subcommand; only executable subcommands may own handlers.
233 1 ▶ my $level = $type == 1 ? 'subcommand' : 'group';
Branch: 1 branch
BranchTrueFalseTotal
$type == 1 ? : 1 0 1
234 1 ▶ my ( $wire_children, $children ) = $self->_options( $declaration->{options} // [], $level );
Condition50%1/2
$declaration->{"options"} // []
l!l
10
MC/DC0%0/1
$declaration->{"options"} // []
$declaration->{"options"}
Truth table50%1/2
A
A= $declaration->{"options"}
Aresult
11
00
235 1 $declaration->{options} = $wire_children;
236 1 $node->{options} = $children;
237 croak 'Invalid subcommand handler'
238 1 ▶ if defined( $declaration->{handler} ) && ref( $declaration->{handler} ) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $declaration->{"handler"} and ref $declaration->{"handler"} ne "CODE" 0 1 1
Condition33%1/3
defined $declaration->{"handler"} and ref $declaration->{"handler"} ne "CODE"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $declaration->{"handler"} and ref $declaration->{"handler"} ne "CODE"
defined $declaration->{"handler"} ref $declaration->{"handler"} ne "CODE"
Truth table33%1/3
A and B
A= defined $declaration->{"handler"}B= ref $declaration->{"handler"} ne "CODE"
ABresult
0X0
100
111
239 1 ▶ croak 'Groups cannot have handlers' if $type == 2 && defined( $declaration->{handler} );
Branch: 1 branch
BranchTrueFalseTotal
if $type == 2 and defined $declaration->{"handler"} 0 1 1
Condition33%1/3
$type == 2 and defined $declaration->{"handler"}
!ll&&!rl&&r
100
MC/DC0%0/2
$type == 2 and defined $declaration->{"handler"}
$type == 2 defined $declaration->{"handler"}
Truth table33%1/3
A and B
A= $type == 2B= defined $declaration->{"handler"}
ABresult
0X0
100
111
240 1 $node->{handler} = delete $declaration->{handler};
241 }
242  
243 4 ▶sub _configure_autocomplete {
Subroutine _configure_autocomplete: called CC=8 SCAR=24.5
244 4 my ( $declaration, $node ) = @_;
245 4 my $type = $declaration->{type};
246  
247 # Only compatible value types may opt into callback-based suggestions.
248 4 ▶ croak 'Autocomplete is valid only for string/integer/number options'
Branch: 1 branch
BranchTrueFalseTotal
unless $type == 3 or $type == 4 or $type == 10 0 4 4
Condition50%3/6
$type == 3 or $type == 4 or $type == 10
l!l&&r!l&&!r
400
$type == 3 or $type == 4
l!l&&r!l&&!r
310
MC/DC0%0/3
$type == 3 or $type == 4 or $type == 10
$type == 3 $type == 4 $type == 10
Truth table50%2/4
A or B or C
A= $type == 3B= $type == 4C= $type == 10
ABCresult
1XX1
01X1
0011
0000
249 unless $type == 3 || $type == 4 || $type == 10;
250 croak 'Autocomplete and choices are mutually exclusive'
251 4 ▶ if exists( $declaration->{choices} ) && $declaration->{autocomplete};
Branch: 1 branch
BranchTrueFalseTotal
if exists $declaration->{"choices"} and $declaration->{"autocomplete"} 1 3 4
Condition66%2/3
exists $declaration->{"choices"} and $declaration->{"autocomplete"}
!ll&&!rl&&r
301
MC/DC50%1/2
exists $declaration->{"choices"} and $declaration->{"autocomplete"}
exists $declaration->{"choices"} $declaration->{"autocomplete"}
Truth table66%2/3
A and B
A= exists $declaration->{"choices"}B= $declaration->{"autocomplete"}
ABresult
0X0
100
111
252  
253 # Retain the callback locally and send only the enabled flag to Discord.
254 3 ▶ if ( ref( $declaration->{autocomplete} ) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $declaration->{'autocomplete'} eq 'CODE') { } 3 0 3
elsif ($declaration->{'autocomplete'}) { } 0 0 0
255 3 $node->{autocomplete} = $declaration->{autocomplete};
256 3 $declaration->{autocomplete} = JSON::PP::true;
257 }
258 elsif ( $declaration->{autocomplete} ) {
259 0 croak 'Enabled autocomplete requires a callback';
260 }
261 else {
262 0 $declaration->{autocomplete} = JSON::PP::false;
263 }
264 }
265  
266 5 ▶sub _valid_numeric_value {
Subroutine _valid_numeric_value: called CC=4 SCAR=14.1
267 5 my ( $type, $value ) = @_;
268 5 ▶ return 0 unless _number($value);
Branch: 1 branch
BranchTrueFalseTotal
unless _number($value) 0 5 5
269 5 ▶ return 1 if $type == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $type == 10 1 4 5
270 4 ▶ return $value == int($value) && abs($value) < 9007199254740992;
Condition66%2/3
$value == int $value && abs $value < 9007199254740992
!ll&&!rl&&r
103
MC/DC50%1/2
$value == int $value && abs $value < 9007199254740992
$value == int $value abs $value < 9007199254740992
Truth table66%2/3
A && B
A= $value == int $valueB= abs $value < 9007199254740992
ABresult
0X0
100
111
271 }
272  
273 3 ▶sub _normalize_choices {
Subroutine _normalize_choices: called CC=14 SCAR=29.5
274 3 my ($declaration) = @_;
275 3 my $type = $declaration->{type};
276 3 ▶ croak 'Choices are invalid for this option type' unless $type == 3 || $type == 4 || $type == 10;
Branch: 1 branch
BranchTrueFalseTotal
unless $type == 3 or $type == 4 or $type == 10 0 3 3
Condition83%5/6
$type == 3 or $type == 4 or $type == 10
l!l&&r!l&&!r
210
$type == 3 or $type == 4
l!l&&r!l&&!r
111
MC/DC0%0/3
$type == 3 or $type == 4 or $type == 10
$type == 3 $type == 4 $type == 10
Truth table75%3/4
A or B or C
A= $type == 3B= $type == 4C= $type == 10
ABCresult
1XX1
01X1
0011
0000
277 croak 'Choices must be an array of at most 25 entries'
278 3 ▶ unless ref( $declaration->{choices} ) eq 'ARRAY' && @{ $declaration->{choices} } <= 25;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $declaration->{"choices"} eq "ARRAY" and @{$declaration->{"choices"};} <= 25 0 3 3
Condition33%1/3
ref $declaration->{"choices"} eq "ARRAY" and @{$declaration->{"choices"};} <= 25
!ll&&!rl&&r
003
MC/DC0%0/2
ref $declaration->{"choices"} eq "ARRAY" and @{$declaration->{"choices"};} <= 25
ref $declaration->{"choices"} eq "ARRAY" @{$declaration->{"choices"};} <= 25
Truth table33%1/3
A and B
A= ref $declaration->{"choices"} eq "ARRAY"B= @{$declaration->{"choices"};} <= 25
ABresult
0X0
100
111
279  
280 # Encode every choice according to its option type, regardless of Perl scalar flags.
281 3 my @choices;
282 3 for my $choice ( @{ $declaration->{choices} } ) {
283 croak 'Invalid choice'
284 unless ref($choice) eq 'HASH'
285 && _description( $choice->{name} )
286 && defined( $choice->{value} )
287 3 ▶ && !ref( $choice->{value} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $choice eq "HASH" and _description($choice->{"name"}) and defined $choice->{"value"} and not ref $choice->{"value"} 0 3 3
Condition33%3/9
ref $choice eq "HASH" and _description($choice->{"name"}) and defined $choice->{"value"} and not ref $choice->{"value"}
!ll&&!rl&&r
003
ref $choice eq "HASH" and _description($choice->{"name"}) and defined $choice->{"value"}
!ll&&!rl&&r
003
ref $choice eq "HASH" and _description($choice->{"name"})
!ll&&!rl&&r
003
MC/DC0%0/4
ref $choice eq "HASH" and _description($choice->{"name"}) and defined $choice->{"value"} and not ref $choice->{"value"}
ref $choice eq "HASH" _description($choice->{"name"}) defined $choice->{"value"} not ref $choice->{"value"}
Truth table20%1/5
A and B and C and not(D)
A= ref $choice eq "HASH"B= _description($choice->{"name"})C= defined $choice->{"value"}D= not ref $choice->{"value"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
288 3 my %wire_choice = %$choice;
289 3 ▶ if ( $type == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 3) { } 1 2 3
290 1 ▶ croak 'Invalid choice value' if length( $wire_choice{value} ) > 100;
Branch: 1 branch
BranchTrueFalseTotal
if length $wire_choice{"value"} > 100 0 1 1
291 1 $wire_choice{value} = "$wire_choice{value}";
292 }
293 else {
294 2 ▶ croak 'Invalid choice value' unless _valid_numeric_value( $type, $wire_choice{value} );
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_numeric_value($type, $wire_choice{"value"}) 0 2 2
295 2 $wire_choice{value} = 0 + $wire_choice{value};
296 }
297 3 push @choices, \%wire_choice;
298 }
299 3 $declaration->{choices} = \@choices;
300 }
301  
302 14 ▶sub _normalize_option_bounds {
Subroutine _normalize_option_bounds: called CC=21 SCAR=36.0
303 14 my ($declaration) = @_;
304 14 my $type = $declaration->{type};
305  
306 # Numeric bounds must match the declared number type and form a valid interval.
307 14 for my $field (qw(min_value max_value)) {
308 27 ▶ next unless exists $declaration->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $declaration->{$field} 24 3 27
309 croak 'Invalid numeric option bound'
310 3 ▶ unless ( $type == 4 || $type == 10 ) && _valid_numeric_value( $type, $declaration->{$field} );
Branch: 1 branch
BranchTrueFalseTotal
unless $type == 4 || $type == 10 and _valid_numeric_value($type, $declaration->{$field}) 1 2 3
Condition50%3/6
$type == 4 || $type == 10 and _valid_numeric_value($type, $declaration->{$field})
!ll&&!rl&&r
012
$type == 4 or $type == 10
l!l&&r!l&&!r
300
MC/DC33%1/3
$type == 4 || $type == 10 and _valid_numeric_value($type, $declaration->{$field})
$type == 4 $type == 10 _valid_numeric_value($type, $declaration->{$field})
Truth table40%2/5
A or B and C
A= $type == 4B= $type == 10C= _valid_numeric_value($type, $declaration->{$field})
ABCresult
00X0
1X00
0100
1X11
0111
311 2 $declaration->{$field} = 0 + $declaration->{$field};
312 }
313 croak 'Minimum option value exceeds maximum'
314 if exists( $declaration->{min_value} )
315 && exists( $declaration->{max_value} )
316 13 ▶ && $declaration->{min_value} > $declaration->{max_value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $declaration->{"min_value"} and exists $declaration->{"max_value"} and $declaration->{"min_value"} > $declaration->{"max_value"} 0 13 13
Condition66%4/6
exists $declaration->{"min_value"} and exists $declaration->{"max_value"} and $declaration->{"min_value"} > $declaration->{"max_value"}
!ll&&!rl&&r
1210
exists $declaration->{"min_value"} and exists $declaration->{"max_value"}
!ll&&!rl&&r
1201
MC/DC0%0/3
exists $declaration->{"min_value"} and exists $declaration->{"max_value"} and $declaration->{"min_value"} > $declaration->{"max_value"}
exists $declaration->{"min_value"} exists $declaration->{"max_value"} $declaration->{"min_value"} > $declaration->{"max_value"}
Truth table50%2/4
A and B and C
A= exists $declaration->{"min_value"}B= exists $declaration->{"max_value"}C= $declaration->{"min_value"} > $declaration->{"max_value"}
ABCresult
0XX0
10X0
1100
1111
317  
318 # String lengths have separate limits and must not be accepted for numeric options.
319 13 for my $field (qw(min_length max_length)) {
320 26 ▶ next unless exists $declaration->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $declaration->{$field} 24 2 26
321 2 my $length = $declaration->{$field};
322 2 ▶ croak 'Invalid string length bound'
Branch: 1 branch
BranchTrueFalseTotal
unless $type == 3 and defined $length and not ref $length and $length =~ /\A\d+\z/u and $length <= 6000 and $field ne "max_length" || $length > 0 0 2 2
Condition38%7/18
$type == 3 and defined $length and not ref $length and $length =~ /\A\d+\z/u and $length <= 6000 and $field ne "max_length" || $length > 0
!ll&&!rl&&r
002
$type == 3 and defined $length and not ref $length and $length =~ /\A\d+\z/u and $length <= 6000
!ll&&!rl&&r
002
$type == 3 and defined $length and not ref $length and $length =~ /\A\d+\z/u
!ll&&!rl&&r
002
$type == 3 and defined $length and not ref $length
!ll&&!rl&&r
002
$type == 3 and defined $length
!ll&&!rl&&r
002
$field ne "max_length" or $length > 0
l!l&&r!l&&!r
110
MC/DC0%0/7
$type == 3 and defined $length and not ref $length and $length =~ /\A\d+\z/u and $length <= 6000 and $field ne "max_length" || $length > 0
$type == 3 defined $length not ref $length $length =~ /\A\d+\z/u $length <= 6000 $field ne "max_length" $length > 0
Truth table25%2/8
A and B and not(C) and D and E and F or G
A= $type == 3B= defined $lengthC= not ref $lengthD= $length =~ /\A\d+\z/uE= $length <= 6000F= $field ne "max_length"G= $length > 0
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110XX0
11111000
111111X1
11111011
323 unless $type == 3
324 && defined($length)
325 && !ref($length)
326 && $length =~ /\A\d+\z/
327 && $length <= 6000
328 && ( $field ne 'max_length' || $length > 0 );
329 2 $declaration->{$field} = 0 + $length;
330 }
331 croak 'Minimum string length exceeds maximum'
332 if exists( $declaration->{min_length} )
333 && exists( $declaration->{max_length} )
334 13 ▶ && $declaration->{min_length} > $declaration->{max_length};
Branch: 1 branch
BranchTrueFalseTotal
if exists $declaration->{"min_length"} and exists $declaration->{"max_length"} and $declaration->{"min_length"} > $declaration->{"max_length"} 1 12 13
Condition66%4/6
exists $declaration->{"min_length"} and exists $declaration->{"max_length"} and $declaration->{"min_length"} > $declaration->{"max_length"}
!ll&&!rl&&r
1201
exists $declaration->{"min_length"} and exists $declaration->{"max_length"}
!ll&&!rl&&r
1201
MC/DC33%1/3
exists $declaration->{"min_length"} and exists $declaration->{"max_length"} and $declaration->{"min_length"} > $declaration->{"max_length"}
exists $declaration->{"min_length"} exists $declaration->{"max_length"} $declaration->{"min_length"} > $declaration->{"max_length"}
Truth table50%2/4
A and B and C
A= exists $declaration->{"min_length"}B= exists $declaration->{"max_length"}C= $declaration->{"min_length"} > $declaration->{"max_length"}
ABCresult
0XX0
10X0
1100
1111
335 }
336  
337 12 ▶sub _normalize_option_filters {
Subroutine _normalize_option_filters: called CC=14 SCAR=50.4
338 12 my ($declaration) = @_;
339  
340 # Channel and attachment filters belong only to their corresponding option types.
341 12 ▶ if ( exists $declaration->{channel_types} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $declaration->{"channel_types"}) 0 12 12
342 croak 'channel_types requires a channel option'
343 0 ▶ unless $declaration->{type} == 7 && ref( $declaration->{channel_types} ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $declaration->{"type"} == 7 and ref $declaration->{"channel_types"} eq "ARRAY" 0 0 0
Condition0%0/3
$declaration->{"type"} == 7 and ref $declaration->{"channel_types"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$declaration->{"type"} == 7 and ref $declaration->{"channel_types"} eq "ARRAY"
$declaration->{"type"} == 7 ref $declaration->{"channel_types"} eq "ARRAY"
Truth table0%0/3
A and B
A= $declaration->{"type"} == 7B= ref $declaration->{"channel_types"} eq "ARRAY"
ABresult
0X0
100
111
344 croak 'Invalid channel type'
345 0 ▶ if grep { !defined($_) || ref($_) || $_ !~ /\A\d+\z/ } @{ $declaration->{channel_types} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ or ref $_ or not $_ =~ /\A\d+\z/u;} @{$$declaration{"channel_types"};} 0 0 0
Condition0%0/6
!defined($_) || ref $_ || !($_ =~ /\A\d+\z/u)
l!l&&r!l&&!r
000
not defined $_ or ref $_
l!l&&r!l&&!r
000
MC/DC0%0/3
!defined($_) || ref $_ || !($_ =~ /\A\d+\z/u)
not defined $_ ref $_ !($_ =~ /\A\d+\z/u)
Truth table0%0/4
not(A) or B || not(C)
A= not defined $_B= ref $_C= !($_ =~ /\A\d+\z/u)
ABCresult
1XX1
01X1
0011
0000
346 0 $declaration->{channel_types} = [ map { 0 + $_ } @{ $declaration->{channel_types} } ];
347 }
348 12 ▶ if ( exists $declaration->{file_types} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $declaration->{"file_types"}) 0 12 12
349 croak 'file_types requires an attachment option'
350 unless $declaration->{type} == 11
351 && ref( $declaration->{file_types} ) eq 'ARRAY'
352 0 ▶ && @{ $declaration->{file_types} } <= 10;
Branch: 1 branch
BranchTrueFalseTotal
unless $declaration->{"type"} == 11 and ref $declaration->{"file_types"} eq "ARRAY" and @{$declaration->{"file_types"};} <= 10 0 0 0
Condition0%0/6
$declaration->{"type"} == 11 and ref $declaration->{"file_types"} eq "ARRAY" and @{$declaration->{"file_types"};} <= 10
!ll&&!rl&&r
000
$declaration->{"type"} == 11 and ref $declaration->{"file_types"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
$declaration->{"type"} == 11 and ref $declaration->{"file_types"} eq "ARRAY" and @{$declaration->{"file_types"};} <= 10
$declaration->{"type"} == 11 ref $declaration->{"file_types"} eq "ARRAY" @{$declaration->{"file_types"};} <= 10
Truth table0%0/4
A and B and C
A= $declaration->{"type"} == 11B= ref $declaration->{"file_types"} eq "ARRAY"C= @{$declaration->{"file_types"};} <= 10
ABCresult
0XX0
10X0
1100
1111
353 croak 'Invalid file type'
354 0 ▶ if grep { !defined($_) || ref($_) || $_ !~ /\A(?:image|video|audio|\.[^\s\/\\\x00-\x1f\x7f]+)\z/ }
Condition0%0/6
!defined($_) || ref $_ || !($_ =~ m[\A(?:image|video|audio|\.[^\s/\\\x00-\x1f\x7f]+)\z]u)
l!l&&r!l&&!r
000
not defined $_ or ref $_
l!l&&r!l&&!r
000
MC/DC0%0/3
!defined($_) || ref $_ || !($_ =~ m[\A(?:image|video|audio|\.[^\s/\\\x00-\x1f\x7f]+)\z]u)
not defined $_ ref $_ !($_ =~ m[\A(?:image|video|audio|\.[^\s/\\\x00-\x1f\x7f]+)\z]u)
Truth table0%0/4
not(A) or B || not(C)
A= not defined $_B= ref $_C= !($_ =~ m[\A(?:image|video|audio|\.[^\s/\\\x00-\x1f\x7f]+)\z]u)
ABCresult
1XX1
01X1
0011
0000
355 0 ▶ @{ $declaration->{file_types} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ or ref $_ or not $_ =~ m[\A(?:image|video|audio|\.[^\s/\\\x00-\x1f\x7f]+)\z]u;} @{$$declaration{"file_types"};} 0 0 0
356 }
357 }
358  
359 30 ▶sub definitions {
Subroutine definitions: called CC=1 SCAR=0.0
Pod documented
360 30 my ($self) = @_;
361  
362 # Return detached wire snapshots, never application callbacks.
363 30 my @definitions = map { $self->{commands}{$_}{wire} } @{ $self->{order} };
364 30 return _clone( \@definitions );
365 }
366  
367 6 ▶sub application_id {
Subroutine application_id: called CC=3 SCAR=11.0
Pod documented
368 6 my ( $self, $id ) = @_;
369  
370 # Treat an explicit argument as a setter and preserve the ID as a string.
371 6 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 4 2 6
372 4 ▶ croak 'Invalid application ID' unless _id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless _id($id) 0 4 4
373 4 $self->{application_id} = "$id";
374 }
375 6 return $self->{application_id};
376 }
377  
378 5 ▶sub component {
Subroutine component: called CC=7 SCAR=23.8
Pod documented
379 5 my ( $self, $id, $handler ) = @_;
380  
381 # Validate registration before replacing a handler for this custom ID.
382 5 ▶ croak 'Command registry is closed' if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 1 4 5
383 4 ▶ croak 'Invalid component ID or handler'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and not ref $id and length $id >= 1 and length $id <= 100 and ref $handler eq "CODE" 0 4 4
Condition33%4/12
defined $id and not ref $id and length $id >= 1 and length $id <= 100 and ref $handler eq "CODE"
!ll&&!rl&&r
004
defined $id and not ref $id and length $id >= 1 and length $id <= 100
!ll&&!rl&&r
004
defined $id and not ref $id and length $id >= 1
!ll&&!rl&&r
004
defined $id and not ref $id
!ll&&!rl&&r
004
MC/DC0%0/5
defined $id and not ref $id and length $id >= 1 and length $id <= 100 and ref $handler eq "CODE"
defined $id not ref $id length $id >= 1 length $id <= 100 ref $handler eq "CODE"
Truth table16%1/6
A and not(B) and C and D and E
A= defined $idB= not ref $idC= length $id >= 1D= length $id <= 100E= ref $handler eq "CODE"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
384 unless defined($id)
385 && !ref($id)
386 && length($id) >= 1
387 && length($id) <= 100
388 && ref($handler) eq 'CODE';
389 4 $self->{components}{$id} = $handler;
390 4 return $self;
391 }
392  
393 1077 ▶sub handler_for {
Subroutine handler_for: called CC=16 SCAR=30.0
Pod documented
394 1077 my ( $self, $context ) = @_;
395  
396 # Components and modal submissions dispatch directly by their custom ID.
397 1077 ▶ return $self->{components}{ $context->custom_id // '' } if $context->type == 3 || $context->type == 5;
Branch: 1 branch
BranchTrueFalseTotal
if $context->type == 3 or $context->type == 5 4 1073 1077
Condition80%4/5
$context->type == 3 or $context->type == 5
l!l&&r!l&&!r
311073
$context->custom_id // ""
l!l
40
MC/DC100%2/2
$context->type == 3 or $context->type == 5
$context->type == 3 $context->type == 5
MC/DC0%0/1
$context->custom_id // ""
$context->custom_id
Truth table100%3/3
A or B
A= $context->type == 3B= $context->type == 5
ABresult
1X1
011
000
Truth table50%1/2
A
A= $context->custom_id
Aresult
11
00
398  
399 # Walk the selected command path, inheriting a parent fallback handler.
400 1073 my $data = $context->raw->{data};
401 1073 ▶ my $node = $self->{commands}{ ( $data->{type} // 1 ) . ':' . ( $data->{name} // '' ) } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $node = $self->{"commands"}{($data->{"type"} // 1) . ':' . ($data->{"name"} // "")} or return 1073 0 1073
Condition50%2/4
$data->{"type"} // 1
l!l
10730
$data->{"name"} // ""
l!l
10730
MC/DC0%0/1
$data->{"type"} // 1
$data->{"type"}
MC/DC0%0/1
$data->{"name"} // ""
$data->{"name"}
Truth table50%1/2
A
A= $data->{"type"}
Aresult
11
00
Truth table50%1/2
A
A= $data->{"name"}
Aresult
11
00
402 1073 my $handler = $node->{handler};
403 1073 for my $part ( @{ $context->command_path } ) {
404 1 ▶ my ($child) = grep { $_->{name} eq $part && $_->{type} <= 2 } @{ $node->{options} // [] };
Condition40%2/5
$_->{'name'} eq $part && $_->{'type'} <= 2
!ll&&!rl&&r
001
$node->{"options"} // []
l!l
10
MC/DC0%0/2
$_->{'name'} eq $part && $_->{'type'} <= 2
$_->{'name'} eq $part $_->{'type'} <= 2
MC/DC0%0/1
$node->{"options"} // []
$node->{"options"}
Truth table33%1/3
A && B
A= $_->{'name'} eq $partB= $_->{'type'} <= 2
ABresult
0X0
100
111
Truth table50%1/2
A
A= $node->{"options"}
Aresult
11
00
405 1 ▶ return unless $child;
Branch: 1 branch
BranchTrueFalseTotal
unless $child 0 1 1
406 1 $node = $child;
407 1 ▶ $handler = $node->{handler} // $handler;
Condition33%1/3
$node->{"handler"} // $handler
l!l&&r!l&&!r
100
MC/DC0%0/2
$node->{"handler"} // $handler
$node->{"handler"} $handler
Truth table33%1/3
A // B
A= $node->{"handler"}B= $handler
ABresult
1X1
011
000
408 }
409  
410 # Autocomplete validates only the focused option; other required values may be absent.
411 1073 ▶ if ( $context->type == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->type == 4) 2 1071 1073
412 2 ▶ my $focused = $context->focused or return;
Branch: 1 branch
BranchTrueFalseTotal
my $focused = $context->focused or return 2 0 2
413 2 ▶ my ($option) = grep { $_->{name} eq $focused->{name} } @{ $node->{options} // [] };
Condition50%1/2
$node->{"options"} // []
l!l
20
MC/DC0%0/1
$node->{"options"} // []
$node->{"options"}
Truth table50%1/2
A
A= $node->{"options"}
Aresult
11
00
414 2 ▶ return $option ? $option->{autocomplete} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$option ? : 2 0 2
415 }
416  
417 # Normal commands must satisfy the local declaration before application code runs.
418 1071 _validate_supplied_options( $node, $context->options );
419 1071 return $handler;
420 }
421  
422 1071 ▶sub _validate_supplied_options {
Subroutine _validate_supplied_options: called CC=7 SCAR=20.9
423 1071 my ( $node, $values ) = @_;
424  
425 # Reject unknown values and missing required values as separate input failures.
426 1071 ▶ my %declared = map { $_->{name} => $_ } @{ $node->{options} // [] };
Condition50%1/2
$node->{"options"} // []
l!l
10710
MC/DC0%0/1
$node->{"options"} // []
$node->{"options"}
Truth table50%1/2
A
A= $node->{"options"}
Aresult
11
00
427 1071 for my $name ( keys %$values ) {
428 2 ▶ my $option = $declared{$name} or croak 'Unknown interaction option';
Branch: 1 branch
BranchTrueFalseTotal
my $option = $declared{$name} or croak("Unknown interaction option") 2 0 2
429 2 _validate_option_value( $option, $values->{$name} );
430 }
431 1071 for my $option ( values %declared ) {
432 croak 'Missing required interaction option'
433 2 ▶ if $option->{wire}{required} && !exists( $values->{ $option->{name} } );
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"wire"}{"required"} and not exists $$values{$option->{"name"}} 0 2 2
Condition33%1/3
$option->{"wire"}{"required"} and not exists $$values{$option->{"name"}}
!ll&&!rl&&r
020
MC/DC0%0/2
$option->{"wire"}{"required"} and not exists $$values{$option->{"name"}}
$option->{"wire"}{"required"} not exists $$values{$option->{"name"}}
Truth table33%1/3
A and not(B)
A= $option->{"wire"}{"required"}B= not exists $$values{$option->{"name"}}
ABresult
0X0
100
111
434 }
435 }
436  
437 2 ▶sub _validate_option_value {
Subroutine _validate_option_value: called CC=27 SCAR=55.3
438 2 my ( $option, $value ) = @_;
439  
440 # Check the value type before applying comparisons or length constraints.
441 2 my $type = $option->{type};
442 2 ▶ croak 'Invalid interaction option value' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
443 2 ▶ if ( $type == 4 || $type == 10 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type == 4 or $type == 10) { } 0 2 2
elsif ($type == 5) { } 0 2 2
elsif ($type >= 6 and $type != 10) { } 0 2 2
elsif ($type == 3) { } 2 0 2
Condition33%1/3
$type == 4 or $type == 10
l!l&&r!l&&!r
002
MC/DC0%0/2
$type == 4 or $type == 10
$type == 4 $type == 10
Truth table33%1/3
A or B
A= $type == 4B= $type == 10
ABresult
1X1
011
000
444 0 ▶ croak 'Invalid numeric interaction option' unless _valid_numeric_value( $type, $value );
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_numeric_value($type, $value) 0 0 0
445 }
446 elsif ( $type == 5 ) {
447 0 ▶ croak 'Invalid boolean interaction option'
Branch: 1 branch
BranchTrueFalseTotal
unless JSON::PP::is_bool($value) or not ref $value and $value =~ /\A[01]\z/u 0 0 0
Condition0%0/6
JSON::PP::is_bool($value) or not ref $value and $value =~ /\A[01]\z/u
l!l&&r!l&&!r
000
not ref $value and $value =~ /\A[01]\z/u
!ll&&!rl&&r
000
MC/DC0%0/3
JSON::PP::is_bool($value) or not ref $value and $value =~ /\A[01]\z/u
JSON::PP::is_bool($value) not ref $value $value =~ /\A[01]\z/u
Truth table0%0/4
A or not(B) and C
A= JSON::PP::is_bool($value)B= not ref $valueC= $value =~ /\A[01]\z/u
ABCresult
1XX1
0111
00X0
0100
448 unless JSON::PP::is_bool($value) || ( !ref($value) && $value =~ /\A[01]\z/ );
449 }
450 ▶ elsif ( $type >= 6 && $type != 10 ) {
Condition33%1/3
$type >= 6 and $type != 10
!ll&&!rl&&r
200
MC/DC0%0/2
$type >= 6 and $type != 10
$type >= 6 $type != 10
Truth table33%1/3
A and B
A= $type >= 6B= $type != 10
ABresult
0X0
100
111
451 0 ▶ croak 'Invalid entity interaction option' unless _id($value);
Branch: 1 branch
BranchTrueFalseTotal
unless _id($value) 0 0 0
452 }
453 elsif ( $type == 3 ) {
454 2 ▶ croak 'Invalid string interaction option' if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 2 2
455 }
456  
457 # Apply declaration bounds and choices after basic type validation.
458 2 my $wire = $option->{wire};
459 croak 'Interaction option outside numeric bounds'
460 if ( $type == 4 || $type == 10 )
461 && ( ( exists( $wire->{min_value} ) && $value < $wire->{min_value} )
462 2 ▶ || ( exists( $wire->{max_value} ) && $value > $wire->{max_value} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $type == 4 || $type == 10 and exists $wire->{"min_value"} && $value < $wire->{"min_value"} || exists $wire->{"max_value"} && $value > $wire->{"max_value"} 0 2 2
Condition13%2/15
$type == 4 || $type == 10 and exists $wire->{"min_value"} && $value < $wire->{"min_value"} || exists $wire->{"max_value"} && $value > $wire->{"max_value"}
!ll&&!rl&&r
200
$type == 4 or $type == 10
l!l&&r!l&&!r
002
exists $wire->{"min_value"} and $value < $wire->{"min_value"} or exists $wire->{"max_value"} and $value > $wire->{"max_value"}
l!l&&r!l&&!r
000
exists $wire->{"min_value"} and $value < $wire->{"min_value"}
!ll&&!rl&&r
000
exists $wire->{"max_value"} and $value > $wire->{"max_value"}
!ll&&!rl&&r
000
MC/DC0%0/6
$type == 4 || $type == 10 and exists $wire->{"min_value"} && $value < $wire->{"min_value"} || exists $wire->{"max_value"} && $value > $wire->{"max_value"}
$type == 4 $type == 10 exists $wire->{"min_value"} $value < $wire->{"min_value"} exists $wire->{"max_value"} $value > $wire->{"max_value"}
Truth table6%1/15
A or B and C and D or E and F
A= $type == 4B= $type == 10C= exists $wire->{"min_value"}D= $value < $wire->{"min_value"}E= exists $wire->{"max_value"}F= $value > $wire->{"max_value"}
ABCDEFresult
00XXXX0
1X0X0X0
1X0X100
1X100X0
1X10100
010X0X0
010X100
01100X0
0110100
1X11XX1
1X0X111
1X10111
0111XX1
010X111
0110111
463 croak 'Interaction option outside string bounds'
464 if $type == 3
465 && ( ( exists( $wire->{min_length} ) && length($value) < $wire->{min_length} )
466 2 ▶ || ( exists( $wire->{max_length} ) && length($value) > $wire->{max_length} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $type == 3 and exists $wire->{"min_length"} && length $value < $wire->{"min_length"} || exists $wire->{"max_length"} && length $value > $wire->{"max_length"} 0 2 2
Condition33%4/12
$type == 3 and exists $wire->{"min_length"} && length $value < $wire->{"min_length"} || exists $wire->{"max_length"} && length $value > $wire->{"max_length"}
!ll&&!rl&&r
020
exists $wire->{"min_length"} and length $value < $wire->{"min_length"} or exists $wire->{"max_length"} and length $value > $wire->{"max_length"}
l!l&&r!l&&!r
002
exists $wire->{"min_length"} and length $value < $wire->{"min_length"}
!ll&&!rl&&r
200
exists $wire->{"max_length"} and length $value > $wire->{"max_length"}
!ll&&!rl&&r
200
MC/DC0%0/5
$type == 3 and exists $wire->{"min_length"} && length $value < $wire->{"min_length"} || exists $wire->{"max_length"} && length $value > $wire->{"max_length"}
$type == 3 exists $wire->{"min_length"} length $value < $wire->{"min_length"} exists $wire->{"max_length"} length $value > $wire->{"max_length"}
Truth table12%1/8
A and B and C or D and E
A= $type == 3B= exists $wire->{"min_length"}C= length $value < $wire->{"min_length"}D= exists $wire->{"max_length"}E= length $value > $wire->{"max_length"}
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
467 2 ▶ if ( $wire->{choices} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wire->{"choices"}) 0 2 2
468 croak 'Interaction option is not a declared choice'
469 0 ▶ unless grep { $_->{value} eq $value } @{ $wire->{choices} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->{"value"} eq $value;} @{$$wire{"choices"};} 0 0 0
470 }
471 }
472  
473 2 ▶sub _failed {
Subroutine _failed: called CC=1 SCAR=0.0
474 2 my ( $self, $message, $category ) = @_;
475 2 my $error = Discord::Client::Error->new( message => $message, category => $category );
476 2 return $self->{loop}->new_future->fail( $message, $category, $error );
477 }
478  
479 6 ▶sub sync {
Subroutine sync: called CC=13 SCAR=26.5
Pod documented
480 6 my ( $self, %args ) = @_;
481  
482 # Validate explicit deployment scope before taking an immutable declaration snapshot.
483 6 ▶ return $self->_failed( 'Command registry is closed', 'shutdown' ) if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 6 6
484 6 ▶ return $self->_failed( 'Command synchronization requires a REST client', 'state' ) unless $self->{rest};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"rest"} 0 6 6
485 6 ▶ return $self->_failed( 'Command synchronization is already running', 'state' ) if $self->{syncing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"syncing"} 1 5 6
486 5 ▶ my $scope = $args{scope} // { global => 1 };
Condition100%2/2
$args{"scope"} // {"global", 1}
l!l
32
MC/DC100%1/1
$args{"scope"} // {"global", 1}
$args{"scope"}
Truth table100%2/2
A
A= $args{"scope"}
Aresult
11
00
487 return $self->_failed( 'Invalid command scope', 'request' )
488 unless ref($scope) eq 'HASH'
489 && (
490 ( keys(%$scope) == 1 && _id( $scope->{guild_id} ) )
491 || ( keys(%$scope) == 1
492 && defined( $scope->{global} )
493 && !ref( $scope->{global} )
494 5 ▶ && $scope->{global} eq '1' )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scope eq "HASH" and keys %$scope == 1 && _id($scope->{"guild_id"}) || keys %$scope == 1 && defined $scope->{"global"} && !ref($scope->{"global"}) && $scope->{"global"} eq 1 1 4 5
Condition72%13/18
ref $scope eq "HASH" and keys %$scope == 1 && _id($scope->{"guild_id"}) || keys %$scope == 1 && defined $scope->{"global"} && !ref($scope->{"global"}) && $scope->{"global"} eq 1
!ll&&!rl&&r
014
keys %$scope == 1 and _id($scope->{"guild_id"}) or keys %$scope == 1 and defined $scope->{"global"} and not ref $scope->{"global"} and $scope->{"global"} eq 1
l!l&&r!l&&!r
221
keys %$scope == 1 and _id($scope->{"guild_id"})
!ll&&!rl&&r
032
keys %$scope == 1 and defined $scope->{"global"} and not ref $scope->{"global"} and $scope->{"global"} eq 1
!ll&&!rl&&r
102
keys %$scope == 1 and defined $scope->{"global"} and not ref $scope->{"global"}
!ll&&!rl&&r
102
keys %$scope == 1 and defined $scope->{"global"}
!ll&&!rl&&r
012
MC/DC28%2/7
ref $scope eq "HASH" and keys %$scope == 1 && _id($scope->{"guild_id"}) || keys %$scope == 1 && defined $scope->{"global"} && !ref($scope->{"global"}) && $scope->{"global"} eq 1
ref $scope eq "HASH" keys %$scope == 1 _id($scope->{"guild_id"}) keys %$scope == 1 defined $scope->{"global"} not ref $scope->{"global"} $scope->{"global"} eq 1
Truth table25%3/12
A and B and C or D and E and not(F) and G
A= ref $scope eq "HASH"B= keys %$scope == 1C= _id($scope->{"guild_id"})D= keys %$scope == 1E= defined $scope->{"global"}F= not ref $scope->{"global"}G= $scope->{"global"} eq 1
ABCDEFGresult
0XXXXXX0
10X0XXX0
10X10XX0
10X110X0
10X11100
1100XXX0
11010XX0
110110X0
11011100
111XXXX1
10X11111
11011111
495 );
496  
497 # Track one deployment and propagate cancellation to its current HTTP stage.
498 4 my $future = $self->{loop}->new_future;
499 4 $self->{syncing} = $future;
500 4 my $operation = { future => $future, scope => $scope, definitions => $self->definitions };
501 $future->on_cancel(
502 1 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
503 1 my $source = $operation->{source};
504 1 ▶ $source->cancel if $source && !$source->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $source and not $source->is_ready 1 0 1
Condition33%1/3
$source and not $source->is_ready
!ll&&!rl&&r
001
MC/DC0%0/2
$source and not $source->is_ready
$source not $source->is_ready
Truth table33%1/3
A and not(B)
A= $sourceB= not $source->is_ready
ABresult
0X0
100
111
505 }
506 4 );
507 4 ▶ $future->on_ready( sub { delete $self->{syncing} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
508  
509 # Discover the application only when its ID is not already known.
510 4 ▶ if ( $self->{application_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'application_id'}) { } 3 1 4
511 3 $self->_send_command_sync($operation);
512 }
513 else {
514 1 $self->_discover_sync_application($operation);
515 }
516 4 return $future;
517 }
518  
519 4 ▶sub _send_command_sync {
Subroutine _send_command_sync: called CC=5 SCAR=17.0
520 4 my ( $self, $operation ) = @_;
521 4 my $future = $operation->{future};
522 4 ▶ return if $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $future->is_ready 0 4 4
523  
524 # Bulk replacement affects exactly the requested global or guild scope.
525 4 my $path = '/applications/' . $self->{application_id};
526 4 my $scope = $operation->{scope};
527 4 ▶ $path .= '/guilds/' . $scope->{guild_id} if exists $scope->{guild_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $scope->{"guild_id"} 2 2 4
528 4 $path .= '/commands';
529  
530 # Observe transport completion without exposing raw failure details.
531 4 my $source = eval { $self->{rest}->request( 'PUT', $path, json => $operation->{definitions} ) };
532 4 $operation->{source} = $source;
533 4 ▶ unless ( blessed($source) && $source->isa('Future') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($source) and $source->isa("Future")) 0 4 4
Condition33%1/3
builtin::blessed($source) and $source->isa("Future")
!ll&&!rl&&r
004
MC/DC0%0/2
builtin::blessed($source) and $source->isa("Future")
builtin::blessed($source) $source->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($source)B= $source->isa("Future")
ABresult
0X0
100
111
534 0 $future->fail( $self->_failed( 'Command synchronization transport failed', 'transport' )->failure );
535 0 return;
536 }
537 $source->on_ready(
538 4 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
539 4 ▶ return if $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $future->is_ready 0 4 4
540 4 ▶ if ( $source->is_done ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($source->is_done) { } 3 1 4
elsif ($source->is_failed) { } 0 1 1
541 3 $future->done( $source->result );
542 }
543 elsif ( $source->is_failed ) {
544 0 $self->_forward_failure( $future, $source );
545 }
546 else {
547 1 $future->cancel;
548 }
549 }
550 4 );
551 }
552  
553 1 ▶sub _discover_sync_application {
Subroutine _discover_sync_application: called CC=3 SCAR=13.5
554 1 my ( $self, $operation ) = @_;
555 1 my $future = $operation->{future};
556  
557 # Resolve the application identity before issuing any command replacement.
558 1 my $discovery = eval { $self->{rest}->request( 'GET', '/oauth2/applications/@me' ) };
559 1 $operation->{source} = $discovery;
560 1 ▶ unless ( blessed($discovery) && $discovery->isa('Future') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($discovery) and $discovery->isa("Future")) 0 1 1
Condition33%1/3
builtin::blessed($discovery) and $discovery->isa("Future")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($discovery) and $discovery->isa("Future")
builtin::blessed($discovery) $discovery->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($discovery)B= $discovery->isa("Future")
ABresult
0X0
100
111
561 0 $future->fail( $self->_failed( 'Application discovery failed', 'transport' )->failure );
562 0 return;
563 }
564 $discovery->on_ready(
565 1 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=24.1
566 1 ▶ return if $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $future->is_ready 0 1 1
567 1 ▶ if ( $discovery->is_done ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($discovery->is_done) { } 1 0 1
elsif ($discovery->is_failed) { } 0 0 0
568 1 my ($application) = $discovery->result;
569 1 ▶ unless ( ref($application) eq 'HASH' && _id( $application->{id} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $application eq "HASH" and _id($application->{"id"})) 0 1 1
Condition33%1/3
ref $application eq "HASH" and _id($application->{"id"})
!ll&&!rl&&r
001
MC/DC0%0/2
ref $application eq "HASH" and _id($application->{"id"})
ref $application eq "HASH" _id($application->{"id"})
Truth table33%1/3
A and B
A= ref $application eq "HASH"B= _id($application->{"id"})
ABresult
0X0
100
111
570 0 $future->fail( $self->_failed( 'Invalid application metadata', 'protocol' )->failure );
571 0 return;
572 }
573 1 $self->{application_id} = "$application->{id}";
574 1 $self->_send_command_sync($operation);
575 }
576 elsif ( $discovery->is_failed ) {
577 0 $self->_forward_failure( $future, $discovery );
578 }
579 else {
580 0 $future->cancel;
581 }
582 }
583 1 );
584 }
585  
586 67 ▶sub close {
Subroutine close: called CC=4 SCAR=14.5
Pod documented
587 67 my ($self) = @_;
588  
589 # Stop deployment and release application closures throughout the declaration tree.
590 67 $self->{closed} = 1;
591 67 ▶ $self->{syncing}->cancel if $self->{syncing} && !$self->{syncing}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"syncing"} and not $self->{"syncing"}->is_ready 0 67 67
Condition33%1/3
$self->{"syncing"} and not $self->{"syncing"}->is_ready
!ll&&!rl&&r
6700
MC/DC0%0/2
$self->{"syncing"} and not $self->{"syncing"}->is_ready
$self->{"syncing"} not $self->{"syncing"}->is_ready
Truth table33%1/3
A and not(B)
A= $self->{"syncing"}B= not $self->{"syncing"}->is_ready
ABresult
0X0
100
111
592 67 _clear_handlers($_) for values %{ $self->{commands} };
593 67 $self->{components} = {};
594 67 return $self->{loop}->new_future->done;
595 }
596  
597 66 ▶sub _clear_handlers {
Subroutine _clear_handlers: called CC=3 SCAR=11.0
598 66 my ($node) = @_;
599 66 delete @{$node}{qw(handler autocomplete)};
600 66 ▶ _clear_handlers($_) for @{ $node->{options} // [] };
Condition100%2/2
$node->{"options"} // []
l!l
5610
MC/DC100%1/1
$node->{"options"} // []
$node->{"options"}
Truth table100%2/2
A
A= $node->{"options"}
Aresult
11
00
601 }
602  
603 0 ▶sub _forward_failure {
Subroutine _forward_failure: not called CC=4 SCAR=30.0
604 0 my ( $self, $future, $source ) = @_;
605 0 my ( undef, $category, $error ) = $source->failure;
606  
607 # Preserve structured SDK failures while hiding arbitrary transport exception text.
608 0 ▶ if ( blessed($error) && $error->isa('Discord::Client::REST::Error') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->isa('Discord::Client::REST::Error')) { } 0 0 0
Condition0%0/3
builtin::blessed($error) and $error->isa("Discord::Client::REST::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($error) and $error->isa("Discord::Client::REST::Error")
builtin::blessed($error) $error->isa("Discord::Client::REST::Error")
Truth table0%0/3
A and B
A= builtin::blessed($error)B= $error->isa("Discord::Client::REST::Error")
ABresult
0X0
100
111
609 0 $future->fail( $error->message, $error->category, $error );
610 }
611 else {
612 0 ▶ $future->fail( $self->_failed( 'Command synchronization failed', $category // 'transport' )->failure );
Condition0%0/2
$category // "transport"
l!l
00
MC/DC0%0/1
$category // "transport"
$category
Truth table0%0/2
A
A= $category
Aresult
11
00
613 }
614 }
615  
616 10 1;
617  
618 __END__
↑ Summary blib/lib/Discord/Client/Interaction.pm »