Gears/Router/Pattern/SigilMatch.pm

filter: Statementstmt 98.7% 76 / 77 Branchbran 82.1% 23 / 28 Conditioncond 90.0% 9 / 10 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 13 / 13 Podpod 66.6% 2 / 3 totaltotal 93.4% 128 / 137 CC 21 SCAR 17.0
CC
21
Cov
94%
build
9.0
_rep_regex
7.0
_build_regex
6.2
CRAP 5.5
« Gears/Config.pm ↑ Summary Gears/X.pm »
1 package Gears::Router::Pattern::SigilMatch;
2 2 $Gears::Router::Pattern::SigilMatch::VERSION = '0.103';
3 2 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Gears::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 extends 'Gears::Router::Pattern';
10  
11 2 has extended 'location' => (
12 handles => [
13 qw(
14 checks
15 defaults
16 )
17 ],
18 );
19  
20 2 has field '_regex' => (
21 isa => RegexpRef,
22 lazy => 1,
23 );
24  
25 has field 'tokens' => (
26 isa => ArrayRef,
27 33 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 );
29  
30 # helpers for matching different types of wildcards
31 my sub noslash ($sigil)
32 ▶{
Subroutine noslash: called CC=1 SCAR=0.0
33 61 return 1 == grep { $sigil eq $_ } ':', '?';
34 }
35  
36 my sub matchall ($sigil)
37 ▶{
Subroutine matchall: called CC=1 SCAR=0.0
38 11 return 1 == grep { $sigil eq $_ } '*', '>';
39 }
40  
41 my sub optional ($sigil)
42 ▶{
Subroutine optional: called CC=1 SCAR=0.0
43 56 return 1 == grep { $sigil eq $_ } '?', '>';
44 }
45  
46 sub _rep_regex
47 {
48 52 ▶ my ($self, $char, $switch, $token, $out) = @_;
Subroutine _rep_regex: called CC=7 SCAR=19.5
49 52 my $qchar = quotemeta $char;
50 52 my $re;
51  
52 52 push $self->tokens->@*, {
53 sigil => $switch,
54 label => $token,
55 };
56  
57 52 my ($prefix, $suffix) = ("(?<$token>", ')');
58 52 ▶ if (noslash($switch)) {
Branch: 2 branches
BranchTrueFalseTotal
if (noslash $switch) { } 41 11 52
elsif (matchall $switch) { } 11 0 11
59 41 ▶ $re = $qchar . $prefix . ($self->checks->{$token} // '[^\/]+') . $suffix;
Condition100%2/2
$self->checks->{$token} // "[^\\/]+"
l!l
932
MC/DC100%1/1
$self->checks->{$token} // "[^\\/]+"
$self->checks->{$token}
Truth table100%2/2
A
A= $self->checks->{$token}
Aresult
11
00
60 }
61 elsif (matchall($switch)) {
62 11 ▶ $re = $qchar . $prefix . ($self->checks->{$token} // '.+') . $suffix;
Condition100%2/2
$self->checks->{$token} // ".+"
l!l
110
MC/DC100%1/1
$self->checks->{$token} // ".+"
$self->checks->{$token}
Truth table100%2/2
A
A= $self->checks->{$token}
Aresult
11
00
63 }
64  
65 52 ▶ if (optional($switch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (optional $switch) 15 37 52
66 15 ▶ $re = "(?:$re)" if $char eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "/" 10 5 15
67 15 $re .= '?';
68 }
69  
70 52 push $out->@*, $re;
71 52 return '{}';
72 }
73  
74 sub _build_regex ($self)
75 ▶{
Subroutine _build_regex: called CC=6 SCAR=18.3
76 33 my $pattern = $self->pattern;
77  
78 33 my $placeholder_pattern = qr{
79 ( [^\0]? ) # preceding char, may change behavior of some placeholders
80 ( [:*?>] ) # placeholder sigil
81 ( \w+ ) # placeholder label
82 }x;
83  
84 # Curly braces and brackets are only used for separation.
85 # We replace all of them with \0, then convert the pattern
86 # into a regular expression. This way if the regular expression
87 # contains curlies, they won't be removed.
88 33 $pattern =~ s/[{}]/\0/g;
89  
90 33 my @rep_regex_parts;
91 33 $pattern =~ s{
92 $placeholder_pattern
93 }{
94 52 $self->_rep_regex($1, $2, $3, \@rep_regex_parts)
95 }egx;
96  
97 # Now remove all curlies remembered as \0 - We will use curlies again for
98 # special behavior in a moment
99 33 $pattern =~ s/\0//g;
100  
101 # remember if the pattern has a trailing slash before we quote it
102 33 my $trailing_slash = $pattern =~ m{/$};
103  
104 # _rep_regex reused curies for {} placeholders, so we want to split the
105 # string by that (and include them in the result by capturing the
106 # separator)
107 33 my @parts = split /(\Q{}\E)/, $pattern, -1;
108  
109 # If we have a placeholder, replace it with next part. If not, quote it to
110 # avoid misusing regex in patterns.
111 33 foreach my $part (@parts) {
112 137 ▶ if ($part eq '{}') {
Branch: 1 branch
BranchTrueFalseTotal
if ($part eq '{}') { } 52 85 137
113 52 $part = shift @rep_regex_parts;
114 }
115 else {
116 85 $part = quotemeta $part;
117 }
118 }
119  
120 33 $pattern = join '', @parts;
121 33 ▶ if ($self->is_bridge) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_bridge) { } 0 33 33
122  
123 # bridge must be followed by a slash or end of string, so that:
124 # - /test matches
125 # - /test/ matches
126 # - /test/something matches
127 # - /testsomething does not match
128 # if the bridge is already followed by a trailing slash, it's not a
129 # concern
130 0 ▶ $pattern .= '(?:/|$)' unless $trailing_slash;
Branch: 1 branch
BranchTrueFalseTotal
unless $trailing_slash 0 0 0
131 }
132 else {
133  
134 # regular pattern must end immediately
135 33 ▶ $pattern .= quotemeta('/') . '?' unless $trailing_slash;
Branch: 1 branch
BranchTrueFalseTotal
unless $trailing_slash 33 0 33
136 33 $pattern .= '$';
137 }
138  
139 33 return qr{^$pattern};
140 }
141  
142 sub BUILD ($self, $)
143 ▶{
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
144 33 $self->_regex; # ensure tokens are created
145 }
146  
147 sub compare ($self, $request_path)
148 ▶{
Subroutine compare: called CC=2 SCAR=6.9
Pod documented
149 150 ▶ return undef unless $request_path =~ $self->_regex;
Branch: 1 branch
BranchTrueFalseTotal
unless $request_path =~ $self->_regex 53 97 150
150  
151 # initialize the named parameters hash and its default values
152 97 my %named = ($self->defaults->%*, %+);
153  
154 # transform into a list of parameters
155 97 return [map { $named{$_->{label}} } $self->tokens->@*];
156 }
157  
158 sub build ($self, %args)
159 ▶{
Subroutine build: called CC=9 SCAR=22.0
Pod documented
160 12 my $pattern = $self->pattern;
161 12 my $checks = $self->checks;
162 12 %args = ($self->defaults->%*, %args);
163  
164 12 foreach my $token ($self->tokens->@*) {
165 13 my $value = $args{$token->{label}};
166  
167 Gears::X->raise("no value for placeholder $token->{sigil}$token->{label}")
168 13 ▶ unless defined $value || optional $token->{sigil};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value or optional $token->{"sigil"} 1 12 13
Condition100%3/3
defined $value or optional $token->{"sigil"}
l!l&&r!l&&!r
931
MC/DC100%2/2
defined $value or optional $token->{"sigil"}
defined $value optional $token->{"sigil"}
Truth table100%3/3
A or B
A= defined $valueB= optional $token->{"sigil"}
ABresult
1X1
011
000
169  
170 12 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 9 3 12
171 9 my $safe = '^A-Za-z0-9\-\._~';
172 9 ▶ $safe .= '/' unless noslash $token->{sigil};
Branch: 1 branch
BranchTrueFalseTotal
unless noslash $token->{"sigil"} 2 7 9
173 9 $value = uri_escape_utf8 $value, "^$safe";
174 }
175  
176 12 my $to_replace = qr{
177 \Q$token->{sigil}\E
178 $token->{label}
179 }x;
180  
181 12 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 9 3 12
182 9 my $check = $checks->{$token->{label}};
183 9 ▶ Gears::X->raise("bad value for placeholder $token->{sigil}$token->{label}")
Branch: 1 branch
BranchTrueFalseTotal
if $check and not $value =~ /^$check$/u 2 7 9
Condition66%2/3
$check and not $value =~ /^$check$/u
!ll&&!rl&&r
702
MC/DC50%1/2
$check and not $value =~ /^$check$/u
$check not $value =~ /^$check$/u
Truth table66%2/3
A and not(B)
A= $checkB= not $value =~ /^$check$/u
ABresult
0X0
100
111
184 if $check && $value !~ /^$check$/;
185  
186 7 $pattern =~ s{\{?$to_replace\}?}{$value};
187 }
188 else {
189 # slash should be removed as well for optional placeholders (if no brackets)
190 3 $pattern =~ s{/$to_replace|\{?$to_replace\}?}{};
191 }
192 }
193  
194 9 return $pattern;
195 }
196  
197 __END__
« Gears/Config.pm ↑ Summary Gears/X.pm »