blib/lib/Magpie/Matcher.pm

filter: Statementstmt 82.8% 130 / 157 Branchbran 78.8% 41 / 52 Conditioncond 46.1% 12 / 26 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 60.0% 9 / 15 Podpod 0.0% 0 / 7 totaltotal 72.9% 197 / 270 CC 46 SCAR 27.9
CC
46
Cov
78%
token_lookup
30.0
eval_match
28.2
_build_evaluation_map
9.0
CRAP 16.2
« lib/Magpie/Resource/Kioku.pm ↑ Summary blib/lib/Magpie/ConfigReader/XML.pm »
1 package Magpie::Matcher;
2 #ABSTRACT: Multi-purpose Dispatcher Magic
3 43 $Magpie::Matcher::VERSION = '1.163200';
4 43 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 43 ▶use HTTP::Negotiate;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 43 ▶use Data::Printer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 43 has plack_request => (
10 is => 'ro',
11 isa => 'Plack::Request',
12 required => 1,
13 trigger => \&choose_variant,
14 );
15  
16 has match_candidates => (
17 traits => ['Array'],
18 is => 'rw',
19 isa => 'ArrayRef[ArrayRef]',
20 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 43 handles => {
22 add_candidates => 'push',
23 },
24 );
25  
26 43 has evaluation_map => (
27 traits => ['Hash'],
28 is => 'rw',
29 isa => 'HashRef[ArrayRef]',
30 handles => {
31 has_candidate => 'exists',
32 get_candidate => 'get',
33 },
34 lazy_build => 1,
35 );
36  
37 114 ▶sub _build_evaluation_map {
Subroutine _build_evaluation_map: called CC=9 SCAR=22.0
38 114 my $self = shift;
39 114 my $candidates = $self->match_candidates;
40 114 my $evaled = {};
41 114 my @submatches = ();
42 114 my @to_skip;
43 # first, eval the frames to see what matches.
44 114 foreach my $frame (@{$candidates}) {
45 440 my @components = ();
46 440 my $machine_token = $frame->[3],
47 my $match_token = $frame->[4];
48 440 ▶ $evaled->{$machine_token} ||= [];
Condition100%2/2
$evaled->{$machine_token} ||= []
l!l
37070
MC/DC100%1/1
$evaled->{$machine_token} ||= []
$evaled->{$machine_token}
Truth table100%2/2
A
A= $evaled->{$machine_token}
Aresult
11
00
49 440 my $added = $self->eval_match($frame);
50 440 ▶ if (scalar @{$added} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$added;} > 0) { } 150 290 440
51 150 my @subs_here = grep {/__MATCH__/} @{$added};
52 150 push @submatches, @subs_here;
53 150 push @{$evaled->{$machine_token}}, [$match_token, $added];
54 }
55 else {
56 290 my @subs_here = grep {/__MATCH__/} @{$frame->[2]};
57 290 push @to_skip, @subs_here;
58 }
59 }
60  
61 # now that we know what matches, flatten the pipelines by resolving the
62 # match tokens
63 114 my $out = {};
64 114 foreach my $machine (keys( %{$evaled} )) {
65 70 $out->{$machine} = [];
66 70 foreach my $slot (@{$evaled->{$machine}}) {
67 150 ▶ next if scalar grep { $_ eq $slot->[0] } (@submatches, @to_skip);
Branch: 1 branch
BranchTrueFalseTotal
if scalar grep({$_ eq $slot->[0];} @submatches, @to_skip) 70 80 150
68 80 my $components = resolve($evaled->{$machine}, $slot->[1]);
69 80 foreach my $component (@{$components}) {
70 275 ▶ if ($component eq '__RESET__') {
Branch: 1 branch
BranchTrueFalseTotal
if ($component eq "__RESET__") 2 273 275
71 2 $out->{$machine} = [];
72 2 next;
73 }
74 273 push @{$out->{$machine}}, $component;
75 }
76 #push @{$out->{$machine}}, @{$components};
77 }
78 }
79 114 return $out;
80 }
81  
82  
83  
84 has accept_matrix => (
85 traits => ['Array'],
86 is => 'rw',
87 isa => 'ArrayRef[ArrayRef]',
88 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 43 );
90  
91 43 has accept_variant => (
92 is => 'rw',
93 isa => 'Str|Undef',
94 #lazy_build => 1,
95 );
96  
97 114 ▶sub choose_variant {
Subroutine choose_variant: called CC=2 SCAR=7.0
Pod undocumented
98 114 my $self = shift;
99 114 my $plack_request = shift;
100 114 my $accept_matrix = $self->accept_matrix;
101 114 ▶ if ($accept_matrix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accept_matrix) 114 0 114
102 114 my $variant = HTTP::Negotiate::choose($accept_matrix, $plack_request->headers);
103 114 $self->accept_variant($variant);
104 }
105  
106 }
107  
108 0 ▶sub _build_accept_variant {
Subroutine _build_accept_variant: not called CC=2 SCAR=17.9
109 0 my $self = shift;
110 0 my $ret = undef;
111 0 my $accept_matrix = $self->accept_matrix;
112 0 ▶ if ($accept_matrix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accept_matrix) 0 0 0
113 0 $ret = HTTP::Negotiate::choose($accept_matrix, $self->plack_request->headers);
114 }
115 0 return $ret;
116 }
117  
118 144 ▶sub resolve {
Subroutine resolve: called CC=7 SCAR=19.5
Pod undocumented
119 144 my $machine = shift;
120 144 my $component_list = shift;
121 144 ▶ my $stack = shift || [];
Condition50%1/2
shift() || []
l!l
0144
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
122 144 foreach my $component (@{$component_list}) {
123 339 ▶ if ($component =~ /__MATCH__/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($component =~ /__MATCH__/) { } 64 275 339
124 64 my @new_list = ();
125 64 foreach my $thing (@{$machine}) {
126 232 ▶ if ($thing->[0] eq $component) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thing->[0] eq $component) 28 204 232
127 28 push @new_list, @{$thing->[1]};
128 }
129 }
130 64 my $resolved = resolve($machine, \@new_list);
131 64 ▶ push @{$stack}, @{$resolved} if $resolved;
Branch: 1 branch
BranchTrueFalseTotal
if $resolved 64 0 64
132 }
133 else {
134 275 push @{$stack}, $component;
135 }
136 }
137 144 return $stack;
138 }
139  
140 440 ▶sub eval_match {
Subroutine eval_match: called CC=23 SCAR=33.4
Pod undocumented
141 440 my $self = shift;
142 440 my $frame = shift;
143 440 my $req = $self->plack_request;
144 440 my $env = $req->env;
145 440 my $path = $req->path_info;
146 440 my $accept_variant = $self->accept_variant;
147 440 my @out = ();
148  
149 440 my $match_type = $frame->[0];
150 440 ▶ if ($match_type eq 'STRING') {
Branch: 7 branches
BranchTrueFalseTotal
if ($match_type eq 'STRING') { } 8 432 440
elsif ($match_type eq 'REGEXP' or $match_type eq 'SCALAR' and &re::is_regexp($frame->[0]) == 1) { } 272 160 432
elsif ($match_type eq 'CODE') { } 0 160 160
elsif ($match_type eq 'HASH') { } 86 74 160
elsif ($match_type eq 'AUTO') { } 10 64 74
elsif ($match_type eq 'ACCEPT') { } 64 0 64
elsif ($match_type eq 'RESET') { } 0 0 0
151 8 ▶ push @out, @{$frame->[2]} if $frame->[1] eq $path;
Branch: 1 branch
BranchTrueFalseTotal
if $frame->[1] eq $path 5 3 8
152 }
153 ▶ elsif ($match_type eq 'REGEXP' || ($match_type eq 'SCALAR' && re::is_regexp($frame->[0]) == 1 )) {
Condition50%3/6
$match_type eq "REGEXP" or $match_type eq "SCALAR" and &re::is_regexp($frame->[0]) == 1
l!l&&r!l&&!r
2720160
$match_type eq "SCALAR" and &re::is_regexp($frame->[0]) == 1
!ll&&!rl&&r
16000
MC/DC33%1/3
$match_type eq "REGEXP" or $match_type eq "SCALAR" and &re::is_regexp($frame->[0]) == 1
$match_type eq "REGEXP" $match_type eq "SCALAR" &re::is_regexp($frame->[0]) == 1
Truth table50%2/4
A or B and C
A= $match_type eq "REGEXP"B= $match_type eq "SCALAR"C= &re::is_regexp($frame->[0]) == 1
ABCresult
1XX1
0111
00X0
0100
154 272 ▶ push @out, @{$frame->[2]} if $path =~ /$frame->[1]/;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$frame->[1]/ 73 199 272
155 }
156 elsif ($match_type eq 'CODE') {
157 0 my $temp = $frame->[1]->($env);
158 0 push @out, @{$temp};
159 }
160 elsif ($match_type eq 'HASH') {
161 86 my $rules = $frame->[1];
162 86 my $matched = 0;
163 86 foreach my $k (keys %{$rules} ) {
164 86 ▶ last unless defined $env->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env->{$k} 38 48 86
165 48 my $val = $rules->{$k};
166 48 my $val_type = reftype $val;
167 48 ▶ if ($val_type &&
Branch: 1 branch
BranchTrueFalseTotal
if ($val_type and $val_type eq 'REGEXP' || $val_type eq 'SCALAR' && &re::is_regexp($val) == 1) { } 30 18 48
Condition33%3/9
$val_type and $val_type eq "REGEXP" || $val_type eq "SCALAR" && &re::is_regexp($val) == 1
!ll&&!rl&&r
18030
$val_type eq "REGEXP" or $val_type eq "SCALAR" and &re::is_regexp($val) == 1
l!l&&r!l&&!r
3000
$val_type eq "SCALAR" and &re::is_regexp($val) == 1
!ll&&!rl&&r
000
MC/DC25%1/4
$val_type and $val_type eq "REGEXP" || $val_type eq "SCALAR" && &re::is_regexp($val) == 1
$val_type $val_type eq "REGEXP" $val_type eq "SCALAR" &re::is_regexp($val) == 1
Truth table40%2/5
A and B or C and D
A= $val_typeB= $val_type eq "REGEXP"C= $val_type eq "SCALAR"D= &re::is_regexp($val) == 1
ABCDresult
0XXX0
100X0
10100
11XX1
10111
168 ( $val_type eq 'REGEXP' || ($val_type eq 'SCALAR' && re::is_regexp($val) == 1 ))
169 ) {
170 30 ▶ $matched++ if $env->{$k} =~ m/$val/;
Branch: 1 branch
BranchTrueFalseTotal
if $env->{$k} =~ /$val/ 28 2 30
171 }
172 else {
173 18 ▶ $matched++ if qq($env->{$k}) eq qq($val);
Branch: 1 branch
BranchTrueFalseTotal
if "$env->{$k}" eq "$val" 8 10 18
174 }
175 }
176 86 ▶ push @out, @{$frame->[2]} if $matched == scalar keys %{$rules};
Branch: 1 branch
BranchTrueFalseTotal
if $matched == scalar keys %{$rules;} 36 50 86
177 }
178 elsif ($match_type eq 'AUTO') {
179 10 push @out, @{$frame->[2]};
180 }
181 elsif ($match_type eq 'ACCEPT') {
182 64 ▶ push @out, @{$frame->[2]} if length $accept_variant && $frame->[1] eq $accept_variant;
Branch: 1 branch
BranchTrueFalseTotal
if length $accept_variant and $frame->[1] eq $accept_variant 26 38 64
Condition100%3/3
length $accept_variant and $frame->[1] eq $accept_variant
!ll&&!rl&&r
122626
MC/DC100%2/2
length $accept_variant and $frame->[1] eq $accept_variant
length $accept_variant $frame->[1] eq $accept_variant
Truth table100%3/3
A and B
A= length $accept_variantB= $frame->[1] eq $accept_variant
ABresult
0X0
100
111
183 }
184 elsif ($match_type eq 'RESET') {
185 0 push @out, '__RESET__';
186 }
187 else {
188 0 warn"I don't know how to match '$match_type', skipping.\n"
189 }
190 440 return \@out;
191 }
192  
193 0 ▶sub machine_token_lookup {
Subroutine machine_token_lookup: not called CC=1 SCAR=6.9
Pod undocumented
194 0 return shift->token_lookup(@_, 3);
195 }
196  
197 0 ▶sub match_token_lookup {
Subroutine match_token_lookup: not called CC=1 SCAR=6.9
Pod undocumented
198 0 return shift->token_lookup(@_, 4);
199 }
200  
201 0 ▶sub token_lookup {
Subroutine token_lookup: not called CC=5 SCAR=34.0
Pod undocumented
202 0 my $self = shift;
203 0 ▶ my $token = shift || '__default__';
Condition0%0/2
shift() || '__default__'
l!l
00
MC/DC0%0/1
shift() || '__default__'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
204 0 ▶ my $index = shift || 4;
Condition0%0/2
shift() || 4
l!l
00
MC/DC0%0/1
shift() || 4
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
205 0 my $candidates = $self->match_candidates;
206 0 my $ret = [];
207 0 foreach my $frame (@{$candidates}) {
208 0 ▶ if ($frame->[$index] eq $token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[$index] eq $token) 0 0 0
209 0 push @{$ret}, $frame;
210 }
211 }
212 0 return $ret;
213 }
214  
215  
216 114 ▶sub construct_pipeline {
Subroutine construct_pipeline: called CC=4 SCAR=13.9
Pod undocumented
217 114 my $self = shift;
218 114 my $tokenized = shift;
219  
220 114 ▶ unless ($tokenized) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tokenized) 0 114 114
221 0 $tokenized = ['__default__'];
222 }
223  
224 114 my @new = ();
225 114 my $map = $self->evaluation_map;
226 114 my @tokens = keys( %{$map} );
227 114 foreach my $step ( @{$tokenized} ) {
228 200 ▶ if ( grep { $_ eq $step } @tokens ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $step;} @tokens) { } 70 130 200
229 70 push @new, @{$map->{$step}};
230 }
231 else {
232 130 push @new, $step;
233 }
234 }
235 114 return \@new;
236 }
237  
238 #SEEALSO: Magpie
239  
240 43 1;
241  
242 __END__
« lib/Magpie/Resource/Kioku.pm ↑ Summary blib/lib/Magpie/ConfigReader/XML.pm »