BoolFindGrep/CLI.pm

filter: Statementstmt 13.1% 24 / 182 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 33.3% 6 / 18 Podpod 100% 2 / 2 totaltotal 11.0% 32 / 289 CC 55 SCAR 43.7
CC
55
Cov
9%
process
156.0
args_checker
156.0
_files_from_checker
72.0
CRAP 78.7
↑ Summary BoolFindGrep/Grep.pm »
1 package App::BoolFindGrep::CLI;
2  
3 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use charnames q(:full);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use English qw[-no_match_vars];
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use App::BoolFindGrep;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.06'; # VERSION
10  
11 0 ▶has args => ( is => q(rw), default => sub { {}; }, );
Subroutine __ANON__: not called CC=1 SCAR=1.2
12 0 ▶has result => ( is => q(rw), default => sub { []; }, );
Subroutine __ANON__: not called CC=1 SCAR=1.2
13 1 has files_with_matches => ( is => q(rw) );
14  
15 0 ▶sub process {
Subroutine process: not called CC=12 SCAR=50.5
Pod documented
16 0 my $self = shift;
17  
18 0 my $controller = App::BoolFindGrep->new( $self->args() );
19 0 local $EVAL_ERROR;
20 0 eval { $controller->process(); };
21 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::BoolFindGrep::CLI::EVAL_ERROR) 0 0 0
22 0 ▶ if ($EVAL_ERROR =~ #
Branch: 1 branch
BranchTrueFalseTotal
if ($App::BoolFindGrep::CLI::EVAL_ERROR =~ /\A(?<msg>.*?) \sat\s (?<module>.*?) \sline\s (?<line>\d+)\.\s*\z /msux) 0 0 0
23 m{\A(?<msg>.*?)
24 \sat\s
25 (?<module>.*?)
26 \sline\s
27 (?<line>\d+)\.\s*\z
28 }msx
29 )
30 {
31 say STDERR sprintf q(%s: %s), $PROGRAM_NAME,
32 0 $LAST_PAREN_MATCH{msg};
33 }
34 0 return;
35 } ## end if ($EVAL_ERROR)
36  
37 0 ▶ if ( defined $self->args->{match_expr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->args->{'match_expr'}) { } 0 0 0
38 0 my ( %result, $num_max );
39 0 foreach my $file ( sort @{ $controller->greped_files() } ) {
40 0 ▶ next unless exists $controller->grep->content_found->{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $controller->grep->content_found->{$file} 0 0 0
41 0 foreach my $number (
42 0 keys %{ $controller->grep->content_found->{$file} } )
43 {
44 0 ▶ if ( !( defined $num_max ) || $number > $num_max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $num_max or $number > $num_max) 0 0 0
Condition0%0/3
not defined $num_max or $number > $num_max
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $num_max or $number > $num_max
not defined $num_max $number > $num_max
Truth table0%0/3
not(A) or B
A= not defined $num_maxB= $number > $num_max
ABresult
1X1
011
000
45 0 $num_max = $number;
46 }
47 $result{$file}{$number}
48 0 = $controller->grep->content_found->{$file}{$number};
49 }
50 }
51 0 foreach my $file ( sort keys %result ) {
52 0 ▶ if ( $self->files_with_matches() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->files_with_matches) 0 0 0
53 0 push @{ $self->result() }, $file;
54 0 next;
55 }
56 0 foreach my $number ( sort { $a <=> $b } keys %{ $result{$file} } )
57 {
58 0 my $string = $result{$file}{$number};
59 0 my $result = sprintf q(%s:%.*d:%s), #
60 $file, length $num_max, $number, $string;
61 0 push @{ $self->result() }, $result;
62 }
63 }
64  
65 } ## end if ( defined $self->args...)
66 0 else { @{ $self->result() } = @{ $controller->found_files() }; }
67  
68 0 @{ $self->result() } = sort @{ $self->result() };
69  
70 0 return 1;
71 } ## end sub process
72  
73 0 ▶sub args_checker {
Subroutine args_checker: not called CC=12 SCAR=50.5
Pod documented
74 0 my $self = shift;
75 0 my %arg = splice @_;
76  
77 0 $self->_args_translator(%arg);
78  
79 0 my %rule = (
80 q(are mutually exclusive.) => [
81 [ qw[file_expr files_from], ],
82 [ qw[line_regexp word_regexp], ],
83 [ qw[directory files_from], ],
84 [ qw[fixed_strings glob_regexp], ],
85 ],
86 q(an empty value was given.) =>
87 [qw[file_delim file_expr files_from match_expr directory]],
88 q(implies) => [
89 { find_type => [q(file_expr)] },
90 { find_ignore_case => [q(file_expr)] },
91 { files_delim => [q(files_from)] },
92 { ignore_case => [q(match_expr)] },
93 { line_regexp => [q(match_expr)] },
94 { word_regexp => [q(match_expr)] },
95 { files_with_matches => [q(match_expr)] },
96 ],
97 );
98  
99 0 foreach my $msg ( keys %rule ) {
100 0 ▶ if ( $msg eq q(are mutually exclusive.) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($msg eq 'are mutually exclusive.') { } 0 0 0
elsif ($msg eq 'an empty value was given.') { } 0 0 0
elsif ($msg eq 'implies') { } 0 0 0
101 return
102 unless $self->_mutually_exclusive_checker( $msg,
103 0 ▶ $rule{$msg} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mutually_exclusive_checker($msg, $rule{$msg}) 0 0 0
104 }
105 elsif ( $msg eq q(an empty value was given.) ) {
106 0 ▶ return unless $self->_empty_value_checker( $msg, $rule{$msg} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_empty_value_checker($msg, $rule{$msg}) 0 0 0
107 }
108 elsif ( $msg eq q(implies) ) {
109 0 ▶ return unless $self->_implies_checker( $msg, $rule{$msg} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_implies_checker($msg, $rule{$msg}) 0 0 0
110 }
111 0 else { die; }
112 }
113  
114 0 foreach my $parameter ( keys %{ $self->args() } ) {
115 0 my $value = $self->args->{$parameter};
116 0 my $checker = sprintf q(_%s_checker), $parameter;
117 0 ▶ if ( $parameter eq q(files_with_matches) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parameter eq 'files_with_matches') { } 0 0 0
elsif ($self->can($checker)) { } 0 0 0
118 0 $self->$parameter( delete $self->args->{$parameter} );
119 }
120 elsif ( $self->can($checker) ) {
121 0 ▶ return unless $self->$checker($value);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$checker($value) 0 0 0
122 }
123 }
124  
125 0 return 1;
126 } ## end sub args_checker
127  
128 0 ▶sub _args_translator {
Subroutine _args_translator: not called CC=7 SCAR=40.3
129 0 my $self = shift;
130 0 my %arg = splice @_;
131  
132 0 my %array2scalar = (
133 file_expr => 1,
134 match_expr => 1,
135 );
136 0 ▶ while ( my ( $arg, $value ) = each %arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %arg) 0 0 0
137 0 my $key = $arg;
138 0 $key =~ tr{-}{_};
139 0 ▶ if ( $key eq q(files_delim) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'files_delim') { } 0 0 0
elsif (exists $array2scalar{$key}) { } 0 0 0
140 0 ▶ if ( $value eq q(\0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "\\0") 0 0 0
141 0 $value = qq(\N{NULL});
142 }
143 }
144 elsif ( exists $array2scalar{$key} ) {
145 0 ▶ my $ref = ref $value // q();
Condition0%0/2
ref $value // ""
l!l
00
MC/DC0%0/1
ref $value // ""
ref $value
Truth table0%0/2
A
A= ref $value
Aresult
11
00
146 0 ▶ $value = "@$value" if $ref eq q(ARRAY);
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 0 0 0
147 }
148 0 $self->args->{$key} = $value;
149 }
150  
151 0 return 1;
152 } ## end sub _args_translator
153  
154 0 ▶sub _mutually_exclusive_checker {
Subroutine _mutually_exclusive_checker: not called CC=3 SCAR=24.8
155 0 my $self = shift;
156 0 my $msg = shift;
157 0 my $pairs = shift;
158  
159 0 foreach my $keys ( @{$pairs} ) {
160 0 my @key = @{$keys};
161 0 ▶ if ( @key == ( grep { exists $self->args->{$_} } @key ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@key == grep({exists $self->args->{$_};} @key)) 0 0 0
162 0 $self->_msg( sprintf q('--%s' and '--%s' ) . $msg, @key );
163 0 return;
164 }
165 }
166  
167 0 return 1;
168 } ## end sub _mutually_exclusive_checker
169  
170 0 ▶sub _empty_value_checker {
Subroutine _empty_value_checker: not called CC=7 SCAR=40.3
171 0 my $self = shift;
172 0 my $msg = shift;
173 0 my $keys = shift;
174  
175 0 foreach my $key ( @{$keys} ) {
176 0 ▶ next unless exists $self->args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->args->{$key} 0 0 0
177 0 ▶ my $ref = ref $self->args->{$key} || q();
Condition0%0/2
ref $self->args->{$key} || ''
l!l
00
MC/DC0%0/1
ref $self->args->{$key} || ''
ref $self->args->{$key}
Truth table0%0/2
A
A= ref $self->args->{$key}
Aresult
11
00
178 0 my @value;
179 0 ▶ if ( $ref eq q() ) { @value = $self->args->{$key}; }
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq '') { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
180 0 elsif ( $ref eq q(ARRAY) ) { @value = @{ $self->args->{$key} }; }
181 0 else { die; }
182  
183 0 ▶ if ( ( grep { $_ eq q() } @value ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep({$_ eq "";} @value) != 0) 0 0 0
184 0 $self->_msg( sprintf q('--%s': ) . $msg, $key );
185 0 return;
186 }
187 }
188  
189 0 return 1;
190 } ## end sub _empty_value_checker
191  
192 0 ▶sub _implies_checker {
Subroutine _implies_checker: not called CC=6 SCAR=37.4
193 0 my $self = shift;
194 0 my $msg = shift;
195 0 my $implies = shift;
196  
197 0 foreach my $imply ( @{$implies} ) {
198 0 foreach my $key ( keys %{$imply} ) {
199 0 ▶ next unless exists $self->args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->args->{$key} 0 0 0
200 0 foreach my $skey ( @{ $imply->{$key} } ) {
201 0 ▶ unless ( exists $self->args->{$skey} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->args->{$skey}) 0 0 0
202 0 $self->_msg( sprintf q('--%s' %s '--%s' option.),
203 $key, $msg, $skey );
204 0 return;
205 }
206 }
207 }
208 }
209  
210 0 return 1;
211 } ## end sub _implies_checker
212  
213 0 ▶sub _files_from_checker {
Subroutine _files_from_checker: not called CC=8 SCAR=42.8
214 0 my $self = shift;
215 0 my $value = shift;
216  
217 0 my $parameter = ( split m{::_(\S+)_checker\z}msx, ( caller 0 )[3] )[-1];
218  
219 0 my $msg;
220 0 foreach ($value) {
221 0 ▶ if ( !/\A(?:-|stdin)\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\A(?:-|stdin)\z/iu) 0 0 0
222 0 ▶ if ( !-e ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not -e $_) { } 0 0 0
elsif (not -f $_) { } 0 0 0
elsif (not -r $_) { } 0 0 0
elsif (-z $_) { } 0 0 0
223 0 $msg = sprintf q('--%s' => nonexistent file '%s'.),
224 $parameter, $value;
225 }
226 elsif ( !-f ) {
227 0 $msg = sprintf q('--%s' => irregular file '%s'.),
228 $parameter, $value;
229 }
230 elsif ( !-r ) {
231 0 $msg = sprintf q('--%s' => unreadable file '%s'.),
232 $parameter, $value;
233 }
234 elsif (-z) {
235 0 $msg = sprintf q('--%s' => empty file '%s'.),
236 $parameter, $value;
237 }
238 } ## end if ( !/\A(?:-|stdin)\z/i)
239 } ## end foreach ($value)
240  
241 0 ▶ if ( defined $msg ) { $self->_msg($msg); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg) 0 0 0
242  
243 0 $self->args->{files_delim} = qq(\N{LINE FEED});
244  
245 0 return 1;
246 } ## end sub _files_from_checker
247  
248 0 ▶sub _find_type_checker {
Subroutine _find_type_checker: not called CC=2 SCAR=17.9
249 0 my $self = shift;
250 0 my $value = shift;
251  
252 0 my $parameter = ( split m{::_(\S+)_checker\z}msx, ( caller 0 )[3] )[-1];
253 0 my %type = (
254 glob => 1, #
255 literal => 1, #
256 regexp => 1, #
257 );
258  
259 0 ▶ unless ( exists $type{$value} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $type{$value}) 0 0 0
260 0 $self->_msg( sprintf q('--%s' => argument invalid '%s'.),
261 $parameter, $value );
262 0 return;
263 }
264  
265 0 return 1;
266 } ## end sub _find_type_checker
267  
268 0 ▶sub _directory_checker {
Subroutine _directory_checker: not called CC=6 SCAR=37.4
269 0 my $self = shift;
270 0 my $values = shift;
271  
272 0 my $parameter = ( split m{::_(\S+)_checker\z}msx, ( caller 0 )[3] )[-1];
273  
274 0 my $msg;
275 0 foreach ( @{$values} ) {
276 0 ▶ if ( !-e ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not -e $_) { } 0 0 0
elsif (not -d $_) { } 0 0 0
elsif (not -r $_) { } 0 0 0
277 0 $msg = sprintf q('--%s' => nonexistent directory '%s'.),
278 $parameter, $_;
279 }
280 elsif ( !-d ) {
281 0 $msg = sprintf q('--%s' => non-directory argument '%s'.),
282 $parameter, $_;
283 }
284 elsif ( !-r ) {
285 0 $msg = sprintf q('--%s' => unreadable directory '%s'.),
286 $parameter, $_;
287 }
288 0 ▶ if ( defined $msg ) { $self->_msg($msg); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg) 0 0 0
289 } ## end foreach ( @{$values} )
290  
291 0 return 1;
292 } ## end sub _directory_checker
293  
294 0 ▶sub _msg {
Subroutine _msg: not called CC=1 SCAR=6.9
295 0 my $self = shift;
296 0 my $msg = shift;
297  
298 0 say STDERR sprintf q(%s: %s), $PROGRAM_NAME, $msg;
299 0 say STDERR sprintf q(Try '%s --help' for more information.),
300 $PROGRAM_NAME;
301  
302 0 return 1;
303 }
304  
305 1 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
306 1 __PACKAGE__->meta->make_immutable;
307  
308 1 1;
309  
310 __END__
↑ Summary BoolFindGrep/Grep.pm »