« lib/Perl/Critic/PolicyParameter/Behavior/Integer.pm
↑ Summary
lib/Perl/Critic/Policy/RegularExpressions/RequireExtendedFormatting.pm »
| 1 | package Perl::Critic::Policy::TestingAndDebugging::RequireUseWarnings; | ||||||||||||||||||||||||||||||||||||||||||
| 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 version (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities $EMPTY }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 40 | ▶ | use Perl::Critic::Utils::Constants qw{ :equivalent_modules }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 40 | our $VERSION = '1.156'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 40 | Readonly::Scalar my $DESC => q{Code before warnings are enabled}; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 40 | Readonly::Scalar my $EXPL => [431]; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 40 | Readonly::Scalar my $MINIMUM_VERSION => version->new(5.006); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 40 | Readonly::Scalar my $PERL_VERSION_WHICH_IMPLIES_WARNINGS => version->new(5.036); | |||||||||||||||||||||||||||||||||||||||||
| 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 "warnings".>, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | default_string => $EMPTY, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | behavior => 'string list', | ||||||||||||||||||||||||||||||||||||||||||
| 34 | list_always_present_values => ['warnings', @WARNINGS_EQUIVALENT_MODULES], | ||||||||||||||||||||||||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 82 | ▶ | sub default_severity { return $SEVERITY_HIGH } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 91 | ▶ | sub default_themes { return qw( core pbp bugs certrule ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 33 | ▶ | sub applies_to { return 'PPI::Document' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 36 | ▶ | 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 | 33 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=11 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 33 | my ( $self, undef, $document ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 33 | my $version = $document->highest_explicit_perl_version(); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 33 | ▶ | return if $version and ($version < $MINIMUM_VERSION or $version >= $PERL_VERSION_WHICH_IMPLIES_WARNINGS); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
$version and $version < $MINIMUM_VERSION || $version >= $PERL_VERSION_WHICH_IMPLIES_WARNINGS
$version < $MINIMUM_VERSION or $version >= $PERL_VERSION_WHICH_IMPLIES_WARNINGS
MC/DC0%0/3
$version and $version < $MINIMUM_VERSION || $version >= $PERL_VERSION_WHICH_IMPLIES_WARNINGS
$version
$version < $MINIMUM_VERSION
$version >= $PERL_VERSION_WHICH_IMPLIES_WARNINGS
Truth table25%1/4
A and B or C
A= $versionB= $version < $MINIMUM_VERSIONC= $version >= $PERL_VERSION_WHICH_IMPLIES_WARNINGS
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | # Find the first 'use warnings' statement | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 33 | my $warn_stmnt = $document->find_first( $self->_generate_is_use_warnings() ); | |||||||||||||||||||||||||||||||||||||||||
| 55 | 33 | ▶ | my $warn_line = $warn_stmnt ? $warn_stmnt->location()->[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Find all statements that aren't 'use', 'require', or 'package' | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 33 | my $stmnts_ref = _find_isnt_include_or_package($document); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 33 | ▶ | return if !$stmnts_ref; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | # If the 'use warnings' statement is not defined, or the other | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # statement appears before the 'use warnings', then it violates. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 31 | my @viols; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 31 | for my $stmnt ( @{ $stmnts_ref } ) { | |||||||||||||||||||||||||||||||||||||||||
| 66 | 205 | ▶ | last if $stmnt->isa('PPI::Statement::End'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 205 | ▶ | last if $stmnt->isa('PPI::Statement::Data'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 205 | my $stmnt_line = $stmnt->location()->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 205 | ▶ | if ( (! defined $warn_line) || ($stmnt_line < $warn_line) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $warn_line or $stmnt_line < $warn_line
MC/DC100%2/2
not defined $warn_line or $stmnt_line < $warn_line
not defined $warn_line
$stmnt_line < $warn_line
Truth table100%3/3
not(A) or B
A= not defined $warn_lineB= $stmnt_line < $warn_line
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 8 | push @viols, $self->violation( $DESC, $EXPL, $stmnt ); | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 31 | return @viols; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 33 | ▶ | sub _generate_is_use_warnings { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_is_use_warnings: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 33 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 479 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=21.2
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 479 | my (undef, $elem) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 479 | ▶ | return 0 if !$elem->isa('PPI::Statement::Include'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 59 | ▶ | return 0 if $elem->type() ne 'use'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | # We only want file-scoped pragmas | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 59 | my $parent = $elem->parent(); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 59 | ▶ | return 0 if !$parent->isa('PPI::Document'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 59 | ▶ | if ( my $pragma = $elem->pragma() ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 58 | ▶ | return 1 if $self->{_equivalent_modules}{$pragma}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | elsif ( my $module = $elem->module() ) { | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | return 1 if $self->{_equivalent_modules}{$module}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 30 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 33 | }; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # Here, we're using the fact that Perl::Critic::Document::find() is optimized | ||||||||||||||||||||||||||||||||||||||||||
| 105 | # to search for elements based on their type. This is faster than using the | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # native PPI::Node::find() method with a custom callback function. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 33 | ▶ | sub _find_isnt_include_or_package { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_isnt_include_or_package: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 33 | my ($doc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 33 | ▶ | my $all_statements = $doc->find('PPI::Statement') or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 31 | my @wanted_statements = grep { _statement_isnt_include_or_package($_) } @{$all_statements}; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 31 | ▶ | return @wanted_statements ? \@wanted_statements : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 296 | ▶ | sub _statement_isnt_include_or_package { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_statement_isnt_include_or_package: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 296 | my ($elem) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 296 | ▶ | return 0 if $elem->isa('PPI::Statement::Package'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 266 | ▶ | return 0 if $elem->isa('PPI::Statement::Include'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 205 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 40 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||||||||||||||||