blib/lib/Web/Dispatch.pm

filter: Statementstmt 98.8% 82 / 83 Branchbran 97.3% 37 / 38 Conditioncond 91.6% 44 / 48 MC/DCmcdc 80.0% 20 / 25 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 2 totaltotal 93.5% 202 / 216 CC 36 SCAR 19.3
CC
36
Cov
96%
_to_try
14.1
_have_result
9.0
_dispatch
7.0
CRAP 6.9
« blib/lib/Web/Dispatch/ParamParser.pm ↑ Summary blib/lib/Web/Dispatch/Predicates.pm »
1 package Web::Dispatch;
2  
3 15 ▶use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 109 ▶sub MAGIC_MIDDLEWARE_KEY { __PACKAGE__.'.middleware' }
Subroutine MAGIC_MIDDLEWARE_KEY: called CC=1 SCAR=0.0
Pod undocumented
7  
8 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Web::Dispatch::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Web::Dispatch::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 with 'Web::Dispatch::ToApp';
13  
14 has dispatch_app => (
15 0 ▶ is => 'lazy', builder => sub { shift->dispatch_object->to_app }
Subroutine _build_dispatch_app: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
16 15 );
17 15 has dispatch_object => (is => 'ro', required => 0, weak_ref => 1);
18 15 has parser_class => (
19 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' }
20 );
21 15 has node_class => (
22 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Node' }
23 );
24 15 has _parser => (is => 'lazy');
25  
26 18 ▶after BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
27 18 my ( $self, %args ) = @_;
28 die "Either dispatch_app or dispatch_object need to be supplied."
29 if !$args{dispatch_app} and !$args{dispatch_object}
30 18 ▶};
Branch: 1 branch
BranchTrueFalseTotal
if not $args{"dispatch_app"} || $args{"dispatch_object"} 0 18 18
Condition66%2/3
$args{'dispatch_app'} || $args{'dispatch_object'}
l!l&&r!l&&!r
1710
MC/DC0%0/2
$args{'dispatch_app'} || $args{'dispatch_object'}
$args{'dispatch_app'} $args{'dispatch_object'}
Truth table66%2/3
A || B
A= $args{'dispatch_app'}B= $args{'dispatch_object'}
ABresult
1X1
011
000
31  
32 13 ▶sub _build__parser {
Subroutine _build__parser: called CC=1 SCAR=0.0
33 13 my ($self) = @_;
34 13 $self->parser_class->new;
35 }
36  
37 68 ▶sub call {
Subroutine call: called CC=4 SCAR=13.9
Pod undocumented
38 68 my ($self, $env) = @_;
39 68 my $res = $self->_dispatch($env, $self->dispatch_app);
40 62 ▶ return $res->[0] if ref($res) eq 'ARRAY' and @{$res} == 1 and ref($res->[0]) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $res eq "ARRAY" and @{$res;} == 1 and ref $res->[0] eq "CODE" 3 59 62
Condition100%6/6
ref $res eq "ARRAY" and @{$res;} == 1 and ref $res->[0] eq "CODE"
!ll&&!rl&&r
5813
ref $res eq "ARRAY" and @{$res;} == 1
!ll&&!rl&&r
1574
MC/DC100%3/3
ref $res eq "ARRAY" and @{$res;} == 1 and ref $res->[0] eq "CODE"
ref $res eq "ARRAY" @{$res;} == 1 ref $res->[0] eq "CODE"
Truth table100%4/4
A and B and C
A= ref $res eq "ARRAY"B= @{$res;} == 1C= ref $res->[0] eq "CODE"
ABCresult
0XX0
10X0
1100
1111
41 59 return $res;
42 }
43  
44 267 ▶sub _dispatch {
Subroutine _dispatch: called CC=7 SCAR=19.5
45 267 my ($self, $env, @match) = @_;
46 267 ▶ while (defined(my $try = shift @match)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $try = shift @match)) 645 3 648
47  
48 645 ▶ return $try if ref($try) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $try eq "ARRAY" 4 641 645
49 641 ▶ if (ref($try) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $try eq "HASH") 94 547 641
50 94 $env = { 'Web::Dispatch.original_env' => $env, %$env, %$try };
51 94 next;
52 }
53  
54 547 my @result = $self->_to_try($try, \@match)->($env, @match);
55 538 ▶ next unless @result and defined($result[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless @result and defined $result[0] 90 448 538
Condition100%3/3
@result and defined $result[0]
!ll&&!rl&&r
891448
MC/DC100%2/2
@result and defined $result[0]
@result defined $result[0]
Truth table100%3/3
A and B
A= @resultB= defined $result[0]
ABresult
0X0
100
111
56  
57 448 my $first = $result[0];
58  
59 448 ▶ if (my $res = $self->_have_result($first, \@result, \@match, $env)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $self->_have_result($first, \@result, \@match, $env)) 249 197 446
60  
61 249 return $res;
62 }
63  
64 # make a copy so we don't screw with it assigning further up
65 197 my $env = $env;
66 197 ▶ unshift @match, sub { $self->_dispatch($env, @result) };
Subroutine __ANON__: called CC=1 SCAR=0.0
67 }
68  
69 3 return;
70 }
71  
72 448 ▶sub _have_result {
Subroutine _have_result: called CC=9 SCAR=22.0
73 448 my ($self, $first, $result, $match, $env) = @_;
74  
75 448 ▶ if (ref($first) eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $first eq 'ARRAY') { } 234 214 448
elsif (builtin::blessed($first) and $first->isa('Plack::Middleware')) { } 7 207 214
elsif (ref $first eq 'HASH' and $$first{MAGIC_MIDDLEWARE_KEY()}) { } 5 202 207
elsif (builtin::blessed($first) and not $first->can('to_app') and not $first->isa('Web::Dispatch::Matcher')) { } 5 371 376
76 234 return $first;
77 }
78 ▶ elsif (blessed($first) && $first->isa('Plack::Middleware')) {
Condition100%3/3
builtin::blessed($first) and $first->isa("Plack::Middleware")
!ll&&!rl&&r
178297
MC/DC100%2/2
builtin::blessed($first) and $first->isa("Plack::Middleware")
builtin::blessed($first) $first->isa("Plack::Middleware")
Truth table100%3/3
A and B
A= builtin::blessed($first)B= $first->isa("Plack::Middleware")
ABresult
0X0
100
111
79 7 return $self->_uplevel_middleware($first, $result);
80 }
81 ▶ elsif (ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY}) {
Condition100%3/3
ref $first eq "HASH" and $$first{MAGIC_MIDDLEWARE_KEY()}
!ll&&!rl&&r
108945
MC/DC100%2/2
ref $first eq "HASH" and $$first{MAGIC_MIDDLEWARE_KEY()}
ref $first eq "HASH" $$first{MAGIC_MIDDLEWARE_KEY()}
Truth table100%3/3
A and B
A= ref $first eq "HASH"B= $$first{MAGIC_MIDDLEWARE_KEY()}
ABresult
0X0
100
111
82 5 return $self->_redispatch_with_middleware($first, $match, $env);
83 }
84 ▶ elsif (
Condition100%6/6
builtin::blessed($first) and not $first->can("to_app") and not $first->isa("Web::Dispatch::Matcher")
!ll&&!rl&&r
174235
builtin::blessed($first) and not $first->can("to_app")
!ll&&!rl&&r
173128
MC/DC100%3/3
builtin::blessed($first) and not $first->can("to_app") and not $first->isa("Web::Dispatch::Matcher")
builtin::blessed($first) not $first->can("to_app") not $first->isa("Web::Dispatch::Matcher")
Truth table100%4/4
A and not(B) and not(C)
A= builtin::blessed($first)B= not $first->can("to_app")C= not $first->isa("Web::Dispatch::Matcher")
ABCresult
0XX0
10X0
1100
1111
85 blessed($first) &&
86 not($first->can('to_app')) &&
87 not($first->isa('Web::Dispatch::Matcher'))
88 ) {
89 5 return $first;
90 }
91 197 return;
92 }
93  
94 7 ▶sub _uplevel_middleware {
Subroutine _uplevel_middleware: called CC=2 SCAR=6.9
95 7 my ($self, $match, $results) = @_;
96 die "Multiple results but first one is a middleware ($match)"
97 7 ▶ if @{$results} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$results;} > 1 2 5 7
98 # middleware needs to uplevel exactly once to wrap the rest of the
99 # level it was created for - next elsif unwraps it
100 5 return { MAGIC_MIDDLEWARE_KEY, $match };
101 }
102  
103 5 ▶sub _redispatch_with_middleware {
Subroutine _redispatch_with_middleware: called CC=1 SCAR=0.0
104 5 my ($self, $first, $match, $env) = @_;
105  
106 5 my $mw = $first->{+MAGIC_MIDDLEWARE_KEY};
107  
108 5 ▶ $mw->app(sub { $self->_dispatch($_[0], @{$match}) });
Subroutine __ANON__: called CC=1 SCAR=0.0
109  
110 5 return $mw->to_app->($env);
111 }
112  
113 547 ▶sub _to_try {
Subroutine _to_try: called CC=14 SCAR=26.4
114 547 my ($self, $try, $more) = @_;
115  
116 # sub (<spec>) {} becomes a dispatcher
117 # sub {} is a PSGI app and can be returned as is
118 # '<spec>' => sub {} becomes a dispatcher
119 # $obj isa WD:Predicates::Matcher => sub { ... } - become a dispatcher
120 # $obj w/to_app method is a Plack::App-like thing - call it to get a PSGI app
121 #
122  
123 547 ▶ if (ref($try) eq 'CODE') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $try eq 'CODE') { } 490 57 547
elsif (not ref $try and ref $more->[0] eq 'CODE' || $more->[0] && !ref($more->[0]) && $self->dispatch_object && $self->dispatch_object->can($more->[0])) { } 4 53 57
elsif (builtin::blessed($try) and $try->isa('Web::Dispatch::Matcher') and ref $more->[0] eq 'CODE') { } 42 11 53
elsif (builtin::blessed($try) and $try->can('to_app')) { } 7 4 11
124 490 ▶ if (defined(my $proto = prototype($try))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $proto = prototype $try)) { } 134 356 490
125 134 $self->_construct_node(match => $proto, run => $try);
126 } else {
127 356 $try
128 }
129 ▶ } elsif (!ref($try)
Condition80%12/15
not ref $try and ref $more->[0] eq "CODE" || $more->[0] && !ref($more->[0]) && $self->dispatch_object && $self->dispatch_object->can($more->[0])
!ll&&!rl&&r
5124
ref $more->[0] eq "CODE" or $more->[0] and not ref $more->[0] and $self->dispatch_object and $self->dispatch_object->can($more->[0])
l!l&&r!l&&!r
312
$more->[0] and not ref $more->[0] and $self->dispatch_object and $self->dispatch_object->can($more->[0])
!ll&&!rl&&r
201
$more->[0] and not ref $more->[0] and $self->dispatch_object
!ll&&!rl&&r
201
$more->[0] and not ref $more->[0]
!ll&&!rl&&r
201
MC/DC50%3/6
not ref $try and ref $more->[0] eq "CODE" || $more->[0] && !ref($more->[0]) && $self->dispatch_object && $self->dispatch_object->can($more->[0])
not ref $try ref $more->[0] eq "CODE" $more->[0] not ref $more->[0] $self->dispatch_object $self->dispatch_object->can($more->[0])
Truth table57%4/7
not(A) and B or C and not(D) and E and F
A= not ref $tryB= ref $more->[0] eq "CODE"C= $more->[0]D= not ref $more->[0]E= $self->dispatch_objectF= $self->dispatch_object->can($more->[0])
ABCDEFresult
0XXXXX0
100XXX0
1010XX0
10110X0
1011100
11XXXX1
1011111
130 and (ref($more->[0]) eq 'CODE'
131 or ($more->[0] and !ref($more->[0]) and $self->dispatch_object
132 and $self->dispatch_object->can($more->[0])))
133 ) {
134 4 $self->_construct_node(match => $try, run => shift(@$more));
135 ▶ } elsif (
Condition100%6/6
builtin::blessed($try) and $try->isa("Web::Dispatch::Matcher") and ref $more->[0] eq "CODE"
!ll&&!rl&&r
10142
builtin::blessed($try) and $try->isa("Web::Dispatch::Matcher")
!ll&&!rl&&r
2843
MC/DC100%3/3
builtin::blessed($try) and $try->isa("Web::Dispatch::Matcher") and ref $more->[0] eq "CODE"
builtin::blessed($try) $try->isa("Web::Dispatch::Matcher") ref $more->[0] eq "CODE"
Truth table100%4/4
A and B and C
A= builtin::blessed($try)B= $try->isa("Web::Dispatch::Matcher")C= ref $more->[0] eq "CODE"
ABCresult
0XX0
10X0
1100
1111
136 (blessed($try) && $try->isa('Web::Dispatch::Matcher'))
137 and (ref($more->[0]) eq 'CODE')
138 ) {
139 42 $self->_construct_node(match => $try, run => shift(@$more));
140 ▶ } elsif (blessed($try) && $try->can('to_app')) {
Condition100%3/3
builtin::blessed($try) and $try->can("to_app")
!ll&&!rl&&r
227
MC/DC100%2/2
builtin::blessed($try) and $try->can("to_app")
builtin::blessed($try) $try->can("to_app")
Truth table100%3/3
A and B
A= builtin::blessed($try)B= $try->can("to_app")
ABresult
0X0
100
111
141 7 $try->to_app;
142 } else {
143 4 die "No idea how we got here with $try";
144 }
145 }
146  
147 180 ▶sub _construct_node {
Subroutine _construct_node: called CC=3 SCAR=11.0
148 180 my ($self, %args) = @_;
149 180 ▶ $args{match} = $self->_parser->parse($args{match}) if !ref $args{match};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"match"} 138 42 180
150 180 ▶ if ( my $obj = $self->dispatch_object) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->dispatch_object) 179 1 180
151 # if possible, call dispatchers as methods of the app object
152 179 my $dispatch_sub = $args{run};
153 179 ▶ $args{run} = sub { $obj->$dispatch_sub(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
154 }
155 180 $self->node_class->new(\%args)->to_app;
156 }
157  
158 15 1;
« blib/lib/Web/Dispatch/ParamParser.pm ↑ Summary blib/lib/Web/Dispatch/Predicates.pm »