« lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitShiftRef.pm
↑ Summary
lib/Perl/Critic/Policy/InputOutput/RequireBracedFileHandleWithPrint.pm »
| 1 | package Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins; | ||||||||||||||||||||||||||||
| 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 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | :booleans :severities :data_conversion :classification :language | ||||||||||||||||||||||||||||
| 10 | 40 | }; | |||||||||||||||||||||||||||
| 11 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 40 | our $VERSION = '1.156'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 40 | Readonly::Array my @ALLOW => qw( my our local return state ); | |||||||||||||||||||||||||||
| 18 | 40 | Readonly::Hash my %ALLOW => hashify( @ALLOW ); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 40 | Readonly::Scalar my $DESC => q{Builtin function called with parentheses}; | |||||||||||||||||||||||||||
| 21 | 40 | Readonly::Scalar my $EXPL => [ 13 ]; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 40 | Readonly::Scalar my $PRECEDENCE_OF_LIST => precedence_of(q{>>}) + 1; | |||||||||||||||||||||||||||
| 24 | 40 | Readonly::Scalar my $PRECEDENCE_OF_COMMA => precedence_of(q{,}); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 27 | # These are all the functions that are considered named unary | ||||||||||||||||||||||||||||
| 28 | # operators. These frequently require parentheses because they have lower | ||||||||||||||||||||||||||||
| 29 | # precedence than ordinary function calls. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 40 | Readonly::Array my @NAMED_UNARY_OPS => qw( | |||||||||||||||||||||||||||
| 32 | alarm glob rand | ||||||||||||||||||||||||||||
| 33 | caller gmtime readlink | ||||||||||||||||||||||||||||
| 34 | chdir hex ref | ||||||||||||||||||||||||||||
| 35 | chroot int require | ||||||||||||||||||||||||||||
| 36 | cos lc return | ||||||||||||||||||||||||||||
| 37 | defined lcfirst rmdir | ||||||||||||||||||||||||||||
| 38 | delete length scalar | ||||||||||||||||||||||||||||
| 39 | do localtime sin | ||||||||||||||||||||||||||||
| 40 | eval lock sleep | ||||||||||||||||||||||||||||
| 41 | exists log sqrt | ||||||||||||||||||||||||||||
| 42 | exit lstat srand | ||||||||||||||||||||||||||||
| 43 | getgrp my stat | ||||||||||||||||||||||||||||
| 44 | gethostbyname oct uc | ||||||||||||||||||||||||||||
| 45 | getnetbyname ord ucfirst | ||||||||||||||||||||||||||||
| 46 | getprotobyname quotemeta umask | ||||||||||||||||||||||||||||
| 47 | undef | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | 40 | Readonly::Hash my %NAMED_UNARY_OPS => hashify( @NAMED_UNARY_OPS ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 90 | ▶ | sub supported_parameters { return () } | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 74 | ▶ | sub default_severity { return $SEVERITY_LOWEST } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 83 | ▶ | sub default_themes { return qw( core pbp cosmetic ) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 31 | ▶ | sub applies_to { return 'PPI::Token::Word' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 332 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=11 SCAR=34.1
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 332 | my ( $self, $elem, undef ) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 332 | ▶ | return if exists $ALLOW{$elem}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 236 | ▶ | return if not is_perl_builtin($elem); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 73 | ▶ | return if not is_function_call($elem); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 70 | my $sibling = $elem->snext_sibling(); | |||||||||||||||||||||||||||
| 68 | 70 | ▶ | return if not $sibling; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 70 | ▶ | if ( $sibling->isa('PPI::Structure::List') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | my $elem_after_parens = $sibling->snext_sibling(); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return if _is_named_unary_with_operator_inside_parens_exemption($elem, $sibling); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | return if _is_named_unary_with_operator_following_parens_exemption($elem, $elem_after_parens); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | return if _is_precedence_exemption($elem_after_parens); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return if _is_equals_exemption($sibling); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | return if _is_sort_exemption($elem, $sibling); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # If we get here, it must be a violation | ||||||||||||||||||||||||||||
| 79 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 70 | return; #ok! | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 85 | # EXCEPTION 1: If the function is a named unary and there is an | ||||||||||||||||||||||||||||
| 86 | # operator with higher precedence right after the parentheses. | ||||||||||||||||||||||||||||
| 87 | # Example: int( 1.5 ) + 0.5; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub _is_named_unary_with_operator_following_parens_exemption { | ||||||||||||||||||||||||||
|
Subroutine
_is_named_unary_with_operator_following_parens_exemption: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 90 | 0 | my ($elem, $elem_after_parens) = @_; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ( _is_named_unary( $elem ) && $elem_after_parens ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
_is_named_unary($elem) and $elem_after_parens
MC/DC0%0/2
_is_named_unary($elem) and $elem_after_parens
_is_named_unary($elem)
$elem_after_parens
Truth table0%0/3
A and B
A= _is_named_unary($elem)B= $elem_after_parens
| |||||||||||||||||||||||||||||
| 93 | # Smaller numbers mean higher precedence | ||||||||||||||||||||||||||||
| 94 | 0 | my $precedence = precedence_of( $elem_after_parens ); | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | return $TRUE if defined $precedence && $precedence < $PRECEDENCE_OF_LIST; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $precedence and $precedence < $PRECEDENCE_OF_LIST
MC/DC0%0/2
defined $precedence and $precedence < $PRECEDENCE_OF_LIST
defined $precedence
$precedence < $PRECEDENCE_OF_LIST
Truth table0%0/3
A and B
A= defined $precedenceB= $precedence < $PRECEDENCE_OF_LIST
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | return $FALSE; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _is_named_unary { | ||||||||||||||||||||||||||
|
Subroutine
_is_named_unary: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | 0 | my ($elem) = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | return exists $NAMED_UNARY_OPS{$elem->content}; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 108 | # EXCEPTION 2, If there is an operator immediately after the | ||||||||||||||||||||||||||||
| 109 | # parentheses, and that operator has precedence greater than | ||||||||||||||||||||||||||||
| 110 | # or equal to a comma. | ||||||||||||||||||||||||||||
| 111 | # Example: join($delim, @list) . "\n"; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub _is_precedence_exemption { | ||||||||||||||||||||||||||
|
Subroutine
_is_precedence_exemption: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 114 | 0 | my ($elem_after_parens) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ( $elem_after_parens ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | # Smaller numbers mean higher precedence | ||||||||||||||||||||||||||||
| 118 | 0 | my $precedence = precedence_of( $elem_after_parens ); | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | return $TRUE if defined $precedence && $precedence <= $PRECEDENCE_OF_COMMA; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $precedence and $precedence <= $PRECEDENCE_OF_COMMA
MC/DC0%0/2
defined $precedence and $precedence <= $PRECEDENCE_OF_COMMA
defined $precedence
$precedence <= $PRECEDENCE_OF_COMMA
Truth table0%0/3
A and B
A= defined $precedenceB= $precedence <= $PRECEDENCE_OF_COMMA
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | return $FALSE; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 126 | # EXCEPTION 3: If the first operator within the parentheses is '=' | ||||||||||||||||||||||||||||
| 127 | # Example: chomp( my $foo = <STDIN> ); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub _is_equals_exemption { | ||||||||||||||||||||||||||
|
Subroutine
_is_equals_exemption: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 130 | 0 | my ($sibling) = @_; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ( my $first_op = $sibling->find_first('PPI::Token::Operator') ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | return $TRUE if $first_op eq q{=}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | return $FALSE; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 140 | # EXCEPTION 4: sort with default comparator but a function for the list data | ||||||||||||||||||||||||||||
| 141 | # Example: sort(foo(@x)) | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub _is_sort_exemption { | ||||||||||||||||||||||||||
|
Subroutine
_is_sort_exemption: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 144 | 0 | my ($elem, $sibling) = @_; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ( $elem eq 'sort' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | my $first_arg = $sibling->schild(0); | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ( $first_arg && $first_arg->isa('PPI::Statement::Expression') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$first_arg and $first_arg->isa("PPI::Statement::Expression")
MC/DC0%0/2
$first_arg and $first_arg->isa("PPI::Statement::Expression")
$first_arg
$first_arg->isa("PPI::Statement::Expression")
Truth table0%0/3
A and B
A= $first_argB= $first_arg->isa("PPI::Statement::Expression")
| |||||||||||||||||||||||||||||
| 149 | 0 | $first_arg = $first_arg->schild(0); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ( $first_arg && $first_arg->isa('PPI::Token::Word') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$first_arg and $first_arg->isa("PPI::Token::Word")
MC/DC0%0/2
$first_arg and $first_arg->isa("PPI::Token::Word")
$first_arg
$first_arg->isa("PPI::Token::Word")
Truth table0%0/3
A and B
A= $first_argB= $first_arg->isa("PPI::Token::Word")
| |||||||||||||||||||||||||||||
| 152 | 0 | my $next_arg = $first_arg->snext_sibling; | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | return $TRUE if $next_arg && $next_arg->isa('PPI::Structure::List'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$next_arg and $next_arg->isa("PPI::Structure::List")
MC/DC0%0/2
$next_arg and $next_arg->isa("PPI::Structure::List")
$next_arg
$next_arg->isa("PPI::Structure::List")
Truth table0%0/3
A and B
A= $next_argB= $next_arg->isa("PPI::Structure::List")
| |||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | return $FALSE; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 161 | # EXCEPTION 5: If the function is a named unary and there is an operator | ||||||||||||||||||||||||||||
| 162 | # inside the parentheses. | ||||||||||||||||||||||||||||
| 163 | # Example: length($foo || $bar); | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub _is_named_unary_with_operator_inside_parens_exemption { | ||||||||||||||||||||||||||
|
Subroutine
_is_named_unary_with_operator_inside_parens_exemption: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 166 | 0 | my ($elem, $parens) = @_; | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | return _is_named_unary($elem) && _contains_operators($parens); | ||||||||||||||||||||||||||
|
Condition0%0/3
_is_named_unary($elem) && _contains_operators($parens)
MC/DC0%0/2
_is_named_unary($elem) && _contains_operators($parens)
_is_named_unary($elem)
_contains_operators($parens)
Truth table0%0/3
A && B
A= _is_named_unary($elem)B= _contains_operators($parens)
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | sub _contains_operators { | ||||||||||||||||||||||||||
|
Subroutine
_contains_operators: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 171 | 0 | my ($parens) = @_; | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | return $TRUE if $parens->find_first('PPI::Token::Operator'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 0 | return $FALSE; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 177 | 40 | 1; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | __END__ | ||||||||||||||||||||||||||||