blib/lib/Plack/Middleware/Redirect.pm

filter: Statementstmt 37.7% 20 / 53 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 66.6% 6 / 9 Podpod 100% 2 / 2 totaltotal 31.1% 28 / 90 CC 12 SCAR 36.0
CC
12
Cov
26%
call
72.0
prepare_app
30.0
_fetch_url_pattern
2.0
CRAP 36.7
↑ Summary
1 package Plack::Middleware::Redirect;
2 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = "0.01";
10  
11 1 ▶use Plack::Util::Accessor qw/url_patterns/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 my $BODY = {
14 301 => "Moved Permanently",
15 302 => "Found"
16 };
17  
18 0 ▶sub prepare_app {
Subroutine prepare_app: not called CC=5 SCAR=34.0
Pod documented
19 0 my $self = shift;
20 0 my $url_patterns = $self->url_patterns;
21  
22 0 ▶ for (my $i = 0; $i < scalar(@$url_patterns); $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$url_patterns) 0 0 0
23 0 my ($from, $to) = _fetch_url_pattern($url_patterns, $i);
24
25 0 my $type = ref $from;
26 0 ▶ if ($type ne 'Regexp' && $type ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "Regexp" and $type ne "") 0 0 0
Condition0%0/3
$type ne "Regexp" and $type ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$type ne "Regexp" and $type ne ""
$type ne "Regexp" $type ne ""
Truth table0%0/3
A and B
A= $type ne "Regexp"B= $type ne ""
ABresult
0X0
100
111
27 0 croak "$from is invalid parameter";
28 }
29 0 ▶ if ($type ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "Regexp") 0 0 0
30 0 $url_patterns->[$i] = qr/$from/;
31 }
32 }
33  
34 }
35  
36 0 ▶sub call {
Subroutine call: not called CC=8 SCAR=42.8
Pod documented
37 0 my ($self, $env) = @_;
38  
39 0 my $url_patterns = $self->url_patterns;
40  
41 0 my $path = $env->{PATH_INFO};
42 0 my $body = "";
43  
44 0 ▶ for ( my $i = 0; $i < scalar(@$url_patterns); $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$url_patterns) 0 0 0
45 0 my ($from, $to) = _fetch_url_pattern($url_patterns, $i);
46  
47 0 ▶ next unless $path =~ m#$from#;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /$from/ 0 0 0
48
49 0 my $type = ref $to;
50  
51 0 ▶ if ($type ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "ARRAY") 0 0 0
52 0 my $to = [$to, 301];
53 }
54  
55 0 my ($to_path, $status_code) = @$to;
56 0 $type = ref $to_path;
57 0 ▶ if ($type eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'CODE') { } 0 0 0
58 0 $path = &$to_path($env, $from);
59 }
60 else {
61 0 $path =~ s#$from#"\"$to_path\""#iee;
62 }
63 0 my $query = $env->{QUERY_STRING};
64 0 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) 0 0 0
65 0 ▶ $path .= $path =~ /\?/
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /\?/ ? : 0 0 0
66 ? '&' . $query
67 : '?' . $query;
68 }
69 0 ▶ my $body = $BODY->{$status_code} || "";
Condition0%0/2
$BODY->{$status_code} || ''
l!l
00
MC/DC0%0/1
$BODY->{$status_code} || ''
$BODY->{$status_code}
Truth table0%0/2
A
A= $BODY->{$status_code}
Aresult
11
00
70 0 return [$status_code, ['Location' => $path], [$body] ];
71 }
72  
73 0 return $self->app->($env);
74 }
75  
76  
77 0 ▶sub _fetch_url_pattern {
Subroutine _fetch_url_pattern: not called CC=1 SCAR=6.9
78 0 my ($url_patterns, $i) = @_;
79 0 return ($url_patterns->[$i], $url_patterns->[$i+1]);
80 }
81  
82 1 1;
83 __END__
↑ Summary