blib/lib/Kelp/Routes/Pattern.pm

filter: Statementstmt 97.6% 127 / 130 Branchbran 93.7% 45 / 48 Conditioncond 93.5% 29 / 31 MC/DCmcdc 88.2% 15 / 17 Subroutinesub 86.3% 19 / 22 Podpod 75.0% 3 / 4 totaltotal 94.4% 238 / 252 CC 38 SCAR 16.8
CC
38
Cov
96%
_rep_build
9.0
match
9.0
_rep_regex
7.0
CRAP 5.3
« blib/lib/Kelp/Request.pm ↑ Summary blib/lib/Kelp.pm »
1 package Kelp::Routes::Pattern;
2  
3 34 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 34 ▶use Kelp::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 our @CARP_NOT = qw(Kelp::Routes);
8  
9 0 ▶attr pattern => sub { die "pattern is required" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
10 34 attr via => undef;
11 220 ▶attr method => sub { $_[0]->via };
Subroutine __ANON__: called CC=1 SCAR=0.0
12 34 attr has_name => undef;
13 155 ▶attr name => sub { $_[0]->pattern };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 58 ▶attr check => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
15 217 ▶attr defaults => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
16 34 attr bridge => 0;
17 34 attr order => 0;
18 291 ▶attr regex => sub { $_[0]->_build_regex };
Subroutine __ANON__: called CC=1 SCAR=0.0
19 0 ▶attr named => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
20 0 ▶attr param => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
21 34 attr to => undef;
22 34 attr dest => undef;
23 34 attr psgi_middleware => undef;
24  
25 # helpers for matching different types of wildcards
26 sub __noslash
27 111 ▶{
Subroutine __noslash: called CC=1 SCAR=0.0
28 111 1 == grep { $_[0] eq $_ } ':', '?';
29 }
30  
31 sub __matchall
32 28 ▶{
Subroutine __matchall: called CC=1 SCAR=0.0
33 28 1 == grep { $_[0] eq $_ } '*', '>';
34 }
35  
36 sub __optional
37 213 ▶{
Subroutine __optional: called CC=1 SCAR=0.0
38 213 1 == grep { $_[0] eq $_ } '?', '>';
39 }
40  
41 sub new
42 291 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
43 291 my $class = shift;
44 291 my $self = $class->SUPER::new(@_);
45 291 ▶ $self->has_name(defined $self->{name} && length $self->{name}); # remember if pattern was named
Condition66%2/3
defined $self->{'name'} && length $self->{'name'}
!ll&&!rl&&r
265026
MC/DC50%1/2
defined $self->{'name'} && length $self->{'name'}
defined $self->{'name'} length $self->{'name'}
Truth table66%2/3
A && B
A= defined $self->{'name'}B= length $self->{'name'}
ABresult
0X0
100
111
46  
47 291 $self->_fix_pattern;
48 291 $self->regex; # Compile the regex
49 291 return $self;
50 }
51  
52 sub _fix_pattern
53 291 ▶{
Subroutine _fix_pattern: called CC=2 SCAR=6.9
54 291 my ($self) = @_;
55 291 my $pattern = $self->pattern;
56 291 ▶ return if ref $pattern; # only fix non-regex patterns
Branch: 1 branch
BranchTrueFalseTotal
if ref $pattern 5 286 291
57  
58 # operations performed
59 286 $pattern =~ s{/+}{/}g;
60  
61 286 $self->pattern($pattern);
62 }
63  
64 sub _rep_regex
65 119 ▶{
Subroutine _rep_regex: called CC=7 SCAR=19.5
66 119 my ($self, $char, $switch, $token) = @_;
67 119 my $qchar = quotemeta $char;
68 119 my $re;
69  
70 119 ▶ my $optional = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
71 119 ▶ return unless __optional($switch);
Branch: 1 branch
BranchTrueFalseTotal
unless __optional($switch) 97 22 119
72 22 ▶ $re = "(?:$re)" if $char eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "/" 17 5 22
73 22 $re .= '?';
74 119 };
75  
76 # no token - only valid for the wildcard * and slurpy >
77 119 ▶ if (!defined $token) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $token) { } 8 111 119
78  
79 # do nothing
80 8 ▶ return $char . $switch
Branch: 1 branch
BranchTrueFalseTotal
unless __matchall($switch) 0 8 8
81 unless __matchall($switch);
82  
83 8 $re = $qchar . '(.+)';
84 }
85 else {
86 111 push @{$self->{_tokens}}, $token;
87  
88 111 my ($prefix, $suffix) = ("(?<$token>", ')');
89 111 ▶ if (__noslash($switch)) {
Branch: 2 branches
BranchTrueFalseTotal
if (__noslash($switch)) { } 100 11 111
elsif (__matchall($switch)) { } 11 0 11
90 100 ▶ $re = $qchar . $prefix . ($self->check->{$token} // '[^\/]+') . $suffix;
Condition100%2/2
$self->check->{$token} // "[^\\/]+"
l!l
1585
MC/DC100%1/1
$self->check->{$token} // "[^\\/]+"
$self->check->{$token}
Truth table100%2/2
A
A= $self->check->{$token}
Aresult
11
00
91 }
92 elsif (__matchall($switch)) {
93 11 ▶ $re = $qchar . $prefix . ($self->check->{$token} // '.+') . $suffix;
Condition100%2/2
$self->check->{$token} // ".+"
l!l
110
MC/DC100%1/1
$self->check->{$token} // ".+"
$self->check->{$token}
Truth table100%2/2
A
A= $self->check->{$token}
Aresult
11
00
94 }
95 }
96  
97 119 $optional->();
98 119 push @{$self->{_rep_regex_parts}}, $re;
99 119 return '{}';
100 }
101  
102 sub _build_regex
103 291 ▶{
Subroutine _build_regex: called CC=7 SCAR=19.5
104 291 my $self = shift;
105 291 $self->{_tokens} = [];
106  
107 291 ▶ return $self->pattern if ref $self->pattern eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->pattern eq "Regexp" 5 286 291
108  
109 286 my $placeholder_pattern = qr{
110 ( [^\0]? ) # preceding char, may change behavior of some placeholders
111 ( [:*?>] ) # placeholder sigil
112 ( \w+ )? # placeholder label
113 }x;
114 286 my $pattern = $self->pattern;
115  
116 # Curly braces and brackets are only used for separation.
117 # We replace all of them with \0, then convert the pattern
118 # into a regular expression. This way if the regular expression
119 # contains curlies, they won't be removed.
120 286 $pattern =~ s/[{}]/\0/g;
121  
122 286 $self->{_rep_regex_parts} = [];
123 286 $pattern =~ s{$placeholder_pattern}{$self->_rep_regex($1, $2, $3)}eg;
124  
125 # Now remove all curlies remembered as \0 - We will use curlies again for
126 # special behavior in a moment
127 286 $pattern =~ s/\0//g;
128  
129 # remember if the pattern has a trailing slash before we quote it
130 286 my $trailing_slash = $pattern =~ m{/$};
131  
132 # _rep_regex reused curies for {} placeholders, so we want to split the
133 # string by that (and include them in the result by capturing the
134 # separator)
135 286 my @parts = split /(\Q{}\E)/, $pattern, -1;
136  
137 # If we have a placeholder, replace it with next part. If not, quote it to
138 # avoid misusing regex in patterns.
139 286 foreach my $part (@parts) {
140 523 ▶ if ($part eq '{}') {
Branch: 1 branch
BranchTrueFalseTotal
if ($part eq '{}') { } 119 404 523
141 119 $part = shift @{$self->{_rep_regex_parts}};
142 }
143 else {
144 404 $part = quotemeta $part;
145 }
146 }
147  
148 286 $pattern = join '', @parts;
149 286 ▶ if ($self->bridge) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bridge) { } 20 266 286
150  
151 # bridge must be followed by a slash or end of string, so that:
152 # - /test matches
153 # - /test/ matches
154 # - /test/something matches
155 # - /testsomething does not match
156 # if the bridge is already followed by a trailing slash, it's not a
157 # concern
158 20 ▶ $pattern .= '(?:/|$)' unless $trailing_slash;
Branch: 1 branch
BranchTrueFalseTotal
unless $trailing_slash 17 3 20
159 }
160 else {
161  
162 # regular pattern must end immediately
163 266 ▶ $pattern .= quotemeta('/') . '?' unless $trailing_slash;
Branch: 1 branch
BranchTrueFalseTotal
unless $trailing_slash 264 2 266
164 266 $pattern .= '$';
165 }
166  
167 286 return qr{^$pattern};
168 }
169  
170 sub _rep_build
171 94 ▶{
Subroutine _rep_build: called CC=9 SCAR=22.0
172 94 my ($self, $switch, $token, %args) = @_;
173  
174 94 ▶ if (!defined $token) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $token) 9 85 94
175 9 ▶ return $switch unless __matchall($switch);
Branch: 1 branch
BranchTrueFalseTotal
unless __matchall($switch) 0 9 9
176 9 $token = $switch;
177 }
178  
179 94 ▶ my $rep = $args{$token} // $self->defaults->{$token} // '';
Condition100%5/5
($args{$token} // $self->defaults->{$token}) // ""
l!l
7420
$args{$token} // $self->defaults->{$token}
l!l&&r!l&&!r
67720
MC/DC100%2/2
($args{$token} // $self->defaults->{$token}) // ""
$args{$token} $self->defaults->{$token}
Truth table100%3/3
A // B
A= $args{$token}B= $self->defaults->{$token}
ABresult
1X1
011
000
180 94 ▶ if (!__optional($switch) && !length $rep) {
Branch: 1 branch
BranchTrueFalseTotal
unless (__optional($switch) or length $rep) 16 78 94
Condition100%3/3
__optional($switch) or length $rep
l!l&&r!l&&!r
275116
MC/DC100%2/2
__optional($switch) or length $rep
__optional($switch) length $rep
Truth table100%3/3
A or B
A= __optional($switch)B= length $rep
ABresult
1X1
011
000
181 16 return '{?' . $token . '}';
182 }
183  
184 78 my $check = $self->check->{$token};
185 78 ▶ if ($check && $args{$token} !~ $check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check and not $args{$token} =~ /$check/) 2 76 78
Condition100%3/3
$check and not $args{$token} =~ /$check/
!ll&&!rl&&r
7242
MC/DC100%2/2
$check and not $args{$token} =~ /$check/
$check not $args{$token} =~ /$check/
Truth table100%3/3
A and not(B)
A= $checkB= not $args{$token} =~ /$check/
ABresult
0X0
100
111
186 2 return '{!' . $token . '}';
187 }
188  
189 76 return $rep;
190 }
191  
192 sub build
193 54 ▶{
Subroutine build: called CC=4 SCAR=13.9
Pod documented
194 54 my ($self, %args) = @_;
195  
196 54 my $pattern = $self->pattern;
197 54 ▶ croak "Can't build a regular expression route"
Branch: 1 branch
BranchTrueFalseTotal
if ref $pattern eq "Regexp" 1 53 54
198 if ref $pattern eq 'Regexp';
199  
200 53 my $placeholder_pattern = qr{
201 \{? # may be embraced in curlies
202 ( [:*?>] ) # placeholder sigil
203 ( \w+ )? # placeholder label
204 \}?
205 }x;
206  
207 53 $pattern =~ s/$placeholder_pattern/$self->_rep_build($1, $2, %args)/eg;
208 53 ▶ if ($pattern =~ /{([!?])(\w+|[*>])}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /{([!?])(\w+|[*>])}/) 17 36 53
209 17 ▶ croak "Can't build '$pattern', " . (
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '!' ? : 2 15 17
210 $1 eq '!'
211 ? "field $2 doesn't match checks"
212 : "Default value for field $2 is missing"
213 );
214 }
215  
216 36 return $pattern;
217 }
218  
219 sub match
220 1390 ▶{
Subroutine match: called CC=9 SCAR=22.0
Pod documented
221 1390 my ($self, $path, $method) = @_;
222 1390 ▶ return 0 if ($self->method && $self->method ne ($method // ''));
Branch: 1 branch
BranchTrueFalseTotal
if $self->method and $self->method ne ($method // "") 74 1316 1390
Condition100%5/5
$self->method and $self->method ne ($method // "")
!ll&&!rl&&r
12526474
$method // ""
l!l
1371
MC/DC100%2/2
$self->method and $self->method ne ($method // "")
$self->method $self->method ne ($method // "")
MC/DC100%1/1
$method // ""
$method
Truth table100%3/3
A and B
A= $self->methodB= $self->method ne ($method // "")
ABresult
0X0
100
111
Truth table100%2/2
A
A= $method
Aresult
11
00
223 1316 ▶ return 0 unless my @matched = $path =~ $self->regex;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@matched) = $path =~ $self->regex 914 402 1316
224 402 my $has_matches = $#+; # see perlvar @+
225  
226 # Initialize the named parameters hash and its default values
227 402 my %named = (%{$self->defaults}, %+);
228  
229 402 ▶ if (@{$self->{_tokens}}) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->{'_tokens'};}) { } 142 260 402
elsif ($has_matches) { } 17 243 260
230  
231 # values of the named placeholders in the order they appear in the
232 # regex.
233 142 @matched = map { $named{$_} } @{$self->{_tokens}};
234 }
235 elsif ($has_matches) {
236 17 ▶ @matched = map { length($_ // '') ? $_ : undef } @matched;
Branch: 1 branch
BranchTrueFalseTotal
length($_ // '') ? : 21 1 22
Condition50%1/2
$_ // ""
l!l
220
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
237 }
238 else {
239 243 @matched = ();
240 }
241  
242 402 $self->named(\%named);
243 402 $self->param(\@matched);
244  
245 402 return 1;
246 }
247  
248 sub compare
249 42 ▶{
Subroutine compare: called CC=3 SCAR=11.0
Pod documented
250 42 my ($self, $other) = @_;
251  
252 42 ▶ return $other->bridge <=> $self->bridge
Condition100%6/6
$other->bridge <=> $self->bridge || $self->order <=> $other->order || $self->pattern cmp $other->pattern
l!l&&r!l&&!r
3381
$other->bridge <=> $self->bridge or $self->order <=> $other->order
l!l&&r!l&&!r
2949
MC/DC100%3/3
$other->bridge <=> $self->bridge || $self->order <=> $other->order || $self->pattern cmp $other->pattern
$other->bridge <=> $self->bridge $self->order <=> $other->order $self->pattern cmp $other->pattern
Truth table100%4/4
A or B || C
A= $other->bridge <=> $self->bridgeB= $self->order <=> $other->orderC= $self->pattern cmp $other->pattern
ABCresult
1XX1
01X1
0011
0000
253 || $self->order <=> $other->order
254 || $self->pattern cmp $other->pattern;
255 }
256  
257 34 1;
258  
259 __END__
« blib/lib/Kelp/Request.pm ↑ Summary blib/lib/Kelp.pm »