| 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
| |||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$retry and exists $$self{"$name$suffix"}
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"}
| |||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if(exists($$self{"$name$suffix"})) { warn 'Generic::External unable to build cache' } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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} // {}
MC/DC0%0/1
$self->{$cachekey} // {}
$self->{$cachekey}
Truth table50%1/2
A
A= $self->{$cachekey}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if($$self{files}&&!$$self{files}{$docfn}) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"files"} and not $self->{"files"}{$docfn}
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}
| |||||||||||||||||||||||||||||||||||
| 53 | 5 | my @violations; | |||||||||||||||||||||||||||||||||
| 54 | 5 | open(my $fh,'<',$$self{_filetsv}); | |||||||||||||||||||||||||||||||||
| 55 | 5 | ▶ | while(<$fh>) { chomp; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 56 | 28 | ▶ | if (/^# Description for (\w+::\w+):\s*(.*)$/) { $$self{$cachekey}{desc}{$1}=$2 } | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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]
$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]
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]
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 93 | 3 | ▶ | $pol||='External'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$pol ||= "External"
MC/DC0%0/1
$pol ||= "External"
$pol
Truth table50%1/2
A
A= $pol
| |||||||||||||||||||||||||||||||||||
| 94 | 3 | my $policy="Perl::Critic::Policy::${cat}::$pol"; | |||||||||||||||||||||||||||||||||
| 95 | 3 | my $path=($policy=~s/::/\//gr); | |||||||||||||||||||||||||||||||||
| 96 | 3 | ▶ | if(!$INC{$path}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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 //= ""
MC/DC0%0/1
$note //= ""
$note
Truth table50%1/2
A
A= $note
| |||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if($note) { $note=" ($note)" } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
$self->{$cachekey}{"expl"}{"${cat}::$pol"} // ""
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
Truth table100%2/2
A
A= $self->{$cachekey}{"expl"}{"${cat}::$pol"}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 124 | 15 | my ($ra,$ca)=(@{$this->location()})[0,1]; | |||||||||||||||||||||||||||||||||
| 125 | 15 | ▶ | if(($rb>$ra)&&($cb==1)) { $cb=0 } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rb > $ra and $cb == 1
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
| |||||||||||||||||||||||||||||||||||
| 126 | 15 | ▶ | if($ra>$line) { return 1 } | ||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||
| 127 | elsif($ra==$line) { | ||||||||||||||||||||||||||||||||||
| 128 | 9 | ▶ | if($rb>$line) { return 1 } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 129 | 7 | ▶ | if($col<$ca) { return 1 } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 130 | 5 | ▶ | if(($ca<=$col)&&($col<$cb)) { return 1 } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ca <= $col and $col < $cb
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@$V and _within($V->[0][0], $V->[0][1], $elem)
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)
| |||||||||||||||||||||||||||||||||||
| 144 | 44 | return; | |||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||
| 147 | 11 | 1; | |||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||||||||