blib/lib/Kelp/Util.pm

filter: Statementstmt 92.8% 117 / 126 Branchbran 79.4% 54 / 68 Conditioncond 81.8% 18 / 22 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 95.6% 22 / 23 Podpod 77.7% 7 / 9 totaltotal 87.3% 228 / 261 CC 45 SCAR 19.9
CC
45
Cov
88%
merge
15.6
_DEBUG
10.3
camelize
9.0
CRAP 7.3
« blib/lib/Kelp/Routes.pm ↑ Summary blib/lib/Kelp/Request.pm »
1 package Kelp::Util;
2  
3 # No Kelp::Base here, because Kelp::Base uses Kelp::Util;
4 58 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 58 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 58 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 58 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 58 ▶use Data::Dumper qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 58 ▶use Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 58 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 58 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 58 ▶use Test::Deep::NoTest qw(eq_deeply);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 58 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # improve error locations of croak
17 58 our @CARP_NOT = (
18 qw(
19 Kelp
20 Kelp::Base
21 Kelp::Routes
22 Kelp::Context
23 )
24 );
25  
26 sub _DEBUG
27 947 ▶{
Subroutine _DEBUG: called CC=4 SCAR=23.3
28 947 my ($stage, @messages) = @_;
29 947 my $env = $ENV{KELP_DEBUG};
30 947 ▶ return if !$env;
Branch: 1 branch
BranchTrueFalseTotal
unless $env 947 0 947
31 0 ▶ return if !grep { lc $env eq $_ } '1', 'all', lc $stage;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {lc $env eq $_;} 1, "all", lc $stage 0 0 0
32  
33 0 local $Data::Dumper::Sortkeys = 1;
34 my $message = join ' ', map {
35 0 ▶ ref $_ ? Data::Dumper::Dumper($_) : $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
36 } @messages;
37  
38 0 print "DEBUG: $message\n";
39 }
40  
41 sub camelize
42 202 ▶{
Subroutine camelize: called CC=9 SCAR=22.0
Pod documented
43 202 my ($string, $base, $class_only) = @_;
44 202 ▶ return $string unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 4 198 202
45  
46 198 ▶ my $sigil = defined $string && $string =~ s/^(\+)// ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $string && $string =~ s/^(\+)// ? : 6 192 198
Condition66%2/3
defined $string and $string =~ s/^(\+)//
!ll&&!rl&&r
01926
MC/DC50%1/2
defined $string and $string =~ s/^(\+)//
defined $string $string =~ s/^(\+)//
Truth table66%2/3
A and B
A= defined $stringB= $string =~ s/^(\+)//
ABresult
0X0
100
111
47 198 ▶ $base = undef if $sigil;
Branch: 1 branch
BranchTrueFalseTotal
if $sigil 6 192 198
48  
49 198 my @parts;
50 198 ▶ if ($string !~ /#/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $string =~ /#/) { } 71 127 198
51  
52 # do not camelize if it doesn't look like a camelize string
53 71 @parts = ($string);
54 }
55 else {
56 127 @parts = split /\#/, $string;
57 127 my $sub = pop @parts;
58  
59 127 ▶ push @parts, $sub
Branch: 1 branch
BranchTrueFalseTotal
if $class_only 24 103 127
60 if $class_only;
61  
62 @parts = map {
63 127 join '', map { ucfirst lc } split /\_/
64 } @parts;
65  
66 127 ▶ push @parts, $sub
Branch: 1 branch
BranchTrueFalseTotal
unless $class_only 103 24 127
67 if !$class_only;
68 }
69  
70 198 ▶ unshift @parts, $base if $base;
Branch: 1 branch
BranchTrueFalseTotal
if $base 81 117 198
71 198 return join('::', @parts);
72 }
73  
74 sub extract_class
75 128 ▶{
Subroutine extract_class: called CC=4 SCAR=13.9
Pod documented
76 128 my ($string) = @_;
77 128 ▶ return undef unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 1 127 128
78  
79 127 ▶ if ($string =~ /^(.+)::(\w+)$/ && $1 ne 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^(.+)::(\w+)$/ and $1 ne "main") 109 18 127
Condition100%3/3
$string =~ /^(.+)::(\w+)$/ and $1 ne "main"
!ll&&!rl&&r
711109
MC/DC100%2/2
$string =~ /^(.+)::(\w+)$/ and $1 ne "main"
$string =~ /^(.+)::(\w+)$/ $1 ne "main"
Truth table100%3/3
A and B
A= $string =~ /^(.+)::(\w+)$/B= $1 ne "main"
ABresult
0X0
100
111
80 109 return $1;
81 }
82  
83 18 return undef;
84 }
85  
86 sub extract_function
87 108 ▶{
Subroutine extract_function: called CC=3 SCAR=11.0
Pod documented
88 108 my ($string) = @_;
89 108 ▶ return undef unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 1 107 108
90  
91 107 ▶ if ($string =~ /^(.+)::(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^(.+)::(\w+)$/) 106 1 107
92 106 return $2;
93 }
94  
95 1 return $string;
96 }
97  
98 sub effective_charset
99 308 ▶{
Subroutine effective_charset: called CC=2 SCAR=6.9
Pod documented
100 308 my $this_charset = shift;
101 308 ▶ return Encode::find_encoding($this_charset) ? $this_charset : undef;
Branch: 1 branch
BranchTrueFalseTotal
&Encode::find_encoding($this_charset) ? : 240 68 308
102 }
103  
104 sub charset_encode
105 246 ▶{
Subroutine charset_encode: called CC=2 SCAR=6.9
Pod undocumented
106 246 my ($charset, $string) = @_;
107  
108 246 ▶ return $string unless $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless $charset 1 245 246
109 245 return Encode::encode $charset, $string;
110 }
111  
112 sub charset_decode
113 628 ▶{
Subroutine charset_decode: called CC=2 SCAR=6.9
Pod undocumented
114 628 my ($charset, $string) = @_;
115  
116 628 ▶ return $string unless $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless $charset 1 627 628
117 627 return Encode::decode $charset, $string;
118 }
119  
120 sub adapt_psgi
121 5 ▶{
Subroutine adapt_psgi: called CC=2 SCAR=7.1
Pod documented
122 5 my ($app) = @_;
123  
124 5 ▶ croak 'Cannot adapt_psgi, unknown destination type - must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $app eq "CODE" 0 5 5
125 unless ref $app eq 'CODE';
126  
127 11 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=21.3
128 11 my $context = shift->context;
129 11 ▶ my $path = charset_encode($context->app->request_charset, pop() // '');
Condition100%2/2
pop() // ""
l!l
65
MC/DC100%1/1
pop() // ""
pop()
Truth table100%2/2
A
A= pop()
Aresult
11
00
130 11 my $env = $context->req->env;
131  
132 # remember script and path
133 11 my $orig_script = $env->{SCRIPT_NAME};
134 11 my $orig_path = $env->{PATH_INFO};
135  
136 # adjust slashes in paths
137 11 ▶ my $trailing_slash = $orig_path =~ m{/$} ? '/' : '';
Branch: 1 branch
BranchTrueFalseTotal
$orig_path =~ m[/$] ? : 4 7 11
138 11 $path =~ s{^/?}{/};
139 11 $path =~ s{/?$}{$trailing_slash};
140  
141 11 my $result = do {
142  
143 # adjust script and path
144 11 local $env->{SCRIPT_NAME} = $orig_path;
145 11 $env->{SCRIPT_NAME} =~ s{\Q$path\E$}{};
146 11 local $env->{PATH_INFO} = $path;
147  
148 # run the callback
149 11 $app->($env);
150 };
151  
152 # produce a response
153 11 ▶ if (ref $result eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $result eq 'ARRAY') { } 11 0 11
elsif (ref $result eq 'CODE') { } 0 0 0
154 11 my ($status, $headers, $body) = @{$result};
155  
156 11 my $res = $context->res;
157 11 ▶ $res->status($status) if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 11 0 11
158 11 ▶ $res->headers($headers) if $headers;
Branch: 1 branch
BranchTrueFalseTotal
if $headers 11 0 11
159 11 ▶ $res->body($body) if $body;
Branch: 1 branch
BranchTrueFalseTotal
if $body 11 0 11
160 11 $res->rendered(1);
161 }
162 elsif (ref $result eq 'CODE') {
163 0 return $result;
164 }
165  
166 # this should be an error unless already rendered
167 11 return;
168 5 };
169 }
170  
171 sub load_package
172 1825 ▶{
Subroutine load_package: called CC=3 SCAR=11.0
Pod documented
173 1825 my $package = shift;
174 1825 state $loaded = {};
175  
176 # only load package once for a given class name
177 1825 ▶ return $loaded->{$package} //= do {
Condition100%3/3
$loaded->{$package} //= do { Plack::Util::load_class($package) unless "Class::Inspector"->loaded($package); $package }
l!l&&r!l&&!r
14373871
MC/DC100%2/2
$loaded->{$package} //= do { Plack::Util::load_class($package) unless "Class::Inspector"->loaded($package); $package }
$loaded->{$package} do { Plack::Util::load_class($package) unless "Class::Inspector"->loaded($package); $package }
Truth table100%3/3
A //= B
A= $loaded->{$package}B= do { Plack::Util::load_class($package) unless "Class::Inspector"->loaded($package); $package }
ABresult
1X1
011
000
178 388 ▶ Plack::Util::load_class($package)
Branch: 1 branch
BranchTrueFalseTotal
unless "Class::Inspector"->loaded($package) 311 77 388
179 unless Class::Inspector->loaded($package);
180 385 $package;
181 };
182 }
183  
184 sub merge
185 166 ▶{
Subroutine merge: called CC=15 SCAR=27.5
Pod documented
186 166 my ($a, $b, $allow_blessed, $sigil) = @_;
187 0 ▶ my $ref = $allow_blessed ? sub { reftype $_[0] // '' } : sub { ref $_[0] };
Subroutine __ANON__: not called CC=2 SCAR=10.1
Subroutine __ANON__: called CC=1 SCAR=1.7
Branch: 1 branch
BranchTrueFalseTotal
$allow_blessed ? : 0 166 166
Condition0%0/2
builtin::reftype($_[0]) // ""
l!l
00
MC/DC0%0/1
builtin::reftype($_[0]) // ""
builtin::reftype($_[0])
Truth table0%0/2
A
A= builtin::reftype($_[0])
Aresult
11
00
188  
189 166 ▶ return $b
Branch: 1 branch
BranchTrueFalseTotal
if not &$ref($a) && &$ref($b) or &$ref($a) ne &$ref($b) 17 149 166
Condition83%5/6
not &$ref($a) && &$ref($b) or &$ref($a) ne &$ref($b)
l!l&&r!l&&!r
152149
&$ref($a) && &$ref($b)
!ll&&!rl&&r
150151
MC/DC66%2/3
not &$ref($a) && &$ref($b) or &$ref($a) ne &$ref($b)
&$ref($a) &$ref($b) &$ref($a) ne &$ref($b)
Truth table75%3/4
not(A && B) or C
A= &$ref($a)B= &$ref($b)C= &$ref($a) ne &$ref($b)
ABCresult
0XX1
10X1
1111
1100
190 if !$ref->($a)
191 || !$ref->($b)
192 || $ref->($a) ne $ref->($b);
193  
194 149 ▶ if ($ref->($a) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (&$ref($a) eq 'ARRAY') { } 47 102 149
elsif (&$ref($a) eq 'HASH') { } 102 0 102
195 47 ▶ return $b unless $sigil;
Branch: 1 branch
BranchTrueFalseTotal
unless $sigil 11 36 47
196 36 ▶ if ($sigil eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq '+') { } 5 31 36
197 5 for my $e (@$b) {
198 11 ▶ push @$a, $e unless grep { eq_deeply($_, $e) } @$a;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {eq_deeply($_, $e);} @$a 6 5 11
199 }
200 }
201 else {
202 $a = [
203 grep {
204 31 my $e = $_;
205 63 !grep { eq_deeply($_, $e) } @$b
206 } @$a
207 ];
208 }
209 36 return $a;
210 }
211 elsif ($ref->($a) eq 'HASH') {
212 102 for my $k (keys %$b) {
213  
214 # If the key is an array then look for a merge sigil
215 113 ▶ my $s = $ref->($b->{$k}) eq 'ARRAY' && $k =~ s/^(\+|\-)// ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
&$ref($b->{$k}) eq 'ARRAY' && $k =~ s/^(\+|\-)// ? : 36 77 113
Condition100%3/3
&$ref($b->{$k}) eq "ARRAY" and $k =~ s/^(\+|\-)//
!ll&&!rl&&r
641336
MC/DC100%2/2
&$ref($b->{$k}) eq "ARRAY" and $k =~ s/^(\+|\-)//
&$ref($b->{$k}) eq "ARRAY" $k =~ s/^(\+|\-)//
Truth table100%3/3
A and B
A= &$ref($b->{$k}) eq "ARRAY"B= $k =~ s/^(\+|\-)//
ABresult
0X0
100
111
216  
217 $a->{$k} =
218 exists $a->{$k}
219 ? merge($a->{$k}, $b->{"$s$k"}, $allow_blessed, $s)
220 113 ▶ : $b->{$k};
Branch: 1 branch
BranchTrueFalseTotal
exists $a->{$k} ? : 89 24 113
221 }
222  
223 102 return $a;
224 }
225 0 return $b;
226 }
227  
228 58 1;
229  
230 __END__
« blib/lib/Kelp/Routes.pm ↑ Summary blib/lib/Kelp/Request.pm »