| 1 | package Perl::Critic::Policy::ValuesAndExpressions::ProhibitQuotesAsQuotelikeOperatorDelimiters; | ||||||||||
| 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 :characters :severities :data_conversion | ||||||||||
| 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::Hash my %DESCRIPTIONS => ( | |||||||||
| 18 | $QUOTE => q{Single-quote used as quote-like operator delimiter}, | ||||||||||
| 19 | $DQUOTE => q{Double-quote used as quote-like operator delimiter}, | ||||||||||
| 20 | $BACKTICK => q{Back-quote (back-tick) used as quote-like operator delimiter}, | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | 40 | Readonly::Scalar my $EXPL => | |||||||||
| 24 | q{Using quotes as delimiters for quote-like operators obfuscates code}; | ||||||||||
| 25 | |||||||||||
| 26 | 40 | Readonly::Array my @OPERATORS => qw{ m q qq qr qw qx s tr y }; | |||||||||
| 27 | |||||||||||
| 28 | 40 | Readonly::Hash my %INFO_RETRIEVERS_BY_PPI_CLASS => ( | |||||||||
| 29 | 'PPI::Token::Quote::Literal' => \&_info_for_single_character_operator, | ||||||||||
| 30 | 'PPI::Token::Quote::Interpolate' => \&_info_for_two_character_operator, | ||||||||||
| 31 | 'PPI::Token::QuoteLike::Command' => \&_info_for_two_character_operator, | ||||||||||
| 32 | 'PPI::Token::QuoteLike::Regexp' => \&_info_for_two_character_operator, | ||||||||||
| 33 | 'PPI::Token::QuoteLike::Words' => \&_info_for_two_character_operator, | ||||||||||
| 34 | 'PPI::Token::Regexp::Match' => \&_info_for_match, | ||||||||||
| 35 | 'PPI::Token::Regexp::Substitute' => \&_info_for_single_character_operator, | ||||||||||
| 36 | 'PPI::Token::Regexp::Transliterate' => \&_info_for_transliterate, | ||||||||||
| 37 | ); | ||||||||||
| 38 | |||||||||||
| 39 | #----------------------------------------------------------------------------- | ||||||||||
| 40 | |||||||||||
| 41 | 93 | ▶ | sub supported_parameters { | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 42 | return ( | ||||||||||
| 43 | { | ||||||||||
| 44 | 93 | name => 'single_quote_allowed_operators', | |||||||||
| 45 | description => | ||||||||||
| 46 | 'The operators to allow single-quotes as delimiters for.', | ||||||||||
| 47 | default_string => 'm s qr qx', | ||||||||||
| 48 | behavior => 'enumeration', | ||||||||||
| 49 | enumeration_values => [ @OPERATORS ], | ||||||||||
| 50 | enumeration_allow_multiple_values => 1, | ||||||||||
| 51 | }, | ||||||||||
| 52 | { | ||||||||||
| 53 | name => 'double_quote_allowed_operators', | ||||||||||
| 54 | description => | ||||||||||
| 55 | 'The operators to allow double-quotes as delimiters for.', | ||||||||||
| 56 | default_string => $EMPTY, | ||||||||||
| 57 | behavior => 'enumeration', | ||||||||||
| 58 | enumeration_values => [ @OPERATORS ], | ||||||||||
| 59 | enumeration_allow_multiple_values => 1, | ||||||||||
| 60 | }, | ||||||||||
| 61 | { | ||||||||||
| 62 | name => 'back_quote_allowed_operators', | ||||||||||
| 63 | description => | ||||||||||
| 64 | 'The operators to allow back-quotes (back-ticks) as delimiters for.', | ||||||||||
| 65 | default_string => $EMPTY, | ||||||||||
| 66 | behavior => 'enumeration', | ||||||||||
| 67 | enumeration_values => [ @OPERATORS ], | ||||||||||
| 68 | enumeration_allow_multiple_values => 1, | ||||||||||
| 69 | }, | ||||||||||
| 70 | ); | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 74 | ▶ | sub default_severity { return $SEVERITY_MEDIUM } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 74 | 73 | ▶ | sub default_themes { return qw( core maintenance ) } | ||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 75 | |||||||||||
| 76 | 31 | ▶ | sub applies_to { | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 77 | 31 | return qw{ | |||||||||
| 78 | PPI::Token::Quote::Interpolate | ||||||||||
| 79 | PPI::Token::Quote::Literal | ||||||||||
| 80 | PPI::Token::QuoteLike::Command | ||||||||||
| 81 | PPI::Token::QuoteLike::Regexp | ||||||||||
| 82 | PPI::Token::QuoteLike::Words | ||||||||||
| 83 | PPI::Token::Regexp::Match | ||||||||||
| 84 | PPI::Token::Regexp::Substitute | ||||||||||
| 85 | PPI::Token::Regexp::Transliterate | ||||||||||
| 86 | }; | ||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | #----------------------------------------------------------------------------- | ||||||||||
| 90 | |||||||||||
| 91 | 55 | ▶ | sub initialize_if_enabled { | ||||||||
|
Subroutine
initialize_if_enabled: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 92 | 55 | my ($self, undef) = @_; | |||||||||
| 93 | |||||||||||
| 94 | $self->{_allowed_operators_by_delimiter} = { | ||||||||||
| 95 | 55 | $QUOTE => $self->_single_quote_allowed_operators(), | |||||||||
| 96 | $DQUOTE => $self->_double_quote_allowed_operators(), | ||||||||||
| 97 | $BACKTICK => $self->_back_quote_allowed_operators(), | ||||||||||
| 98 | }; | ||||||||||
| 99 | |||||||||||
| 100 | 55 | return $TRUE; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | #----------------------------------------------------------------------------- | ||||||||||
| 104 | |||||||||||
| 105 | 55 | ▶ | sub _single_quote_allowed_operators { | ||||||||
|
Subroutine
_single_quote_allowed_operators: called CC=1 SCAR=0.0
| |||||||||||
| 106 | 55 | my ( $self ) = @_; | |||||||||
| 107 | |||||||||||
| 108 | 55 | return $self->{_single_quote_allowed_operators}; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 55 | ▶ | sub _double_quote_allowed_operators { | ||||||||
|
Subroutine
_double_quote_allowed_operators: called CC=1 SCAR=0.0
| |||||||||||
| 112 | 55 | my ( $self ) = @_; | |||||||||
| 113 | |||||||||||
| 114 | 55 | return $self->{_double_quote_allowed_operators}; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 55 | ▶ | sub _back_quote_allowed_operators { | ||||||||
|
Subroutine
_back_quote_allowed_operators: called CC=1 SCAR=0.0
| |||||||||||
| 118 | 55 | my ( $self ) = @_; | |||||||||
| 119 | |||||||||||
| 120 | 55 | return $self->{_back_quote_allowed_operators}; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 1 | ▶ | sub _allowed_operators_by_delimiter { | ||||||||
|
Subroutine
_allowed_operators_by_delimiter: called CC=1 SCAR=0.0
| |||||||||||
| 124 | 1 | my ( $self ) = @_; | |||||||||
| 125 | |||||||||||
| 126 | 1 | return $self->{_allowed_operators_by_delimiter}; | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | #----------------------------------------------------------------------------- | ||||||||||
| 130 | |||||||||||
| 131 | 1 | ▶ | sub violates { | ||||||||
|
Subroutine
violates: called CC=4 SCAR=17.3
Pod
documented
| |||||||||||
| 132 | 1 | my ( $self, $elem, undef ) = @_; | |||||||||
| 133 | |||||||||||
| 134 | 1 | my $info_retriever = $INFO_RETRIEVERS_BY_PPI_CLASS{ ref $elem }; | |||||||||
| 135 | 1 | ▶ | return if not $info_retriever; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | |||||||||||
| 137 | 1 | my ($operator, $delimiter) = $info_retriever->( $elem ); | |||||||||
| 138 | |||||||||||
| 139 | my $allowed_operators = | ||||||||||
| 140 | 1 | $self->_allowed_operators_by_delimiter()->{$delimiter}; | |||||||||
| 141 | 1 | ▶ | return if not $allowed_operators; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | |||||||||||
| 143 | 0 | ▶ | if ( not $allowed_operators->{$operator} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 0 | return $self->violation( $DESCRIPTIONS{$delimiter}, $EXPL, $elem ); | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 0 | return; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | #----------------------------------------------------------------------------- | ||||||||||
| 151 | |||||||||||
| 152 | 0 | ▶ | sub _info_for_single_character_operator { | ||||||||
|
Subroutine
_info_for_single_character_operator: not called CC=1 SCAR=6.9
| |||||||||||
| 153 | 0 | my ( $elem ) = @_; | |||||||||
| 154 | |||||||||||
| 155 | ## no critic (ProhibitParensWithBuiltins) | ||||||||||
| 156 | 0 | return ( substr ($elem, 0, 1), substr ($elem, 1, 1) ); | |||||||||
| 157 | ## use critic | ||||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | #----------------------------------------------------------------------------- | ||||||||||
| 161 | |||||||||||
| 162 | 0 | ▶ | sub _info_for_two_character_operator { | ||||||||
|
Subroutine
_info_for_two_character_operator: not called CC=1 SCAR=6.9
| |||||||||||
| 163 | 0 | my ( $elem ) = @_; | |||||||||
| 164 | |||||||||||
| 165 | ## no critic (ProhibitParensWithBuiltins) | ||||||||||
| 166 | 0 | return ( substr ($elem, 0, 2), substr ($elem, 2, 1) ); | |||||||||
| 167 | ## use critic | ||||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | #----------------------------------------------------------------------------- | ||||||||||
| 171 | |||||||||||
| 172 | 1 | ▶ | sub _info_for_match { | ||||||||
|
Subroutine
_info_for_match: called CC=2 SCAR=7.6
| |||||||||||
| 173 | 1 | my ( $elem ) = @_; | |||||||||
| 174 | |||||||||||
| 175 | 1 | ▶ | if ( $elem =~ m/ ^ m /xms ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 0 | return ('m', substr $elem, 1, 1); | |||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | 1 | return ('m', q{/}); | |||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | #----------------------------------------------------------------------------- | ||||||||||
| 183 | |||||||||||
| 184 | 0 | ▶ | sub _info_for_transliterate { | ||||||||
|
Subroutine
_info_for_transliterate: not called CC=2 SCAR=17.9
| |||||||||||
| 185 | 0 | my ( $elem ) = @_; | |||||||||
| 186 | |||||||||||
| 187 | 0 | ▶ | if ( $elem =~ m/ ^ tr /xms ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 188 | 0 | return ('tr', substr $elem, 2, 1); | |||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | 0 | return ('y', substr $elem, 1, 1); | |||||||||
| 192 | } | ||||||||||
| 193 | |||||||||||
| 194 | |||||||||||
| 195 | 40 | 1; | |||||||||
| 196 | |||||||||||
| 197 | __END__ | ||||||||||