| Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Perl/Critic/Policy/ValuesAndExpressions/ProhibitMismatchedOperators.pm |
| Statements | Executed 23 statements in 811µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 17µs | 17µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@9 |
| 1 | 1 | 1 | 8µs | 58µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@15 |
| 1 | 1 | 1 | 8µs | 11µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@11 |
| 1 | 1 | 1 | 8µs | 29µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@12 |
| 1 | 1 | 1 | 7µs | 19µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@10 |
| 1 | 1 | 1 | 7µs | 187µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@14 |
| 1 | 1 | 1 | 6µs | 7µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::default_severity |
| 1 | 1 | 1 | 2µs | 2µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::supported_parameters |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::_get_potential_leading_operator |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::_get_token_compatibility |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::_have_stringy_x |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::_is_file_operator |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::applies_to |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::default_themes |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::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::ValuesAndExpressions::ProhibitMismatchedOperators; | ||||
| 9 | 2 | 44µs | 1 | 17µs | # spent 17µs within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@9 which was called:
# once (17µs+0s) by Module::Pluggable::Object::_require at line 9 # spent 17µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@9 |
| 10 | 2 | 21µs | 2 | 30µs | # spent 19µs (7+11) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@10 which was called:
# once (7µs+11µs) by Module::Pluggable::Object::_require at line 10 # spent 19µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@10
# spent 11µs making 1 call to strict::import |
| 11 | 2 | 20µs | 2 | 15µs | # spent 11µs (8+4) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@11 which was called:
# once (8µs+4µs) by Module::Pluggable::Object::_require at line 11 # spent 11µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@11
# spent 4µs making 1 call to warnings::import |
| 12 | 2 | 23µs | 2 | 50µs | # spent 29µs (8+21) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@12 which was called:
# once (8µs+21µs) by Module::Pluggable::Object::_require at line 12 # spent 29µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@12
# spent 21µs making 1 call to Exporter::import |
| 13 | |||||
| 14 | 2 | 27µs | 2 | 366µs | # spent 187µs (7+179) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@14 which was called:
# once (7µs+179µs) by Module::Pluggable::Object::_require at line 14 # spent 187µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@14
# spent 179µs making 1 call to Exporter::import |
| 15 | 2 | 503µs | 2 | 108µs | # spent 58µs (8+50) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@15 which was called:
# once (8µs+50µs) by Module::Pluggable::Object::_require at line 15 # spent 58µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::BEGIN@15
# spent 50µs making 1 call to base::import |
| 16 | |||||
| 17 | 1 | 600ns | our $VERSION = '1.121'; | ||
| 18 | |||||
| 19 | #----------------------------------------------------------------------------- | ||||
| 20 | |||||
| 21 | 1 | 2µs | 1 | 29µs | Readonly::Scalar my $DESC => q<Mismatched operator>; # spent 29µs making 1 call to Readonly::Scalar |
| 22 | 1 | 1µs | 1 | 21µs | Readonly::Scalar my $EXPL => q<Numeric/string operators and operands should match>; # spent 21µs making 1 call to Readonly::Scalar |
| 23 | |||||
| 24 | # token compatibility [ numeric, string ] | ||||
| 25 | 1 | 13µs | 7 | 97µs | Readonly::Hash my %TOKEN_COMPATIBILITY => ( # spent 90µs making 1 call to Readonly::Hash
# spent 7µs making 6 calls to Readonly::Scalar::FETCH, avg 1µs/call |
| 26 | 'PPI::Token::Number' => [$TRUE, $FALSE], | ||||
| 27 | 'PPI::Token::Symbol' => [$TRUE, $TRUE ], | ||||
| 28 | 'PPI::Token::Quote' => [$FALSE, $TRUE ], | ||||
| 29 | ); | ||||
| 30 | |||||
| 31 | Readonly::Hash my %FILE_OPERATOR_COMPATIBILITY => | ||||
| 32 | 1 | 97µs | 53 | 575µs | map {; "-$_" => [$TRUE, $FALSE] } # spent 524µs making 1 call to Readonly::Hash
# spent 51µs making 52 calls to Readonly::Scalar::FETCH, avg 988ns/call |
| 33 | qw< r w x o R W X O e z s f d l p S b c t u g k T B M A >; | ||||
| 34 | |||||
| 35 | 1 | 1µs | 1 | 22µs | Readonly::Scalar my $TOKEN_COMPATIBILITY_INDEX_NUMERIC => 0; # spent 22µs making 1 call to Readonly::Scalar |
| 36 | 1 | 1µs | 1 | 21µs | Readonly::Scalar my $TOKEN_COMPATIBILITY_INDEX_STRING => 1; # spent 21µs making 1 call to Readonly::Scalar |
| 37 | |||||
| 38 | Readonly::Hash my %OPERATOR_TYPES => ( | ||||
| 39 | # numeric | ||||
| 40 | ( | ||||
| 41 | map { $_ => $TOKEN_COMPATIBILITY_INDEX_NUMERIC } | ||||
| 42 | qw[ == != > >= < <= + - * / += -= *= /= ] | ||||
| 43 | ), | ||||
| 44 | # string | ||||
| 45 | 1 | 40µs | 23 | 65µs | map { $_ => $TOKEN_COMPATIBILITY_INDEX_STRING } # spent 44µs making 1 call to Readonly::Hash
# spent 22µs making 22 calls to Readonly::Scalar::FETCH, avg 991ns/call |
| 46 | qw< eq ne lt gt le ge . .= >, | ||||
| 47 | ); | ||||
| 48 | |||||
| 49 | #----------------------------------------------------------------------------- | ||||
| 50 | |||||
| 51 | 1 | 4µs | # spent 2µs within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::supported_parameters which was called:
# once (2µs+0s) by Perl::Critic::Policy::new at line 88 of Perl/Critic/Policy.pm | ||
| 52 | 1 | 1µs | # spent 7µs (6+1) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators::default_severity which was called:
# once (6µs+1µs) by Perl::Critic::Policy::get_severity at line 331 of Perl/Critic/Policy.pm | ||
| 53 | sub default_themes { return qw< core bugs certrule > } | ||||
| 54 | sub applies_to { return 'PPI::Token::Operator' } | ||||
| 55 | |||||
| 56 | #----------------------------------------------------------------------------- | ||||
| 57 | |||||
| 58 | sub violates { | ||||
| 59 | my ($self, $elem) = @_; | ||||
| 60 | |||||
| 61 | my $elem_text = $elem->content(); | ||||
| 62 | |||||
| 63 | return if not exists $OPERATOR_TYPES{$elem_text}; | ||||
| 64 | |||||
| 65 | my $leading_operator = $self->_get_potential_leading_operator($elem) | ||||
| 66 | or return; | ||||
| 67 | |||||
| 68 | my $next_elem = $elem->snext_sibling() or return; | ||||
| 69 | |||||
| 70 | if ( $next_elem->isa('PPI::Token::Operator') ) { | ||||
| 71 | $elem_text .= $next_elem->content(); | ||||
| 72 | $next_elem = $next_elem->snext_sibling(); | ||||
| 73 | } | ||||
| 74 | |||||
| 75 | return if not exists $OPERATOR_TYPES{$elem_text}; | ||||
| 76 | my $operator_type = $OPERATOR_TYPES{$elem_text}; | ||||
| 77 | |||||
| 78 | my $leading_operator_compatibility = | ||||
| 79 | $self->_get_token_compatibility($leading_operator); | ||||
| 80 | my $next_compatibility = $self->_get_token_compatibility($next_elem); | ||||
| 81 | |||||
| 82 | return if | ||||
| 83 | ( | ||||
| 84 | ! defined $leading_operator_compatibility | ||||
| 85 | || $leading_operator_compatibility->[$operator_type] | ||||
| 86 | ) | ||||
| 87 | && ( | ||||
| 88 | ! defined $next_compatibility | ||||
| 89 | || $next_compatibility->[$operator_type] | ||||
| 90 | ); | ||||
| 91 | |||||
| 92 | return if | ||||
| 93 | $operator_type | ||||
| 94 | && defined $leading_operator_compatibility | ||||
| 95 | && ! $leading_operator_compatibility->[$operator_type] | ||||
| 96 | && $self->_have_stringy_x($leading_operator); # RT 54524 | ||||
| 97 | |||||
| 98 | return $self->violation($DESC, $EXPL, $elem); | ||||
| 99 | } | ||||
| 100 | |||||
| 101 | #----------------------------------------------------------------------------- | ||||
| 102 | |||||
| 103 | sub _get_token_compatibility { | ||||
| 104 | my ($self, $elem) = @_; | ||||
| 105 | |||||
| 106 | return $FILE_OPERATOR_COMPATIBILITY{ $elem->content() } | ||||
| 107 | if $self->_is_file_operator($elem); | ||||
| 108 | |||||
| 109 | for my $class (keys %TOKEN_COMPATIBILITY) { | ||||
| 110 | return $TOKEN_COMPATIBILITY{$class} if $elem->isa($class); | ||||
| 111 | } | ||||
| 112 | |||||
| 113 | return; | ||||
| 114 | } | ||||
| 115 | |||||
| 116 | #----------------------------------------------------------------------------- | ||||
| 117 | |||||
| 118 | sub _have_stringy_x { | ||||
| 119 | my ($self, $elem) = @_; | ||||
| 120 | |||||
| 121 | return if not $elem; | ||||
| 122 | |||||
| 123 | my $prev_oper = $elem->sprevious_sibling() or return; | ||||
| 124 | |||||
| 125 | return if not $prev_oper->isa('PPI::Token::Operator'); | ||||
| 126 | return if 'x' ne $prev_oper->content(); | ||||
| 127 | |||||
| 128 | return !! $prev_oper->sprevious_sibling(); | ||||
| 129 | } | ||||
| 130 | |||||
| 131 | #----------------------------------------------------------------------------- | ||||
| 132 | |||||
| 133 | sub _get_potential_leading_operator { | ||||
| 134 | my ($self, $elem) = @_; | ||||
| 135 | |||||
| 136 | my $previous_element = $elem->sprevious_sibling() or return; | ||||
| 137 | |||||
| 138 | if ( $self->_get_token_compatibility($previous_element) ) { | ||||
| 139 | my $previous_sibling = $previous_element->sprevious_sibling(); | ||||
| 140 | if ( | ||||
| 141 | $previous_sibling and $self->_is_file_operator($previous_sibling) | ||||
| 142 | ) { | ||||
| 143 | $previous_element = $previous_sibling; | ||||
| 144 | } | ||||
| 145 | } | ||||
| 146 | |||||
| 147 | return $previous_element; | ||||
| 148 | } | ||||
| 149 | |||||
| 150 | #----------------------------------------------------------------------------- | ||||
| 151 | |||||
| 152 | sub _is_file_operator { | ||||
| 153 | my ($self, $elem) = @_; | ||||
| 154 | |||||
| 155 | return if not $elem; | ||||
| 156 | return if not $elem->isa('PPI::Token::Operator'); | ||||
| 157 | return !! $FILE_OPERATOR_COMPATIBILITY{ $elem->content() } | ||||
| 158 | } | ||||
| 159 | |||||
| 160 | 1 | 12µs | 1; | ||
| 161 | |||||
| 162 | __END__ |