Salus/Table.pm

filter: Statementstmt 55.8% 153 / 274 Branchbran 32.3% 22 / 68 Conditioncond 30.0% 6 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 54.0% 20 / 37 Podpod n/a totaltotal 48.7% 201 / 412 CC 53 SCAR 23.6
CC
53
Cov
50%
__ANON__
30.0
__ANON__
20.0
__ANON__
20.0
CRAP 10.6
↑ Summary Salus/Row.pm »
1 package Salus::Table;
2 3 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Rope;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Rope::Autoload;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Text::CSV_XS qw/csv/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Types::Standard qw/Str ArrayRef Bool/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Salus::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Salus::Row::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Digest::SHA qw/hmac_sha256_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Text::Diff qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 property file => (
13 initable => 1,
14 writeable => 0,
15 configurable => 1,
16 enumerable => 1,
17 type => Str,
18 );
19  
20 3 property secret => (
21 initable => 1,
22 writeable => 0,
23 configurable => 1,
24 enumerable => 1,
25 type => Str,
26 );
27  
28 3 property unprotected_read => (
29 initable => 1,
30 writeable => 0,
31 configurable => 1,
32 enumerable => 1,
33 type => Bool,
34 value => 0
35 );
36  
37 3 property headers => (
38 initable => 1,
39 writeable => 0,
40 configurable => 1,
41 required => 1,
42 enumerable => 1,
43 type => ArrayRef,
44 value => []
45 );
46  
47 3 property rows => (
48 initable => 1,
49 writeable => 0,
50 configurable => 1,
51 enumerable => 1,
52 type => ArrayRef,
53 value => [],
54 );
55  
56 6 ▶private hmac => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
57 6 my ($self, $data) = @_;
58 6 ▶ return hmac_sha256_hex($data, $self->secret ? $self->secret : ());
Branch: 1 branch
BranchTrueFalseTotal
$self->secret ? : 0 6 6
59 3 };
60  
61 4 ▶function count => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 4 return scalar @{$_[0]->rows};
63 3 };
64  
65 1 ▶function read => sub {
Subroutine __ANON__: called CC=15 SCAR=28.7
66 1 my ($self, $file, $read) = @_;
67 1 ▶ $file ||= $self->file;
Condition33%1/3
$file ||= $self->file
l!l&&r!l&&!r
010
MC/DC0%0/2
$file ||= $self->file
$file $self->file
Truth table33%1/3
A ||= B
A= $fileB= $self->file
ABresult
1X1
011
000
68 1 my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 });
69 1 ▶ open my $fh, "<:encoding(utf8)", $file or die "${file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf8)", $file or die "${file}: $!" 1 0 1
70 1 ▶ if (!$self->unprotected_read) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->unprotected_read) 1 0 1
71 1 my $data = do { local $/; <$fh> };
72 1 seek $fh, 0, 0;
73 1 ▶ my $match = $data =~ s/\n+(.*)$// && $1;
Condition33%1/3
$data =~ s/\n+(.*)$// && $1
!ll&&!rl&&r
001
MC/DC0%0/2
$data =~ s/\n+(.*)$// && $1
$data =~ s/\n+(.*)$// $1
Truth table33%1/3
A && B
A= $data =~ s/\n+(.*)$//B= $1
ABresult
0X0
100
111
74 1 ▶ if ($self->hmac($data) !~ m/^$match$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->hmac($data) =~ /^$match$/) 0 1 1
75 0 die "HMAC does not match for file ${file}";
76 }
77 }
78 1 my ($line, $salus, @rows) = (0, 0);
79 1 ▶ while (my $columns = $csv->getline($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $columns = $csv->getline($fh)) 4 0 4
80 4 ▶ if (0 == $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $line) 1 3 4
81 1 ▶ $salus = 1 if ($columns->[-1] eq 'SALUS');
Branch: 1 branch
BranchTrueFalseTotal
if $columns->[-1] eq "SALUS" 1 0 1
82 1 $line++;
83 1 next;
84 }
85 3 ▶ last if scalar @{$columns} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$columns;} == 1 1 2 3
86 2 my @cols;
87 2 ▶ if ($salus) {
Branch: 1 branch
BranchTrueFalseTotal
if ($salus) 2 0 2
88 2 $salus = pop @{$columns};
89 2 ▶ $csv->combine(@{$columns}) or die 'kaput' . $!;
Branch: 1 branch
BranchTrueFalseTotal
$csv->combine(@{$columns;}) or die 'kaput' . $! 2 0 2
90 2 ▶ if ($self->hmac($csv->string()) !~ m/^$salus$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->hmac($csv->string) =~ /^$salus$/) 0 2 2
91 0 die "HMAC does not match for row ${line} in file ${file}";
92 }
93 }
94 2 ▶ for (my $i = 0; $i < scalar @{$columns}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$columns;}) 8 2 10
95 8 push @cols, Salus::Row::Column->new({
96 header => $self->headers->[$i],
97 value => $columns->[$i]
98 });
99 }
100 2 push @rows, Salus::Row->new(
101 columns => \@cols
102 );
103 2 $line++;
104 }
105 1 close $fh;
106  
107 1 ▶ return \@rows if $read;
Branch: 1 branch
BranchTrueFalseTotal
if $read 0 1 1
108  
109 1 $self->rows = \@rows;
110 3 };
111  
112 0 ▶function combine => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
113 0 my ($self, $file, $primary) = @_;
114
115 0 my $rows = $self->read($file, 1);
116  
117 ROW:
118 0 for my $row (@{$rows}) {
119 0 for my $r (@{$self->rows}) {
120 0 ▶ if ($r->get_col($primary)->value =~ $row->get_col($primary)->value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->get_col($primary)->value =~ $row->get_col($primary)->value) 0 0 0
121 0 for (@{$row->columns}) {
122 0 $r->get_col($_->header->index)->value = $_->value;
123 }
124 0 next ROW;
125 }
126 }
127 0 push @{$self->rows}, $row;
128 }
129 3 };
130  
131 1 ▶function write => sub {
Subroutine __ANON__: called CC=7 SCAR=19.8
132 1 my ($self, $file) = @_;
133 1 ▶ $file ||= $self->file;
Condition33%1/3
$file ||= $self->file
l!l&&r!l&&!r
100
MC/DC0%0/2
$file ||= $self->file
$file $self->file
Truth table33%1/3
A ||= B
A= $fileB= $self->file
ABresult
1X1
011
000
134 1 my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 });
135 1 ▶ open my $fh, ">:encoding(utf8)", $file or die "${file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(utf8)", $file or die "${file}: $!" 1 0 1
136 my @headers = map {
137 4 ▶ $_->label || $_->name
Condition33%1/3
$_->label || $_->name
l!l&&r!l&&!r
400
MC/DC0%0/2
$_->label || $_->name
$_->label $_->name
Truth table33%1/3
A || B
A= $_->labelB= $_->name
ABresult
1X1
011
000
138 1 } @{$self->headers};
139 1 push @headers, 'SALUS';
140 1 $csv->say($fh, \@headers);
141 1 for my $row (@{$self->rows}) {
142 2 my $row_array = $row->as_array;
143 2 $csv->combine(@{$row_array});
144 2 my $row_hmac = $self->hmac($csv->string());
145 2 push @{$row_array}, $row_hmac;
146 2 $csv->say($fh, $row_array);
147 }
148 1 close $fh;
149 1 ▶ open $fh, "<:encoding(utf8)", $file or die "${file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<:encoding(utf8)", $file or die "${file}: $!" 1 0 1
150 1 my $data = do { local $/; <$fh> };
151 1 close $fh;
152 1 my $file_hmac = $self->hmac($data);
153 1 ▶ open my $gfh, ">>:encoding(utf8)", $file or die "${file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $gfh, ">>:encoding(utf8)", $file or die "${file}: $!" 1 0 1
154 1 seek $gfh, 0, 2;
155 1 print $gfh $file_hmac . "\n";
156 1 close $gfh;
157 3 };
158  
159 1 ▶function add_row => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
160 1 my ($self, $columns) = @_;
161 1 my @cols;
162 1 ▶ for (my $i = 0; $i < scalar @{$columns}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$columns;}) 4 1 5
163 4 push @cols, Salus::Row::Column->new({
164 header => $self->headers->[$i],
165 value => $columns->[$i]
166 });
167 }
168 1 push @{$self->rows}, Salus::Row->new(
169 columns => \@cols
170 );
171 3 };
172  
173 0 ▶function add_rows => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
174 0 my ($self, $rows) = @_;
175 0 for my $columns (@{$rows}) {
176 0 my @cols;
177 0 ▶ for (my $i = 0; $i < scalar @{$columns}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$columns;}) 0 0 0
178 0 push @cols, Salus::Row::Column->new({
179 header => $self->headers->[$i],
180 value => $columns->[$i]
181 });
182 }
183 0 push @{$self->rows}, Salus::Row->new(
184 columns => \@cols
185 );
186 }
187 3 };
188  
189 1 ▶function add_row_hash => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
190 1 ▶ my ($self, $columns) = (shift, ref $_[0] ? $_[0] : {@_});
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 0 1
191 1 my @cols;
192 1 for my $header (@{$self->headers}) {
193 push @cols, Salus::Row::Column->new({
194 header => $header,
195 4 ▶ value => $columns->{$header->label} || $columns->{$header->name}
Condition66%2/3
$$columns{$header->label} || $$columns{$header->name}
l!l&&r!l&&!r
130
MC/DC0%0/2
$$columns{$header->label} || $$columns{$header->name}
$$columns{$header->label} $$columns{$header->name}
Truth table66%2/3
A || B
A= $$columns{$header->label}B= $$columns{$header->name}
ABresult
1X1
011
000
196 });
197 }
198 1 push @{$self->rows}, Salus::Row->new(
199 columns => \@cols
200 );
201 3 };
202  
203 12 ▶function get_row => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
204 12 my ($self, $row) = @_;
205 12 return $self->rows->[$row];
206 3 };
207  
208 10 ▶function get_row_col => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
209 10 my ($self, $row, $col) = @_;
210 10 $self->get_row($row)->get_col($col);
211 3 };
212  
213 0 ▶function set_row => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
214 0 my ($self, $row, $cols) = @_;
215 0 $row = $self->get_row($row);
216 0 ▶ for (my $i = 0; $i < scalar @{$cols}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$cols;}) 0 0 0
217 0 ▶ $row->set_col($i, $cols->[$i] // "");
Condition0%0/2
$cols->[$i] // ""
l!l
00
MC/DC0%0/1
$cols->[$i] // ""
$cols->[$i]
Truth table0%0/2
A
A= $cols->[$i]
Aresult
11
00
218 }
219 3 };
220  
221 2 ▶function set_row_col => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
222 2 my ($self, $row, $col, $value) = @_;
223 2 $self->get_row($row)->set_col($col, $value);
224 3 };
225  
226 2 ▶function delete_row => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
227 2 my ($self, $row) = @_;
228 2 splice @{ $self->rows }, $row, 1;
229 3 };
230  
231 0 ▶function delete_row_col => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
232 0 my ($self, $row, $col) = @_;
233 0 $self->get_row($row)->delete_col($col);
234 3 };
235  
236 0 ▶function sort => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
237 0 my ($self, $col, $order, $return) = @_;
238 0 $col = $self->find_column_index($col);
239 my @rows = $order eq 'asc'
240 0 ? sort { $a->get_col($col)->value cmp $b->get_col($col)->value } @{$self->rows}
241 0 ▶ : sort { $b->get_col($col)->value cmp $a->get_col($col)->value } @{$self->rows};
Branch: 1 branch
BranchTrueFalseTotal
$order eq 'asc' ? : 0 0 0
242 0 ▶ $self->rows = \@rows unless $return;
Branch: 1 branch
BranchTrueFalseTotal
unless $return 0 0 0
243 0 return \@rows;
244 3 };
245  
246 0 ▶function search => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
247 0 my ($self, $col, $search) = @_;
248 0 $col = $self->find_column_index($col);
249 0 my ($i, @indexes) = (0);
250 my @rows = grep {
251 0 ▶ if ( $_->get_col($col)->value =~ m/$search/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->get_col($col)->value =~ /$search/i) 0 0 0
252 0 push @indexes, $i++;
253 0 return $_;
254 }
255 0 $i++;
256 0 return ();
257 0 } @{$self->rows};
258 0 return (\@rows, \@indexes);
259 3 };
260  
261 0 ▶function find => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
262 0 my ($self, $col, $search) = @_;
263 0 $col = $self->find_column_index($col);
264 0 my ($i, $found) = (0, undef);
265 0 for ( @{$self->rows} ) {
266 0 ▶ if ($_->get_col($col)->value =~ m/$search/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->get_col($col)->value =~ /$search/i) 0 0 0
267 0 $found = $i;
268 0 last;
269 }
270 0 $i++;
271 }
272 0 return $found;
273 3 };
274  
275 0 ▶function find_column_index => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
276 0 my ($self, $col) = @_;
277 0 ▶ if ($col !~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($col =~ /^\d+$/) 0 0 0
278 0 for (@{$self->headers}) {
279 0 ▶ if ($_->name =~ m/^($col)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->name =~ /^($col)$/) 0 0 0
280 0 $col = $_->index;
281 }
282 }
283 }
284 0 return $col;
285 3 };
286  
287 0 ▶function sum => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
288 0 my ($self, $col) = @_;
289 0 $col = $self->find_column_index($col);
290 0 my $sum = 0;
291 0 for (@{$self->rows}) {
292 0 my $c = $_->get_col($col);
293 0 ▶ if ($c->value !~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->value =~ /^\d+$/) 0 0 0
294 0 die "Cannot sum column as it has non numeric values";
295 }
296 0 $sum += $c->value;
297 }
298 0 return $sum;
299 3 };
300  
301 0 ▶function mean => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
302 0 my ($self, $col) = @_;
303 0 my $sum = $self->sum($col);
304 0 return $sum / scalar @{$self->rows};
305 3 };
306  
307 0 ▶function median => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
308 0 my ($self, $col, $as_row) = @_;
309 0 $col = $self->find_column_index($col);
310 0 my $rows = $self->sort($col, 'asc', 1);
311 0 my $median = int(scalar @{$rows} / 2);
312 0 ▶ if ($median % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($median % 2 != 0) 0 0 0
313 0 $median += 1;
314 }
315 0 ▶ return $as_row ? $rows->[$median - 1] : $rows->[$median - 1]->get_col($col)->value;
Branch: 1 branch
BranchTrueFalseTotal
$as_row ? : 0 0 0
316 3 };
317  
318 0 ▶function mode => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
319 0 my ($self, $col) = @_;
320 0 $col = $self->find_column_index($col);
321 0 my %map;
322 0 $map{$_->get_col($col)->value}++ for (@{$self->rows});
323 0 my ($key, $mode) = ('', 0);
324 0 for my $k (keys %map) {
325 0 ▶ if ($map{$k} > $mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map{$k} > $mode) 0 0 0
326 0 $key = $k;
327 0 $mode = $map{$k};
328 }
329 }
330 0 return ($key, $mode);
331 3 };
332  
333 0 ▶function min => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
334 0 my ($self, $col, $as_row) = @_;
335 0 $col = $self->find_column_index($col);
336 0 my $rows = $self->sort($col, 'asc', 1);
337 0 ▶ return $as_row ? $rows->[0] : $rows->[0]->get_col($col)->value;
Branch: 1 branch
BranchTrueFalseTotal
$as_row ? : 0 0 0
338 3 };
339  
340 0 ▶function max => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
341 0 my ($self, $col, $as_row) = @_;
342 0 $col = $self->find_column_index($col);
343 0 my $rows = $self->sort($col, 'desc', 1);
344 0 ▶ return $as_row ? $rows->[0] : $rows->[0]->get_col($col)->value;
Branch: 1 branch
BranchTrueFalseTotal
$as_row ? : 0 0 0
345 3 };
346  
347 0 ▶function headers_as_array => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
348 0 my ($self) = @_;
349 my @array = map {
350 $_->{label} || $_->{name}
351 0 ▶ } @{$self->headers};
Condition0%0/3
$_->{'label'} || $_->{'name'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{'label'} || $_->{'name'}
$_->{'label'} $_->{'name'}
Truth table0%0/3
A || B
A= $_->{'label'}B= $_->{'name'}
ABresult
1X1
011
000
352 0 return \@array;
353 3 };
354  
355 0 ▶function headers_stringify => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
356 0 my ($self) = @_;
357 my @array = map {
358 $_->{label} ? sprintf("%s (%s) (%s)", $_->{label}, $_->{name}, $_->{index}) : sptrintf("%s (%s)", $_->{name}, $_->{index})
359 0 ▶ } @{$self->headers};
Branch: 1 branch
BranchTrueFalseTotal
$_->{'label'} ? : 0 0 0
360 0 return \@array;
361 3 };
362  
363 0 ▶function diff_files => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
364 0 my ($self, $file1, $file2) = @_;
365 0 return Text::Diff::diff $file1, $file2, { STYLE => "Context" };
366 3 };
367  
368 3 1;
369  
370 __END__
↑ Summary Salus/Row.pm »