PerlCritic 仅标记 ProhibitMagicNumbers 违规 10 次

PerlCritic flags ProhibitMagicNumbers violation 10 times only

我 运行 对以下代码的 PerlCritic:

my @map;
$map{3}=3;
$map{2}=2;
$map{1}=1;
$map{6}=6;
$map{4}=4;
$map{12}=13;
$map{5}=5;
$map{11}=11;
$map{7}=7;
$map{23}=23;
$map{15}=15;
$map{47}=49;
$map{10}=10;
$map{31}=32;
$map{95}=104;
$map{21}=22;
$map{63}=69;
$map{190}=222;
$map{14}=14;
$map{42}=46;
$map{127}=147;
$map{381}=473;
$map{9}=9;
$map{28}=30;
$map{84}=98;
$map{254}=314;
$map{762}=1009;
$map{18}=19;
$map{56}=63;
$map{169}=207;
$map{508}=668;
$map{1525}=2150;
$map{37}=41;
$map{113}=134;

for (my $var = 0; $var < 10; $var++) {
    my $input = <>;
    if($input eq """")
    {
        goto getout;
    }
    chomp($input);
    print coinSub($input);
    print ""\n"";
};
getout:;
sub coinSub{
    my $coin = shift;
    if($map{$coin} eq """")
    {
        my $coinA = int($coin / 2);
        my $coinB = int($coin / 3);
        my $coinC = int($coin / 4);
        if($coinA < 2 or $coinB < 2 or $coinB < 2)
        {
            goto subEnd;
        }
        $coinA = coinSub($coinA);
        $coinB = coinSub($coinB);
        $coinC = coinSub($coinC);

subEnd:;
        my $exchangeAmt = $coinA + $coinB + $coinC;
        $map{$coin} = max($exchangeAmt,$coin);
        return max($exchangeAmt,$coin);
    }
    else
    {
        return $map{$coin};
    }
};
sub max{
    my $no1 = shift;
    my $no2 = shift;
    if($no1 > $no2)
    {
        return $no1;
    }
    else
    {
        return $no2;
    }
};

我使用的命令是(文件名是test_9.pl)

perlcritic --brutal --theme core --verbose "%f, %P, %m, %l\n" test_9.pl

这是我在运行上面的命令

之后得到的结果
test_9.pl, Perl::Critic::Policy::CodeLayout::RequireTidyCode, perltidy had errors!!, 1
test_9.pl, Perl::Critic::Policy::Modules::RequireExplicitPackage, Code not contained in explicit package, 1
test_9.pl, Perl::Critic::Policy::Modules::RequireVersionVar, No package-scoped "$VERSION" variable found, 1
test_9.pl, Perl::Critic::Policy::TestingAndDebugging::RequireUseStrict, Code before strictures are enabled, 1
test_9.pl, Perl::Critic::Policy::TestingAndDebugging::RequireUseWarnings, Code before warnings are enabled, 1
test_9.pl, Perl::Critic::Policy::Variables::ProhibitUnusedVariables, "@map" is declared but not used, 1
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers, 3 is not one of the allowed literal values (0, 1, 2). Use the Readonly or Const::Fast module or the "constant" pragma instead, 2
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers, 3 is not one of the allowed literal values (0, 1, 2). Use the Readonly or Const::Fast module or the "constant" pragma instead, 2
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers, 6 is not one of the allowed literal values (0, 1, 2). Use the Readonly or Const::Fast module or the "constant" pragma instead, 5
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers, 6 is not one of the allowed literal values (0, 1, 2). Use the Readonly or Const::Fast module or the "constant" pragma instead, 5
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers, 4 is not one of the allowed literal values (0, 1, 2). Use the Readonly or Const::Fast module or the "constant" pragma instead, 6
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers, 4 is not one of the allowed literal values (0, 1, 2). Use the Readonly or Const::Fast module or the "constant" pragma instead, 6
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers, 12 is not one of the allowed literal values (0, 1, 2). Use the Readonly or Const::Fast module or the "constant" pragma instead, 7
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers, 13 is not one of the allowed literal values (0, 1, 2). Use the Readonly or Const::Fast module or the "constant" pragma instead, 7
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers, 5 is not one of the allowed literal values (0, 1, 2). Use the Readonly or Const::Fast module or the "constant" pragma instead, 8
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers, 5 is not one of the allowed literal values (0, 1, 2). Use the Readonly or Const::Fast module or the "constant" pragma instead, 8
test_9.pl, Perl::Critic::Policy::ControlStructures::ProhibitCStyleForLoops, C-style "for" loop used, 37
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyQuotes, Quotes used with a string containing no non-whitespace characters, 39
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals, Useless interpolation of literal string, 39
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyQuotes, Quotes used with a string containing no non-whitespace characters, 39
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals, Useless interpolation of literal string, 39
test_9.pl, Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins, Builtin function called with parentheses, 43
test_9.pl, Perl::Critic::Policy::InputOutput::RequireCheckedSyscalls, Return value of flagged function ignored - print, 44
test_9.pl, Perl::Critic::Policy::InputOutput::RequireCheckedSyscalls, Return value of flagged function ignored - print, 45
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyQuotes, Quotes used with a string containing no non-whitespace characters, 45
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals, Useless interpolation of literal string, 45
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyQuotes, Quotes used with a string containing no non-whitespace characters, 45
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals, Useless interpolation of literal string, 45
test_9.pl, Perl::Critic::Policy::NamingConventions::Capitalization, Label "getout" is not all upper case, 47
test_9.pl, Perl::Critic::Policy::NamingConventions::Capitalization, Subroutine "coinSub" is not all lower case or all upper case, 48
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyQuotes, Quotes used with a string containing no non-whitespace characters, 50
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals, Useless interpolation of literal string, 50
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyQuotes, Quotes used with a string containing no non-whitespace characters, 50
test_9.pl, Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals, Useless interpolation of literal string, 50
test_9.pl, Perl::Critic::Policy::NamingConventions::Capitalization, Local lexical variable "$coinA" is not all lower case or all upper case, 52
test_9.pl, Perl::Critic::Policy::NamingConventions::Capitalization, Local lexical variable "$coinB" is not all lower case or all upper case, 53
test_9.pl, Perl::Critic::Policy::NamingConventions::Capitalization, Local lexical variable "$coinC" is not all lower case or all upper case, 54
test_9.pl, Perl::Critic::Policy::CodeLayout::ProhibitTrailingWhitespace, Found "\t" at the end of the line, 62
test_9.pl, Perl::Critic::Policy::NamingConventions::Capitalization, Label "subEnd" is not all upper case, 63
test_9.pl, Perl::Critic::Policy::NamingConventions::Capitalization, Local lexical variable "$exchangeAmt" is not all lower case or all upper case, 64
test_9.pl, Perl::Critic::Policy::Modules::RequireEndWithOne, Module does not end with "1;", 73

可以看出,有很多硬编码值的实例,但 PerlCritic 仅标记违规 Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers 10 次。有没有办法让 PerlCritic 标记错误超过 10 次?

我认为仅使用 perlcritic 命令选项无法增加 ProhibitMagicNumbers 违规次数。

但是,通过搜索 Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers 的源代码,我可以看到这个限制是由 default_maximum_violations_per_document 子设置的。 t/02_policy.t 测试展示了如何更改限制。

您可以通过向代码维护者提交问题来请求更简单的方法来更改值。