Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Perl/Critic/Policy/BuiltinFunctions/ProhibitBooleanGrep.pm |
Statements | Executed 20 statements in 505µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 19µs | 19µs | BEGIN@10 | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
1 | 1 | 1 | 8µs | 58µs | BEGIN@16 | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
1 | 1 | 1 | 7µs | 28µs | BEGIN@13 | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
1 | 1 | 1 | 7µs | 411µs | BEGIN@15 | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
1 | 1 | 1 | 7µs | 11µs | BEGIN@12 | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
1 | 1 | 1 | 7µs | 18µs | BEGIN@11 | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
1 | 1 | 1 | 6µs | 8µs | default_severity | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
1 | 1 | 1 | 1µs | 1µs | supported_parameters | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
0 | 0 | 0 | 0s | 0s | _does_parent_cause_boolean | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
0 | 0 | 0 | 0s | 0s | _does_prev_sibling_cause_boolean | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
0 | 0 | 0 | 0s | 0s | _is_in_boolean_context | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
0 | 0 | 0 | 0s | 0s | applies_to | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
0 | 0 | 0 | 0s | 0s | default_themes | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
0 | 0 | 0 | 0s | 0s | violates | Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::
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::BuiltinFunctions::ProhibitBooleanGrep; | ||||
9 | |||||
10 | 2 | 44µs | 1 | 19µs | # spent 19µs within Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::BEGIN@10 which was called:
# once (19µs+0s) by Module::Pluggable::Object::_require at line 10 # spent 19µs making 1 call to Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::BEGIN@10 |
11 | 2 | 20µs | 2 | 28µs | # spent 18µs (7+11) within Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::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::BuiltinFunctions::ProhibitBooleanGrep::BEGIN@11
# spent 11µs making 1 call to strict::import |
12 | 2 | 18µs | 2 | 16µs | # spent 11µs (7+4) within Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::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::BuiltinFunctions::ProhibitBooleanGrep::BEGIN@12
# spent 4µs making 1 call to warnings::import |
13 | 2 | 27µs | 2 | 48µs | # spent 28µs (7+20) within Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::BEGIN@13 which was called:
# once (7µs+20µs) by Module::Pluggable::Object::_require at line 13 # spent 28µs making 1 call to Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::BEGIN@13
# spent 20µs making 1 call to Exporter::import |
14 | |||||
15 | 2 | 24µs | 2 | 816µs | # spent 411µs (7+404) within Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::BEGIN@15 which was called:
# once (7µs+404µs) by Module::Pluggable::Object::_require at line 15 # spent 411µs making 1 call to Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::BEGIN@15
# spent 404µs making 1 call to Exporter::import |
16 | 2 | 351µs | 2 | 108µs | # spent 58µs (8+50) within Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::BEGIN@16 which was called:
# once (8µs+50µs) by Module::Pluggable::Object::_require at line 16 # spent 58µs making 1 call to Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::BEGIN@16
# spent 50µs making 1 call to base::import |
17 | |||||
18 | 1 | 600ns | our $VERSION = '1.121'; | ||
19 | |||||
20 | #----------------------------------------------------------------------------- | ||||
21 | |||||
22 | 1 | 2µs | 1 | 29µs | Readonly::Scalar my $DESC => q{"grep" used in boolean context}; # spent 29µs making 1 call to Readonly::Scalar |
23 | 1 | 2µs | 1 | 43µs | Readonly::Scalar my $EXPL => [71,72]; # spent 43µs making 1 call to Readonly::Scalar |
24 | |||||
25 | 1 | 3µs | 2 | 36µs | Readonly::Hash my %POSTFIX_CONDITIONALS => hashify( qw(if unless while until) ); # spent 33µs making 1 call to Readonly::Hash
# spent 4µs making 1 call to Perl::Critic::Utils::hashify |
26 | 1 | 2µs | 2 | 26µs | Readonly::Hash my %BOOLEAN_OPERATORS => hashify( qw(&& || ! not or and)); # spent 24µs making 1 call to Readonly::Hash
# spent 3µs making 1 call to Perl::Critic::Utils::hashify |
27 | |||||
28 | #----------------------------------------------------------------------------- | ||||
29 | |||||
30 | 1 | 4µs | # spent 1µs within Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::supported_parameters which was called:
# once (1µs+0s) by Perl::Critic::Policy::new at line 88 of Perl/Critic/Policy.pm | ||
31 | 1 | 2µs | # spent 8µs (6+1) within Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep::default_severity which was called:
# once (6µs+1µs) by Perl::Critic::Policy::get_severity at line 331 of Perl/Critic/Policy.pm | ||
32 | sub default_themes { return qw( core pbp performance certrec ) } | ||||
33 | sub applies_to { return 'PPI::Token::Word' } | ||||
34 | |||||
35 | #----------------------------------------------------------------------------- | ||||
36 | |||||
37 | sub violates { | ||||
38 | my ( $self, $elem, undef ) = @_; | ||||
39 | |||||
40 | return if $elem ne 'grep'; | ||||
41 | return if not is_function_call($elem); | ||||
42 | return if not _is_in_boolean_context($elem); | ||||
43 | |||||
44 | return $self->violation( $DESC, $EXPL, $elem ); | ||||
45 | } | ||||
46 | |||||
47 | #----------------------------------------------------------------------------- | ||||
48 | |||||
49 | sub _is_in_boolean_context { | ||||
50 | my ($token) = @_; | ||||
51 | |||||
52 | return _does_prev_sibling_cause_boolean($token) || _does_parent_cause_boolean($token); | ||||
53 | } | ||||
54 | |||||
55 | sub _does_prev_sibling_cause_boolean { | ||||
56 | my ($token) = @_; | ||||
57 | |||||
58 | my $prev = $token->sprevious_sibling; | ||||
59 | return if !$prev; | ||||
60 | return 1 if $prev->isa('PPI::Token::Word') and $POSTFIX_CONDITIONALS{$prev}; | ||||
61 | return if not ($prev->isa('PPI::Token::Operator') and $BOOLEAN_OPERATORS{$prev}); | ||||
62 | my $next = $token->snext_sibling; | ||||
63 | return 1 if not $next; # bizarre: grep with no arguments | ||||
64 | |||||
65 | # loose heuristic: unparenthesized grep has no following non-boolean operators | ||||
66 | return 1 if not $next->isa('PPI::Structure::List'); | ||||
67 | |||||
68 | $next = $next->snext_sibling; | ||||
69 | return 1 if not $next; | ||||
70 | return 1 if $next->isa('PPI::Token::Operator') and $BOOLEAN_OPERATORS{$next}; | ||||
71 | return; | ||||
72 | } | ||||
73 | |||||
74 | sub _does_parent_cause_boolean { | ||||
75 | my ($token) = @_; | ||||
76 | |||||
77 | my $prev = $token->sprevious_sibling; | ||||
78 | return if $prev; | ||||
79 | my $parent = $token->statement->parent; | ||||
80 | for (my $node = $parent; $node; $node = $node->parent) { ## no critic (CStyleForLoop) | ||||
81 | next if $node->isa('PPI::Structure::List'); | ||||
82 | return 1 if $node->isa('PPI::Structure::Condition'); | ||||
83 | } | ||||
84 | |||||
85 | return; | ||||
86 | } | ||||
87 | |||||
88 | 1 | 6µs | 1; | ||
89 | |||||
90 | __END__ |