| 1 | package Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion; | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 7 | 40 | ▶ | use Perl::Critic::Utils qw{ :booleans :characters :severities }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils::PPI qw{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | get_next_element_in_same_simple_statement | ||||||||||||||||||||||||||||
| 10 | get_previous_module_used_on_same_line | ||||||||||||||||||||||||||||
| 11 | is_ppi_simple_statement | ||||||||||||||||||||||||||||
| 12 | 40 | }; | |||||||||||||||||||||||||||
| 13 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 40 | our $VERSION = '1.156'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 40 | Readonly::Scalar my $DOLLAR => q<$>; | |||||||||||||||||||||||||||
| 22 | # All uses of the $DOLLAR variable below are to prevent false failures in | ||||||||||||||||||||||||||||
| 23 | # xt/93_version.t. | ||||||||||||||||||||||||||||
| 24 | 40 | Readonly::Scalar my $VERSION_MODULE => q<version>; | |||||||||||||||||||||||||||
| 25 | 40 | Readonly::Scalar my $VERSION_VARIABLE => $DOLLAR . q<VERSION>; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 40 | Readonly::Scalar my $DESC => | |||||||||||||||||||||||||||
| 28 | $DOLLAR . q<VERSION value should not come from outside module>; | ||||||||||||||||||||||||||||
| 29 | 40 | Readonly::Scalar my $EXPL => | |||||||||||||||||||||||||||
| 30 | q<If the version comes from outside the module, you can get everything from unexpected version changes to denial-of-service attacks.>; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 91 | ▶ | sub supported_parameters { return ( | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||
| 36 | 91 | name => 'forbid_use_version', | |||||||||||||||||||||||||||
| 37 | description => | ||||||||||||||||||||||||||||
| 38 | qq<Make "use version; our ${DOLLAR}VERSION = qv('1.2.3');" a violation of this policy.>, | ||||||||||||||||||||||||||||
| 39 | default_string => $FALSE, | ||||||||||||||||||||||||||||
| 40 | behavior => 'boolean', | ||||||||||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 74 | ▶ | sub default_severity { return $SEVERITY_MEDIUM } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 73 | ▶ | sub default_themes { return qw( core maintenance ) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 31 | ▶ | sub applies_to { return 'PPI::Token::Symbol' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 173 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=9 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 173 | my ( $self, $elem, $doc ) = @_; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # Any variable other than $VERSION is ignored. | ||||||||||||||||||||||||||||
| 54 | 173 | ▶ | return if $VERSION_VARIABLE ne $elem->content(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # We are only interested in assignments to $VERSION, but it might be a | ||||||||||||||||||||||||||||
| 57 | # list assignment, so if we do not find an assignment, we move up the | ||||||||||||||||||||||||||||
| 58 | # parse tree. If we hit a statement (or no parent at all) we do not | ||||||||||||||||||||||||||||
| 59 | # understand the code to be an assignment statement, and we simply return. | ||||||||||||||||||||||||||||
| 60 | 27 | my $operator; | |||||||||||||||||||||||||||
| 61 | return if | ||||||||||||||||||||||||||||
| 62 | 27 | ▶ | not $operator = get_next_element_in_same_simple_statement( $elem ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $operator = get_next_element_in_same_simple_statement($elem) or $Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion::EQUAL ne $operator
MC/DC0%0/2
not $operator = get_next_element_in_same_simple_statement($elem) or $Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion::EQUAL ne $operator
not $operator = get_next_element_in_same_simple_statement($elem)
$Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion::EQUAL ne $operator
Truth table33%1/3
not(A) or B
A= not $operator = get_next_element_in_same_simple_statement($elem)B= $Perl::Critic::Policy::ValuesAndExpressions::ProhibitComplexVersion::EQUAL ne $operator
| |||||||||||||||||||||||||||||
| 63 | or $EQUAL ne $operator; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Find the simple statement we are in. If we can not find it, abandon the | ||||||||||||||||||||||||||||
| 66 | # attempt to analyze the code. | ||||||||||||||||||||||||||||
| 67 | 27 | ▶ | my $statement = _get_simple_statement( $elem ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | or return; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Check all symbols in the statement for violation. | ||||||||||||||||||||||||||||
| 71 | 27 | my $exception; | |||||||||||||||||||||||||||
| 72 | 27 | ▶ | return $exception if | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | $exception = | ||||||||||||||||||||||||||||
| 74 | $self->_validate_fully_qualified_symbols($elem, $statement, $doc); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # At this point we have found no data that is explicitly from outside the | ||||||||||||||||||||||||||||
| 77 | # file. If the author wants to use a $VERSION from another module, _and_ | ||||||||||||||||||||||||||||
| 78 | # wants MM->parse_version to understand it, the other module must be used | ||||||||||||||||||||||||||||
| 79 | # on the same line. So we assume no violation unless this has been done. | ||||||||||||||||||||||||||||
| 80 | 27 | ▶ | my $module = get_previous_module_used_on_same_line( $elem ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | or return; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # We make an exception for 'use version' unless configured otherwise; so | ||||||||||||||||||||||||||||
| 84 | # let it be written, so let it be done. | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | return if $module eq $VERSION_MODULE and not $self->{_forbid_use_version}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$module eq $VERSION_MODULE and not $self->{"_forbid_use_version"}
MC/DC0%0/2
$module eq $VERSION_MODULE and not $self->{"_forbid_use_version"}
$module eq $VERSION_MODULE
not $self->{"_forbid_use_version"}
Truth table0%0/3
A and not(B)
A= $module eq $VERSION_MODULEB= not $self->{"_forbid_use_version"}
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # We assume nefarious intent if we have any other module used on the same | ||||||||||||||||||||||||||||
| 88 | # line as the $VERSION assignment. | ||||||||||||||||||||||||||||
| 89 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Return the simple statement that contains our element. The classification | ||||||||||||||||||||||||||||
| 95 | # done by is_ppi_simple_statement is not quite good enough in this case -- if | ||||||||||||||||||||||||||||
| 96 | # our parent is a PPI::Structure::List, we want to keep looking. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 27 | ▶ | sub _get_simple_statement { | ||||||||||||||||||||||||||
|
Subroutine
_get_simple_statement: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||
| 99 | 27 | my ( $elem ) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 27 | my $statement = $elem; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 27 | ▶ | while ( $statement) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 54 | my $parent; | |||||||||||||||||||||||||||
| 105 | 54 | ▶ | if ( is_ppi_simple_statement( $statement ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 27 | ▶ | return $statement if | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$parent = $statement->parent and $parent->isa("PPI::Structure::List")
MC/DC0%0/2
$parent = $statement->parent and $parent->isa("PPI::Structure::List")
$parent = $statement->parent
$parent->isa("PPI::Structure::List")
Truth table33%1/3
A and B
A= $parent = $statement->parentB= $parent->isa("PPI::Structure::List")
| |||||||||||||||||||||||||||||
| 107 | not $parent = $statement->parent() | ||||||||||||||||||||||||||||
| 108 | or not $parent->isa( 'PPI::Structure::List' ); | ||||||||||||||||||||||||||||
| 109 | 0 | $statement = $parent; | |||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 27 | $statement = $statement->parent(); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | return; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 27 | ▶ | sub _validate_fully_qualified_symbols { | ||||||||||||||||||||||||||
|
Subroutine
_validate_fully_qualified_symbols: called CC=14 SCAR=36.5
| |||||||||||||||||||||||||||||
| 121 | 27 | my ( $self, $elem, $statement, $doc ) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Find the package(s) in this file. | ||||||||||||||||||||||||||||
| 124 | my %local_package = | ||||||||||||||||||||||||||||
| 125 | 27 | map { $_->schild( 1 ) => 1 } | |||||||||||||||||||||||||||
| 126 | 27 | ▶ | @{ $doc->find( 'PPI::Statement::Package' ) || [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$doc->find('PPI::Statement::Package') || []
MC/DC0%0/1
$doc->find('PPI::Statement::Package') || []
$doc->find('PPI::Statement::Package')
Truth table50%1/2
A
A= $doc->find('PPI::Statement::Package')
| |||||||||||||||||||||||||||||
| 127 | 27 | $local_package{main} = 1; # For completeness. | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # Check all symbols in the statement for violation. | ||||||||||||||||||||||||||||
| 130 | 27 | foreach my $symbol ( | |||||||||||||||||||||||||||
| 131 | 27 | ▶ | @{ $statement->find( 'PPI::Token::Symbol' ) || [] } | ||||||||||||||||||||||||||
|
Condition50%1/2
$statement->find('PPI::Token::Symbol') || []
MC/DC0%0/1
$statement->find('PPI::Token::Symbol') || []
$statement->find('PPI::Token::Symbol')
Truth table50%1/2
A
A= $statement->find('PPI::Token::Symbol')
| |||||||||||||||||||||||||||||
| 132 | ) { | ||||||||||||||||||||||||||||
| 133 | 27 | ▶ | if ( $symbol->canonical() =~ m< \A [@\$%&] ([\w:]*) :: >smx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $local_package{ $1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | or return $self->violation( $DESC, $EXPL, $elem ); | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # Check all interpolatable strings in the statement for violation. | ||||||||||||||||||||||||||||
| 140 | # TODO this does not correctly handle "@{[some_expression()]}". | ||||||||||||||||||||||||||||
| 141 | 27 | foreach my $string ( | |||||||||||||||||||||||||||
| 142 | @{ | ||||||||||||||||||||||||||||
| 143 | $statement->find( | ||||||||||||||||||||||||||||
| 144 | 216 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 145 | return | ||||||||||||||||||||||||||||
| 146 | 216 | ▶ | $_[1]->isa('PPI::Token::Quote::Double') | ||||||||||||||||||||||||||
|
Condition33%1/3
$_[1]->isa('PPI::Token::Quote::Double') || $_[1]->isa('PPI::Token::Quote::Interpolate')
MC/DC0%0/2
$_[1]->isa('PPI::Token::Quote::Double') || $_[1]->isa('PPI::Token::Quote::Interpolate')
$_[1]->isa('PPI::Token::Quote::Double')
$_[1]->isa('PPI::Token::Quote::Interpolate')
Truth table33%1/3
A || B
A= $_[1]->isa('PPI::Token::Quote::Double')B= $_[1]->isa('PPI::Token::Quote::Interpolate')
| |||||||||||||||||||||||||||||
| 147 | || $_[1]->isa('PPI::Token::Quote::Interpolate'); | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | ) | ||||||||||||||||||||||||||||
| 150 | 27 | ▶ | or [] | ||||||||||||||||||||||||||
|
Condition50%1/2
$statement->find(sub {
return $_[1]->isa('PPI::Token::Quote::Double') || $_[1]->isa('PPI::Token::Quote::Interpolate');
}
) || []
MC/DC0%0/1
$statement->find(sub {
return $_[1]->isa('PPI::Token::Quote::Double') || $_[1]->isa('PPI::Token::Quote::Interpolate');
}
) || []
$statement->find(sub {
return $_[1]->isa('PPI::Token::Quote::Double') || $_[1]->isa('PPI::Token::Quote::Interpolate');
}
)
Truth table50%1/2
A
A= $statement->find(sub {
return $_[1]->isa('PPI::Token::Quote::Double') || $_[1]->isa('PPI::Token::Quote::Interpolate');
}
)
| |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | ) { | ||||||||||||||||||||||||||||
| 153 | 0 | my $unquoted = $string->string(); | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | while ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | $unquoted =~ | ||||||||||||||||||||||||||||
| 156 | m< | ||||||||||||||||||||||||||||
| 157 | (?: \A | [^\\] ) | ||||||||||||||||||||||||||||
| 158 | (?: \\{2} )* | ||||||||||||||||||||||||||||
| 159 | [@\$] | ||||||||||||||||||||||||||||
| 160 | [{]? | ||||||||||||||||||||||||||||
| 161 | ([\w:]*) | ||||||||||||||||||||||||||||
| 162 | :: | ||||||||||||||||||||||||||||
| 163 | >gsmx | ||||||||||||||||||||||||||||
| 164 | ) { | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | next if $local_package{ $1 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # Check all words in the statement for violation. | ||||||||||||||||||||||||||||
| 172 | 27 | ▶ | foreach my $symbol ( @{ $statement->find( 'PPI::Token::Word' ) || [] } ) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$statement->find('PPI::Token::Word') || []
MC/DC0%0/1
$statement->find('PPI::Token::Word') || []
$statement->find('PPI::Token::Word')
Truth table50%1/2
A
A= $statement->find('PPI::Token::Word')
| |||||||||||||||||||||||||||||
| 173 | 27 | ▶ | if ( $symbol->content() =~ m/ \A ([\w:]*) :: /smx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | return $self->violation( $DESC, $EXPL, $elem ) | ||||||||||||||||||||||||||||
| 175 | 0 | ▶ | if not $local_package{ $1 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 27 | return; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 40 | 1; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||||||||||