blib/lib/Plack/Builder/Conditionals.pm

filter: Statementstmt 95.1% 98 / 103 Branchbran 100% 30 / 30 Conditioncond 76.1% 16 / 21 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 88.4% 23 / 26 Podpod 100% 8 / 8 totaltotal 91.0% 184 / 202 CC 25 SCAR 13.6
CC
25
Cov
94%
__ANON__
8.1
method
7.0
addr
5.0
CRAP 3.9
↑ Summary
1 package Plack::Builder::Conditionals;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Net::CIDR::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use List::MoreUtils qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Plack::Middleware::Conditional;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.05';
11  
12 4 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
13 4 my $class = shift;
14 4 my $caller = caller;
15 4 my %args = @_;
16  
17 4 my @EXPORT = qw/match_if addr path method header browser any all/;
18  
19 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 my $i=0;
21 4 for my $sub (@EXPORT) {
22 32 ▶ my $sub_name = $args{'-prefix'} ? $args{'-prefix'} . '_' . $sub : $sub;
Branch: 1 branch
BranchTrueFalseTotal
$args{'-prefix'} ? : 8 24 32
23 32 *{"$caller\::$sub_name"} = \&$sub;
24 }
25 }
26  
27 3 ▶sub match_if {
Subroutine match_if: called CC=2 SCAR=6.9
Pod documented
28 3 my $condition = shift;
29 3 my ( $mw, @args ) = @_;
30
31 3 ▶ if (ref $mw ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mw ne "CODE") 1 2 3
32 1 my $mw_class = Plack::Util::load_class($mw, 'Plack::Middleware');
33 1 ▶ $mw = sub { $mw_class->wrap($_[0], @args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
34 }
35  
36 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 3 Plack::Middleware::Conditional->wrap(
38 $_[0],
39 condition => $condition,
40 builder => $mw
41 );
42 }
43 3 }
44  
45 6 ▶sub addr {
Subroutine addr: called CC=5 SCAR=16.1
Pod documented
46 6 my $not;
47 my $ip;
48 6 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 5 1 6
49 5 $ip = $_[0];
50 }
51 else {
52 1 $not = $_[0];
53 1 $ip = $_[1];
54 }
55  
56 6 ▶ my @ip = ref $ip ? @$ip : ($ip);
Branch: 1 branch
BranchTrueFalseTotal
ref $ip ? : 4 2 6
57 6 my $cidr4 = Net::CIDR::Lite->new();
58 6 my $cidr6 = Net::CIDR::Lite->new();
59 6 for my $ip ( @ip ) {
60 9 ▶ if ( $ip =~ m!:! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /:/) { } 3 6 9
61 3 $cidr6->add_any($ip);
62 }
63 else {
64 6 $cidr4->add_any($ip);
65 }
66 }
67  
68 6 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
69 6 my $env = shift;
70 6 my $find_ip;
71 6 ▶ if ( $env->{REMOTE_ADDR} =~ m!:! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{'REMOTE_ADDR'} =~ /:/) { } 2 4 6
72 2 $find_ip = $cidr6->find($env->{REMOTE_ADDR});
73 }
74 else {
75 4 $find_ip = $cidr4->find($env->{REMOTE_ADDR});
76 }
77 6 ▶ return (defined $not && $not eq '!') ? !$find_ip : $find_ip;
Branch: 1 branch
BranchTrueFalseTotal
defined $not && $not eq '!' ? : 1 5 6
Condition66%2/3
defined $not and $not eq "!"
!ll&&!rl&&r
501
MC/DC50%1/2
defined $not and $not eq "!"
defined $not $not eq "!"
Truth table66%2/3
A and B
A= defined $notB= $not eq "!"
ABresult
0X0
100
111
78 6 };
79 }
80  
81 23 ▶sub _match {
Subroutine _match: called CC=2 SCAR=6.9
82 23 my $key = shift;
83 23 my $not;
84 my $val;
85 23 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 14 9 23
86 14 $val = $_[0];
87 }
88 else {
89 9 $not = $_[0];
90 9 $val = $_[1];
91 }
92  
93 29 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=20.9
94 29 my $env = shift;
95 29 my $ret;
96 29 ▶ if ( ref $val && ref $val eq 'Regexp' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val and ref $val eq 'Regexp') { } 9 20 29
elsif (defined $val) { } 18 2 20
Condition66%2/3
ref $val and ref $val eq "Regexp"
!ll&&!rl&&r
2009
MC/DC50%1/2
ref $val and ref $val eq "Regexp"
ref $val ref $val eq "Regexp"
Truth table66%2/3
A and B
A= ref $valB= ref $val eq "Regexp"
ABresult
0X0
100
111
97 9 ▶ $ret = exists $env->{$key} && $env->{$key} =~ m!$val!;
Condition66%2/3
exists $env->{$key} && $env->{$key} =~ /$val/
!ll&&!rl&&r
045
MC/DC50%1/2
exists $env->{$key} && $env->{$key} =~ /$val/
exists $env->{$key} $env->{$key} =~ /$val/
Truth table66%2/3
A && B
A= exists $env->{$key}B= $env->{$key} =~ /$val/
ABresult
0X0
100
111
98 }
99 elsif ( defined $val ) {
100 18 ▶ $ret = exists $env->{$key} && $env->{$key} eq $val;
Condition100%3/3
exists $env->{$key} && $env->{$key} eq $val
!ll&&!rl&&r
297
MC/DC100%2/2
exists $env->{$key} && $env->{$key} eq $val
exists $env->{$key} $env->{$key} eq $val
Truth table100%3/3
A && B
A= exists $env->{$key}B= $env->{$key} eq $val
ABresult
0X0
100
111
101 }
102 else {
103 2 $ret = exists $env->{$key};
104 }
105 29 ▶ return ( defined $not && $not eq '!' ) ? !$ret : $ret;
Branch: 1 branch
BranchTrueFalseTotal
defined $not && $not eq '!' ? : 7 22 29
Condition66%2/3
defined $not and $not eq "!"
!ll&&!rl&&r
2207
MC/DC50%1/2
defined $not and $not eq "!"
defined $not $not eq "!"
Truth table66%2/3
A and B
A= defined $notB= $not eq "!"
ABresult
0X0
100
111
106 23 };
107 }
108  
109 8 ▶sub path {
Subroutine path: called CC=1 SCAR=0.0
Pod documented
110 8 _match( 'PATH_INFO', @_ );
111 }
112  
113 9 ▶sub method {
Subroutine method: called CC=7 SCAR=19.5
Pod documented
114 9 my $not;
115 my $method;
116 9 ▶ if ( defined $_[0] && $_[0] eq '!' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] eq "!") 4 5 9
Condition100%3/3
defined $_[0] and $_[0] eq "!"
!ll&&!rl&&r
144
MC/DC100%2/2
defined $_[0] and $_[0] eq "!"
defined $_[0] $_[0] eq "!"
Truth table100%3/3
A and B
A= defined $_[0]B= $_[0] eq "!"
ABresult
0X0
100
111
117 4 $not = shift;
118 }
119 9 ▶ return sub { 1 } unless @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 8 9
120 8 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 6 2 8
121 6 $method = $_[0];
122 }
123 else {
124 2 my $alternatives = join '|', map { quotemeta($_) } @_;
125 2 $method = qr/^(?:$alternatives)$/i;
126 }
127 8 ▶ if ( defined $method && ! ref $method ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $method and not ref $method) 4 4 8
Condition66%2/3
defined $method and not ref $method
!ll&&!rl&&r
044
MC/DC50%1/2
defined $method and not ref $method
defined $method not ref $method
Truth table66%2/3
A and not(B)
A= defined $methodB= not ref $method
ABresult
0X0
100
111
128 4 $method = uc $method;
129 }
130 8 _match( 'REQUEST_METHOD', grep { defined } $not, $method );
131 }
132  
133 5 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
Pod documented
134 5 my $header = shift;
135 5 $header =~ s/-/_/g;
136 5 $header = 'HTTP_' . uc($header);
137 5 _match( $header, @_ );
138 }
139  
140 2 ▶sub browser {
Subroutine browser: called CC=1 SCAR=0.0
Pod documented
141 2 _match( "HTTP_USER_AGENT", @_ );
142 }
143  
144 0 ▶sub any {
Subroutine any: not called CC=1 SCAR=6.9
Pod documented
145 0 my @match = @_;
146 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 my $env = shift;
148 0 ▶ List::MoreUtils::any { $_->($env) } @match;
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 0 };
150 }
151  
152 1 ▶sub all {
Subroutine all: called CC=1 SCAR=0.0
Pod documented
153 1 my @match = @_;
154 4 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
155 4 my $env = shift;
156 4 ▶ List::MoreUtils::all { $_->($env) } @match;
Subroutine __ANON__: called CC=1 SCAR=0.0
157 1 };
158 }
159  
160  
161 4 1;
162 __END__
↑ Summary