| 1 | package Perl::Critic::Policy::logicLAB::RequireVersionFormat; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # $Id$ | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | use base | ||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | qw(Perl::Critic::Policy::Modules::RequireVersionVar Perl::Critic::Policy); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Perl::Critic::Utils qw{ $SEVERITY_MEDIUM :booleans }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use List::MoreUtils qw(any); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | Readonly::Scalar my $EXPL => | |||||||||||||||||||||||||||||||||||||||||
| 18 | q{"$VERSION" variable should conform with the configured}; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | Readonly::Scalar my $DESC => q{"$VERSION" variable not conforming}; | |||||||||||||||||||||||||||||||||||||||||
| 20 | ## critic [ValuesAndExpressions::RequireInterpolationOfMetachars] | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use constant supported_parameters => qw(strict_quotes ignore_quotes formats); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use constant default_severity => $SEVERITY_MEDIUM; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use constant default_themes => qw(logiclab); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use constant applies_to => 'PPI::Document'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | my @strip_tokens = qw( | |||||||||||||||||||||||||||||||||||||||||
| 27 | PPI::Token::Structure | ||||||||||||||||||||||||||||||||||||||||||
| 28 | PPI::Token::Whitespace | ||||||||||||||||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | my @parsable_tokens = qw( | |||||||||||||||||||||||||||||||||||||||||
| 32 | PPI::Token::Quote::Double | ||||||||||||||||||||||||||||||||||||||||||
| 33 | PPI::Token::Quote::Single | ||||||||||||||||||||||||||||||||||||||||||
| 34 | PPI::Token::Number::Float | ||||||||||||||||||||||||||||||||||||||||||
| 35 | PPI::Token::Number::Version | ||||||||||||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 29 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=14 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 29 | my ( $self, $elem, $doc ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 29 | my $version_spec = q{}; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 29 | my $separator; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 29 | ▶ | if ( my $stmt = $doc->find_first( \&_is_version_declaration_statement ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 28 | my $tokenizer = PPI::Tokenizer->new( \$stmt ); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 28 | my $tokens = $tokenizer->all_tokens; | |||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 28 | ( $version_spec, $separator ) = $self->_extract_version($tokens); | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 29 | ▶ | if ( $version_spec and $self->{_strict_quotes} and $separator ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$version_spec and $self->{"_strict_quotes"} and $separator
$version_spec and $self->{"_strict_quotes"}
MC/DC0%0/3
$version_spec and $self->{"_strict_quotes"} and $separator
$version_spec
$self->{"_strict_quotes"}
$separator
Truth table50%2/4
A and B and C
A= $version_specB= $self->{"_strict_quotes"}C= $separator
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ( $separator ne q{'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | return $self->violation( $DESC, $EXPL, $doc ); | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 29 | ▶ | if ( $version_spec and $self->{_ignore_quotes} and $separator ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$version_spec and $self->{"_ignore_quotes"} and $separator
$version_spec and $self->{"_ignore_quotes"}
MC/DC66%2/3
$version_spec and $self->{"_ignore_quotes"} and $separator
$version_spec
$self->{"_ignore_quotes"}
$separator
Truth table75%3/4
A and B and C
A= $version_specB= $self->{"_ignore_quotes"}C= $separator
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 14 | $version_spec =~ s/$separator//xsmg; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 29 | my $ok; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 29 | foreach my $format ( @{ $self->{_formats} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 65 | 31 | ▶ | if ( $version_spec and $version_spec =~ m/$format/xsm ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$version_spec and $version_spec =~ /$format/msx
MC/DC100%2/2
$version_spec and $version_spec =~ /$format/msx
$version_spec
$version_spec =~ /$format/msx
Truth table100%3/3
A and B
A= $version_specB= $version_spec =~ /$format/msx
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 15 | $ok++; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 29 | ▶ | if ( $version_spec and not $ok ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$version_spec and not $ok
MC/DC100%2/2
$version_spec and not $ok
$version_spec
not $ok
Truth table100%3/3
A and not(B)
A= $version_specB= not $ok
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 13 | return $self->violation( $DESC, $EXPL, $doc ); | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 16 | return; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | sub _parse_formats { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_formats: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | my ( $self, $config_string ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | my @formats = split m{ \s* [||] \s* }xms, $config_string; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | return \@formats; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | ▶ | sub initialize_if_enabled { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
initialize_if_enabled: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | my ( $self, $config ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | #Setting the default | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | $self->{_formats} = [qw(\A\d+\.\d+(_\d+)?\z)]; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 2 | ▶ | $self->{_strict_quotes} = $config->get('strict_quotes') || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->get('strict_quotes') || 0
MC/DC0%0/1
$config->get('strict_quotes') || 0
$config->get('strict_quotes')
Truth table50%1/2
A
A= $config->get('strict_quotes')
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | ▶ | $self->{_ignore_quotes} = $config->get('ignore_quotes') || 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->get('ignore_quotes') || 1
MC/DC0%0/1
$config->get('ignore_quotes') || 1
$config->get('ignore_quotes')
Truth table50%1/2
A
A= $config->get('ignore_quotes')
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 2 | my $formats = $config->get('formats'); | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 2 | ▶ | if ($formats) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | $self->{_formats} = $self->_parse_formats($formats); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | return $TRUE; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 28 | ▶ | sub _extract_version { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_version: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 28 | my ( $self, $tokens ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | ##stripping whitespace and structure tokens | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 28 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 28 | foreach my $t ( @{$tokens} ) { | |||||||||||||||||||||||||||||||||||||||||
| 109 | 137 | ▶ | if ( any { ref $t eq $_ } @strip_tokens ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 107 | splice @{$tokens}, $i, 1; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 137 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | #Trying to locate and match version containing token | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 28 | foreach my $t ( @{$tokens} ) { | |||||||||||||||||||||||||||||||||||||||||
| 117 | 108 | ▶ | if ( any { ref $t eq $_ } @parsable_tokens ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 27 | ▶ | if ( $t->{separator} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 14 | return ( $t->content, $t->{separator} ); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 13 | return $t->content; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 48 | ▶ | sub _is_version_declaration_statement { ## no critic (ArgUnpacking) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_version_declaration_statement: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 48 | ▶ | return 1 if _is_our_version(@_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 34 | ▶ | return 1 if _is_vars_package_version(@_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 20 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 48 | ▶ | sub _is_our_version { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_our_version: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 48 | my ( undef, $elem ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 48 | ▶ | return if not $elem; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 48 | ▶ | $elem->isa('PPI::Statement::Variable') || return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 14 | ▶ | $elem->type() eq 'our' || return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 14 | ▶ | return any { $_ eq '$VERSION' } $elem->variables(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 34 | ▶ | sub _is_vars_package_version { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_vars_package_version: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 34 | my ( undef, $elem ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 34 | ▶ | return if not $elem; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 34 | ▶ | $elem->isa('PPI::Statement') || return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 40 | ▶ | return any { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 40 | ▶ | $_->isa('PPI::Token::Symbol') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | and $_->content =~ m{ \A \$(\S+::)*VERSION \z }xms; | ||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 17 | $elem->children(); | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||||||||||||||||