Scan/Matches.pm

filter: Statementstmt 32.2% 30 / 93 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 9 / 15 Podpod 0.0% 0 / 3 totaltotal 30.2% 39 / 129 CC 14 SCAR 26.1
CC
14
Cov
27%
__txt
30.0
__html
30.0
add
12.0
CRAP 13.6
« Scan/Ruleset.pm ↑ Summary Scan/Cache.pm »
1 =head1 NAME
2  
3 File::OSS::Scan::Matches - store scan result about file matches
4  
5 =head1 VERSION
6  
7 version 0.04
8  
9 =head1 SYNOPSIS
10  
11 use File::OSS::Scan::Matches;
12  
13 File::OSS::Scan::Matches->init();
14  
15 File::OSS::Scan::Matches->add(
16 {
17 'name' => $h_file->{'name'},
18 'path' => $h_file->{'path'},
19 'size' => $h_file->{'size'},
20 'mtime' => $h_file->{'mtime'},
21 },
22 $function_name,
23 $certainty_level,
24 join(' ', @$args),
25 $message
26 );
27  
28 my $matches = File::OSS::Scan::Matches->get_matches();
29  
30 =head1 DESCRIPTION
31  
32 This is an internal module used by L<File::OSS::Scan> to store scan results,
33 and should not be called directly.
34  
35 =head1 SEE ALSO
36  
37 =over 4
38  
39 =item * L<File::OSS::Scan>
40  
41 =back
42  
43 =head1 AUTHOR
44  
45 Harry Wang <harry.wang@outlook.com>
46  
47 =head1 COPYRIGHT AND LICENSE
48  
49 This software is Copyright (c) 2014 by Harry Wang.
50  
51 This is free software, licensed under:
52  
53 Artistic License 1.0
54  
55 =cut
56  
57 package File::OSS::Scan::Matches;
58  
59 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 1 ▶use Fatal qw( open close );
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use Data::Dumper; # for debug
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 1 ▶use File::OSS::Scan::Constant qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 1 our $VERSION = '0.04';
71  
72 # global var ...
73 1 our $matches = undef;
74  
75 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
76 0 my $self = shift;
77 0 undef $matches;
78  
79 0 return SUCCESS;
80 }
81  
82 0 ▶sub add {
Subroutine add: not called CC=3 SCAR=24.8
Pod undocumented
83 0 my $self = shift;
84  
85 0 ▶ my ( $h_file, $func, $cert, $args, $msg )
Branch: 1 branch
BranchTrueFalseTotal
my($h_file, $func, $cert, $args, $msg) = or return 0 0 0 0
86 = @_ or return SUCCESS;
87  
88 0 my $key = $h_file->{'path'};
89 my $new_h_file = {
90 'name' => $h_file->{'name'},
91 'size' => $h_file->{'size'},
92 0 'mtime' => $h_file->{'mtime'},
93 };
94  
95 $matches->{$key} = $new_h_file
96 0 ▶ if ( not exists $matches->{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $File::OSS::Scan::Matches::matches->{$key} 0 0 0
97  
98 0 push @{$matches->{$key}->{'matches'}}, {
99 'func' => $func,
100 'cert' => $cert,
101 'args' => $args,
102 'msg' => $msg,
103 };
104  
105 0 return SUCCESS;
106 }
107  
108 0 ▶sub get_matches {
Subroutine get_matches: not called CC=3 SCAR=20.3
Pod undocumented
109 0 my $var = $_[0] . "::matches";
110 0 my $fmt = $_[1];
111  
112 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 0 ▶ if ( not defined $fmt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fmt) { } 0 0 0
115 0 return $$var;
116 }
117 else {
118 0 my $func = __PACKAGE__ . "::__" . $fmt;
119  
120 0 ▶ if ( defined(&$func) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$func) { } 0 0 0
121 0 return &$func($$var);
122 }
123 else {
124 0 croak "can't find function $func in " . __PACKAGE__;
125 }
126 }
127 }
128  
129 0 ▶sub __txt {
Subroutine __txt: not called CC=5 SCAR=34.0
130 0 my $result = shift;
131 0 my $ret = undef;
132  
133 0 ▶ if ( defined $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) 0 0 0
134 0 $ret = '';
135 0 my $num = 1;
136  
137 0 foreach my $path ( sort keys %$result ) {
138 my ( $matches, $mtime, $name, $size )
139 0 = @{$result->{$path}}{
140 0 qw/matches mtime name size/
141 };
142  
143 0 my $mtime_stamp = localtime($mtime);
144 0 $ret .= sprintf( "%-16s %-50s\n", "Matches \#\($num\):", $path );
145 0 $ret .= sprintf( "%-16s %-20s %-10s %-20s\n", "",
146 "name:$name", "size:$size", "mtime:$mtime_stamp" );
147  
148 0 ▶ if ( defined $matches ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $matches) 0 0 0
149 0 my $num_of_match = 1;
150  
151 0 foreach my $match ( @$matches ) {
152 $ret .= sprintf("%-20s %-5s %-5s %-20s %-20s\n",
153 "", "\<$num_of_match\>\.", $match->{'cert'} . '%',
154 0 $match->{'func'}, $match->{'args'} );
155  
156 0 $ret .= " " x 24 . "$match->{'msg'}\n";
157 0 $num_of_match++;
158 }
159 }
160  
161 0 $num++;
162 }
163 }
164  
165 0 return $ret;
166 }
167  
168 # mainly for the body content of mail
169 0 ▶sub __html {
Subroutine __html: not called CC=5 SCAR=34.0
170 0 my $result = shift;
171 0 my $ret = undef;
172  
173 0 ▶ if ( defined $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) 0 0 0
174 0 my $num = 1;
175 0 $ret = '';
176  
177 0 foreach my $path ( sort keys %$result ) {
178 my ( $matches, $mtime, $name, $size )
179 0 = @{$result->{$path}}{
180 0 qw/matches mtime name size/
181 };
182  
183 0 my $mtime_stamp = localtime($mtime);
184 0 $ret .= "<table id='scan_result_table' cellspacing='0'>\n";
185 0 $ret .= "<tr><th width='700px'>(#$num) $path</th><th width='50px'>$size</th><th width='250px'>$mtime_stamp</th></tr>\n";
186  
187 0 ▶ if ( defined $matches ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $matches) 0 0 0
188 0 my $num_of_match = 1;
189  
190 0 foreach my $match ( @$matches ) {
191 0 $ret .= "<tr><table>\n" .
192 "<tr><td width='50px'>\<$num_of_match\></td>\n" .
193 "<td width='50px'>$match->{'cert'}\%</td>\n" .
194 "<td width='200px'>$match->{'func'}</td>\n" .
195 "<td width='300px'>$match->{'args'}</td>\n" .
196 "<td width='400px'>$match->{'msg'}</td>\n" .
197 "</tr></table></tr>\n";
198  
199 0 $num_of_match++;
200 }
201 }
202  
203 0 $num++;
204 }
205  
206 0 $ret .= "</table>\n";
207  
208 0 my $css = join(q(), <DATA>);
209 0 $ret = $css . "\n" . $ret;
210 }
211  
212 0 return $ret;
213 }
214  
215 0 ▶sub __json {
Subroutine __json: not called CC=2 SCAR=17.9
216 0 my $result = shift;
217 0 my $ret = undef;
218  
219 0 ▶ $ret = JSON::to_json( $result, { pretty => 1 } )
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 0 0 0
220 if ( defined $result );
221  
222 0 return $ret;
223 }
224  
225  
226  
227 1 1;
228  
229  
230 __DATA__
« Scan/Ruleset.pm ↑ Summary Scan/Cache.pm »