# Copyright 2009 Kevin Ryde
# Perl-Critic-Pulp is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Perl-Critic-Pulp is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Perl-Critic-Pulp. If not, see .
package Perl::Critic::Policy::Modules::ProhibitUseQuotedVersion;
use 5.006;
use strict;
use warnings;
use base 'Perl::Critic::Policy';
use Perl::Critic::Utils qw(:severities);
use version;
use Perl::Critic::Pulp;
our $VERSION = 22;
use constant DEBUG => 0;
sub supported_parameters { return; }
sub default_severity { return $SEVERITY_MEDIUM; }
sub default_themes { return qw(pulp bugs); }
sub applies_to { return 'PPI::Statement::Include'; }
sub violates {
my ($self, $elem, $document) = @_;
defined ($elem->module) || return; # if a "use 5.005" etc
my $arg = $elem->schild(2) || return; # if a "use Foo" with no args
$arg->isa('PPI::Token::Quote') || return;
_elem_is_last_of_statement ($arg) || return;
# This is a strict match of what a module version number is like.
# Previously looser forms like '.500' were matched too, but of course
# unquoting that doesn't give something that's checked by perl itself.
# There don't seem to be forms like .500 used in practice, so don't think
# it's important.
#
my $str = $arg->string;
$str =~ $Perl::Critic::Pulp::use_module_version_number_re
or return;
return $self->violation
("Don't use a quoted string version number in a \"use\" statement",
'',
$arg);
}
# return true if $elem is the last thing in its statement, apart from an
# optional terminating ";"
#
sub _elem_is_last_of_statement {
my ($elem) = @_;
my $next = $elem->snext_sibling;
return (! $next
|| ($next->isa('PPI::Token::Structure')
&& $next eq ';'
&& ! $next->snext_sibling));
}
1;
__END__
=head1 NAME
Perl::Critic::Policy::Modules::ProhibitUseQuotedVersion - avoid quoted version number string in a "use" statement
=head1 DESCRIPTION
This policy is part of the L|Perl::Critic::Pulp>
addon. It prohibits a quoted version number string as the sole argument to
a C