MetaAPI/Roles/Listable.pm

filter: Statementstmt 94.8% 37 / 39 Branchbran 65.0% 13 / 20 Conditioncond 45.4% 5 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 75.3% 61 / 81 CC 17 SCAR 29.1
CC
17
Cov
79%
_list
21.5
BEGIN
1.0
BEGIN
1.0
CRAP 18.4
« MetaAPI/Roles/GetFromId.pm ↑ Summary MetaAPI.pm »
1 package OpenStack::MetaAPI::Roles::Listable;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub _list {
9 20 ▶ my ($self, $all_args, $caller_args) = @_;
Subroutine _list: called CC=17 SCAR=30.7
10  
11 # all_args are arguments from the internal OpenStack::MetaAPI::API
12 # caller_args are coming from the user to filter the results
13 # if some filters are also arguments to the request
14 # then appending them to the query will shorten the output and run it faster
15  
16 20 my @all;
17 {
18 20 my ($uri, @extra) = @$all_args;
19 20 ▶ $uri = $self->root_uri($uri)
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ m[^/v] 7 13 20
20 if $uri !~ m{^/v}; # can be removed once dynmaic methods are used
21  
22 20 my $extra_filters =
23 $self->api_specs()->query_filters_for('/get', $uri, $caller_args);
24  
25 20 ▶ if ($extra_filters) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_filters) 2 18 20
26 2 ▶ if (scalar @extra == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @extra == 1) { } 2 0 2
elsif (scalar @extra > 1) { } 0 0 0
27 2 push @extra, {};
28 } elsif (scalar @extra > 1) {
29 0 die "Too many args when calling _list for all...";
30 }
31 2 $extra[-1] = {%{$extra[-1]}, %$extra_filters};
32 }
33  
34 20 @all = $self->client->all($uri, @extra);
35 }
36  
37 20 my @args = @$caller_args;
38  
39 # apply our filters to the raw results
40 20 my $nargs = scalar @args;
41 20 ▶ if ($nargs && $nargs % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nargs and $nargs % 2 == 0) 17 3 20
Condition66%2/3
$nargs and $nargs % 2 == 0
!ll&&!rl&&r
3017
MC/DC50%1/2
$nargs and $nargs % 2 == 0
$nargs $nargs % 2 == 0
Truth table66%2/3
A and B
A= $nargsB= $nargs % 2 == 0
ABresult
0X0
100
111
42 17 my %opts = @args;
43 17 foreach my $filter (sort keys %opts) {
44 17 my @keep;
45 17 ▶ my $filter_isa = ref $opts{$filter} // '';
Condition50%1/2
ref $opts{$filter} // ""
l!l
170
MC/DC0%0/1
ref $opts{$filter} // ""
ref $opts{$filter}
Truth table50%1/2
A
A= ref $opts{$filter}
Aresult
11
00
46 17 foreach my $candidate (@all) {
47 43 ▶ next unless ref $candidate;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $candidate 0 43 43
48 43 ▶ if ($filter_isa eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter_isa eq 'Regexp') { } 0 43 43
49  
50 # can use a regexp as a filter
51 next
52 unless $candidate->{$filter}
53 0 ▶ && $candidate->{$filter} =~ $opts{$filter};
Branch: 1 branch
BranchTrueFalseTotal
unless $candidate->{$filter} and $candidate->{$filter} =~ /$opts{$filter}/ 0 0 0
Condition0%0/3
$candidate->{$filter} and $candidate->{$filter} =~ /$opts{$filter}/
!ll&&!rl&&r
000
MC/DC0%0/2
$candidate->{$filter} and $candidate->{$filter} =~ /$opts{$filter}/
$candidate->{$filter} $candidate->{$filter} =~ /$opts{$filter}/
Truth table0%0/3
A and B
A= $candidate->{$filter}B= $candidate->{$filter} =~ /$opts{$filter}/
ABresult
0X0
100
111
54 } else {
55  
56 # otherwise do one 'eq' check
57 next
58 unless $candidate->{$filter}
59 43 ▶ && $candidate->{$filter} eq $opts{$filter};
Branch: 1 branch
BranchTrueFalseTotal
unless $candidate->{$filter} and $candidate->{$filter} eq $opts{$filter} 28 15 43
Condition66%2/3
$candidate->{$filter} and $candidate->{$filter} eq $opts{$filter}
!ll&&!rl&&r
02815
MC/DC50%1/2
$candidate->{$filter} and $candidate->{$filter} eq $opts{$filter}
$candidate->{$filter} $candidate->{$filter} eq $opts{$filter}
Truth table66%2/3
A and B
A= $candidate->{$filter}B= $candidate->{$filter} eq $opts{$filter}
ABresult
0X0
100
111
60 }
61  
62 15 push @keep, $candidate;
63 }
64  
65 17 @all = @keep;
66  
67 }
68 }
69  
70 # avoid to return a list when possible
71 20 ▶ return $all[0] if scalar @all <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @all <= 1 18 2 20
72  
73 # return a list
74 2 return @all;
75 }
76  
77 5 1;
78  
79 __END__
80  
81 =pod
82  
83 =encoding UTF-8
84  
85 =head1 NAME
86  
87 OpenStack::MetaAPI::Roles::Listable
88  
89 =head1 VERSION
90  
91 version 0.002
92  
93 =head1 AUTHOR
94  
95 Nicolas R <atoomic@cpan.org>
96  
97 =head1 COPYRIGHT AND LICENSE
98  
99 This software is copyright (c) 2019 by cPanel, Inc.
100  
101 This is free software; you can redistribute it and/or modify it under
102 the same terms as the Perl 5 programming language system itself.
103  
104 =cut
« MetaAPI/Roles/GetFromId.pm ↑ Summary MetaAPI.pm »