| Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Perl/Critic/Policy/CodeLayout/ProhibitParensWithBuiltins.pm |
| Statements | Executed 26 statements in 806µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 16µs | 16µs | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@10 |
| 1 | 1 | 1 | 10µs | 30µs | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@13 |
| 1 | 1 | 1 | 7µs | 18µs | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@11 |
| 1 | 1 | 1 | 7µs | 145µs | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@15 |
| 1 | 1 | 1 | 7µs | 58µs | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@20 |
| 1 | 1 | 1 | 7µs | 11µs | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@12 |
| 1 | 1 | 1 | 7µs | 498µs | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@17 |
| 1 | 1 | 1 | 6µs | 7µs | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::default_severity |
| 1 | 1 | 1 | 2µs | 2µs | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::supported_parameters |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::_contains_operators |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::_is_equals_exemption |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::_is_named_unary |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::_is_named_unary_with_operator_following_parens_exemption |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::_is_named_unary_with_operator_inside_parens_exemption |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::_is_precedence_exemption |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::_is_sort_exemption |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::applies_to |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::default_themes |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::violates |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | ############################################################################## | ||||
| 2 | # $URL$ | ||||
| 3 | # $Date$ | ||||
| 4 | # $Author$ | ||||
| 5 | # $Revision$ | ||||
| 6 | ############################################################################## | ||||
| 7 | |||||
| 8 | package Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins; | ||||
| 9 | |||||
| 10 | 2 | 39µs | 1 | 16µs | # spent 16µs within Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@10 which was called:
# once (16µs+0s) by Module::Pluggable::Object::_require at line 10 # spent 16µs making 1 call to Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@10 |
| 11 | 2 | 20µs | 2 | 29µs | # spent 18µs (7+11) within Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@11 which was called:
# once (7µs+11µs) by Module::Pluggable::Object::_require at line 11 # spent 18µs making 1 call to Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@11
# spent 11µs making 1 call to strict::import |
| 12 | 2 | 18µs | 2 | 14µs | # spent 11µs (7+4) within Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@12 which was called:
# once (7µs+4µs) by Module::Pluggable::Object::_require at line 12 # spent 11µs making 1 call to Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@12
# spent 4µs making 1 call to warnings::import |
| 13 | 2 | 22µs | 2 | 50µs | # spent 30µs (10+20) within Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@13 which was called:
# once (10µs+20µs) by Module::Pluggable::Object::_require at line 13 # spent 30µs making 1 call to Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@13
# spent 20µs making 1 call to Exporter::import |
| 14 | |||||
| 15 | 2 | 27µs | 2 | 284µs | # spent 145µs (7+138) within Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@15 which was called:
# once (7µs+138µs) by Module::Pluggable::Object::_require at line 15 # spent 145µs making 1 call to Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@15
# spent 138µs making 1 call to Exporter::Tiny::import |
| 16 | |||||
| 17 | 1 | 200ns | # spent 498µs (7+491) within Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@17 which was called:
# once (7µs+491µs) by Module::Pluggable::Object::_require at line 19 | ||
| 18 | :booleans :severities :data_conversion :classification :language | ||||
| 19 | 1 | 25µs | 2 | 988µs | }; # spent 498µs making 1 call to Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@17
# spent 491µs making 1 call to Exporter::import |
| 20 | 2 | 578µs | 2 | 109µs | # spent 58µs (7+51) within Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@20 which was called:
# once (7µs+51µs) by Module::Pluggable::Object::_require at line 20 # spent 58µs making 1 call to Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::BEGIN@20
# spent 51µs making 1 call to base::import |
| 21 | |||||
| 22 | 1 | 600ns | our $VERSION = '1.121'; | ||
| 23 | |||||
| 24 | #----------------------------------------------------------------------------- | ||||
| 25 | |||||
| 26 | 1 | 2µs | 1 | 28µs | Readonly::Array my @ALLOW => qw( my our local return state ); # spent 28µs making 1 call to Readonly::Array |
| 27 | 1 | 16µs | 3 | 50µs | Readonly::Hash my %ALLOW => hashify( @ALLOW ); # spent 26µs making 1 call to Readonly::Hash
# spent 23µs making 1 call to Perl::Critic::Utils::hashify
# spent 1µs making 1 call to Readonly::Array::FETCHSIZE |
| 28 | |||||
| 29 | 1 | 1µs | 1 | 24µs | Readonly::Scalar my $DESC => q{Builtin function called with parentheses}; # spent 24µs making 1 call to Readonly::Scalar |
| 30 | 1 | 2µs | 1 | 52µs | Readonly::Scalar my $EXPL => [ 13 ]; # spent 52µs making 1 call to Readonly::Scalar |
| 31 | |||||
| 32 | 1 | 4µs | 2 | 42µs | Readonly::Scalar my $PRECENDENCE_OF_LIST => precedence_of(q{>>}) + 1; # spent 33µs making 1 call to Readonly::Scalar
# spent 9µs making 1 call to Perl::Critic::Utils::precedence_of |
| 33 | 1 | 3µs | 2 | 28µs | Readonly::Scalar my $PRECEDENCE_OF_COMMA => precedence_of(q{,}); # spent 21µs making 1 call to Readonly::Scalar
# spent 7µs making 1 call to Perl::Critic::Utils::precedence_of |
| 34 | |||||
| 35 | #----------------------------------------------------------------------------- | ||||
| 36 | # These are all the functions that are considered named unary | ||||
| 37 | # operators. These frequently require parentheses because they have lower | ||||
| 38 | # precedence than ordinary function calls. | ||||
| 39 | |||||
| 40 | 1 | 2µs | 1 | 48µs | Readonly::Array my @NAMED_UNARY_OPS => qw( # spent 48µs making 1 call to Readonly::Array |
| 41 | alarm glob rand | ||||
| 42 | caller gmtime readlink | ||||
| 43 | chdir hex ref | ||||
| 44 | chroot int require | ||||
| 45 | cos lc return | ||||
| 46 | defined lcfirst rmdir | ||||
| 47 | delete length scalar | ||||
| 48 | do localtime sin | ||||
| 49 | eval lock sleep | ||||
| 50 | exists log sqrt | ||||
| 51 | exit lstat srand | ||||
| 52 | getgrp my stat | ||||
| 53 | gethostbyname oct uc | ||||
| 54 | getnetbyname ord ucfirst | ||||
| 55 | getprotobyname quotemeta umask | ||||
| 56 | undef | ||||
| 57 | ); | ||||
| 58 | 1 | 25µs | 3 | 3.25ms | Readonly::Hash my %NAMED_UNARY_OPS => hashify( @NAMED_UNARY_OPS ); # spent 3.16ms making 1 call to Perl::Critic::Utils::hashify
# spent 91µs making 1 call to Readonly::Hash
# spent 1µs making 1 call to Readonly::Array::FETCHSIZE |
| 59 | |||||
| 60 | #----------------------------------------------------------------------------- | ||||
| 61 | |||||
| 62 | 1 | 4µs | # spent 2µs within Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::supported_parameters which was called:
# once (2µs+0s) by Perl::Critic::Policy::new at line 88 of Perl/Critic/Policy.pm | ||
| 63 | 1 | 2µs | # spent 7µs (6+1) within Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins::default_severity which was called:
# once (6µs+1µs) by Perl::Critic::Policy::get_severity at line 331 of Perl/Critic/Policy.pm | ||
| 64 | sub default_themes { return qw( core pbp cosmetic ) } | ||||
| 65 | sub applies_to { return 'PPI::Token::Word' } | ||||
| 66 | |||||
| 67 | #----------------------------------------------------------------------------- | ||||
| 68 | |||||
| 69 | sub violates { | ||||
| 70 | my ( $self, $elem, undef ) = @_; | ||||
| 71 | |||||
| 72 | return if exists $ALLOW{$elem}; | ||||
| 73 | return if not is_perl_builtin($elem); | ||||
| 74 | return if not is_function_call($elem); | ||||
| 75 | |||||
| 76 | my $sibling = $elem->snext_sibling(); | ||||
| 77 | return if not $sibling; | ||||
| 78 | if ( $sibling->isa('PPI::Structure::List') ) { | ||||
| 79 | my $elem_after_parens = $sibling->snext_sibling(); | ||||
| 80 | |||||
| 81 | return if _is_named_unary_with_operator_inside_parens_exemption($elem, $sibling); | ||||
| 82 | return if _is_named_unary_with_operator_following_parens_exemption($elem, $elem_after_parens); | ||||
| 83 | return if _is_precedence_exemption($elem_after_parens); | ||||
| 84 | return if _is_equals_exemption($sibling); | ||||
| 85 | return if _is_sort_exemption($elem, $sibling); | ||||
| 86 | |||||
| 87 | # If we get here, it must be a violation | ||||
| 88 | return $self->violation( $DESC, $EXPL, $elem ); | ||||
| 89 | } | ||||
| 90 | return; #ok! | ||||
| 91 | } | ||||
| 92 | |||||
| 93 | #----------------------------------------------------------------------------- | ||||
| 94 | # EXCEPTION 1: If the function is a named unary and there is an | ||||
| 95 | # operator with higher precedence right after the parentheses. | ||||
| 96 | # Example: int( 1.5 ) + 0.5; | ||||
| 97 | |||||
| 98 | sub _is_named_unary_with_operator_following_parens_exemption { | ||||
| 99 | my ($elem, $elem_after_parens) = @_; | ||||
| 100 | |||||
| 101 | if ( _is_named_unary( $elem ) && $elem_after_parens ){ | ||||
| 102 | # Smaller numbers mean higher precedence | ||||
| 103 | my $precedence = precedence_of( $elem_after_parens ); | ||||
| 104 | return $TRUE if defined $precedence && $precedence < $PRECENDENCE_OF_LIST; | ||||
| 105 | } | ||||
| 106 | |||||
| 107 | return $FALSE; | ||||
| 108 | } | ||||
| 109 | |||||
| 110 | sub _is_named_unary { | ||||
| 111 | my ($elem) = @_; | ||||
| 112 | |||||
| 113 | return exists $NAMED_UNARY_OPS{$elem->content}; | ||||
| 114 | } | ||||
| 115 | |||||
| 116 | #----------------------------------------------------------------------------- | ||||
| 117 | # EXCEPTION 2, If there is an operator immediately after the | ||||
| 118 | # parentheses, and that operator has precedence greater than | ||||
| 119 | # or equal to a comma. | ||||
| 120 | # Example: join($delim, @list) . "\n"; | ||||
| 121 | |||||
| 122 | sub _is_precedence_exemption { | ||||
| 123 | my ($elem_after_parens) = @_; | ||||
| 124 | |||||
| 125 | if ( $elem_after_parens ){ | ||||
| 126 | # Smaller numbers mean higher precedence | ||||
| 127 | my $precedence = precedence_of( $elem_after_parens ); | ||||
| 128 | return $TRUE if defined $precedence && $precedence <= $PRECEDENCE_OF_COMMA; | ||||
| 129 | } | ||||
| 130 | |||||
| 131 | return $FALSE; | ||||
| 132 | } | ||||
| 133 | |||||
| 134 | #----------------------------------------------------------------------------- | ||||
| 135 | # EXCEPTION 3: If the first operator within the parentheses is '=' | ||||
| 136 | # Example: chomp( my $foo = <STDIN> ); | ||||
| 137 | |||||
| 138 | sub _is_equals_exemption { | ||||
| 139 | my ($sibling) = @_; | ||||
| 140 | |||||
| 141 | if ( my $first_op = $sibling->find_first('PPI::Token::Operator') ){ | ||||
| 142 | return $TRUE if $first_op eq q{=}; | ||||
| 143 | } | ||||
| 144 | |||||
| 145 | return $FALSE; | ||||
| 146 | } | ||||
| 147 | |||||
| 148 | #----------------------------------------------------------------------------- | ||||
| 149 | # EXCEPTION 4: sort with default comparator but a function for the list data | ||||
| 150 | # Example: sort(foo(@x)) | ||||
| 151 | |||||
| 152 | sub _is_sort_exemption { | ||||
| 153 | my ($elem, $sibling) = @_; | ||||
| 154 | |||||
| 155 | if ( $elem eq 'sort' ) { | ||||
| 156 | my $first_arg = $sibling->schild(0); | ||||
| 157 | if ( $first_arg && $first_arg->isa('PPI::Statement::Expression') ) { | ||||
| 158 | $first_arg = $first_arg->schild(0); | ||||
| 159 | } | ||||
| 160 | if ( $first_arg && $first_arg->isa('PPI::Token::Word') ) { | ||||
| 161 | my $next_arg = $first_arg->snext_sibling; | ||||
| 162 | return $TRUE if $next_arg && $next_arg->isa('PPI::Structure::List'); | ||||
| 163 | } | ||||
| 164 | } | ||||
| 165 | |||||
| 166 | return $FALSE; | ||||
| 167 | } | ||||
| 168 | |||||
| 169 | #----------------------------------------------------------------------------- | ||||
| 170 | # EXCEPTION 5: If the function is a named unary and there is an operator | ||||
| 171 | # inside the parentheses. | ||||
| 172 | # Example: length($foo || $bar); | ||||
| 173 | |||||
| 174 | sub _is_named_unary_with_operator_inside_parens_exemption { | ||||
| 175 | my ($elem, $parens) = @_; | ||||
| 176 | return _is_named_unary($elem) && _contains_operators($parens); | ||||
| 177 | } | ||||
| 178 | |||||
| 179 | sub _contains_operators { | ||||
| 180 | my ($parens) = @_; | ||||
| 181 | return $TRUE if $parens->find_first('PPI::Token::Operator'); | ||||
| 182 | return $FALSE; | ||||
| 183 | } | ||||
| 184 | |||||
| 185 | #----------------------------------------------------------------------------- | ||||
| 186 | 1 | 17µs | 1; | ||
| 187 | |||||
| 188 | __END__ |