« lib/Perl/Critic/Policy/RegularExpressions/ProhibitFixedStringMatches.pm
↑ Summary
lib/Perl/Critic/Utils/PPI.pm »
| 1 | package Perl::Critic::Policy::TestingAndDebugging::RequireUseStrict; | ||||||||||||||||||||||||||||
| 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 version 0.77; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use Scalar::Util qw{ blessed }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities $EMPTY }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 40 | ▶ | use Perl::Critic::Utils::Constants qw{ :equivalent_modules }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 40 | our $VERSION = '1.156'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 40 | Readonly::Scalar my $DESC => q{Code before strictures are enabled}; | |||||||||||||||||||||||||||
| 20 | 40 | Readonly::Scalar my $EXPL => [ 429 ]; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 40 | Readonly::Scalar my $PERL_VERSION_WHICH_IMPLIES_STRICTURE => qv('v5.11.0'); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 91 | ▶ | sub supported_parameters { | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | return ( | ||||||||||||||||||||||||||||
| 28 | { | ||||||||||||||||||||||||||||
| 29 | 91 | name => 'equivalent_modules', | |||||||||||||||||||||||||||
| 30 | description => | ||||||||||||||||||||||||||||
| 31 | q<The additional modules to treat as equivalent to "strict".>, | ||||||||||||||||||||||||||||
| 32 | default_string => $EMPTY, | ||||||||||||||||||||||||||||
| 33 | behavior => 'string list', | ||||||||||||||||||||||||||||
| 34 | list_always_present_values => ['strict', @STRICT_EQUIVALENT_MODULES], | ||||||||||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 85 | ▶ | sub default_severity { return $SEVERITY_HIGHEST } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 91 | ▶ | sub default_themes { return qw( core pbp bugs certrule certrec ) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 37 | ▶ | sub applies_to { return 'PPI::Document' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 40 | ▶ | sub default_maximum_violations_per_document { return 1; } | ||||||||||||||||||||||||||
|
Subroutine
default_maximum_violations_per_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 37 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 37 | my ( $self, undef, $doc ) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Find the first 'use strict' statement | ||||||||||||||||||||||||||||
| 51 | 37 | my $strict_stmnt = $doc->find_first( $self->_generate_is_use_strict() ); | |||||||||||||||||||||||||||
| 52 | 37 | ▶ | my $strict_line = $strict_stmnt ? $strict_stmnt->location()->[0] : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Find all statements that aren't 'use', 'require', or 'package' | ||||||||||||||||||||||||||||
| 55 | 37 | my $stmnts_ref = _find_isnt_include_or_package($doc); | |||||||||||||||||||||||||||
| 56 | 37 | ▶ | return if not $stmnts_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # If the 'use strict' statement is not defined, or the other | ||||||||||||||||||||||||||||
| 59 | # statement appears before the 'use strict', then it violates. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 35 | my @viols; | |||||||||||||||||||||||||||
| 62 | 35 | for my $stmnt ( @{ $stmnts_ref } ) { | |||||||||||||||||||||||||||
| 63 | 212 | ▶ | last if $stmnt->isa('PPI::Statement::End'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 212 | ▶ | last if $stmnt->isa('PPI::Statement::Data'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 212 | my $stmnt_line = $stmnt->location()->[0]; | |||||||||||||||||||||||||||
| 67 | 212 | ▶ | if ( (! defined $strict_line) || ($stmnt_line < $strict_line) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $strict_line or $stmnt_line < $strict_line
MC/DC100%2/2
not defined $strict_line or $stmnt_line < $strict_line
not defined $strict_line
$stmnt_line < $strict_line
Truth table100%3/3
not(A) or B
A= not defined $strict_lineB= $stmnt_line < $strict_line
| |||||||||||||||||||||||||||||
| 68 | 11 | push @viols, $self->violation( $DESC, $EXPL, $stmnt ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 35 | return @viols; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 37 | ▶ | sub _generate_is_use_strict { | ||||||||||||||||||||||||||
|
Subroutine
_generate_is_use_strict: called CC=1 SCAR=1.5
| |||||||||||||||||||||||||||||
| 77 | 37 | my ($self) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 325 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=39.2
| |||||||||||||||||||||||||||||
| 80 | 325 | my (undef, $elem) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 325 | ▶ | return 0 if !$elem->isa('PPI::Statement::Include'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 31 | ▶ | return 0 if $elem->type() ne 'use'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # We only want file-scoped pragmas | ||||||||||||||||||||||||||||
| 86 | 31 | my $parent = $elem->parent(); | |||||||||||||||||||||||||||
| 87 | 31 | ▶ | return 0 if !$parent->isa('PPI::Document'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 31 | ▶ | if ( my $pragma = $elem->pragma() ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 90 | 30 | ▶ | return 1 if $self->{_equivalent_modules}{$pragma}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | elsif ( my $module = $elem->module() ) { | ||||||||||||||||||||||||||||
| 93 | 1 | ▶ | return 1 if $self->{_equivalent_modules}{$module}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | elsif ( my $version = $elem->version() ) { | ||||||||||||||||||||||||||||
| 96 | # Currently Adam returns a string here. He has said he may return | ||||||||||||||||||||||||||||
| 97 | # a version object in the future, so best be prepared. | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ( not blessed( $version ) or not $version->isa( 'version' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($version) and $version->isa("version")
MC/DC0%0/2
builtin::blessed($version) and $version->isa("version")
builtin::blessed($version)
$version->isa("version")
Truth table0%0/3
A and B
A= builtin::blessed($version)B= $version->isa("version")
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ( 'v' ne substr $version, 0, 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
"v" ne substr($version, 0, 1) and $version =~ tr/.// > 1
MC/DC0%0/2
"v" ne substr($version, 0, 1) and $version =~ tr/.// > 1
"v" ne substr($version, 0, 1)
$version =~ tr/.// > 1
Truth table0%0/3
A and B
A= "v" ne substr($version, 0, 1)B= $version =~ tr/.// > 1
| |||||||||||||||||||||||||||||
| 100 | and ( $version =~ tr/././ ) > 1 ) { | ||||||||||||||||||||||||||||
| 101 | 0 | $version = 'v' . $version; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 0 | $version = version->parse( $version ); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | return 1 if $PERL_VERSION_WHICH_IMPLIES_STRICTURE <= $version; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | return 0; | |||||||||||||||||||||||||||
| 109 | 37 | }; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 113 | # Here, we're using the fact that Perl::Critic::Document::find() is optimized | ||||||||||||||||||||||||||||
| 114 | # to search for elements based on their type. This is faster than using the | ||||||||||||||||||||||||||||
| 115 | # native PPI::Node::find() method with a custom callback function. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 37 | ▶ | sub _find_isnt_include_or_package { | ||||||||||||||||||||||||||
|
Subroutine
_find_isnt_include_or_package: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 118 | 37 | my ($doc) = @_; | |||||||||||||||||||||||||||
| 119 | 37 | ▶ | my $all_statements = $doc->find('PPI::Statement') or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 35 | my @wanted_statements = grep { _statement_isnt_include_or_package($_) } @{$all_statements}; | |||||||||||||||||||||||||||
| 121 | 35 | ▶ | return @wanted_statements ? \@wanted_statements : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 306 | ▶ | sub _statement_isnt_include_or_package { | ||||||||||||||||||||||||||
|
Subroutine
_statement_isnt_include_or_package: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 127 | 306 | my ($elem) = @_; | |||||||||||||||||||||||||||
| 128 | 306 | ▶ | return 0 if $elem->isa('PPI::Statement::Package'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 275 | ▶ | return 0 if $elem->isa('PPI::Statement::Include'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 212 | return 1; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 40 | 1; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||