Perl::Critic
目前沒有任何東西可以提供您需要的東西,但很可能會添加一項策略來執行類似操作。不幸的是,PPI
不夠全面,無法正確識別程序中每個令牌的做法,因此它需要更多的編碼。
這爲use File::Temp
聲明程序檢查,嘗試使用任何
use File::Temp 'tempdir';
use File::Temp q(tempdir);
use File::Temp "tempdir";
use File::Temp qq(tempdir);
use File::Temp qw/ tempdir /;
(與任何分隔符爲q
,qq
和qw
形式)導入tempdir
。它還會檢查看起來像函數調用的PPI::Token::Word
節點,該節點等於File::Temp::tempdir
。
package Perl::Critic::Policy::Prohibit_tempdir;
use strict;
use warnings;
use Perl::Critic::Utils qw{ is_function_call :severities };
use Scalar::Util 'blessed';
use base 'Perl::Critic::Policy';
my $DESC = 'Temp::File::tempdir function';
my $EXPL = 'The tempdir function from Temp::File is deprecated. Use newdir method instead';
sub default_severity { $SEVERITY_HIGH };
sub applies_to{ qw/ PPI::Statement::Include PPI::Token::Word/}
sub violates {
my ($self, $elem) = @_;
if ($elem->isa('PPI::Statement::Include')) {
return unless $elem->type eq 'use';
my $module = $elem->module;
return unless $module and $module eq 'File::Temp';
for my $kid ($elem->children) {
next unless blessed($kid) =~ /^PPI::Token::Quote/;
if ($kid->can('string') and $kid->string eq 'tempdir'
or $kid->can('literal') and grep $_ eq 'tempdir', $kid->literal) {
return $self->violation($DESC, $EXPL, $elem);
}
}
}
else {
if (is_function_call($elem) and $elem eq 'File::Temp::tempdir') {
return $self->violation($DESC, $EXPL, $elem);
}
}
return;
}
1;
與此代碼
use strict;
use warnings;
use File::Temp 'tempdir';
use File::Temp "tempdir";
use File::Temp qw/ tempdir /;
my $dir = tempdir();
$dir = tempdir;
$dir = File::Temp::tempdir;
my $ft = File::Temp->new;
$dir = $ft->newdir;
生成從perlcritic -4 test.pl
Code not contained in explicit package at line 1, column 1. Violates encapsulation. (Severity: 4)
Temp::File::tempdir function at line 4, column 1. The tempdir function from Temp::File is deprecated. Use newdir method instead. (Severity: 4)
Temp::File::tempdir function at line 5, column 1. The tempdir function from Temp::File is deprecated. Use newdir method instead. (Severity: 4)
Temp::File::tempdir function at line 6, column 1. The tempdir function from Temp::File is deprecated. Use newdir method instead. (Severity: 4)
Temp::File::tempdir function at line 10, column 8. The tempdir function from Temp::File is deprecated. Use newdir method instead. (Severity: 4)
Module does not end with "1;" at line 13, column 1. Must end with a recognizable true value. (Severity: 4)
好奇此輸出。有可配置的'ProhibitEvilModules'和'ProhibitEvilVariables'策略,但沒有'ProhibitEvilMethods'? – mob