« blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringyEval.pm
↑ Summary
blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitBooleanGrep.pm »
| 1 | package Perl::Critic::Policy::Objects::ProhibitIndirectSyntax; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 Perl::Critic::Utils qw{ :severities :classification }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Readonly; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 40 | our $VERSION = '1.150'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | #----------------------------------------------------------------------------- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 40 | Readonly::Hash my %COMMA => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | q<,> => 1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | q{=>} => 1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 40 | Readonly::Scalar my $DOLLAR => q<$>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 40 | Readonly::Scalar my $DESC => 'Subroutine "%s" called using indirect syntax'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 40 | Readonly::Scalar my $EXPL => [ 349 ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | #----------------------------------------------------------------------------- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | sub supported_parameters { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | return ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 91 | ▶ | name => 'forbid', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | description => 'Indirect method syntax is forbidden for these methods.', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | behavior => 'string list', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | list_always_present_values => [ qw{ new } ], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 75 | ▶ | sub default_severity { return $SEVERITY_HIGH } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 86 | ▶ | sub default_themes { return qw( core pbp maintenance certrule ) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 33 | ▶ | sub applies_to { return 'PPI::Token::Word' } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | #----------------------------------------------------------------------------- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | sub violates { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 349 | ▶ | my ( $self, $elem, $doc ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=11 SCAR=46.2
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # We are only interested in the functions we have been told to check. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # Do this before calling is_function_call() because we want to weed | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # out as many candidate tokens as possible before calling it. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 349 | ▶ | return if not $self->{_forbid}->{$elem->content()}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # Make sure it really is a function call. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return if not is_function_call($elem); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # Per perlobj, it is only an indirect object call if the next sibling | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # is a word, a scalar symbol, or a block. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $object = $elem->snext_sibling() or return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | return if not ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $object->isa( 'PPI::Token::Word' ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$object->isa("PPI::Token::Word") or $object->isa("PPI::Token::Symbol") and $DOLLAR eq $object->raw_type or $object->isa("PPI::Structure::Block")
$object->isa("PPI::Token::Word") or $object->isa("PPI::Token::Symbol") and $DOLLAR eq $object->raw_type
$object->isa("PPI::Token::Symbol") and $DOLLAR eq $object->raw_type
MC/DC0%0/4
$object->isa("PPI::Token::Word") or $object->isa("PPI::Token::Symbol") and $DOLLAR eq $object->raw_type or $object->isa("PPI::Structure::Block")
$object->isa("PPI::Token::Word")
$object->isa("PPI::Token::Symbol")
$DOLLAR eq $object->raw_type
$object->isa("PPI::Structure::Block")
Truth table0%0/6
A or B and C or D
A= $object->isa("PPI::Token::Word")B= $object->isa("PPI::Token::Symbol")C= $DOLLAR eq $object->raw_typeD= $object->isa("PPI::Structure::Block")
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | or $object->isa( 'PPI::Token::Symbol' ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | and $DOLLAR eq $object->raw_type() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | or $object->isa( 'PPI::Structure::Block' ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # Per perlobj, it is not an indirect object call if the operator after | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # the possible indirect object is a comma. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( my $operator = $object->snext_sibling() ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | return if | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | $operator->isa( 'PPI::Token::Operator' ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | and $COMMA{ $operator->content() }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$operator->isa("PPI::Token::Operator") and $COMMA{$operator->content}
MC/DC0%0/2
$operator->isa("PPI::Token::Operator") and $COMMA{$operator->content}
$operator->isa("PPI::Token::Operator")
$COMMA{$operator->content}
Truth table0%0/3
A and B
A= $operator->isa("PPI::Token::Operator")B= $COMMA{$operator->content}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $message = sprintf $DESC, $elem->content(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | return $self->violation( $message, $EXPL, $elem ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 40 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | __END__ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | #----------------------------------------------------------------------------- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | =pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | =head1 NAME | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | Perl::Critic::Policy::Objects::ProhibitIndirectSyntax - Prohibit indirect object call syntax. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | =head1 AFFILIATION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | This Policy is part of the core L<Perl::Critic|Perl::Critic> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | distribution. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | =head1 DESCRIPTION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | Indirect object syntax is commonly used in other object-oriented languages for | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | instantiating objects. Perl allows this, but to say that it supports it may be | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | going too far. Instead of writing | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | my $foo = new Foo; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | it is preferable to write | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | my $foo = Foo->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | The problem is that Perl needs to make a number of assumptions at compile time | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | to disambiguate the first form, so it tends to be fragile and to produce | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | hard-to-track-down bugs. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | =head1 CONFIGURATION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | Indirect object syntax is also hard for Perl::Critic to disambiguate, so this | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | policy only checks certain subroutine calls. The names of the subroutines can | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | be configured using the C<forbid> configuration option: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | [Objects::ProhibitIndirectSyntax] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | forbid = create destroy | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | The C<new> subroutine is configured by default; any additional C<forbid> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | values are in addition to C<new>. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | =head1 CAVEATS | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | The general situation can not be handled via static analysis. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | =head1 SEE ALSO | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | L<Perl::Critic::Policy::Dynamic::NoIndirect|Perl::Critic::Policy::Dynamic::NoIndirect> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | and L<indirect|indirect> both do a better job with this, but they require that you | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | compile/execute your code. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | =head1 AUTHOR | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | Thomas R. Wyant, III F<wyant at cpan dot org> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | =head1 COPYRIGHT | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | Copyright (c) 2009-2023 Tom Wyant | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | This program is free software; you can redistribute it and/or modify | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | it under the same terms as Perl itself. The full text of this license | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | can be found in the LICENSE file included with this module. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # Local Variables: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # mode: cperl | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # cperl-indent-level: 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # fill-column: 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # indent-tabs-mode: nil | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # c-indentation-style: bsd | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # End: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||