blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringyEval.pm

filter: Statementstmt 47.4% 37 / 78 Branchbran 11.1% 6 / 54 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 12 / 16 Podpod 80.0% 4 / 5 totaltotal 37.9% 60 / 158 CC 30 SCAR 39.9
CC
30
Cov
33%
_string_eval_is_an_include
156.0
_string_eval_accept_include
42.0
_string_eval_accept_follow_on
30.0
CRAP 53.8
« blib/lib/Perl/Critic/Policy/Subroutines/RequireFinalReturn.pm ↑ Summary blib/lib/Perl/Critic/Policy/Objects/ProhibitIndirectSyntax.pm »
1 package Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use PPI::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use Perl::Critic::Utils qw{ :booleans :severities :classification :ppi $SCOLON };
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 our $VERSION = '1.150';
15  
16 #-----------------------------------------------------------------------------
17  
18 40 Readonly::Scalar my $DESC => q{Expression form of "eval"};
19 40 Readonly::Scalar my $EXPL => [ 161 ];
20  
21 #-----------------------------------------------------------------------------
22  
23 # The maximum number of statements that may appear in an import-only eval
24 # string:
25 40 Readonly::Scalar my $MAX_STATEMENTS => 3;
26  
27 #-----------------------------------------------------------------------------
28  
29 sub supported_parameters {
30 return (
31 {
32 91 ▶ name => 'allow_includes',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
33 description => q<Allow eval of "use" and "require" strings.>,
34 default_string => '0',
35 behavior => 'boolean',
36 },
37 );
38 }
39 83 ▶sub default_severity { return $SEVERITY_HIGHEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
40 92 ▶sub default_themes { return qw( core pbp bugs certrule ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
41 37 ▶sub applies_to { return 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
42  
43 #-----------------------------------------------------------------------------
44  
45 sub violates {
46 361 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=7 SCAR=21.0
Pod documented
47  
48 361 ▶ return if $elem->content() ne 'eval';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->content ne "eval" 353 8 361
49 8 ▶ return if not is_function_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless is_function_call($elem) 0 8 8
50  
51 8 my $argument = first_arg($elem);
52 8 ▶ return if not $argument;
Branch: 1 branch
BranchTrueFalseTotal
unless $argument 0 8 8
53 8 ▶ return if $argument->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
if $argument->isa("PPI::Structure::Block") 0 8 8
54 return if
55 8 ▶ $self->{_allow_includes} and _string_eval_is_an_include($argument);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_allow_includes"} and _string_eval_is_an_include($argument) 0 8 8
Condition33%1/3
$self->{"_allow_includes"} and _string_eval_is_an_include($argument)
!ll&&!rl&&r
800
MC/DC0%0/2
$self->{"_allow_includes"} and _string_eval_is_an_include($argument)
$self->{"_allow_includes"} _string_eval_is_an_include($argument)
Truth table33%1/3
A and B
A= $self->{"_allow_includes"}B= _string_eval_is_an_include($argument)
ABresult
0X0
100
111
56  
57 8 return $self->violation( $DESC, $EXPL, $elem );
58 }
59  
60 sub _string_eval_is_an_include {
61 0 ▶ my ($eval_argument) = @_;
Subroutine _string_eval_is_an_include: not called CC=12 SCAR=50.5
62  
63 0 ▶ return if not $eval_argument->isa('PPI::Token::Quote');
Branch: 1 branch
BranchTrueFalseTotal
unless $eval_argument->isa("PPI::Token::Quote") 0 0 0
64  
65 0 my $string = $eval_argument->string();
66 0 my $document;
67  
68 0 ▶ eval { $document = PPI::Document->new(\$string); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $document = "PPI::Document"->new(\$string); 1 } } or return 0 0 0
69 or return;
70  
71 0 my @statements = $document->schildren;
72  
73 0 ▶ return if @statements > $MAX_STATEMENTS;
Branch: 1 branch
BranchTrueFalseTotal
if @statements > $MAX_STATEMENTS 0 0 0
74  
75 0 my $structure = join q{,}, map { $_->class } @statements;
76  
77 0 my $package_class = qr{PPI::Statement::Package}xms;
78 0 my $include_class = qr{PPI::Statement::Include}xms;
79 0 my $statement_class = qr{PPI::Statement}xms;
80  
81 0 ▶ return if $structure !~ m{
Branch: 1 branch
BranchTrueFalseTotal
unless $structure =~ / ^ (?:$package_class,)? # Optional "package" $include_class (?:,$statement_class)? # Optional follow-on number $ /msx 0 0 0
82 ^
83 (?:$package_class,)? # Optional "package"
84 $include_class
85 (?:,$statement_class)? # Optional follow-on number
86 $
87 }xms;
88  
89 0 ▶ my $is_q = $eval_argument->isa('PPI::Token::Quote::Single')
Branch: 1 branch
BranchTrueFalseTotal
my $is_q = $eval_argument->isa("PPI::Token::Quote::Single") or $eval_argument->isa("PPI::Token::Quote::Literal") 0 0 0
90 or $eval_argument->isa('PPI::Token::Quote::Literal');
91  
92 0 for my $statement (@statements) {
93 0 ▶ if ( $statement->isa('PPI::Statement::Package') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($statement->isa('PPI::Statement::Package')) { } 0 0 0
elsif ($statement->isa('PPI::Statement::Include')) { } 0 0 0
94 0 ▶ _string_eval_accept_package($statement) or return;
Branch: 1 branch
BranchTrueFalseTotal
_string_eval_accept_package($statement) or return 0 0 0
95 } elsif ( $statement->isa('PPI::Statement::Include') ) {
96 0 ▶ _string_eval_accept_include( $statement, $is_q ) or return;
Branch: 1 branch
BranchTrueFalseTotal
_string_eval_accept_include($statement, $is_q) or return 0 0 0
97 } else {
98 0 ▶ _string_eval_accept_follow_on($statement) or return;
Branch: 1 branch
BranchTrueFalseTotal
_string_eval_accept_follow_on($statement) or return 0 0 0
99 }
100 }
101  
102 0 return $TRUE;
103 }
104  
105 sub _string_eval_accept_package {
106 0 ▶ my ($package) = @_;
Subroutine _string_eval_accept_package: not called CC=4 SCAR=30.0
107  
108 0 ▶ return if not defined $package; # RT 60179
Branch: 1 branch
BranchTrueFalseTotal
unless defined $package 0 0 0
109 0 ▶ return if not $package->isa('PPI::Statement::Package');
Branch: 1 branch
BranchTrueFalseTotal
unless $package->isa("PPI::Statement::Package") 0 0 0
110 0 ▶ return if not $package->file_scoped;
Branch: 1 branch
BranchTrueFalseTotal
unless $package->file_scoped 0 0 0
111  
112 0 return $TRUE;
113 }
114  
115 sub _string_eval_accept_include {
116 0 ▶ my ( $include, $is_single_quoted ) = @_;
Subroutine _string_eval_accept_include: not called CC=6 SCAR=37.4
117  
118 0 ▶ return if not defined $include; # RT 60179
Branch: 1 branch
BranchTrueFalseTotal
unless defined $include 0 0 0
119 0 ▶ return if not $include->isa('PPI::Statement::Include');
Branch: 1 branch
BranchTrueFalseTotal
unless $include->isa("PPI::Statement::Include") 0 0 0
120 0 ▶ return if $include->type() eq 'no';
Branch: 1 branch
BranchTrueFalseTotal
if $include->type eq "no" 0 0 0
121  
122 0 ▶ if ($is_single_quoted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_single_quoted) 0 0 0
123 # Don't allow funky inclusion of arbitrary code (note we do allow
124 # interpolated values in interpolating strings because they can't
125 # entirely screw with the syntax).
126 0 ▶ return if $include->find('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
if $include->find("PPI::Token::Symbol") 0 0 0
127 }
128  
129 0 return $TRUE;
130 }
131  
132 sub _string_eval_accept_follow_on {
133 0 ▶ my ($follow_on) = @_;
Subroutine _string_eval_accept_follow_on: not called CC=5 SCAR=34.0
134  
135 0 ▶ return if not $follow_on->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $follow_on->isa("PPI::Statement") 0 0 0
136  
137 0 my @follow_on_components = $follow_on->schildren();
138  
139 0 ▶ return if @follow_on_components > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @follow_on_components > 2 0 0 0
140 0 ▶ return if not $follow_on_components[0]->isa('PPI::Token::Number');
Branch: 1 branch
BranchTrueFalseTotal
unless $follow_on_components[0]->isa("PPI::Token::Number") 0 0 0
141 0 ▶ return $TRUE if @follow_on_components == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @follow_on_components == 1 0 0 0
142  
143 0 return $follow_on_components[1]->content() eq $SCOLON;
144 }
145  
146 40 1;
147  
148 __END__
149  
150 #-----------------------------------------------------------------------------
151  
152 =pod
153  
154 =for stopwords SIGNES
155  
156 =head1 NAME
157  
158 Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval - Write C<eval { my $foo; bar($foo) }> instead of C<eval "my $foo; bar($foo);">.
159  
160  
161 =head1 AFFILIATION
162  
163 This Policy is part of the core L<Perl::Critic|Perl::Critic>
164 distribution.
165  
166  
167 =head1 DESCRIPTION
168  
169 The string form of C<eval> is recompiled every time it is executed,
170 whereas the block form is only compiled once. Also, the string form
171 doesn't give compile-time warnings.
172  
173 eval "print $foo"; # not ok
174 eval {print $foo}; # ok
175  
176  
177 =head1 CONFIGURATION
178  
179 There is an C<allow_includes> boolean option for this Policy. If set, then
180 strings that look like they only include an optional "package" statement
181 followed by a single "use" or "require" statement (with the possible following
182 statement that consists of a single number) are allowed. With this option
183 set, the following are flagged as indicated:
184  
185 eval 'use Foo'; # ok
186 eval 'require Foo'; # ok
187 eval "use $thingy;"; # ok
188 eval "require $thingy;"; # ok
189 eval 'package Pkg; use Foo'; # ok
190 eval 'package Pkg; require Foo'; # ok
191 eval "package $pkg; use $thingy;"; # ok
192 eval "package $pkg; require $thingy;"; # ok
193 eval "use $thingy; 1;"; # ok
194 eval "require $thingy; 1;"; # ok
195 eval "package $pkg; use $thingy; 1;"; # ok
196 eval "package $pkg; require $thingy; 1;"; # ok
197  
198 eval 'use Foo; blah;'; # still not ok
199 eval 'require Foo; 2; 1;'; # still not ok
200 eval 'use $thingy;'; # still not ok
201 eval 'no Foo'; # still not ok
202  
203 If you don't understand why the number is allowed, see
204 L<Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval|Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval>.
205  
206 This option inspired by Ricardo SIGNES'
207 L<Perl::Critic::Policy::Lax::ProhibitStringyEval::ExceptForRequire|Perl::Critic::Policy::Lax::ProhibitStringyEval::ExceptForRequire>.
208  
209  
210 =head1 SEE ALSO
211  
212 L<Perl::Critic::Policy::BuiltinFunctions::RequireBlockGrep|Perl::Critic::Policy::BuiltinFunctions::RequireBlockGrep>
213  
214 L<Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap|Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap>
215  
216  
217 =head1 AUTHOR
218  
219 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
220  
221  
222 =head1 COPYRIGHT
223  
224 Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
225  
226 This program is free software; you can redistribute it and/or modify
227 it under the same terms as Perl itself. The full text of this license
228 can be found in the LICENSE file included with this module.
229  
230 =cut
231  
232 # Local Variables:
233 # mode: cperl
234 # cperl-indent-level: 4
235 # fill-column: 78
236 # indent-tabs-mode: nil
237 # c-indentation-style: bsd
238 # End:
239 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/Subroutines/RequireFinalReturn.pm ↑ Summary blib/lib/Perl/Critic/Policy/Objects/ProhibitIndirectSyntax.pm »