| 1 | package Perl::Critic::Policy::ProhibitNoWarningsRedefine; | ||
| 2 | $Perl::Critic::Policy::ProhibitNoWarningsRedefine::VERSION = '1.000'; | ||
| 3 | # ABSTRACT: Mock with Test::MockModule, don't clobber the symbol table by hand. | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use re '/aa'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 0 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use Perl::Critic::Utils qw{ :severities :classification :ppi }; | ||
| 15 | use parent qw{Perl::Critic::Policy}; | ||
| 16 | |||
| 17 | |||
| 18 | Readonly::Scalar my $DESC => q{Switching off a 'redefine' warning to patch the symbol table}; | ||
| 19 | Readonly::Scalar my $EXPL => q{Use Test::MockModule in strict mode, which checks the sub exists and unmocks itself}; | ||
| 20 | |||
| 21 | |||
| 22 | sub supported_parameters { | ||
| 23 | return ({ | ||
| 24 | name => 'categories', | ||
| 25 | description => 'Warning categories that may not be switched off.', | ||
| 26 | default_string => 'redefine', | ||
| 27 | behavior => 'string list', | ||
| 28 | }); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub default_severity { return $SEVERITY_HIGH } | ||
| 32 | sub default_themes { return qw(maintenance tests) } | ||
| 33 | sub applies_to { return 'PPI::Statement::Include' } | ||
| 34 | |||
| 35 | |||
| 36 | sub violates { | ||
| 37 | my ( $self, $elem, undef ) = @_; | ||
| 38 | |||
| 39 | return if $elem->type() ne 'no'; | ||
| 40 | |||
| 41 | my $module = $elem->module(); | ||
| 42 | return if !defined $module || $module ne 'warnings'; | ||
| 43 | |||
| 44 | # A bare `no warnings;` takes every category with it, ours included. | ||
| 45 | my @named = _named_categories($elem); | ||
| 46 | return $self->violation( $DESC, $EXPL, $elem ) if !@named; | ||
| 47 | |||
| 48 | foreach my $category (@named) { | ||
| 49 | return $self->violation( $DESC, $EXPL, $elem ) if $self->{_categories}{$category}; | ||
| 50 | } | ||
| 51 | |||
| 52 | return; # ok! | ||
| 53 | } | ||
| 54 | |||
| 55 | # Every literal category named in the statement, from a qw() list or a run of | ||
| 56 | # quoted strings alike. | ||
| 57 | sub _named_categories { | ||
| 58 | my ($elem) = @_; | ||
| 59 | |||
| 60 | my @names; | ||
| 61 | foreach my $token ( $elem->schildren() ) { | ||
| 62 | if ( $token->isa('PPI::Token::QuoteLike::Words') ) { | ||
| 63 | push @names, $token->literal(); | ||
| 64 | } | ||
| 65 | elsif ( $token->isa('PPI::Token::Quote') ) { | ||
| 66 | push @names, $token->string(); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | return grep { defined $_ && length $_ } @names; | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; | ||
| 74 | |||
| 75 | __END__ | ||