blib/lib/Perl/Critic/Policy/PreferredBinaries.pm

filter: Statementstmt 24.3% 47 / 193 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 42.4% 14 / 33 Podpod 66.6% 2 / 3 totaltotal 13.8% 63 / 456 CC 88 SCAR 42.5
CC
88
Cov
12%
_commands_in
132.0
violates
110.0
_named_command
110.0
CRAP 70.1
↑ Summary
1 package Perl::Critic::Policy::PreferredBinaries;
2 1 $Perl::Critic::Policy::PreferredBinaries::VERSION = '0.002';
3 # 5.014, which is as low as the house style allows: RequireDefault wants /aa on
4 # every pattern, and /aa arrived in 5.14. Kept that low rather than at the
5 # 5.041 applications here are written against, because a policy runs on
6 # whatever perl the code being criticised is criticised with.
7 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Perl::Critic::Utils qw{ :severities :classification :ppi $SEVERITY_MEDIUM $TRUE $FALSE };
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Perl::Critic::Exception::Configuration::Generic ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Config::INI::Reader ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # ABSTRACT: Recommend a perl sub over shelling out to a binary that does the same job
25  
26  
27  
28 0 ▶sub supported_parameters {
Subroutine supported_parameters: not called CC=1 SCAR=6.9
Pod undocumented
29 return (
30 {
31 0 name => 'config',
32 description => 'Config::INI file listing binaries and what to use instead.',
33 behavior => 'string',
34 },
35  
36 # The library runners that hand a command to the system the way
37 # `system` does. They are always read because a policy that caught only
38 # `system` would be a policy people route around by reaching for
39 # IPC::Run3, which is a step sideways rather than the step this is
40 # asking for.
41 {
42 name => 'runners',
43 description => 'More subs to read as runners of a command.',
44 behavior => 'string list',
45 list_always_present_values => [qw{ run3 run capture capturex runx systemx }],
46 },
47 );
48 }
49  
50 1 ▶use constant default_severity => $SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use constant default_themes => qw{ maintenance certrec };
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use constant applies_to => qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
54 PPI::Token::Word
55 PPI::Token::QuoteLike::Backtick
56 PPI::Token::QuoteLike::Command
57 1 };
58  
59 1 ▶use constant DEFAULT_CONFIG => '.preferred_binaries.ini';
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 # The builtins that hand a command to the system. The library runners that do
62 # the same are the defaults of the runners parameter.
63 1 my %BUILTIN = map { $_ => 1 } qw{ system exec };
64 1 my %PIPE_OPS = map { $_ => 1 } ( '-|', '|-' );
65  
66 0 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: not called CC=1 SCAR=6.9
Pod documented
67 0 my ( $self, $config ) = @_;
68  
69 0 $self->{_preferred} = $self->_parse_config( $self->_config_file($config) );
70  
71 # A call is matched on the last part of its name, so a runner configured
72 # with its package has to be too, or it would never match anything.
73 0 $self->{_runners} = { map { ( split( q{::}, $_ ) )[-1] => 1 } keys %{ $self->{_runners} } };
74  
75 0 return $TRUE;
76 }
77  
78 0 ▶sub _config_file {
Subroutine _config_file: not called CC=4 SCAR=30.0
79 0 my ( $self, $config ) = @_;
80  
81 0 ▶ $config //= $self->__get_config;
Condition0%0/3
$config //= $self->__get_config
l!l&&r!l&&!r
000
MC/DC0%0/2
$config //= $self->__get_config
$config $self->__get_config
Truth table0%0/3
A //= B
A= $configB= $self->__get_config
ABresult
1X1
011
000
82  
83 0 my $file = $config->get('config');
84 0 ▶ $file = DEFAULT_CONFIG unless defined $file && length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 0 0 0
Condition0%0/3
defined $file and length $file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table0%0/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
85 0 $file =~ s{^~}{$ENV{HOME} // q{}};
86  
87 0 return $file;
88 }
89  
90 # A missing file is not an error. This policy is in the default set of the
91 # distributions that use it, and most of them have nothing to say about
92 # binaries -- refusing to run would make every one of those a configuration
93 # problem to suppress.
94 0 ▶sub _parse_config {
Subroutine _parse_config: not called CC=9 SCAR=45.0
95 0 my ( $self, $file ) = @_;
96  
97 0 ▶ return {} unless length $file && -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless length $file and -e $file 0 0 0
Condition0%0/3
length $file and -e $file
!ll&&!rl&&r
000
MC/DC0%0/2
length $file and -e $file
length $file -e $file
Truth table0%0/3
A and B
A= length $fileB= -e $file
ABresult
0X0
100
111
98  
99 # Slurped rather than read_file, for compatibility with Test::MockFile.
100 0 my $content;
101 {
102 0 local $/ = undef;
103 0 ▶ open( my $fh, '<', $file ) or $self->_throw("Cannot open config file '$file': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or $self->_throw("Cannot open config file '${file}': $!") 0 0 0
104 0 $content = <$fh>;
105 0 close($fh);
106 }
107  
108 0 ▶ my $parsed = eval { Config::INI::Reader->read_string($content) }
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = eval { do { "Config::INI::Reader"->read_string($content) } } or $self->_throw("Invalid configuration file '${file}'") 0 0 0
109 or $self->_throw("Invalid configuration file '$file'");
110  
111 0 my %preferred;
112 0 foreach my $section ( keys %$parsed ) {
113 0 ▶ next if $section eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $section eq "_" 0 0 0
114  
115 0 my $entry = $parsed->{$section};
116 0 ▶ next unless ref $entry eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 0 0
117  
118 # The section name is a command line: the binary and any leading
119 # arguments that have to match with it.
120 0 my @words = split( ' ', $section );
121 0 ▶ next unless @words;
Branch: 1 branch
BranchTrueFalseTotal
unless @words 0 0 0
122  
123 0 $words[0] = File::Basename::basename( $words[0] );
124 0 $preferred{ join( ' ', @words ) } = $entry;
125 }
126  
127 0 return \%preferred;
128 }
129  
130 0 ▶sub _throw {
Subroutine _throw: not called CC=1 SCAR=6.9
131 0 my ( $self, $message ) = @_;
132  
133 0 return Perl::Critic::Exception::Configuration::Generic->throw( message => __PACKAGE__ . " $message" );
134 }
135  
136 0 ▶sub violates {
Subroutine violates: not called CC=10 SCAR=47.0
Pod documented
137 0 my ( $self, $elem ) = @_;
138  
139 0 ▶ return () unless %{ $self->{_preferred} // {} };
Branch: 1 branch
BranchTrueFalseTotal
unless %{$self->{"_preferred"} // {};} 0 0 0
Condition0%0/2
$self->{"_preferred"} // {}
l!l
00
MC/DC0%0/1
$self->{"_preferred"} // {}
$self->{"_preferred"}
Truth table0%0/2
A
A= $self->{"_preferred"}
Aresult
11
00
140  
141 0 my @violations;
142 0 foreach my $command ( $self->_commands_in($elem) ) {
143 0 ▶ my ( $key, $entry ) = $self->_match($command) or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $self->_match($command) or next 0 0 0
144  
145 0 ▶ my $reason = $entry->{reason} // q{};
Condition0%0/2
$entry->{"reason"} // ""
l!l
00
MC/DC0%0/1
$entry->{"reason"} // ""
$entry->{"reason"}
Truth table0%0/2
A
A= $entry->{"reason"}
Aresult
11
00
146 0 ▶ $reason = substr( $reason, 1, -1 ) if $reason =~ m/\A".*"\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if $reason =~ /\A".*"\z/aas 0 0 0
147  
148 my $what =
149 defined $entry->{prefer}
150 0 ▶ ? "Prefer $entry->{prefer} to shelling out to '$key'"
Branch: 1 branch
BranchTrueFalseTotal
defined $entry->{'prefer'} ? : 0 0 0
151 : "Shelling out to '$key' is not recommended";
152  
153 # A severity named on the entry rather than on the policy, which is how
154 # Perl::Critic::Policy::PreferredModules does the same thing: the
155 # violation takes the policy's severity as it is made, so the way to
156 # give it another is to have another in place while it is made.
157 0 ▶ local $self->{_severity} = $entry->{severity} if defined $entry->{severity};
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->{"severity"} 0 0 0
158  
159 0 ▶ push(
Branch: 1 branch
BranchTrueFalseTotal
length $reason ? : 0 0 0
160 @violations,
161 $self->violation(
162 $what,
163 ( length $reason ? $reason : "$key has an answer in perl; use it rather than a process" ),
164 $elem,
165 )
166 );
167 }
168  
169 0 return @violations;
170 }
171  
172 # The command lines this element hands to a shell, as arrayrefs of leading
173 # literal words. More than one, because IPC::Run takes a pipeline of arrayrefs.
174 0 ▶sub _commands_in {
Subroutine _commands_in: not called CC=11 SCAR=48.8
175 0 my ( $self, $elem ) = @_;
176  
177 0 ▶ return ( _words_from_string( _inside_quotelike($elem) ) )
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::QuoteLike::Backtick") or $elem->isa("PPI::Token::QuoteLike::Command") 0 0 0
Condition0%0/3
$elem->isa("PPI::Token::QuoteLike::Backtick") or $elem->isa("PPI::Token::QuoteLike::Command")
l!l&&r!l&&!r
000
MC/DC0%0/2
$elem->isa("PPI::Token::QuoteLike::Backtick") or $elem->isa("PPI::Token::QuoteLike::Command")
$elem->isa("PPI::Token::QuoteLike::Backtick") $elem->isa("PPI::Token::QuoteLike::Command")
Truth table0%0/3
A or B
A= $elem->isa("PPI::Token::QuoteLike::Backtick")B= $elem->isa("PPI::Token::QuoteLike::Command")
ABresult
1X1
011
000
178 if $elem->isa('PPI::Token::QuoteLike::Backtick') || $elem->isa('PPI::Token::QuoteLike::Command');
179  
180 0 ▶ return () unless $elem->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Token::Word") 0 0 0
181  
182 0 my $word = "$elem";
183  
184 0 ▶ return $self->_from_open($elem) if $word eq 'open' && is_function_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "open" and is_function_call($elem) 0 0 0
Condition0%0/3
$word eq "open" and is_function_call($elem)
!ll&&!rl&&r
000
MC/DC0%0/2
$word eq "open" and is_function_call($elem)
$word eq "open" is_function_call($elem)
Truth table0%0/3
A and B
A= $word eq "open"B= is_function_call($elem)
ABresult
0X0
100
111
185 0 ▶ return $self->_from_arguments($elem) if $BUILTIN{$word} && is_function_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
if $BUILTIN{$word} and is_function_call($elem) 0 0 0
Condition0%0/3
$BUILTIN{$word} and is_function_call($elem)
!ll&&!rl&&r
000
MC/DC0%0/2
$BUILTIN{$word} and is_function_call($elem)
$BUILTIN{$word} is_function_call($elem)
Truth table0%0/3
A and B
A= $BUILTIN{$word}B= is_function_call($elem)
ABresult
0X0
100
111
186 0 ▶ return $self->_from_runner($elem) if $self->{_runners}{ ( split( q{::}, $word ) )[-1] // q{} } && !is_hash_key($elem);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_runners"}{(split(/::/aa, $word, 0))[-1] // ""} and not is_hash_key($elem) 0 0 0
Condition0%0/5
$self->{"_runners"}{(split(/::/aa, $word, 0))[-1] // ""} and not is_hash_key($elem)
!ll&&!rl&&r
000
(split(/::/aa, $word, 0))[-1] // ""
l!l
00
MC/DC0%0/2
$self->{"_runners"}{(split(/::/aa, $word, 0))[-1] // ""} and not is_hash_key($elem)
$self->{"_runners"}{(split(/::/aa, $word, 0))[-1] // ""} not is_hash_key($elem)
MC/DC0%0/1
(split(/::/aa, $word, 0))[-1] // ""
(split(/::/aa, $word, 0))[-1]
Truth table0%0/3
A and not(B)
A= $self->{"_runners"}{(split(/::/aa, $word, 0))[-1] // ""}B= not is_hash_key($elem)
ABresult
0X0
100
111
Truth table0%0/2
A
A= (split(/::/aa, $word, 0))[-1]
Aresult
11
00
187  
188 0 return ();
189 }
190  
191 # open( $fh, '-|', 'curl', ... ) and the two-argument open( $fh, "curl ... |" ).
192 0 ▶sub _from_open {
Subroutine _from_open: not called CC=7 SCAR=40.3
193 0 my ( $self, $elem ) = @_;
194  
195 0 my @args = parse_arg_list($elem);
196 0 ▶ return () unless @args >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @args >= 2 0 0 0
197  
198 0 my $mode = _literal( _first_token( $args[1] ) );
199 0 ▶ return () unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 0 0
200  
201 0 ▶ if ( $PIPE_OPS{$mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PIPE_OPS{$mode}) 0 0 0
202 0 ▶ return () unless @args >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @args >= 3 0 0 0
203 0 return ( _words_from_args( @args[ 2 .. $#args ] ) );
204 }
205  
206 # Two-argument form: the mode is the command, with a pipe on one end.
207 0 ▶ return () unless $mode =~ s{\A\s*\|}{} || $mode =~ s{\|\s*\z}{};
Branch: 1 branch
BranchTrueFalseTotal
unless $mode =~ s/\A\s*\|//aa or $mode =~ s/\|\s*\z//aa 0 0 0
Condition0%0/3
$mode =~ s/\A\s*\|//aa or $mode =~ s/\|\s*\z//aa
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode =~ s/\A\s*\|//aa or $mode =~ s/\|\s*\z//aa
$mode =~ s/\A\s*\|//aa $mode =~ s/\|\s*\z//aa
Truth table0%0/3
A or B
A= $mode =~ s/\A\s*\|//aaB= $mode =~ s/\|\s*\z//aa
ABresult
1X1
011
000
208  
209 0 return ( _words_from_string($mode) );
210 }
211  
212 0 ▶sub _from_arguments {
Subroutine _from_arguments: not called CC=2 SCAR=17.9
213 0 my ( $self, $elem ) = @_;
214  
215 0 my @args = parse_arg_list($elem);
216 0 ▶ return () unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 0 0
217  
218 # A single string argument is a shell command line; a list, or an arrayref
219 # as the runners take, is already split into words.
220 0 return ( _words_from_args(@args) );
221 }
222  
223 # A runner is matched by name, and its command is either positional -- the
224 # first argument, often an arrayref, as IPC::Run3 and IPC::Run take it -- or the
225 # value of a named `command =>`, which is how IPC::Cmd::run takes it, in any
226 # position among the other named arguments.
227 0 ▶sub _from_runner {
Subroutine _from_runner: not called CC=3 SCAR=24.8
228 0 my ( $self, $elem ) = @_;
229  
230 0 my @args = parse_arg_list($elem);
231 0 ▶ return () unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 0 0
232  
233 0 my @named = _named_command(@args);
234 0 ▶ return @named if @named;
Branch: 1 branch
BranchTrueFalseTotal
if @named 0 0 0
235  
236 0 return ( _words_from_args(@args) );
237 }
238  
239 0 ▶sub _named_command {
Subroutine _named_command: not called CC=10 SCAR=47.0
240 0 my (@args) = @_;
241  
242 0 ▶ my @tokens = map { ref $_ eq 'ARRAY' ? @$_ : $_ } @args;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
243 0 foreach my $i ( 0 .. $#tokens ) {
244 0 my $token = $tokens[$i];
245 0 ▶ next unless Scalar::Util::blessed($token) && $token->isa('PPI::Token::Word') && "$token" eq 'command';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($token) and $token->isa("PPI::Token::Word") and "$token" eq "command" 0 0 0
Condition0%0/6
builtin::blessed($token) and $token->isa("PPI::Token::Word") and "$token" eq "command"
!ll&&!rl&&r
000
builtin::blessed($token) and $token->isa("PPI::Token::Word")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($token) and $token->isa("PPI::Token::Word") and "$token" eq "command"
builtin::blessed($token) $token->isa("PPI::Token::Word") "$token" eq "command"
Truth table0%0/4
A and B and C
A= builtin::blessed($token)B= $token->isa("PPI::Token::Word")C= "$token" eq "command"
ABCresult
0XX0
10X0
1100
1111
246  
247 0 my $value = $i + 1;
248 0 ▶ $value++ while $value <= $#tokens && Scalar::Util::blessed( $tokens[$value] ) && $tokens[$value]->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
$value <= $#tokens and builtin::blessed($tokens[$value]) and $tokens[$value]->isa("PPI::Token::Operator") and do { ++$value } 0 0 0
Condition0%0/6
$value <= $#tokens and builtin::blessed($tokens[$value]) and $tokens[$value]->isa("PPI::Token::Operator")
!ll&&!rl&&r
000
$value <= $#tokens and builtin::blessed($tokens[$value])
!ll&&!rl&&r
000
MC/DC0%0/3
$value <= $#tokens and builtin::blessed($tokens[$value]) and $tokens[$value]->isa("PPI::Token::Operator")
$value <= $#tokens builtin::blessed($tokens[$value]) $tokens[$value]->isa("PPI::Token::Operator")
Truth table0%0/4
A and B and C
A= $value <= $#tokensB= builtin::blessed($tokens[$value])C= $tokens[$value]->isa("PPI::Token::Operator")
ABCresult
0XX0
10X0
1100
1111
249 0 ▶ return () if $value > $#tokens;
Branch: 1 branch
BranchTrueFalseTotal
if $value > $#tokens 0 0 0
250  
251 0 return _words_from_args( [ $tokens[$value] ] );
252 }
253  
254 0 return ();
255 }
256  
257 # Each argument is a list of PPI tokens. An arrayref argument is a command in
258 # its own right, which is how IPC::Run3 and IPC::Run spell one.
259 0 ▶sub _words_from_args {
Subroutine _words_from_args: not called CC=10 SCAR=47.0
260 0 my (@args) = @_;
261  
262 0 my @commands;
263 my @literal;
264  
265 0 foreach my $arg (@args) {
266 0 ▶ my @tokens = ref $arg eq 'ARRAY' ? @$arg : ($arg);
Branch: 1 branch
BranchTrueFalseTotal
ref $arg eq 'ARRAY' ? : 0 0 0
267  
268 0 ▶ if ( @tokens == 1 && Scalar::Util::blessed( $tokens[0] ) && $tokens[0]->isa('PPI::Structure::Constructor') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens == 1 and builtin::blessed($tokens[0]) and $tokens[0]->isa("PPI::Structure::Constructor")) 0 0 0
Condition0%0/6
@tokens == 1 and builtin::blessed($tokens[0]) and $tokens[0]->isa("PPI::Structure::Constructor")
!ll&&!rl&&r
000
@tokens == 1 and builtin::blessed($tokens[0])
!ll&&!rl&&r
000
MC/DC0%0/3
@tokens == 1 and builtin::blessed($tokens[0]) and $tokens[0]->isa("PPI::Structure::Constructor")
@tokens == 1 builtin::blessed($tokens[0]) $tokens[0]->isa("PPI::Structure::Constructor")
Truth table0%0/4
A and B and C
A= @tokens == 1B= builtin::blessed($tokens[0])C= $tokens[0]->isa("PPI::Structure::Constructor")
ABCresult
0XX0
10X0
1100
1111
269 0 push( @commands, _words_from_constructor( $tokens[0] ) );
270 0 next;
271 }
272  
273 0 my @words = map { _literal_words($_) } @tokens;
274  
275 # Once a word cannot be read literally there is nothing further to say
276 # about this command: what follows may be arguments to something else
277 # entirely.
278 0 ▶ last unless @words;
Branch: 1 branch
BranchTrueFalseTotal
unless @words 0 0 0
279 0 push( @literal, @words );
280 }
281  
282 0 ▶ unshift( @commands, \@literal ) if @literal;
Branch: 1 branch
BranchTrueFalseTotal
if @literal 0 0 0
283  
284 # One bare string is a shell command line rather than a word.
285 0 ▶ if ( @literal == 1 && $literal[0] =~ m/\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@literal == 1 and $literal[0] =~ /\s/aa) 0 0 0
Condition0%0/3
@literal == 1 and $literal[0] =~ /\s/aa
!ll&&!rl&&r
000
MC/DC0%0/2
@literal == 1 and $literal[0] =~ /\s/aa
@literal == 1 $literal[0] =~ /\s/aa
Truth table0%0/3
A and B
A= @literal == 1B= $literal[0] =~ /\s/aa
ABresult
0X0
100
111
286 0 $commands[0] = ( _words_from_string( $literal[0] ) )[0];
287 }
288  
289 0 return @commands;
290 }
291  
292 # An arrayref argument is a command in its own right. Its children are an
293 # expression rather than a run of tokens, so this descends one level to find
294 # them.
295 0 ▶sub _words_from_constructor {
Subroutine _words_from_constructor: not called CC=10 SCAR=47.0
296 0 my ($node) = @_;
297  
298 0 my @children = $node->schildren;
299 0 ▶ @children = $children[0]->schildren
Branch: 1 branch
BranchTrueFalseTotal
if @children == 1 and builtin::blessed($children[0]) and $children[0]->isa("PPI::Statement") 0 0 0
Condition0%0/6
@children == 1 and builtin::blessed($children[0]) and $children[0]->isa("PPI::Statement")
!ll&&!rl&&r
000
@children == 1 and builtin::blessed($children[0])
!ll&&!rl&&r
000
MC/DC0%0/3
@children == 1 and builtin::blessed($children[0]) and $children[0]->isa("PPI::Statement")
@children == 1 builtin::blessed($children[0]) $children[0]->isa("PPI::Statement")
Truth table0%0/4
A and B and C
A= @children == 1B= builtin::blessed($children[0])C= $children[0]->isa("PPI::Statement")
ABCresult
0XX0
10X0
1100
1111
300 if @children == 1 && Scalar::Util::blessed( $children[0] ) && $children[0]->isa('PPI::Statement');
301  
302 0 my @words;
303 0 foreach my $token (@children) {
304 0 ▶ next if Scalar::Util::blessed($token) && $token->isa('PPI::Token::Operator') && "$token" eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($token) and $token->isa("PPI::Token::Operator") and "$token" eq "," 0 0 0
Condition0%0/6
builtin::blessed($token) and $token->isa("PPI::Token::Operator") and "$token" eq ","
!ll&&!rl&&r
000
builtin::blessed($token) and $token->isa("PPI::Token::Operator")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($token) and $token->isa("PPI::Token::Operator") and "$token" eq ","
builtin::blessed($token) $token->isa("PPI::Token::Operator") "$token" eq ","
Truth table0%0/4
A and B and C
A= builtin::blessed($token)B= $token->isa("PPI::Token::Operator")C= "$token" eq ","
ABCresult
0XX0
10X0
1100
1111
305  
306 0 my @literal = _literal_words($token);
307 0 ▶ last unless @literal;
Branch: 1 branch
BranchTrueFalseTotal
unless @literal 0 0 0
308 0 push( @words, @literal );
309 }
310  
311 0 ▶ return @words ? ( \@words ) : ();
Branch: 1 branch
BranchTrueFalseTotal
@words ? : 0 0 0
312 }
313  
314 # parse_arg_list gives an arrayref of tokens per argument; this is the first
315 # one, for the arguments that are a single literal.
316 0 ▶sub _first_token {
Subroutine _first_token: not called CC=2 SCAR=17.9
317 0 my ($arg) = @_;
318  
319 0 ▶ return $arg unless ref $arg eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg eq "ARRAY" 0 0 0
320 0 return $arg->[0];
321 }
322  
323 # A qw{} list is several words; anything else quoted is one.
324 0 ▶sub _literal_words {
Subroutine _literal_words: not called CC=9 SCAR=45.0
325 0 my ($token) = @_;
326  
327 # parse_arg_list hands back an arrayref per argument, and an argument can
328 # itself be a list -- so anything that is not a PPI node is not a word.
329 0 ▶ return () unless Scalar::Util::blessed($token);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($token) 0 0 0
330 0 ▶ return () if $token->isa('PPI::Token::Operator') && "$token" eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if $token->isa("PPI::Token::Operator") and "$token" eq "," 0 0 0
Condition0%0/3
$token->isa("PPI::Token::Operator") and "$token" eq ","
!ll&&!rl&&r
000
MC/DC0%0/2
$token->isa("PPI::Token::Operator") and "$token" eq ","
$token->isa("PPI::Token::Operator") "$token" eq ","
Truth table0%0/3
A and B
A= $token->isa("PPI::Token::Operator")B= "$token" eq ","
ABresult
0X0
100
111
331 0 ▶ return () if $token->isa('PPI::Token::Whitespace');
Branch: 1 branch
BranchTrueFalseTotal
if $token->isa("PPI::Token::Whitespace") 0 0 0
332 0 ▶ return () if $token->isa('PPI::Structure::List') || $token->isa('PPI::Structure::Constructor');
Branch: 1 branch
BranchTrueFalseTotal
if $token->isa("PPI::Structure::List") or $token->isa("PPI::Structure::Constructor") 0 0 0
Condition0%0/3
$token->isa("PPI::Structure::List") or $token->isa("PPI::Structure::Constructor")
l!l&&r!l&&!r
000
MC/DC0%0/2
$token->isa("PPI::Structure::List") or $token->isa("PPI::Structure::Constructor")
$token->isa("PPI::Structure::List") $token->isa("PPI::Structure::Constructor")
Truth table0%0/3
A or B
A= $token->isa("PPI::Structure::List")B= $token->isa("PPI::Structure::Constructor")
ABresult
1X1
011
000
333 0 ▶ return $token->literal if $token->isa('PPI::Token::QuoteLike::Words');
Branch: 1 branch
BranchTrueFalseTotal
if $token->isa("PPI::Token::QuoteLike::Words") 0 0 0
334  
335 0 my $literal = _literal($token);
336 0 ▶ return defined $literal ? ($literal) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $literal ? : 0 0 0
337 }
338  
339 0 ▶sub _literal {
Subroutine _literal: not called CC=4 SCAR=30.0
340 0 my ($token) = @_;
341  
342 0 ▶ return undef unless Scalar::Util::blessed($token) && $token->isa('PPI::Token::Quote');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($token) and $token->isa("PPI::Token::Quote") 0 0 0
Condition0%0/3
builtin::blessed($token) and $token->isa("PPI::Token::Quote")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($token) and $token->isa("PPI::Token::Quote")
builtin::blessed($token) $token->isa("PPI::Token::Quote")
Truth table0%0/3
A and B
A= builtin::blessed($token)B= $token->isa("PPI::Token::Quote")
ABresult
0X0
100
111
343  
344 # ->literal is only on the ones with no interpolation to worry about;
345 # ->string on an interpolating quote hands back the source, which is a
346 # perfectly good answer for the leading words of a command.
347 0 ▶ return $token->can('literal') ? $token->literal : $token->string;
Branch: 1 branch
BranchTrueFalseTotal
$token->can('literal') ? : 0 0 0
348 }
349  
350 # What is between the delimiters of a backtick or qx token. PPI records the
351 # separator and not the span, so this takes the delimiters off the content
352 # rather than asking: one leading `qx`, then one character at each end, which
353 # covers `...`, qx{...}, qx(...) and qx/.../ alike.
354 0 ▶sub _inside_quotelike {
Subroutine _inside_quotelike: not called CC=2 SCAR=17.9
355 0 my ($elem) = @_;
356  
357 0 my $content = $elem->content;
358 0 $content =~ s/\Aqx//;
359  
360 0 ▶ return q{} if length($content) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $content < 2 0 0 0
361 0 return substr( $content, 1, -1 );
362 }
363  
364 # The leading words of a shell command line, stopping at anything that is not
365 # one -- a variable, a pipe, a redirection.
366 0 ▶sub _words_from_string {
Subroutine _words_from_string: not called CC=5 SCAR=34.0
367 0 my ($string) = @_;
368  
369 0 ▶ return () unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
370  
371 0 my @words;
372 0 foreach my $word ( split( ' ', $string ) ) {
373 0 ▶ last if $word =~ m/[\$\@\|<>;&`]/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /[\$\@\|<>;&`]/aa 0 0 0
374 0 push( @words, $word );
375 }
376  
377 0 ▶ return @words ? ( \@words ) : ();
Branch: 1 branch
BranchTrueFalseTotal
@words ? : 0 0 0
378 }
379  
380 # The longest configured section whose words are the leading words of this
381 # command. Longest first, so [ssh-keygen -y] beats [ssh-keygen].
382 0 ▶sub _match {
Subroutine _match: not called CC=5 SCAR=34.0
383 0 my ( $self, $command ) = @_;
384  
385 0 ▶ return () unless ref $command eq 'ARRAY' && @$command;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $command eq "ARRAY" and @$command 0 0 0
Condition0%0/3
ref $command eq "ARRAY" and @$command
!ll&&!rl&&r
000
MC/DC0%0/2
ref $command eq "ARRAY" and @$command
ref $command eq "ARRAY" @$command
Truth table0%0/3
A and B
A= ref $command eq "ARRAY"B= @$command
ABresult
0X0
100
111
386  
387 0 my @words = @$command;
388 0 $words[0] = File::Basename::basename( $words[0] );
389  
390 0 foreach my $length ( reverse 1 .. scalar @words ) {
391 0 my $key = join( ' ', @words[ 0 .. $length - 1 ] );
392 0 ▶ my $entry = $self->{_preferred}{$key} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->{"_preferred"}{$key} or next 0 0 0
393 0 return ( $key, $entry );
394 }
395  
396 0 return ();
397 }
398  
399 1 1;
400  
401 __END__
↑ Summary