| Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Perl/Critic/Policy/ValuesAndExpressions/ProhibitMagicNumbers.pm |
| Statements | Executed 42 statements in 1.62ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 3 | 3 | 1 | 87µs | 99µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:regcomp (opcode) |
| 1 | 1 | 1 | 26µs | 28µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_determine_allowed_values |
| 1 | 1 | 1 | 25µs | 58µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_parse_allowed_values |
| 1 | 1 | 1 | 16µs | 16µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@10 |
| 1 | 1 | 1 | 15µs | 15µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::supported_parameters |
| 1 | 1 | 1 | 10µs | 21µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@11 |
| 1 | 1 | 1 | 7µs | 420µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@16 |
| 1 | 1 | 1 | 7µs | 11µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@12 |
| 1 | 1 | 1 | 7µs | 28µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@14 |
| 1 | 1 | 1 | 7µs | 61µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@18 |
| 1 | 1 | 1 | 6µs | 7µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::default_severity |
| 3 | 3 | 1 | 4µs | 4µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:qr (opcode) |
| 1 | 1 | 1 | 4µs | 4µs | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:sort (opcode) |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_determine_checked_types |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_element_is_a_package_statement_version_number |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_element_is_in_a_constant_subroutine |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_element_is_in_a_plan_statement |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_element_is_in_an_include_readonly_or_version_statement |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_element_is_sole_component_of_a_subscript |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_element_is_to_the_right_of_a_fat_comma |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::applies_to |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::default_maximum_violations_per_document |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::default_themes |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::initialize_if_enabled |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::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::ProhibitMagicNumbers; | ||||
| 9 | |||||
| 10 | 2 | 46µs | 1 | 16µs | # spent 16µs within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::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::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@10 |
| 11 | 2 | 20µs | 2 | 32µs | # spent 21µs (10+11) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@11 which was called:
# once (10µs+11µs) by Module::Pluggable::Object::_require at line 11 # spent 21µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@11
# spent 11µs making 1 call to strict::import |
| 12 | 2 | 18µs | 2 | 15µs | # spent 11µs (7+4) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::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::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@12
# spent 4µs making 1 call to warnings::import |
| 13 | |||||
| 14 | 2 | 24µs | 2 | 49µs | # spent 28µs (7+21) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@14 which was called:
# once (7µs+21µs) by Module::Pluggable::Object::_require at line 14 # spent 28µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@14
# spent 21µs making 1 call to Exporter::import |
| 15 | |||||
| 16 | 2 | 25µs | 2 | 834µs | # spent 420µs (7+413) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@16 which was called:
# once (7µs+413µs) by Module::Pluggable::Object::_require at line 16 # spent 420µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@16
# spent 413µs making 1 call to Exporter::import |
| 17 | |||||
| 18 | 2 | 1.31ms | 2 | 114µs | # spent 61µs (7+53) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@18 which was called:
# once (7µs+53µs) by Module::Pluggable::Object::_require at line 18 # spent 61µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::BEGIN@18
# spent 53µs making 1 call to base::import |
| 19 | |||||
| 20 | 1 | 600ns | our $VERSION = '1.121'; | ||
| 21 | |||||
| 22 | #---------------------------------------------------------------------------- | ||||
| 23 | |||||
| 24 | 1 | 2µs | 1 | 31µs | Readonly::Scalar my $EXPL => # spent 31µs making 1 call to Readonly::Scalar |
| 25 | q{Unnamed numeric literals make code less maintainable}; | ||||
| 26 | 1 | 900ns | 1 | 21µs | Readonly::Scalar my $USE_READONLY_OR_CONSTANT => # spent 21µs making 1 call to Readonly::Scalar |
| 27 | ' Use the Readonly or Const::Fast module or the "constant" pragma instead'; | ||||
| 28 | 1 | 4µs | 2 | 23µs | Readonly::Scalar my $TYPE_NOT_ALLOWED_SUFFIX => # spent 21µs making 1 call to Readonly::Scalar
# spent 2µs making 1 call to Readonly::Scalar::FETCH |
| 29 | ") are not allowed.$USE_READONLY_OR_CONSTANT"; | ||||
| 30 | |||||
| 31 | 1 | 27µs | 5 | 47µs | Readonly::Scalar my $UNSIGNED_NUMBER => # spent 23µs making 1 call to Readonly::Scalar
# spent 20µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:regcomp
# spent 3µs making 2 calls to Readonly::Scalar::FETCH, avg 1µs/call
# spent 2µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:qr |
| 32 | qr{ | ||||
| 33 | \d+ (?: [$PERIOD] \d+ )? # 1, 1.5, etc. | ||||
| 34 | | [$PERIOD] \d+ # .3, .7, etc. | ||||
| 35 | }xms; | ||||
| 36 | 1 | 22µs | 4 | 42µs | Readonly::Scalar my $SIGNED_NUMBER => qr/ [-+]? $UNSIGNED_NUMBER /xms; # spent 22µs making 1 call to Readonly::Scalar
# spent 18µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:regcomp
# spent 1µs making 1 call to Readonly::Scalar::FETCH
# spent 1µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:qr |
| 37 | |||||
| 38 | 1 | 53µs | 11 | 94µs | Readonly::Scalar my $RANGE => # spent 61µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:regcomp
# spent 23µs making 1 call to Readonly::Scalar
# spent 9µs making 8 calls to Readonly::Scalar::FETCH, avg 1µs/call
# spent 1µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:qr |
| 39 | qr{ | ||||
| 40 | \A | ||||
| 41 | ($SIGNED_NUMBER) | ||||
| 42 | [$PERIOD] [$PERIOD] | ||||
| 43 | ($SIGNED_NUMBER) | ||||
| 44 | (?: | ||||
| 45 | [$COLON] by [$LEFT_PAREN] | ||||
| 46 | ($UNSIGNED_NUMBER) | ||||
| 47 | [$RIGHT_PAREN] | ||||
| 48 | )? | ||||
| 49 | \z | ||||
| 50 | }xms; | ||||
| 51 | |||||
| 52 | 1 | 900ns | 1 | 20µs | Readonly::Scalar my $SPECIAL_ARRAY_SUBSCRIPT_EXEMPTION => -1; # spent 20µs making 1 call to Readonly::Scalar |
| 53 | |||||
| 54 | #---------------------------------------------------------------------------- | ||||
| 55 | |||||
| 56 | # spent 15µs within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::supported_parameters which was called:
# once (15µs+0s) by Perl::Critic::Policy::new at line 88 of Perl/Critic/Policy.pm | ||||
| 57 | return ( | ||||
| 58 | { | ||||
| 59 | 1 | 18µs | name => 'allowed_values', | ||
| 60 | description => 'Individual and ranges of values to allow, and/or "all_integers".', | ||||
| 61 | default_string => '0 1 2', | ||||
| 62 | parser => \&_parse_allowed_values, | ||||
| 63 | }, | ||||
| 64 | { | ||||
| 65 | name => 'allowed_types', | ||||
| 66 | description => 'Kind of literals to allow.', | ||||
| 67 | default_string => 'Float', | ||||
| 68 | behavior => 'enumeration', | ||||
| 69 | enumeration_values => [ qw{ Binary Exp Float Hex Octal } ], | ||||
| 70 | enumeration_allow_multiple_values => 1, | ||||
| 71 | }, | ||||
| 72 | { | ||||
| 73 | name => 'allow_to_the_right_of_a_fat_comma', | ||||
| 74 | description => | ||||
| 75 | q[Should anything to the right of a "=>" be allowed?], | ||||
| 76 | default_string => '1', | ||||
| 77 | behavior => 'boolean', | ||||
| 78 | }, | ||||
| 79 | { | ||||
| 80 | name => 'constant_creator_subroutines', | ||||
| 81 | description => q{Names of subroutines that create constants}, | ||||
| 82 | behavior => 'string list', | ||||
| 83 | list_always_present_values => [ | ||||
| 84 | qw< | ||||
| 85 | Readonly Readonly::Scalar Readonly::Array Readonly::Hash | ||||
| 86 | const | ||||
| 87 | >, | ||||
| 88 | ], | ||||
| 89 | }, | ||||
| 90 | ); | ||||
| 91 | } | ||||
| 92 | |||||
| 93 | 1 | 2µs | # spent 7µs (6+1) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::default_severity which was called:
# once (6µs+1µs) by Perl::Critic::Policy::get_severity at line 331 of Perl/Critic/Policy.pm | ||
| 94 | sub default_themes { return qw( core maintenance certrec ) } | ||||
| 95 | sub applies_to { return 'PPI::Token::Number' } | ||||
| 96 | |||||
| 97 | sub default_maximum_violations_per_document { return 10; } | ||||
| 98 | |||||
| 99 | #---------------------------------------------------------------------------- | ||||
| 100 | |||||
| 101 | sub initialize_if_enabled { | ||||
| 102 | my ($self, $config) = @_; | ||||
| 103 | |||||
| 104 | $self->_determine_checked_types(); | ||||
| 105 | |||||
| 106 | return $TRUE; | ||||
| 107 | } | ||||
| 108 | |||||
| 109 | # spent 58µs (25+33) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_parse_allowed_values which was called:
# once (25µs+33µs) by Perl::Critic::PolicyParameter::parse_and_validate_config_value at line 231 of Perl/Critic/PolicyParameter.pm | ||||
| 110 | 1 | 600ns | my ($self, $parameter, $config_string) = @_; | ||
| 111 | |||||
| 112 | 1 | 3µs | 1 | 28µs | my ( $all_integers_allowed, $allowed_values ) # spent 28µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_determine_allowed_values |
| 113 | = _determine_allowed_values($config_string); | ||||
| 114 | |||||
| 115 | 1 | 500ns | my $allowed_string = ' is not one of the allowed literal values ('; | ||
| 116 | 1 | 200ns | if ($all_integers_allowed) { | ||
| 117 | $allowed_string .= 'all integers'; | ||||
| 118 | |||||
| 119 | if ( %{$allowed_values} ) { | ||||
| 120 | $allowed_string .= ', '; | ||||
| 121 | } | ||||
| 122 | } | ||||
| 123 | $allowed_string | ||||
| 124 | 1 | 16µs | 2 | 5µs | .= ( join ', ', sort { $a <=> $b } keys %{$allowed_values} ) . ').' # spent 4µs making 1 call to Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:sort
# spent 2µs making 1 call to Readonly::Scalar::FETCH |
| 125 | . $USE_READONLY_OR_CONSTANT; | ||||
| 126 | |||||
| 127 | 1 | 900ns | $self->{_allowed_values} = $allowed_values; | ||
| 128 | 1 | 700ns | $self->{_all_integers_allowed} = $all_integers_allowed; | ||
| 129 | 1 | 700ns | $self->{_allowed_string} = $allowed_string; | ||
| 130 | |||||
| 131 | 1 | 4µs | return; | ||
| 132 | } | ||||
| 133 | |||||
| 134 | # spent 28µs (26+2) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_determine_allowed_values which was called:
# once (26µs+2µs) by Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_parse_allowed_values at line 112 | ||||
| 135 | 1 | 300ns | my ($config_string) = @_; | ||
| 136 | |||||
| 137 | 1 | 200ns | my @allowed_values; | ||
| 138 | 1 | 100ns | my @potential_allowed_values; | ||
| 139 | 1 | 500ns | my $all_integers_allowed = 0; | ||
| 140 | |||||
| 141 | 1 | 700ns | if ( defined $config_string ) { | ||
| 142 | my @allowed_values_strings = | ||||
| 143 | grep {$_} split m/\s+/xms, $config_string; | ||||
| 144 | |||||
| 145 | foreach my $value_string (@allowed_values_strings) { | ||||
| 146 | if ($value_string eq 'all_integers') { | ||||
| 147 | $all_integers_allowed = 1; | ||||
| 148 | } elsif ( $value_string =~ m/ \A $SIGNED_NUMBER \z /xms ) { | ||||
| 149 | push @potential_allowed_values, $value_string + 0; | ||||
| 150 | } elsif ( $value_string =~ m/$RANGE/xms ) { | ||||
| 151 | my ( $minimum, $maximum, $increment ) = ($1, $2, $3); | ||||
| 152 | $increment ||= 1; | ||||
| 153 | |||||
| 154 | $minimum += 0; | ||||
| 155 | $maximum += 0; | ||||
| 156 | $increment += 0; | ||||
| 157 | |||||
| 158 | for ( ## no critic (ProhibitCStyleForLoops) | ||||
| 159 | my $value = $minimum; | ||||
| 160 | $value <= $maximum; | ||||
| 161 | $value += $increment | ||||
| 162 | ) { | ||||
| 163 | push @potential_allowed_values, $value; | ||||
| 164 | } | ||||
| 165 | } else { | ||||
| 166 | die q{Invalid value for allowed_values: }, $value_string, | ||||
| 167 | q{. Must be a number, a number range, or}, | ||||
| 168 | qq{ "all_integers".\n}; | ||||
| 169 | } | ||||
| 170 | } | ||||
| 171 | |||||
| 172 | if ($all_integers_allowed) { | ||||
| 173 | @allowed_values = grep { $_ != int $_ } @potential_allowed_values; | ||||
| 174 | } else { | ||||
| 175 | @allowed_values = @potential_allowed_values; | ||||
| 176 | } | ||||
| 177 | } else { | ||||
| 178 | 1 | 500ns | @allowed_values = (2); | ||
| 179 | } | ||||
| 180 | |||||
| 181 | 1 | 800ns | if ( not $all_integers_allowed ) { | ||
| 182 | push @allowed_values, 0, 1; | ||||
| 183 | } | ||||
| 184 | 1 | 4µs | 1 | 2µs | my %allowed_values = hashify(@allowed_values); # spent 2µs making 1 call to Perl::Critic::Utils::hashify |
| 185 | |||||
| 186 | 1 | 5µs | return ( $all_integers_allowed, \%allowed_values ); | ||
| 187 | } | ||||
| 188 | |||||
| 189 | sub _determine_checked_types { | ||||
| 190 | my ($self) = @_; | ||||
| 191 | |||||
| 192 | my %checked_types = ( | ||||
| 193 | 'PPI::Token::Number::Binary' => 'Binary literals (', | ||||
| 194 | 'PPI::Token::Number::Float' => 'Floating-point literals (', | ||||
| 195 | 'PPI::Token::Number::Exp' => 'Exponential literals (', | ||||
| 196 | 'PPI::Token::Number::Hex' => 'Hexadecimal literals (', | ||||
| 197 | 'PPI::Token::Number::Octal' => 'Octal literals (', | ||||
| 198 | 'PPI::Token::Number::Version' => 'Version literals (', | ||||
| 199 | ); | ||||
| 200 | |||||
| 201 | # This will be set by the enumeration behavior specified in | ||||
| 202 | # supported_parameters() above. | ||||
| 203 | my $allowed_types = $self->{_allowed_types}; | ||||
| 204 | |||||
| 205 | foreach my $allowed_type ( keys %{$allowed_types} ) { | ||||
| 206 | delete $checked_types{"PPI::Token::Number::$allowed_type"}; | ||||
| 207 | |||||
| 208 | if ( $allowed_type eq 'Exp' ) { | ||||
| 209 | |||||
| 210 | # because an Exp isa(Float). | ||||
| 211 | delete $checked_types{'PPI::Token::Number::Float'}; | ||||
| 212 | } | ||||
| 213 | } | ||||
| 214 | |||||
| 215 | $self->{_checked_types} = \%checked_types; | ||||
| 216 | |||||
| 217 | return; | ||||
| 218 | } | ||||
| 219 | |||||
| 220 | |||||
| 221 | sub violates { | ||||
| 222 | my ( $self, $elem, undef ) = @_; | ||||
| 223 | |||||
| 224 | if ( $self->{_allow_to_the_right_of_a_fat_comma} ) { | ||||
| 225 | return if _element_is_to_the_right_of_a_fat_comma($elem); | ||||
| 226 | } | ||||
| 227 | |||||
| 228 | return if _element_is_in_an_include_readonly_or_version_statement( | ||||
| 229 | $self, $elem, | ||||
| 230 | ); | ||||
| 231 | return if _element_is_in_a_plan_statement($elem); | ||||
| 232 | return if _element_is_in_a_constant_subroutine($elem); | ||||
| 233 | return if _element_is_a_package_statement_version_number($elem); | ||||
| 234 | |||||
| 235 | my $literal = $elem->literal(); | ||||
| 236 | if ( | ||||
| 237 | defined $literal | ||||
| 238 | and not ( | ||||
| 239 | $self->{_all_integers_allowed} | ||||
| 240 | and int $literal == $literal | ||||
| 241 | ) | ||||
| 242 | and not defined $self->{_allowed_values}{$literal} | ||||
| 243 | and not ( | ||||
| 244 | _element_is_sole_component_of_a_subscript($elem) | ||||
| 245 | and $literal == $SPECIAL_ARRAY_SUBSCRIPT_EXEMPTION | ||||
| 246 | ) | ||||
| 247 | ) { | ||||
| 248 | return | ||||
| 249 | $self->violation( | ||||
| 250 | $elem->content() . $self->{_allowed_string}, | ||||
| 251 | $EXPL, | ||||
| 252 | $elem, | ||||
| 253 | ); | ||||
| 254 | } | ||||
| 255 | |||||
| 256 | |||||
| 257 | my ( $number_type, $type_string ); | ||||
| 258 | |||||
| 259 | while ( | ||||
| 260 | ( $number_type, $type_string ) = ( each %{ $self->{_checked_types} } ) | ||||
| 261 | ) { | ||||
| 262 | if ( $elem->isa($number_type) ) { | ||||
| 263 | return | ||||
| 264 | $self->violation( | ||||
| 265 | $type_string . $elem->content() . $TYPE_NOT_ALLOWED_SUFFIX, | ||||
| 266 | $EXPL, | ||||
| 267 | $elem, | ||||
| 268 | ); | ||||
| 269 | } | ||||
| 270 | } | ||||
| 271 | |||||
| 272 | return; | ||||
| 273 | } | ||||
| 274 | |||||
| 275 | sub _element_is_to_the_right_of_a_fat_comma { | ||||
| 276 | my ($elem) = @_; | ||||
| 277 | |||||
| 278 | my $previous = $elem->sprevious_sibling() or return; | ||||
| 279 | |||||
| 280 | $previous->isa('PPI::Token::Operator') or return; | ||||
| 281 | |||||
| 282 | return $previous->content() eq q[=>]; | ||||
| 283 | } | ||||
| 284 | |||||
| 285 | sub _element_is_sole_component_of_a_subscript { | ||||
| 286 | my ($elem) = @_; | ||||
| 287 | |||||
| 288 | my $parent = $elem->parent(); | ||||
| 289 | if ( $parent and $parent->isa('PPI::Statement::Expression') ) { | ||||
| 290 | if ( $parent->schildren() > 1 ) { | ||||
| 291 | return 0; | ||||
| 292 | } | ||||
| 293 | |||||
| 294 | my $grandparent = $parent->parent(); | ||||
| 295 | if ( | ||||
| 296 | $grandparent | ||||
| 297 | and $grandparent->isa('PPI::Structure::Subscript') | ||||
| 298 | ) { | ||||
| 299 | return 1; | ||||
| 300 | } | ||||
| 301 | } | ||||
| 302 | |||||
| 303 | return 0; | ||||
| 304 | } | ||||
| 305 | |||||
| 306 | sub _element_is_in_an_include_readonly_or_version_statement { | ||||
| 307 | my ($self, $elem) = @_; | ||||
| 308 | |||||
| 309 | my $parent = $elem->parent(); | ||||
| 310 | while ($parent) { | ||||
| 311 | if ( $parent->isa('PPI::Statement') ) { | ||||
| 312 | return 1 if $parent->isa('PPI::Statement::Include'); | ||||
| 313 | |||||
| 314 | if ( $parent->isa('PPI::Statement::Variable') ) { | ||||
| 315 | if ( $parent->type() eq 'our' ) { | ||||
| 316 | my @variables = $parent->variables(); | ||||
| 317 | if ( | ||||
| 318 | scalar @variables == 1 | ||||
| 319 | and $variables[0] eq '$VERSION' ## no critic (RequireInterpolationOfMetachars) | ||||
| 320 | ) { | ||||
| 321 | return 1; | ||||
| 322 | } | ||||
| 323 | } | ||||
| 324 | |||||
| 325 | return 0; | ||||
| 326 | } | ||||
| 327 | |||||
| 328 | my $first_token = $parent->first_token(); | ||||
| 329 | if ( $first_token->isa('PPI::Token::Word') ) { | ||||
| 330 | if ( $self->{_constant_creator_subroutines}{ | ||||
| 331 | $first_token->content() } ) { | ||||
| 332 | return 1; | ||||
| 333 | } | ||||
| 334 | } elsif ($parent->isa('PPI::Structure::Block')) { | ||||
| 335 | return 0; | ||||
| 336 | } | ||||
| 337 | } | ||||
| 338 | |||||
| 339 | $parent = $parent->parent(); | ||||
| 340 | } | ||||
| 341 | |||||
| 342 | return 0; | ||||
| 343 | } | ||||
| 344 | |||||
| 345 | # Allow "plan tests => 39;". | ||||
| 346 | |||||
| 347 | 1 | 800ns | 1 | 19µs | Readonly::Scalar my $PLAN_STATEMENT_MINIMUM_TOKENS => 4; # spent 19µs making 1 call to Readonly::Scalar |
| 348 | |||||
| 349 | sub _element_is_in_a_plan_statement { | ||||
| 350 | my ($elem) = @_; | ||||
| 351 | |||||
| 352 | my $parent = $elem->parent(); | ||||
| 353 | return 0 if not $parent; | ||||
| 354 | |||||
| 355 | return 0 if not $parent->isa('PPI::Statement'); | ||||
| 356 | |||||
| 357 | my @children = $parent->schildren(); | ||||
| 358 | return 0 if @children < $PLAN_STATEMENT_MINIMUM_TOKENS; | ||||
| 359 | |||||
| 360 | return 0 if not $children[0]->isa('PPI::Token::Word'); | ||||
| 361 | return 0 if $children[0]->content() ne 'plan'; | ||||
| 362 | |||||
| 363 | return 0 if not $children[1]->isa('PPI::Token::Word'); | ||||
| 364 | return 0 if $children[1]->content() ne 'tests'; | ||||
| 365 | |||||
| 366 | return 0 if not $children[2]->isa('PPI::Token::Operator'); | ||||
| 367 | return 0 if $children[2]->content() ne '=>'; | ||||
| 368 | |||||
| 369 | return 1; | ||||
| 370 | } | ||||
| 371 | |||||
| 372 | sub _element_is_in_a_constant_subroutine { | ||||
| 373 | my ($elem) = @_; | ||||
| 374 | |||||
| 375 | my $parent = $elem->parent(); | ||||
| 376 | return 0 if not $parent; | ||||
| 377 | |||||
| 378 | return 0 if not $parent->isa('PPI::Statement'); | ||||
| 379 | |||||
| 380 | my $following = $elem->snext_sibling(); | ||||
| 381 | if ($following) { | ||||
| 382 | return 0 if not $following->isa('PPI::Token::Structure'); | ||||
| 383 | return 0 if $following->content() ne $SCOLON; | ||||
| 384 | return 0 if $following->snext_sibling(); | ||||
| 385 | } | ||||
| 386 | |||||
| 387 | my $preceding = $elem->sprevious_sibling(); | ||||
| 388 | if ($preceding) { | ||||
| 389 | return 0 if not $preceding->isa('PPI::Token::Word'); | ||||
| 390 | return 0 if $preceding->content() ne 'return'; | ||||
| 391 | return 0 if $preceding->sprevious_sibling(); | ||||
| 392 | } | ||||
| 393 | |||||
| 394 | return 0 if $parent->snext_sibling(); | ||||
| 395 | return 0 if $parent->sprevious_sibling(); | ||||
| 396 | |||||
| 397 | my $grandparent = $parent->parent(); | ||||
| 398 | return 0 if not $grandparent; | ||||
| 399 | |||||
| 400 | return 0 if not $grandparent->isa('PPI::Structure::Block'); | ||||
| 401 | |||||
| 402 | my $greatgrandparent = $grandparent->parent(); | ||||
| 403 | return 0 if not $greatgrandparent; | ||||
| 404 | return 0 if not $greatgrandparent->isa('PPI::Statement::Sub'); | ||||
| 405 | |||||
| 406 | return 1; | ||||
| 407 | } | ||||
| 408 | |||||
| 409 | sub _element_is_a_package_statement_version_number { | ||||
| 410 | my ($elem) = @_; | ||||
| 411 | |||||
| 412 | my $parent = $elem->statement() | ||||
| 413 | or return 0; | ||||
| 414 | |||||
| 415 | $parent->isa( 'PPI::Statement::Package' ) | ||||
| 416 | or return 0; | ||||
| 417 | |||||
| 418 | my $version = $parent->schild( 2 ) | ||||
| 419 | or return 0; | ||||
| 420 | |||||
| 421 | return $version == $elem; | ||||
| 422 | } | ||||
| 423 | |||||
| 424 | 1 | 9µs | 1; | ||
| 425 | |||||
| 426 | __END__ | ||||
# spent 4µs within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:qr which was called 3 times, avg 1µs/call:
# once (2µs+0s) by Module::Pluggable::Object::_require at line 31
# once (1µs+0s) by Module::Pluggable::Object::_require at line 38
# once (1µs+0s) by Module::Pluggable::Object::_require at line 36 | |||||
# spent 99µs (87+13) within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:regcomp which was called 3 times, avg 33µs/call:
# once (53µs+9µs) by Module::Pluggable::Object::_require at line 38
# once (18µs+3µs) by Module::Pluggable::Object::_require at line 31
# once (16µs+1µs) by Module::Pluggable::Object::_require at line 36 | |||||
# spent 4µs within Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::CORE:sort which was called:
# once (4µs+0s) by Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers::_parse_allowed_values at line 124 |