blib/lib/String/BlackWhiteList.pm

filter: Statementstmt 97.7% 44 / 45 Branchbran 94.4% 17 / 18 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 97.5% 81 / 83 CC 14 SCAR 15.1
CC
14
Cov
97%
update
6.0
valid
5.0
valid_relaxed
5.0
CRAP 4.5
↑ Summary
1 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package String::BlackWhiteList;
6 3 our $VERSION = '1.100860';
7 # ABSTRACT: Match a string against a blacklist and a whitelist
8 3 ▶use parent 'Class::Accessor::Complex';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 __PACKAGE__
10 ->mk_new
11 ->mk_scalar_accessors(qw(black_re white_re))
12 ->mk_array_accessors(qw(blacklist whitelist))
13 ->mk_boolean_accessors(qw(is_literal_text));
14  
15 4 ▶sub update {
Subroutine update: called CC=6 SCAR=18.0
Pod documented
16 4 my $self = shift;
17 4 my @blacklist = $self->blacklist;
18 4 my @whitelist = $self->whitelist;
19 4 my %seen;
20 4 $seen{$_} = 1 for @blacklist;
21 4 for (@whitelist) {
22 100 ▶ next unless $seen{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$_} 100 0 100
23 0 warn "[$_] is both blacklisted and whitelisted\n";
24 }
25 4 my $is_literal_text = $self->is_literal_text; # caching
26 my $black_re =
27 join '|',
28 4 ▶ map { $is_literal_text ? '\b' . quotemeta($_) . '(\b|\s|$)' : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$is_literal_text ? : 42 42 84
29 $self->blacklist;
30 4 $self->black_re(qr/$black_re/i);
31 my $white_re =
32 join '|',
33 4 ▶ map { $is_literal_text ? '\b' . quotemeta($_) . '(\b|\s|$)' : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$is_literal_text ? : 50 50 100
34 $self->whitelist;
35 4 $self->white_re(qr/$white_re/i);
36 4 $self;
37 }
38  
39 93 ▶sub valid {
Subroutine valid: called CC=5 SCAR=16.1
Pod documented
40 93 my ($self, $s) = @_;
41 93 ▶ return 1 unless defined $s && length $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s 2 91 93
Condition100%3/3
defined $s and length $s
!ll&&!rl&&r
1191
MC/DC100%2/2
defined $s and length $s
defined $s length $s
Truth table100%3/3
A and B
A= defined $sB= length $s
ABresult
0X0
100
111
42 91 my $white_re = $self->white_re;
43 91 my $black_re = $self->black_re;
44 91 ▶ if ($s =~ s/$white_re//gi) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s =~ s/$white_re//gi) { } 51 40 91
elsif ($s =~ qr/$black_re/i) { } 32 8 40
45  
46 # If part of the string matches the whitelist, but another part of it
47 # matches the blacklist, it is still considered invalid. So we deleted
48 # the part that matched the whitelist and examine the remainder.
49 #
50 # This is necessary because the blacklist can be more general than the
51 # whitelist. For example, you are testing whether a street address
52 # string is a pobox, and you put 'Post' and 'P.O. BOX' in the
53 # blacklist, but put 'Post Street' in the blacklist. Now it's clear
54 # why we had to delete the part that matched the whitelist. Also
55 # consider the case of 'P.O. BOX 37, Post Street 9'.
56 51 return $s !~ qr/$black_re/i;
57 } elsif ($s =~ qr/$black_re/i) {
58 32 return 0;
59 } else {
60 8 return 1;
61 }
62 }
63  
64 87 ▶sub valid_relaxed {
Subroutine valid_relaxed: called CC=5 SCAR=16.1
Pod documented
65 87 my ($self, $s) = @_;
66 87 ▶ return 1 unless defined $s && length $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s 2 85 87
Condition100%3/3
defined $s and length $s
!ll&&!rl&&r
1185
MC/DC100%2/2
defined $s and length $s
defined $s length $s
Truth table100%3/3
A and B
A= defined $sB= length $s
ABresult
0X0
100
111
67 85 my $white_re = $self->white_re;
68 85 my $black_re = $self->black_re;
69 85 ▶ return 1 if $s =~ qr/$white_re/i;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ qr/$white_re/i 51 34 85
70 34 ▶ return 0 if $s =~ qr/$black_re/i;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ qr/$black_re/i 28 6 34
71 6 return 1;
72 }
73 3 1;
74  
75  
76 __END__
↑ Summary