blib/lib/Mojolicious/Plugin/CanonicalURL.pm

filter: Statementstmt 100% 184 / 184 Branchbran 93.6% 118 / 126 Conditioncond 89.2% 50 / 56 MC/DCmcdc 81.2% 26 / 32 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 95.1% 394 / 414 CC 86 SCAR 30.3
CC
86
Cov
96%
_create_should_canonicalize_request_sub_string
30.1
_validate_should_canonicalize_request_config
26.0
_parse_and_validate_config
19.2
CRAP 20.6
↑ Summary
1 package Mojolicious::Plugin::CanonicalURL;
2 4 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Mojo::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Sub::Quote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION = '0.06';
10  
11 4 our @EXPORT_OK = qw(remove_trailing_slashes);
12  
13 308 ▶sub register {
Subroutine register: called CC=7 SCAR=19.5
Pod documented
14 308 my (undef, $app, $config) = @_;
15  
16 my (
17 308 $should_canonicalize_request_config,
18 $should_not_canonicalize_request_config,
19 $inline_code,
20 $end_with_slash,
21 $canonicalize_before_render,
22 %captures
23 ) = _parse_and_validate_config($config);
24  
25 259 my $sub_string = '';
26 259 my ($path_declared, $path_with_no_slashes_at_the_end_declared);
27 259 ▶ if (defined $should_canonicalize_request_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $should_canonicalize_request_config) 109 150 259
28 109 ($path_declared, $path_with_no_slashes_at_the_end_declared, $sub_string) = _create_should_canonicalize_request_sub_string(
29 config => $should_canonicalize_request_config,
30 captures => \%captures,
31 sub_string => $sub_string,
32 should_canonicalize_request => 1,
33 path_declared => $path_declared,
34 path_with_no_slashes_at_the_end_declared => $path_with_no_slashes_at_the_end_declared,
35 );
36 }
37 259 ▶ if (defined $should_not_canonicalize_request_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $should_not_canonicalize_request_config) 104 155 259
38 104 ($path_declared, $path_with_no_slashes_at_the_end_declared, $sub_string) = _create_should_canonicalize_request_sub_string(
39 config => $should_not_canonicalize_request_config,
40 captures => \%captures,
41 sub_string => $sub_string,
42 should_canonicalize_request => undef,
43 path_declared => $path_declared,
44 path_with_no_slashes_at_the_end_declared => $path_with_no_slashes_at_the_end_declared,
45 );
46 }
47 259 ▶ $sub_string .= $inline_code if $inline_code;
Branch: 1 branch
BranchTrueFalseTotal
if $inline_code 12 247 259
48  
49 259 ▶ $sub_string .= 'my $_mpcu_path = $c->req->url->path->to_string;' unless $path_declared;
Branch: 1 branch
BranchTrueFalseTotal
unless $path_declared 149 110 259
50 259 ▶ if ($end_with_slash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_with_slash) { } 10 249 259
51 10 $sub_string .= q{
52 my $_mpcu_path_length = length($_mpcu_path);
53 return $next->() if $_mpcu_path_length != 0 and rindex($_mpcu_path, '/') == $_mpcu_path_length - 1 and ($_mpcu_path_length < 2 or rindex($_mpcu_path, '//') != $_mpcu_path_length - 2);
54  
55 while (rindex($_mpcu_path, '/') == length($_mpcu_path) - 1) {
56 substr $_mpcu_path, -1, 1, '';
57 }
58  
59 my $url = $c->req->url->clone;
60 $url->path($_mpcu_path)->path->trailing_slash(1);
61  
62 $c->res->code(301);
63 $c->redirect_to($url);
64 };
65 } else {
66 249 $sub_string .= q{
67 return $next->() if $_mpcu_path eq '/' or rindex($_mpcu_path, '/') != length($_mpcu_path) - 1 or $_mpcu_path eq '';
68  
69 while (rindex($_mpcu_path, '/') == length($_mpcu_path) - 1) {
70 substr $_mpcu_path, -1, 1, '';
71 }
72  
73 $c->res->code(301);
74 $c->redirect_to($c->req->url->clone->path($_mpcu_path));
75 };
76 }
77  
78 # Potentially flaky for a minor speed improvment. Could just assign $next and $c above to @_.
79 # Or could use Mojo::Template, but that would be awkward writing perl code.
80 259 $sub_string =~ s/\$next\b/\$_[0]/g;
81 259 $sub_string =~ s/\$c\b/\$_[1]/g;
82  
83 259 $app->hook(around_action => _quote_sub($sub_string, \%captures));
84  
85 259 ▶ if ($canonicalize_before_render) {
Branch: 1 branch
BranchTrueFalseTotal
if ($canonicalize_before_render) 91 168 259
86 # replace return $next->() with return
87 91 $sub_string =~ s/return\s+\$_\[0\]->\(\)/return/g;
88  
89 # replace $_[1] with $_[0] since $c is now the first argument
90 91 $sub_string =~ s/\$_\[1\]/\$_[0]/gm;
91  
92 # we could set a stash variable if we failed to canonicalize in
93 # around_action, but the performance hit isn't big
94 91 $sub_string = "return if \$_[0]->res->is_redirect;$sub_string";
95 91 $app->hook(before_render => _quote_sub($sub_string, \%captures));
96 }
97 }
98  
99 350 ▶sub _quote_sub {
Subroutine _quote_sub: called CC=1 SCAR=0.0
100 350 my ($sub_string, $captures) = @_;
101 350 return Sub::Quote::quote_sub $sub_string, $captures, {no_install => 1, no_defer => 1};
102 }
103  
104 308 ▶sub _parse_and_validate_config {
Subroutine _parse_and_validate_config: called CC=19 SCAR=29.5
105 308 my ($config) = @_;
106  
107 my (
108 308 $should_canonicalize_request,
109 $should_not_canonicalize_request,
110 $inline_code,
111 $end_with_slash,
112 $canonicalize_before_render
113 );
114 308 my %captures;
115 308 ▶ if (defined $config) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) 308 0 308
116 308 ▶ Carp::confess 'config must be a hash reference, but was ' . Scalar::Util::reftype $config
Branch: 1 branch
BranchTrueFalseTotal
if not defined builtin::reftype($config) or builtin::reftype($config) ne "HASH" 0 308 308
Condition33%1/3
not defined builtin::reftype($config) or builtin::reftype($config) ne "HASH"
l!l&&r!l&&!r
00308
MC/DC0%0/2
not defined builtin::reftype($config) or builtin::reftype($config) ne "HASH"
not defined builtin::reftype($config) builtin::reftype($config) ne "HASH"
Truth table33%1/3
not(A) or B
A= not defined builtin::reftype($config)B= builtin::reftype($config) ne "HASH"
ABresult
1X1
011
000
117 if not defined Scalar::Util::reftype $config
118 or Scalar::Util::reftype $config ne 'HASH';
119  
120 308 ▶ if (%$config) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$config) 303 5 308
121 303 my $captures_allowed;
122 303 ▶ if (exists $config->{should_canonicalize_request}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"should_canonicalize_request"}) 131 172 303
123 ($should_canonicalize_request, $captures_allowed) =
124 131 _validate_should_canonicalize_request_config(delete $config->{should_canonicalize_request}, 1);
125 }
126 283 ▶ if (exists $config->{should_not_canonicalize_request}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"should_not_canonicalize_request"}) 125 158 283
127 ($should_not_canonicalize_request, $captures_allowed) = _validate_should_canonicalize_request_config(
128 delete $config->{should_not_canonicalize_request},
129 undef,
130 125 );
131 }
132  
133 263 ▶ if (exists $config->{inline_code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"inline_code"}) 17 246 263
134 17 $inline_code = delete $config->{inline_code};
135 17 ▶ Carp::confess 'inline_code must be a true scalar value'
Branch: 1 branch
BranchTrueFalseTotal
unless not defined builtin::reftype($inline_code) and $inline_code 4 13 17
Condition100%3/3
not defined builtin::reftype($inline_code) and $inline_code
!ll&&!rl&&r
1313
MC/DC100%2/2
not defined builtin::reftype($inline_code) and $inline_code
not defined builtin::reftype($inline_code) $inline_code
Truth table100%3/3
not(A) and B
A= not defined builtin::reftype($inline_code)B= $inline_code
ABresult
0X0
100
111
136 unless not defined Scalar::Util::reftype $inline_code and $inline_code;
137 13 $captures_allowed = 1;
138 }
139  
140 259 ▶ if (exists $config->{canonicalize_before_render}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"canonicalize_before_render"}) 184 75 259
141 Carp::confess 'canonicalize_before_render must be a scalar value'
142 184 ▶ if defined Scalar::Util::reftype $config->{canonicalize_before_render};
Branch: 1 branch
BranchTrueFalseTotal
if defined builtin::reftype($config->{"canonicalize_before_render"}) 1 183 184
143 183 $canonicalize_before_render = delete $config->{canonicalize_before_render};
144 }
145  
146 258 ▶ if ($captures_allowed and exists $config->{captures}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($captures_allowed and exists $config->{"captures"}) 31 227 258
Condition100%3/3
$captures_allowed and exists $config->{"captures"}
!ll&&!rl&&r
1824531
MC/DC100%2/2
$captures_allowed and exists $config->{"captures"}
$captures_allowed exists $config->{"captures"}
Truth table100%3/3
A and B
A= $captures_allowedB= exists $config->{"captures"}
ABresult
0X0
100
111
147 31 %captures = %{delete $config->{captures}};
148 31 ▶ Carp::confess 'captures cannot be empty' unless %captures;
Branch: 1 branch
BranchTrueFalseTotal
unless %captures 1 30 31
149 }
150  
151 Carp::confess
152 'captures only applies when inline_code is set or a scalar reference is passed to should_canonicalize_request or should_not_canonicalize_request'
153 257 ▶ if exists $config->{captures};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"captures"} 2 255 257
154  
155 255 ▶ if (exists $config->{end_with_slash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"end_with_slash"}) 20 235 255
156 20 $end_with_slash = delete $config->{end_with_slash};
157 20 ▶ Carp::confess 'end_with_slash must be a scalar value' if defined Scalar::Util::reftype $end_with_slash;
Branch: 1 branch
BranchTrueFalseTotal
if defined builtin::reftype($end_with_slash) 0 20 20
158 }
159  
160 255 ▶ Carp::confess 'unknown keys passed in config: ' . Mojo::Util::dumper $config if keys %$config;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$config 1 254 255
161 }
162 }
163  
164 return (
165 259 $should_canonicalize_request,
166 $should_not_canonicalize_request,
167 $inline_code,
168 $end_with_slash,
169 $canonicalize_before_render,
170 %captures
171 );
172 }
173  
174 256 ▶sub _validate_should_canonicalize_request_config {
Subroutine _validate_should_canonicalize_request_config: called CC=26 SCAR=32.6
175 256 my ($config, $should_canonicalize_request) = @_;
176  
177 256 my $captures_allowed;
178 256 my $config_name = _get_should_canonicalize_request_config_name($should_canonicalize_request);
179 256 ▶ my $config_reftype = Scalar::Util::reftype $config || '';
Condition100%2/2
builtin::reftype($config) || ''
l!l
22432
MC/DC100%1/1
builtin::reftype($config) || ''
builtin::reftype($config)
Truth table100%2/2
A
A= builtin::reftype($config)
Aresult
11
00
180 Carp::confess
181 "$config_name must be a scalar that evaluates to true and starts with a '/', a REGEXP, a SCALAR, a subroutine, an array reference, or a hash reference"
182 unless $config
183 and ((not $config_reftype and index($config, '/') == 0)
184 256 ▶ or grep { $config_reftype eq $_ } qw/ARRAY HASH REGEXP SCALAR CODE/);
Branch: 1 branch
BranchTrueFalseTotal
unless $config and !$config_reftype && index($config, "/") == 0 || grep({$config_reftype eq $_;} "ARRAY", "HASH", "REGEXP", "SCALAR", "CODE") 8 248 256
Condition100%9/9
$config and !$config_reftype && index($config, "/") == 0 || grep({$config_reftype eq $_;} "ARRAY", "HASH", "REGEXP", "SCALAR", "CODE")
!ll&&!rl&&r
44248
not $config_reftype and index($config, "/") == 0 or grep {$config_reftype eq $_;} "ARRAY", "HASH", "REGEXP", "SCALAR", "CODE"
l!l&&r!l&&!r
262224
not $config_reftype and index($config, "/") == 0
!ll&&!rl&&r
224226
MC/DC100%4/4
$config and !$config_reftype && index($config, "/") == 0 || grep({$config_reftype eq $_;} "ARRAY", "HASH", "REGEXP", "SCALAR", "CODE")
$config not $config_reftype index($config, "/") == 0 grep {$config_reftype eq $_;} "ARRAY", "HASH", "REGEXP", "SCALAR", "CODE"
Truth table83%5/6
A and not(B) and C or D
A= $configB= not $config_reftypeC= index($config, "/") == 0D= grep {$config_reftype eq $_;} "ARRAY", "HASH", "REGEXP", "SCALAR", "CODE"
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
185  
186 248 ▶ if (defined $config_reftype and $config_reftype eq 'SCALAR') {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $config_reftype and $config_reftype eq 'SCALAR') { } 39 209 248
elsif (defined $config_reftype and $config_reftype eq 'ARRAY') { } 91 118 209
elsif (defined $config_reftype and $config_reftype eq 'HASH') { } 42 76 118
Condition66%2/3
defined $config_reftype and $config_reftype eq "SCALAR"
!ll&&!rl&&r
020939
MC/DC50%1/2
defined $config_reftype and $config_reftype eq "SCALAR"
defined $config_reftype $config_reftype eq "SCALAR"
Truth table66%2/3
A and B
A= defined $config_reftypeB= $config_reftype eq "SCALAR"
ABresult
0X0
100
111
187 39 $captures_allowed = 1;
188 ▶ } elsif (defined $config_reftype and $config_reftype eq 'ARRAY') {
Condition66%2/3
defined $config_reftype and $config_reftype eq "ARRAY"
!ll&&!rl&&r
011891
MC/DC50%1/2
defined $config_reftype and $config_reftype eq "ARRAY"
defined $config_reftype $config_reftype eq "ARRAY"
Truth table66%2/3
A and B
A= defined $config_reftypeB= $config_reftype eq "ARRAY"
ABresult
0X0
100
111
189 91 ▶ Carp::confess "array passed to $config_name must not be empty" unless @$config;
Branch: 1 branch
BranchTrueFalseTotal
unless @$config 2 89 91
190  
191 89 for (@$config) {
192 154 ▶ Carp::confess "elements of $config_name must be a true value" unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 6 148 154
193  
194 148 my $reftype = Scalar::Util::reftype $_;
195 148 ▶ Carp::confess
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $reftype or $reftype eq "CODE" or $reftype eq "HASH" or $reftype eq "REGEXP" or $reftype eq "SCALAR" 2 146 148
Condition100%12/12
not defined $reftype or $reftype eq "CODE" or $reftype eq "HASH" or $reftype eq "REGEXP" or $reftype eq "SCALAR"
l!l&&r!l&&!r
122242
not defined $reftype or $reftype eq "CODE" or $reftype eq "HASH" or $reftype eq "REGEXP"
l!l&&r!l&&!r
833926
not defined $reftype or $reftype eq "CODE" or $reftype eq "HASH"
l!l&&r!l&&!r
463765
not defined $reftype or $reftype eq "CODE"
l!l&&r!l&&!r
3412102
MC/DC100%5/5
not defined $reftype or $reftype eq "CODE" or $reftype eq "HASH" or $reftype eq "REGEXP" or $reftype eq "SCALAR"
not defined $reftype $reftype eq "CODE" $reftype eq "HASH" $reftype eq "REGEXP" $reftype eq "SCALAR"
Truth table100%6/6
not(A) or B or C or D or E
A= not defined $reftypeB= $reftype eq "CODE"C= $reftype eq "HASH"D= $reftype eq "REGEXP"E= $reftype eq "SCALAR"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
196 "elements of $config_name must have a reftype of undef (scalar), CODE, HASH, REGEXP, or SCALAR but was '$reftype'"
197 unless not defined $reftype
198 or $reftype eq 'CODE'
199 or $reftype eq 'HASH'
200 or $reftype eq 'REGEXP'
201 or $reftype eq 'SCALAR';
202 146 ▶ Carp::confess "elements of $config_name must begin with a '/' when they are scalar"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $reftype and index($_, "/") != 0 2 144 146
Condition100%3/3
not defined $reftype and index($_, "/") != 0
!ll&&!rl&&r
112322
MC/DC100%2/2
not defined $reftype and index($_, "/") != 0
not defined $reftype index($_, "/") != 0
Truth table100%3/3
not(A) and B
A= not defined $reftypeB= index($_, "/") != 0
ABresult
0X0
100
111
203 if not defined $reftype and index($_, '/') != 0;
204  
205 144 ▶ if (defined $reftype and $reftype eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reftype and $reftype eq "SCALAR") 24 120 144
Condition100%3/3
defined $reftype and $reftype eq "SCALAR"
!ll&&!rl&&r
328824
MC/DC100%2/2
defined $reftype and $reftype eq "SCALAR"
defined $reftype $reftype eq "SCALAR"
Truth table100%3/3
A and B
A= defined $reftypeB= $reftype eq "SCALAR"
ABresult
0X0
100
111
206 24 $captures_allowed = 1;
207 }
208  
209 144 ▶ if (defined $reftype and $reftype eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reftype and $reftype eq "HASH") 37 107 144
Condition100%3/3
defined $reftype and $reftype eq "HASH"
!ll&&!rl&&r
327537
MC/DC100%2/2
defined $reftype and $reftype eq "HASH"
defined $reftype $reftype eq "HASH"
Truth table100%3/3
A and B
A= defined $reftypeB= $reftype eq "HASH"
ABresult
0X0
100
111
210 37 _validate_starts_with_hash($config_name, $_);
211 }
212 }
213 ▶ } elsif (defined $config_reftype and $config_reftype eq 'HASH') {
Condition66%2/3
defined $config_reftype and $config_reftype eq "HASH"
!ll&&!rl&&r
07642
MC/DC50%1/2
defined $config_reftype and $config_reftype eq "HASH"
defined $config_reftype $config_reftype eq "HASH"
Truth table66%2/3
A and B
A= defined $config_reftypeB= $config_reftype eq "HASH"
ABresult
0X0
100
111
214 42 _validate_starts_with_hash($config_name, $config);
215 }
216  
217 216 return ($config, $captures_allowed);
218 }
219  
220 79 ▶sub _validate_starts_with_hash {
Subroutine _validate_starts_with_hash: called CC=6 SCAR=17.9
221 79 my ($config_name, $hash) = @_;
222 79 my %copy = %$hash;
223 79 ▶ Carp::confess "must provide key 'starts_with' to hash in $config_name" unless exists $copy{starts_with};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $copy{"starts_with"} 4 75 79
224 75 ▶ Carp::confess 'value for starts_with must not be undef' unless defined $copy{starts_with};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $copy{"starts_with"} 4 71 75
225 Carp::confess 'value for starts_with must be a scalar'
226 71 ▶ unless not defined Scalar::Util::reftype $copy{starts_with};
Branch: 1 branch
BranchTrueFalseTotal
if defined builtin::reftype($copy{"starts_with"}) 4 67 71
227 Carp::confess q{value for starts_with must begin with a '/'}
228 67 ▶ unless index(delete $copy{starts_with}, '/') == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index(delete $copy{"starts_with"}, "/") == 0 4 63 67
229 63 ▶ Carp::confess "unknown keys/values passed in hash inside of $config_name: " . Mojo::Util::dumper \%copy
Branch: 1 branch
BranchTrueFalseTotal
if %copy 4 59 63
230 if %copy
231 }
232  
233 213 ▶sub _create_should_canonicalize_request_sub_string {
Subroutine _create_should_canonicalize_request_sub_string: called CC=30 SCAR=34.0
234 213 my %args = @_;
235 my ($config, $captures, $sub_string, $should_canonicalize_request, $path_declared, $path_with_no_slashes_at_the_end_declared) =
236 213 @{{@_}}{qw/config captures sub_string should_canonicalize_request path_declared path_with_no_slashes_at_the_end_declared/};
237 213 my $path_with_no_slashes_at_the_end_declared_code = q{
238 my $_mpcu_path_with_no_slashes_at_the_end = $_mpcu_path;
239 while (rindex($_mpcu_path_with_no_slashes_at_the_end, '/') == length($_mpcu_path_with_no_slashes_at_the_end) - 1) {
240 substr $_mpcu_path_with_no_slashes_at_the_end, -1, 1, '';
241 }
242 };
243  
244 213 my $config_name = _get_should_canonicalize_request_config_name($should_canonicalize_request);
245 213 my $config_variable_name = "\$$config_name";
246 213 ▶ my $if_or_unless = $should_canonicalize_request ? 'unless' : 'if';
Branch: 1 branch
BranchTrueFalseTotal
$should_canonicalize_request ? : 109 104 213
247 213 my $reftype = Scalar::Util::reftype $config;
248 213 ▶ if (not defined $reftype) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $reftype) { } 23 190 213
elsif ($reftype eq 'REGEXP') { } 18 172 190
elsif ($reftype eq 'SCALAR') { } 39 133 172
elsif ($reftype eq 'CODE') { } 32 101 133
elsif ($reftype eq 'HASH') { } 32 69 101
249 23 $config =~ s#/+\z##m;
250 23 $captures->{$config_variable_name} = \$config;
251  
252 23 ▶ unless ($path_with_no_slashes_at_the_end_declared) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path_with_no_slashes_at_the_end_declared) 23 0 23
253 23 ▶ unless ($path_declared) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path_declared) 23 0 23
254 23 $sub_string .= 'my $_mpcu_path = $c->req->url->path->to_string;';
255 23 $path_declared = 1;
256 }
257  
258 23 $sub_string .= $path_with_no_slashes_at_the_end_declared_code;
259 23 $path_with_no_slashes_at_the_end_declared = 1;
260 }
261 23 $sub_string .= "return \$next->() $if_or_unless \$_mpcu_path_with_no_slashes_at_the_end eq $config_variable_name;";
262 } elsif ($reftype eq 'REGEXP') {
263 18 ▶ unless ($path_declared) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path_declared) 12 6 18
264 12 $sub_string .= 'my $_mpcu_path = $c->req->url->path->to_string;';
265 12 $path_declared = 1;
266 }
267  
268 18 $captures->{$config_variable_name} = \$config;
269 18 $sub_string .= "return \$next->() $if_or_unless \$_mpcu_path =~ $config_variable_name;";
270 } elsif ($reftype eq 'SCALAR') {
271 39 my $code = $$config;
272 39 ▶ $code = "return \$next->() $if_or_unless $code" if $code !~ /\A\s*return/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /\A\s*return/u 27 12 39
273 39 ▶ $code .= ';' unless $code =~ /;\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /;\s*\z/u 29 10 39
274  
275 39 ▶ Carp::confess 'code must contain return $next->()' unless $code =~ /return\s+\$next->\(\)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /return\s+\$next->\(\)/u 0 39 39
276  
277 39 $sub_string .= $code;
278 } elsif ($reftype eq 'CODE') {
279 32 $captures->{$config_variable_name} = \$config;
280 32 $sub_string .= qq{
281 local \$_ = \$c;
282 return \$next->() $if_or_unless $config_variable_name->();
283 };
284 } elsif ($reftype eq 'HASH') {
285 32 ▶ unless ($path_declared) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path_declared) 32 0 32
286 32 $sub_string .= 'my $_mpcu_path = $c->req->url->path->to_string;';
287 32 $path_declared = 1;
288 }
289  
290 32 my $starts_with = $config->{starts_with};
291 32 $captures->{$config_variable_name} = \$starts_with;
292 32 $sub_string .= qq{return \$next->() $if_or_unless index(\$_mpcu_path, $config_variable_name) == 0;}
293 } else {
294 69 ▶ if (grep { not defined Scalar::Util::reftype $_ } @$config and not $path_with_no_slashes_at_the_end_declared) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not defined builtin::reftype($_);} @$config and not $path_with_no_slashes_at_the_end_declared) 32 37 69
Condition66%2/3
grep {not defined builtin::reftype($_);} @$config and not $path_with_no_slashes_at_the_end_declared
!ll&&!rl&&r
37032
MC/DC50%1/2
grep {not defined builtin::reftype($_);} @$config and not $path_with_no_slashes_at_the_end_declared
grep {not defined builtin::reftype($_);} @$config not $path_with_no_slashes_at_the_end_declared
Truth table66%2/3
A and not(B)
A= grep {not defined builtin::reftype($_);} @$configB= not $path_with_no_slashes_at_the_end_declared
ABresult
0X0
100
111
295 32 ▶ unless ($path_declared) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path_declared) 32 0 32
296 32 $sub_string .= 'my $_mpcu_path = $c->req->url->path->to_string;';
297 32 $path_declared = 1;
298 }
299  
300 32 $sub_string .= $path_with_no_slashes_at_the_end_declared_code;
301 32 $path_with_no_slashes_at_the_end_declared = 1;
302 }
303  
304 69 ▶ unless ($path_declared) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path_declared) 37 32 69
305 37 for my $config_item (@$config) {
306 47 my $reftype = Scalar::Util::reftype $config_item;
307 47 ▶ if ($reftype eq 'REGEXP' or $reftype eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq "REGEXP" or $reftype eq "HASH") 11 36 47
Condition100%3/3
$reftype eq "REGEXP" or $reftype eq "HASH"
l!l&&r!l&&!r
9236
MC/DC100%2/2
$reftype eq "REGEXP" or $reftype eq "HASH"
$reftype eq "REGEXP" $reftype eq "HASH"
Truth table100%3/3
A or B
A= $reftype eq "REGEXP"B= $reftype eq "HASH"
ABresult
1X1
011
000
308 11 $sub_string .= 'my $_mpcu_path = $c->req->url->path->to_string;';
309 11 $path_declared = 1;
310 11 last;
311 }
312 }
313 }
314  
315 69 ▶ $sub_string .= 'return $next->() unless' if $should_canonicalize_request;
Branch: 1 branch
BranchTrueFalseTotal
if $should_canonicalize_request 37 32 69
316 69 for my $index (0 .. $#$config) {
317 134 my $reftype_of_item = Scalar::Util::reftype $config->[$index];
318 134 my $condition;
319  
320 134 ▶ if (not defined $reftype_of_item) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $reftype_of_item) { } 32 102 134
elsif ($reftype_of_item eq 'CODE') { } 12 90 102
elsif ($reftype_of_item eq 'HASH') { } 27 63 90
elsif ($reftype_of_item eq 'REGEXP') { } 39 24 63
321 32 $config->[$index] =~ s#/+\z##;
322 32 my $var_name = '$_mpcu_' . $config_name . "_eq_$index";
323 32 my $value = $config->[$index];
324 32 $captures->{$var_name} = \$value;
325  
326 32 $condition = "\$_mpcu_path_with_no_slashes_at_the_end eq $var_name";
327 } elsif ($reftype_of_item eq 'CODE') {
328 12 my $var_name = '$_mpcu_' . $config_name . "_code_$index";
329 12 my $value = $config->[$index];
330 12 $captures->{$var_name} = \$value;
331 12 $condition .= "do { local \$_ = \$c; $var_name->(); }";
332 } elsif ($reftype_of_item eq 'HASH') {
333 27 my $var_name = '$_mpcu_' . $config_name . "_starts_with_$index";
334 27 my $value = $config->[$index]{starts_with};
335 27 $captures->{$var_name} = \$value;
336 27 $condition = "index(\$_mpcu_path, $var_name) == 0";
337 } elsif ($reftype_of_item eq 'REGEXP') {
338 39 my $var_name = '$_mpcu_' . $config_name . "_regexp_$index";
339 39 my $value = $config->[$index];
340 39 $captures->{$var_name} = \$value;
341 39 $condition = "\$_mpcu_path =~ $var_name";
342 } else {
343 24 $condition = ${$config->[$index]};
344 }
345  
346 134 ▶ if ($should_canonicalize_request) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_canonicalize_request) { } 72 62 134
347 72 ▶ $sub_string .= " ||" if $index != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index != 0 35 37 72
348 72 $sub_string .= " $condition";
349 } else {
350 62 $sub_string .= "return \$next->() if $condition;";
351 }
352 }
353  
354 69 ▶ $sub_string .= ';' if $should_canonicalize_request;
Branch: 1 branch
BranchTrueFalseTotal
if $should_canonicalize_request 37 32 69
355 }
356  
357 213 return ($path_declared, $path_with_no_slashes_at_the_end_declared, $sub_string);
358 }
359  
360 469 ▶sub _get_should_canonicalize_request_config_name {
Subroutine _get_should_canonicalize_request_config_name: called CC=2 SCAR=6.9
361 469 ▶ return shift() ? 'should_canonicalize_request' : 'should_not_canonicalize_request';
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 240 229 469
362 }
363  
364 16 ▶sub remove_trailing_slashes {
Subroutine remove_trailing_slashes: called CC=2 SCAR=6.9
Pod documented
365 16 my ($path) = "$_[0]"; # turn possible Mojo::Path into string.
366  
367 16 ▶ while (rindex($path, '/') == length($path) - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($path, "/") == length($path) - 1) 14 16 30
368 14 substr $path, -1, 1, '';
369 }
370  
371 16 return $path;
372 }
373  
374 4 1;
375 __END__
↑ Summary