CriticDB/DB.pm

filter: Statementstmt 66.9% 81 / 121 Branchbran 46.8% 15 / 32 Conditioncond 37.5% 6 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 72.2% 13 / 18 Podpod 0.0% 0 / 8 totaltotal 56.6% 115 / 203 CC 28 SCAR 22.9
CC
28
Cov
60%
report
42.0
newer
12.0
_violation
7.3
CRAP 9.9
« CriticDB/Collector.pm ↑ Summary CriticDB/DB/Stor.pm »
1 package App::CriticDB::DB;
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 3 ▶use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Perl::Critic::Violation;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use App::CriticDB::DB::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION='0.0.6';
11  
12 3 my %engines=(
13 storable=>'App::CriticDB::DB::Stor',
14 );
15  
16 4 ▶sub new {
Subroutine new: called CC=6 SCAR=19.5
Pod undocumented
17 4 my (undef,%opt)=@_;
18 4 ▶ $opt{mode}//='not provided';
Condition50%1/2
$opt{"mode"} //= "not provided"
l!l
40
MC/DC0%0/1
$opt{"mode"} //= "not provided"
$opt{"mode"}
Truth table50%1/2
A
A= $opt{"mode"}
Aresult
11
00
19 4 my ($class,%self);
20 4 ▶ if($opt{mode} eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'mode'} eq 'file') { } 4 0 4
21 0 ▶ if(!$opt{file}) { confess('File storage requires filename') }
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"file"}) 0 4 4
22 4 ▶ $opt{type}//='storable';
Condition50%1/2
$opt{"type"} //= "storable"
l!l
40
MC/DC0%0/1
$opt{"type"} //= "storable"
$opt{"type"}
Truth table50%1/2
A
A= $opt{"type"}
Aresult
11
00
23 4 %self=map {$_=>$opt{$_}} qw/mode file type/;
24 4 ▶ $class=$engines{$opt{type}}//$engines{storable};
Condition33%1/3
$engines{$opt{"type"}} // $engines{"storable"}
l!l&&r!l&&!r
400
MC/DC0%0/2
$engines{$opt{"type"}} // $engines{"storable"}
$engines{$opt{"type"}} $engines{"storable"}
Truth table33%1/3
A // B
A= $engines{$opt{"type"}}B= $engines{"storable"}
ABresult
1X1
011
000
25 4 eval "require $class";
26 }
27 0 else { confess("Storage type not available: $opt{mode}") }
28 4 my $self=bless(\%self,$class);
29 4 $self->read();
30 4 return $self;
31 }
32  
33 4 ▶sub _initStore {
Subroutine _initStore: called CC=1 SCAR=0.0
34 4 my ($self)=@_;
35 return (
36 4 version=>1001,
37 index=>{
38 policy=>App::CriticDB::DB::Index->new(values=>'id',prefix=>'p:'),
39 'policy-file'=>App::CriticDB::DB::Index->new(values=>'set'),
40 },
41 file=>{},
42 );
43 }
44  
45 3 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
46 3 my ($self)=@_;
47 3 %{$$self{store}}=$self->_initStore();
48 3 return $self->write();
49 }
50  
51 8 ▶sub _filemtime {
Subroutine _filemtime: called CC=2 SCAR=6.9
52 8 my ($fn)=@_;
53 8 ▶ if(!-e $fn) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $fn) 5 3 8
54 3 return (stat($fn))[9];
55 }
56  
57 7 ▶sub _fileNewer {
Subroutine _fileNewer: called CC=3 SCAR=11.2
58 7 my ($self,$fn,$ts)=@_;
59 7 ▶ if(!$ts) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ts) 4 3 7
60 3 my $tm=_filemtime($fn);
61 0 ▶ if(!$tm) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($tm) 0 3 3
62 3 return ($tm>$ts);
63 }
64  
65 9 ▶sub _violation {
Subroutine _violation: called CC=7 SCAR=19.9
66 9 my ($self,$fn,$V)=@_;
67 9 my %res;
68 9 my %remap=(
69 '_description' =>'desc',
70 '_explanation' =>'expl',
71 '_policy' =>'policy',
72 '_severity' =>'sev',
73 '_source' =>'code',
74 );
75 9 ▶ if('Perl::Critic::Violation' eq ref($V)) {
Branch: 2 branches
BranchTrueFalseTotal
if ('Perl::Critic::Violation' eq ref $V) { } 0 9 9
elsif ('HASH' eq ref $V) { } 9 0 9
76 %res=(
77 0 (map {$remap{$_}=>$$V{$_}} keys(%remap)),
78 line =>$V->line_number(),
79 col =>$V->column_number(),
80 )
81 }
82 9 elsif('HASH' eq ref($V)) { %res=%$V }
83 0 else { confess('Invalid type of violation') }
84 9 $res{file}=$fn; # added for IndexSet construction
85 9 foreach my $ka (keys %res) {
86 24 ▶ if(defined($$self{store}{index}{$ka})) { $res{$ka}=$$self{store}{index}{$ka}->upsert($res{$ka}) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"store"}{"index"}{$ka}) 9 15 24
87 24 foreach my $kb (grep {$_ ne $ka} keys %res) {
88 72 ▶ if($$self{store}{index}{"$ka-$kb"}) { $$self{store}{index}{"$ka-$kb"}->add(@res{$ka,$kb}) }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"store"}{"index"}{"$ka-$kb"}) 9 63 72
89 }
90 }
91 9 delete($res{file}); # not needed in the stored violation
92 9 return %res;
93 }
94  
95 5 ▶sub store {
Subroutine store: called CC=5 SCAR=17.8
Pod undocumented
96 5 my ($self,%opt)=@_;
97 0 ▶ if(!$opt{file}) { return $self }
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"file"}) 0 5 5
98 5 ▶ my @violations=map {+{$self->_violation($opt{file},$_)}} @{$opt{violations}//[]};
Condition50%1/2
$opt{"violations"} // []
l!l
50
MC/DC0%0/1
$opt{"violations"} // []
$opt{"violations"}
Truth table50%1/2
A
A= $opt{"violations"}
Aresult
11
00
99 5 $$self{store}{file}{$opt{file}}{violations}=\@violations;
100 5 ▶ $$self{store}{file}{$opt{file}}{mtime}=_filemtime($opt{file})//$opt{mtime}//time();
Condition40%2/5
(_filemtime($opt{"file"}) // $opt{"mtime"}) // time
l!l
05
_filemtime($opt{"file"}) // $opt{"mtime"}
l!l&&r!l&&!r
005
MC/DC0%0/2
(_filemtime($opt{"file"}) // $opt{"mtime"}) // time
_filemtime($opt{"file"}) $opt{"mtime"}
Truth table33%1/3
A // B
A= _filemtime($opt{"file"})B= $opt{"mtime"}
ABresult
1X1
011
000
101 5 return $self;
102 }
103  
104 0 ▶sub flush {
Subroutine flush: not called CC=1 SCAR=6.9
Pod undocumented
105 0 my ($self,$fn)=@_;
106 0 return $self->write(fn=>$fn);
107 }
108  
109 0 ▶sub newer {
Subroutine newer: not called CC=3 SCAR=24.8
Pod undocumented
110 0 my ($self,$fn)=@_;
111 0 ▶ if(!$$self{store}{file}{$fn}) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"store"}{"file"}{$fn}) 0 0 0
112 0 ▶ return $self->_fileNewer($fn,$$self{store}{file}{$fn}{mtime}//0);
Condition0%0/2
$self->{"store"}{"file"}{$fn}{"mtime"} // 0
l!l
00
MC/DC0%0/1
$self->{"store"}{"file"}{$fn}{"mtime"} // 0
$self->{"store"}{"file"}{$fn}{"mtime"}
Truth table0%0/2
A
A= $self->{"store"}{"file"}{$fn}{"mtime"}
Aresult
11
00
113 }
114  
115 1 ▶sub cleanup {
Subroutine cleanup: called CC=3 SCAR=11.0
Pod undocumented
116 1 my ($self)=@_;
117 1 ▶ if(my @remove=grep {!-e $_} keys %{$$self{store}{file}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@remove) = grep({not -e $_;} keys %{$$self{"store"}{"file"};})) 1 0 1
118 1 foreach my $idx (values %{$$self{store}{index}}) { $idx->remove(@remove) }
119 1 delete(@{$$self{store}{file}}{@remove});
120 }
121 1 return $self;
122 }
123  
124 0 ▶sub read { confess('Unimplemented abstract') }
Subroutine read: not called CC=1 SCAR=6.9
Pod undocumented
125 0 ▶sub write { confess('Unimplemented abstract') }
Subroutine write: not called CC=1 SCAR=6.9
Pod undocumented
126  
127 3 my @reportIterator;
128 0 ▶sub report {
Subroutine report: not called CC=6 SCAR=37.4
Pod undocumented
129 0 my ($self)=@_;
130 0 ▶ if(!@reportIterator) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@reportIterator) 0 0 0
131 0 @reportIterator=map {[$_]} sort keys %{$$self{store}{file}};
132 0 push @reportIterator,[]; # terminate the iterator
133 }
134 0 ▶ while(@reportIterator) {
Branch: 1 branch
BranchTrueFalseTotal
if (@reportIterator) 0 0 0
135 0 my ($fn,$violation)=@{shift(@reportIterator)};
136 0 ▶ if($violation) { return $violation }
Branch: 1 branch
BranchTrueFalseTotal
if ($violation) 0 0 0
137 0 ▶ if(!$fn) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($fn) 0 0 0
138 0 my @violations;
139 0 foreach my $V (@{$$self{store}{file}{$fn}{violations}}) {
140 0 my $violation=Perl::Critic::Violation->new(@$V{qw/desc expl/},bless({},'PPI::Statement'),$$V{sev});
141 0 $$violation{_filename}=$fn;
142 0 $$violation{_source}=$$V{code};
143 0 $$violation{_policy}=$$self{store}{index}{policy}->value($$V{policy});
144 0 @{$$violation{_location}}[2..4]=(@$V{qw/col line/},$fn);
145 0 push @violations,[$fn,$violation];
146 }
147 0 unshift(@reportIterator,@violations);
148 }
149 0 return;
150 }
151  
152 3 1;
153  
154 __END__
« CriticDB/Collector.pm ↑ Summary CriticDB/DB/Stor.pm »