« lib/Perl/Critic/OptionsProcessor.pm
↑ Summary
lib/Perl/Critic/Policy/BuiltinFunctions/RequireBlockMap.pm »
| 1 | package Perl::Critic::Policy::Modules::RequireVersionVar; | ||||||||||||||||||||
| 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 List::SomeUtils qw(any); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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::Scalar my $DESC => q{No package-scoped "$VERSION" variable found}; ## no critic (RequireInterpolation) | |||||||||||||||||||
| 18 | 40 | Readonly::Scalar my $EXPL => [ 404 ]; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 91 | ▶ | sub supported_parameters { return () } | ||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 23 | 78 | ▶ | sub default_severity { return $SEVERITY_LOW } | ||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 24 | 85 | ▶ | sub default_themes { return qw(core pbp readability) } | ||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 25 | 31 | ▶ | sub applies_to { return 'PPI::Document' } | ||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 31 | ▶ | sub violates { | ||||||||||||||||||
|
Subroutine
violates: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 30 | 31 | my ( $self, undef, $doc ) = @_; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 31 | ▶ | return if $doc->find_first( \&_is_version_declaration ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | #If we get here, then no $VERSION was found | ||||||||||||||||||||
| 35 | 4 | return $self->violation( $DESC, $EXPL, $doc ); | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 592 | ▶ | sub _is_version_declaration { ## no critic (ArgUnpacking) | ||||||||||||||||||
|
Subroutine
_is_version_declaration: called CC=6 SCAR=18.8
| |||||||||||||||||||||
| 41 | 592 | ▶ | return 1 if _is_our_version(@_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 565 | ▶ | return 1 if _is_vars_version(@_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 565 | ▶ | return 1 if _is_package_version(@_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 565 | ▶ | return 1 if _is_readonly_version(@_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 565 | ▶ | return 1 if _is_package_argument_version(@_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 565 | return 0; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 592 | ▶ | sub _is_our_version { | ||||||||||||||||||
|
Subroutine
_is_our_version: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 52 | 592 | my (undef, $elem) = @_; | |||||||||||||||||||
| 53 | 592 | ▶ | $elem->isa('PPI::Statement::Variable') || return 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 27 | ▶ | $elem->type() eq 'our' || return 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 27 | ▶ | return any { $_ eq '$VERSION' } $elem->variables(); ## no critic (RequireInterpolation) | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 565 | ▶ | sub _is_vars_version { | ||||||||||||||||||
|
Subroutine
_is_vars_version: called CC=3 SCAR=11.4
| |||||||||||||||||||||
| 61 | 565 | my (undef, $elem) = @_; | |||||||||||||||||||
| 62 | 565 | ▶ | $elem->isa('PPI::Statement::Include') || return 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 55 | ▶ | $elem->pragma() eq 'vars' || return 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | return $elem =~ m{ \$VERSION }xms; #Crude, but usually works | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 565 | ▶ | sub _is_package_version { | ||||||||||||||||||
|
Subroutine
_is_package_version: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 70 | 565 | my (undef, $elem) = @_; | |||||||||||||||||||
| 71 | 565 | ▶ | $elem->isa('PPI::Token::Symbol') || return 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 6 | return $elem =~ m{ \A \$ \S+ ::VERSION \z }xms; | |||||||||||||||||||
| 73 | #TODO: ensure that it is in _this_ package! | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 565 | ▶ | sub _is_readonly_version { | ||||||||||||||||||
|
Subroutine
_is_readonly_version: called CC=7 SCAR=31.7
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | #--------------------------------------------------------------- | ||||||||||||||||||||
| 81 | # Readonly VERSION statements usually come in one of two forms: | ||||||||||||||||||||
| 82 | # | ||||||||||||||||||||
| 83 | # Readonly our $VERSION = 1.0; | ||||||||||||||||||||
| 84 | # Readonly::Scalar our $VERSION = 1.0; | ||||||||||||||||||||
| 85 | #--------------------------------------------------------------- | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 565 | my (undef, $elem) = @_; | |||||||||||||||||||
| 88 | 565 | ▶ | $elem->isa('PPI::Token::Symbol') || return 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 6 | ▶ | return 0 if $elem !~ m{ \A \$VERSION \z }xms; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | ▶ | my $psib = $elem->sprevious_sibling() || return 0; | ||||||||||||||||||
|
Condition0%0/2
$elem->sprevious_sibling || (return 0)
MC/DC0%0/1
$elem->sprevious_sibling || (return 0)
$elem->sprevious_sibling
Truth table0%0/2
A
A= $elem->sprevious_sibling
| |||||||||||||||||||||
| 92 | 0 | ▶ | return 0 if $psib ne 'our'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | ▶ | my $ppsib = $psib->sprevious_sibling() || return 0; | ||||||||||||||||||
|
Condition0%0/2
$psib->sprevious_sibling || (return 0)
MC/DC0%0/1
$psib->sprevious_sibling || (return 0)
$psib->sprevious_sibling
Truth table0%0/2
A
A= $psib->sprevious_sibling
| |||||||||||||||||||||
| 95 | 0 | ▶ | return $ppsib eq 'Readonly' || $ppsib eq 'Readonly::Scalar'; | ||||||||||||||||||
|
Condition0%0/3
$ppsib eq 'Readonly' || $ppsib eq 'Readonly::Scalar'
MC/DC0%0/2
$ppsib eq 'Readonly' || $ppsib eq 'Readonly::Scalar'
$ppsib eq 'Readonly'
$ppsib eq 'Readonly::Scalar'
Truth table0%0/3
A || B
A= $ppsib eq 'Readonly'B= $ppsib eq 'Readonly::Scalar'
| |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 565 | ▶ | sub _is_package_argument_version { | ||||||||||||||||||
|
Subroutine
_is_package_argument_version: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 101 | 565 | my (undef, $elem) = @_; | |||||||||||||||||||
| 102 | 565 | ▶ | $elem->isa( 'PPI::Statement::Package' ) or return 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | # Perldoc for 5.12.3 documents the statement as | ||||||||||||||||||||
| 104 | # package NAMESPACE VERSION | ||||||||||||||||||||
| 105 | # with no comma, and the compiler in fact does not accept one. | ||||||||||||||||||||
| 106 | 28 | ▶ | my $ver = $elem->schild( 2 ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | or return 0; | ||||||||||||||||||||
| 108 | 28 | return $ver->isa( 'PPI::Token::Number' ); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 40 | 1; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | __END__ | ||||||||||||||||||||