« lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitComplexMappings.pm
↑ Summary
lib/Perl/Critic/Policy/Subroutines/RequireFinalReturn.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.156'; | |||||||||||||||||||||||||||
| 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 | 91 | ▶ | sub supported_parameters { | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | return ( | ||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||
| 32 | 91 | name => 'allow_includes', | |||||||||||||||||||||||||||
| 33 | description => q<Allow eval of "use" and "require" strings.>, | ||||||||||||||||||||||||||||
| 34 | default_string => '0', | ||||||||||||||||||||||||||||
| 35 | behavior => 'boolean', | ||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 82 | ▶ | sub default_severity { return $SEVERITY_HIGHEST } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 91 | ▶ | 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 | 361 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=7 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 361 | my ( $self, $elem, undef ) = @_; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 361 | ▶ | return if $elem->content() ne 'eval'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 8 | ▶ | return if not is_function_call($elem); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 8 | my $argument = first_arg($elem); | |||||||||||||||||||||||||||
| 52 | 8 | ▶ | return if not $argument; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 8 | ▶ | return if $argument->isa('PPI::Structure::Block'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | return if | ||||||||||||||||||||||||||||
| 55 | 8 | ▶ | $self->{_allow_includes} and _string_eval_is_an_include($argument); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"_allow_includes"} and _string_eval_is_an_include($argument)
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)
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 8 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub _string_eval_is_an_include { | ||||||||||||||||||||||||||
|
Subroutine
_string_eval_is_an_include: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||
| 61 | 0 | my ($eval_argument) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | return if not $eval_argument->isa('PPI::Token::Quote'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | or return; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my @statements = $document->schildren; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | return if @statements > $MAX_STATEMENTS; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | _string_eval_accept_package($statement) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } elsif ( $statement->isa('PPI::Statement::Include') ) { | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | _string_eval_accept_include( $statement, $is_q ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | _string_eval_accept_follow_on($statement) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | return $TRUE; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub _string_eval_accept_package { | ||||||||||||||||||||||||||
|
Subroutine
_string_eval_accept_package: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 106 | 0 | my ($package) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | return if not defined $package; # RT 60179 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | return if not $package->isa('PPI::Statement::Package'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return if not $package->file_scoped; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | return $TRUE; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub _string_eval_accept_include { | ||||||||||||||||||||||||||
|
Subroutine
_string_eval_accept_include: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 116 | 0 | my ( $include, $is_single_quoted ) = @_; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | return if not defined $include; # RT 60179 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return if not $include->isa('PPI::Statement::Include'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return if $include->type() eq 'no'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($is_single_quoted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | return $TRUE; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub _string_eval_accept_follow_on { | ||||||||||||||||||||||||||
|
Subroutine
_string_eval_accept_follow_on: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 133 | 0 | my ($follow_on) = @_; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | return if not $follow_on->isa('PPI::Statement'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my @follow_on_components = $follow_on->schildren(); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | return if @follow_on_components > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | return if not $follow_on_components[0]->isa('PPI::Token::Number'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | return $TRUE if @follow_on_components == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | return $follow_on_components[1]->content() eq $SCOLON; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 40 | 1; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||