blib/lib/Email/Filter/Rules.pm

filter: Statementstmt 100% 45 / 45 Branchbran 95.4% 21 / 22 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 98.8% 87 / 88 CC 18 SCAR 21.9
CC
18
Cov
99%
new
12.0
apply_rules
7.0
BEGIN
1.0
CRAP 8.9
↑ Summary
1 package Email::Filter::Rules;
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 3 ▶use constant RULE_SPLIT_LIMIT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = 1.2;
7  
8 7 ▶sub new {
Subroutine new: called CC=12 SCAR=24.9
Pod documented
9 7 my ( $class, %args ) = @_;
10 7 ▶ return unless $args{rules};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"rules"} 1 6 7
11 6 my @rules = ();
12  
13 # added the \n check because I got a warning if a did a -e test on
14 # a scalar containing newlines
15 6 ▶ if ( $args{rules} !~ /\n/s && -e $args{rules} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $args{'rules'} =~ /\n/s and -e $args{'rules'}) { } 1 5 6
elsif (ref $args{'rules'} eq 'ARRAY') { } 4 1 5
Condition100%3/3
not $args{"rules"} =~ /\n/s and -e $args{"rules"}
!ll&&!rl&&r
141
MC/DC100%2/2
not $args{"rules"} =~ /\n/s and -e $args{"rules"}
not $args{"rules"} =~ /\n/s -e $args{"rules"}
Truth table100%3/3
not(A) and B
A= not $args{"rules"} =~ /\n/sB= -e $args{"rules"}
ABresult
0X0
100
111
16 1 ▶ open my $fh, '<', $args{rules} or die "Can't open file $args{rules}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $args{"rules"} or die qq[Can't open file $args{"rules"}: $!] 1 0 1
17 1 @rules = <$fh>;
18 1 close $fh;
19 }
20 elsif ( ref $args{rules} eq 'ARRAY' ) {
21 4 @rules = @{ $args{rules} };
22 }
23 else {
24 1 @rules = split /\n/, $args{rules};
25 }
26 6 ▶ return unless scalar @rules;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @rules 1 5 6
27  
28 5 my @rule_data = ();
29  
30 5 for my $line (@rules) {
31 51 chomp($line);
32 51 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 3 48 51
33 48 my ( $folder, $methods, $substring ) = split( /\s+/, $line, RULE_SPLIT_LIMIT );
34 48 ▶ next unless $methods && $folder && $substring;
Branch: 1 branch
BranchTrueFalseTotal
unless $methods and $folder and $substring 20 28 48
Condition100%6/6
$methods and $folder and $substring
!ll&&!rl&&r
17328
$methods and $folder
!ll&&!rl&&r
12531
MC/DC100%3/3
$methods and $folder and $substring
$methods $folder $substring
Truth table100%4/4
A and B and C
A= $methodsB= $folderC= $substring
ABCresult
0XX0
10X0
1100
1111
35 28 my $escaped_substring = quotemeta($substring);
36 28 my @methods = split( /\:/, $methods );
37 28 push @rule_data, {
38 methods => \@methods,
39 substring => $escaped_substring,
40 folder => $folder,
41 };
42 }
43  
44 my $obj = {
45 rule_data => \@rule_data,
46 debug => $args{debug},
47 5 };
48  
49 5 return bless $obj, $class;
50 }
51  
52 5 ▶sub apply_rules {
Subroutine apply_rules: called CC=7 SCAR=19.5
Pod documented
53 5 my ( $self, $email ) = @_;
54 5 for my $rule ( @{ $self->{rule_data} } ) {
55 9 for my $method ( @{ $rule->{methods} } ) {
56 9 my $substring = $rule->{substring};
57 9 ▶ next unless $email->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $email->can($method) 2 7 9
58 7 my $testing = $email->$method;
59 7 ▶ next unless $testing;
Branch: 1 branch
BranchTrueFalseTotal
unless $testing 2 5 7
60 warn "testing $method \"$testing\" with $substring\n"
61 5 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 1 4 5
62  
63 # going to keep the 'i' now, may pass this in somehow
64 5 ▶ return $rule->{folder} if $testing =~ /$substring/is;
Branch: 1 branch
BranchTrueFalseTotal
if $testing =~ /$substring/is 3 2 5
65 }
66 }
67 2 return;
68 }
69  
70 3 1;
71  
72 __END__
↑ Summary