Policy/Generic/External.pm

filter: Statementstmt 89.3% 109 / 122 Branchbran 79.3% 46 / 58 Conditioncond 59.3% 19 / 32 MC/DCmcdc 31.5% 6 / 19 Subroutinesub 88.8% 16 / 18 Podpod 60.0% 6 / 10 totaltotal 77.9% 202 / 259 CC 42 SCAR 21.4
CC
42
Cov
82%
retrieveFiles
17.1
_within
12.0
initialize_if_enabled
5.6
CRAP 8.5
« Policy/ValuesAndExpressions/RequireSlices.pm ↑ Summary Policy/References/RequireSigils.pm »
1 package Perl::Critic::Policy::Generic::External;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Perl::Critic::Utils qw/:severities/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 our $VERSION = '0.1.4';
10  
11 5 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
12 return (
13 {
14 5 name => 'filetsv',
15 description => 'File of violations (TSV).',
16 default_string => '',
17 behavior => 'string',
18 },
19 );
20 }
21  
22 3 ▶sub applies_to { return qw/PPI::Token/ }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
23 0 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: not called CC=1 SCAR=6.9
Pod documented
24 0 ▶sub default_themes { return qw// }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
25  
26 # Create a randomly-named cache inside the policy instance.
27 11 my $cachekey;
28 5 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=5 SCAR=17.3
Pod documented
29 5 my ($self,$config)=@_;
30 5 my %opt=(
31 tsv=>$config->get('filetsv'),
32 );
33 5 ▶ if(!grep {!!$_} values(%opt)) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {not !$_;} values %opt) 1 4 5
34 4 my $name='__GENERICEXTERNAL';
35 4 my $suffix=int(rand(1e9));
36 4 my $retry=3;
37 0 ▶ while($retry&&exists($$self{"$name$suffix"})) { $suffix=int(rand(1e9)); $retry-- }
Branch: 1 branch
BranchTrueFalseTotal
if ($retry and exists $$self{"$name$suffix"}) 0 4 4
Condition33%1/3
$retry and exists $$self{"$name$suffix"}
!ll&&!rl&&r
040
MC/DC0%0/2
$retry and exists $$self{"$name$suffix"}
$retry exists $$self{"$name$suffix"}
Truth table33%1/3
A and B
A= $retryB= exists $$self{"$name$suffix"}
ABresult
0X0
100
111
38 0 ▶ if(exists($$self{"$name$suffix"})) { warn 'Generic::External unable to build cache' }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$self{"$name$suffix"}) { } 0 4 4
39 4 else { $cachekey="$name$suffix" }
40 4 return 1;
41 }
42  
43 2 ▶sub cache {
Subroutine cache: called CC=2 SCAR=7.1
Pod undocumented
44 2 my ($self)=@_;
45 2 ▶ return %{$$self{$cachekey}//{}};
Condition50%1/2
$self->{$cachekey} // {}
l!l
20
MC/DC0%0/1
$self->{$cachekey} // {}
$self->{$cachekey}
Truth table50%1/2
A
A= $self->{$cachekey}
Aresult
11
00
46 }
47  
48 5 ▶sub retrieveFiles {
Subroutine retrieveFiles: called CC=14 SCAR=28.4
Pod undocumented
49 5 my ($self,$docfn)=@_;
50 5 $docfn=~s/^\.\///;
51 5 ▶ if($$self{_filetsv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_filetsv"}) 5 0 5
52 0 ▶ if($$self{files}&&!$$self{files}{$docfn}) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"files"} and not $self->{"files"}{$docfn}) 0 5 5
Condition33%1/3
$self->{"files"} and not $self->{"files"}{$docfn}
!ll&&!rl&&r
500
MC/DC0%0/2
$self->{"files"} and not $self->{"files"}{$docfn}
$self->{"files"} not $self->{"files"}{$docfn}
Truth table33%1/3
A and not(B)
A= $self->{"files"}B= not $self->{"files"}{$docfn}
ABresult
0X0
100
111
53 5 my @violations;
54 5 open(my $fh,'<',$$self{_filetsv});
55 5 ▶ while(<$fh>) { chomp;
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 28 5 33
56 28 ▶ if (/^# Description for (\w+::\w+):\s*(.*)$/) { $$self{$cachekey}{desc}{$1}=$2 }
Branch: 3 branches
BranchTrueFalseTotal
if (/^# Description for (\w+::\w+):\s*(.*)$/) { } 5 23 28
elsif (/^# Explanation for (\w+::\w+):\s*(.*)$/) { } 5 18 23
elsif (/.+\t.+\t.+\t.+\t.+/) { } 13 5 18
57 5 elsif(/^# Explanation for (\w+::\w+):\s*(.*)$/) { $$self{$cachekey}{expl}{$1}=$2 }
58 elsif(/.+\t.+\t.+\t.+\t.+/) {
59 13 my ($srcfn,@data)=split(/\t/,$_);
60 13 ▶ if($#data>=3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#data >= 3) 13 0 13
61 13 $data[2]=~s/[^A-Za-z0-9_]//g;
62 13 $data[3]=~s/[^A-Za-z0-9_]//g;
63 0 ▶ if ($data[2]=~/^[^A-Za-z]/) { print STDERR "Invalid category: $data[2]\n" }
Branch: 2 branches
BranchTrueFalseTotal
if ($data[2] =~ /^[^A-Za-z]/) { } 0 13 13
elsif ($data[3] =~ /^[^A-Za-z]/) { } 0 13 13
64 0 elsif($data[3]=~/^[^A-Za-z]/) { print STDERR "Invalid category: $data[3]\n" }
65 else {
66 # just prototyping, TODO use regexp or something else
67 13 ▶ if($srcfn eq $docfn) { push @violations,\@data }
Branch: 1 branch
BranchTrueFalseTotal
if ($srcfn eq $docfn) 5 8 13
68 13 $$self{$cachekey}{files}{$srcfn}=1;
69 }
70 }
71 }
72 }
73 5 close($fh);
74 5 @{$$self{$cachekey}{violations}}=sort {
75 5 ▶ ($$a[0] cmp $$b[0])
Condition33%2/6
$Perl::Critic::Policy::Generic::External::a->[0] cmp $Perl::Critic::Policy::Generic::External::b->[0] || $Perl::Critic::Policy::Generic::External::a->[1] <=> $Perl::Critic::Policy::Generic::External::b->[1] || $Perl::Critic::Policy::Generic::External::a->[2] <=> $Perl::Critic::Policy::Generic::External::b->[2]
l!l&&r!l&&!r
100
$Perl::Critic::Policy::Generic::External::a->[0] cmp $Perl::Critic::Policy::Generic::External::b->[0] or $Perl::Critic::Policy::Generic::External::a->[1] <=> $Perl::Critic::Policy::Generic::External::b->[1]
l!l&&r!l&&!r
010
MC/DC0%0/3
$Perl::Critic::Policy::Generic::External::a->[0] cmp $Perl::Critic::Policy::Generic::External::b->[0] || $Perl::Critic::Policy::Generic::External::a->[1] <=> $Perl::Critic::Policy::Generic::External::b->[1] || $Perl::Critic::Policy::Generic::External::a->[2] <=> $Perl::Critic::Policy::Generic::External::b->[2]
$Perl::Critic::Policy::Generic::External::a->[0] cmp $Perl::Critic::Policy::Generic::External::b->[0] $Perl::Critic::Policy::Generic::External::a->[1] <=> $Perl::Critic::Policy::Generic::External::b->[1] $Perl::Critic::Policy::Generic::External::a->[2] <=> $Perl::Critic::Policy::Generic::External::b->[2]
Truth table25%1/4
A or B || C
A= $Perl::Critic::Policy::Generic::External::a->[0] cmp $Perl::Critic::Policy::Generic::External::b->[0]B= $Perl::Critic::Policy::Generic::External::a->[1] <=> $Perl::Critic::Policy::Generic::External::b->[1]C= $Perl::Critic::Policy::Generic::External::a->[2] <=> $Perl::Critic::Policy::Generic::External::b->[2]
ABCresult
1XX1
01X1
0011
0000
76 || ($$a[1]<=>$$b[1])
77 || ($$a[2]<=>$$b[2])
78 } @violations;
79 }
80 5 return;
81 }
82  
83 3 ▶sub prepare_to_scan_document {
Subroutine prepare_to_scan_document: called CC=2 SCAR=7.4
Pod documented
84 3 my ($self,$doc)=@_;
85 0 ▶ if(!$cachekey) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 0 3 3
86 3 $self->retrieveFiles($doc->filename());
87 3 return 1;
88 }
89  
90 3 ▶sub _withPolicy {
Subroutine _withPolicy: called CC=5 SCAR=16.5
91 3 my ($self,$cat,$pol)=@_;
92 0 ▶ if(!$cat) { return $self }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cat) 0 3 3
93 3 ▶ $pol||='External';
Condition50%1/2
$pol ||= "External"
l!l
30
MC/DC0%0/1
$pol ||= "External"
$pol
Truth table50%1/2
A
A= $pol
Aresult
11
00
94 3 my $policy="Perl::Critic::Policy::${cat}::$pol";
95 3 my $path=($policy=~s/::/\//gr);
96 3 ▶ if(!$INC{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{$path}) 2 1 3
97 2 my $eval="package $policy".q|{
98 our @ISA='Perl::Critic::Policy';
99 sub dispatch { my ($self,@arg)=@_; return $self->violation(@arg) }
100 };|;
101 # print STDERR "Building $policy\n";
102 2 ▶ eval($eval);
Subroutine dispatch: called CC=1 SCAR=0.0
Subroutine dispatch: called CC=1 SCAR=0.0
103 0 ▶ if($@) { die "Failed to create dynamic $policy" }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
104 2 $INC{$path}='/dev/null';
105 }
106 3 return $policy->new();
107 }
108  
109 3 ▶sub invalid {
Subroutine invalid: called CC=5 SCAR=17.0
Pod undocumented
110 3 my ($self,$elem,undef,undef,$cat,$pol,$note)=@_;
111 3 ▶ $note//='';
Condition50%1/2
$note //= ""
l!l
03
MC/DC0%0/1
$note //= ""
$note
Truth table50%1/2
A
A= $note
Aresult
11
00
112 0 ▶ if($note) { $note=" ($note)" }
Branch: 1 branch
BranchTrueFalseTotal
if ($note) 0 3 3
113 3 my $policy=$self->_withPolicy($cat,$pol);
114 3 ▶ $policy->dispatch($$self{$cachekey}{desc}{"${cat}::$pol"}//$pol,$$self{$cachekey}{expl}{"${cat}::$pol"}//'',$elem);
Condition80%4/5
$self->{$cachekey}{"desc"}{"${cat}::$pol"} // $pol
l!l&&r!l&&!r
210
$self->{$cachekey}{"expl"}{"${cat}::$pol"} // ""
l!l
21
MC/DC0%0/2
$self->{$cachekey}{"desc"}{"${cat}::$pol"} // $pol
$self->{$cachekey}{"desc"}{"${cat}::$pol"} $pol
MC/DC100%1/1
$self->{$cachekey}{"expl"}{"${cat}::$pol"} // ""
$self->{$cachekey}{"expl"}{"${cat}::$pol"}
Truth table66%2/3
A // B
A= $self->{$cachekey}{"desc"}{"${cat}::$pol"}B= $pol
ABresult
1X1
011
000
Truth table100%2/2
A
A= $self->{$cachekey}{"expl"}{"${cat}::$pol"}
Aresult
11
00
115 }
116  
117 # Assume that $line,$col inputs are ordered.
118 # If a $this shows up past ($line,$col), the violation was missed and should emit.
119 27 ▶sub _within {
Subroutine _within: called CC=12 SCAR=24.9
120 27 my ($line,$col,$this)=@_;
121 27 my $next=$this->next_token();
122 27 my ($rb,$cb)=(@{$next->location()})[0,1];
123 27 ▶ if($rb<$line) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($rb < $line) 12 15 27
124 15 my ($ra,$ca)=(@{$this->location()})[0,1];
125 15 ▶ if(($rb>$ra)&&($cb==1)) { $cb=0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rb > $ra and $cb == 1) 1 14 15
Condition100%3/3
$rb > $ra and $cb == 1
!ll&&!rl&&r
771
MC/DC100%2/2
$rb > $ra and $cb == 1
$rb > $ra $cb == 1
Truth table100%3/3
A and B
A= $rb > $raB= $cb == 1
ABresult
0X0
100
111
126 15 ▶ if($ra>$line) { return 1 }
Branch: 4 branches
BranchTrueFalseTotal
if ($ra > $line) { } 1 14 15
elsif ($ra == $line) { } 9 5 14
elsif ($rb > $line) { } 1 4 5
elsif ($col < $cb) { } 1 3 4
127 elsif($ra==$line) {
128 9 ▶ if($rb>$line) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rb > $line) 2 7 9
129 7 ▶ if($col<$ca) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($col < $ca) 2 5 7
130 5 ▶ if(($ca<=$col)&&($col<$cb)) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ca <= $col and $col < $cb) 3 2 5
Condition66%2/3
$ca <= $col and $col < $cb
!ll&&!rl&&r
023
MC/DC50%1/2
$ca <= $col and $col < $cb
$ca <= $col $col < $cb
Truth table66%2/3
A and B
A= $ca <= $colB= $col < $cb
ABresult
0X0
100
111
131 2 return;
132 }
133 1 elsif($rb>$line) { return 1 } # ra<line
134 1 elsif($col<$cb) { return 1 } # ra<line, rb=line
135 3 else { return }
136 }
137  
138 47 ▶sub violates {
Subroutine violates: called CC=4 SCAR=14.0
Pod documented
139 47 my ($self,$elem,$doc)=@_;
140 0 ▶ if(!$cachekey) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 0 47 47
141 # print ' loc:',join(':',map{$_//'u'} @{$elem->location()}),"\n";
142 47 my $V=$$self{$cachekey}{violations};
143 47 ▶ while(@$V&&_within($$V[0][0],$$V[0][1],$elem)) { return $self->invalid($elem,@{shift @$V}) }
Branch: 1 branch
BranchTrueFalseTotal
if (@$V and _within($V->[0][0], $V->[0][1], $elem)) 3 44 47
Condition100%3/3
@$V and _within($V->[0][0], $V->[0][1], $elem)
!ll&&!rl&&r
30143
MC/DC100%2/2
@$V and _within($V->[0][0], $V->[0][1], $elem)
@$V _within($V->[0][0], $V->[0][1], $elem)
Truth table100%3/3
A and B
A= @$VB= _within($V->[0][0], $V->[0][1], $elem)
ABresult
0X0
100
111
144 44 return;
145 }
146  
147 11 1;
148  
149 __END__
« Policy/ValuesAndExpressions/RequireSlices.pm ↑ Summary Policy/References/RequireSigils.pm »