Search code examples
perlperl-critic

PerlCritic flags ProhibitMagicNumbers violation 10 times only


I ran PerlCritic on the following code:

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;
    }
};

The command I used was (the name of the file was test_9.pl)

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

This is the result I got after running the command above

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

As it can be seen, there are many instances of hardcoded values, yet PerlCritic flag the violation Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers only 10 times. Is there a way to make PerlCritic flag an error more than 10 times?


Solution

  • I can see no way to increase the number of ProhibitMagicNumbers violations using just the perlcritic command options.

    However, searching through the source code for Perl::Critic::Policy::ValuesAndExpressions::ProhibitMagicNumbers, I can see this limit is set by the default_maximum_violations_per_document sub. The t/02_policy.t test shows how to change the limit.

    You could request a simpler way to change the value by submitting an issue to the code maintainers.