« blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitCommaSeparatedStatements.pm
↑ Summary
blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitConstantPragma.pm »
| 1 | package Perl::Lint::Policy::ValuesAndExpressions::ProhibitComplexVersion; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Perl::Lint::Constants::Type; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use parent "Perl::Lint::Policy"; | ||
| 6 | |||
| 7 | use constant { | ||
| 8 | DESC => '$VERSION value should not come from outside module', | ||
| 9 | EXPL => 'If the version comes from outside the module, you can get everything from unexpected version changes to denial-of-service attacks.', | ||
| 10 | }; | ||
| 11 | |||
| 12 | sub evaluate { | ||
| 13 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 14 | |||
| 15 | my $forbid_use_version; | ||
| 16 | if (my $this_policies_arg = $args->{prohibit_complex_version}) { | ||
| 17 | $forbid_use_version = $this_policies_arg->{forbid_use_version}; | ||
| 18 | } | ||
| 19 | |||
| 20 | my @violations; | ||
| 21 | for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) { | ||
| 22 | $token_type = $token->{type}; | ||
| 23 | $token_data = $token->{data}; | ||
| 24 | |||
| 25 | if ($token_type == OUR_DECL) { | ||
| 26 | $token = $tokens->[++$i]; | ||
| 27 | $token_type = $token->{type}; | ||
| 28 | $token_data = $token->{data}; | ||
| 29 | |||
| 30 | my $is_version_assigned = 0; | ||
| 31 | |||
| 32 | if ($token_type == LEFT_PAREN) { | ||
| 33 | my $left_paren_num = 1; | ||
| 34 | for ($i++; $token = $tokens->[$i]; $i++) { | ||
| 35 | $token_type = $token->{type}; | ||
| 36 | $token_data = $token->{data}; | ||
| 37 | if ($token_type == LEFT_PAREN) { | ||
| 38 | $left_paren_num++; | ||
| 39 | } | ||
| 40 | elsif ($token_type == RIGHT_PAREN) { | ||
| 41 | if (--$left_paren_num <= 0) { | ||
| 42 | last; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | elsif ( | ||
| 46 | ($token_type == VAR || $token_type == GLOBAL_VAR) && | ||
| 47 | $token_data eq '$VERSION' | ||
| 48 | ) { | ||
| 49 | $is_version_assigned = 1; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | elsif ( | ||
| 54 | ($token_type == VAR || $token_type == GLOBAL_VAR) && | ||
| 55 | $token_data eq '$VERSION' | ||
| 56 | ) { | ||
| 57 | $is_version_assigned = 1; | ||
| 58 | } | ||
| 59 | |||
| 60 | if ($is_version_assigned) { | ||
| 61 | $i++; # skip assign symbol | ||
| 62 | |||
| 63 | $token = $tokens->[++$i]; | ||
| 64 | $token_type = $token->{type}; | ||
| 65 | $token_data = $token->{data}; | ||
| 66 | if ( | ||
| 67 | $token_type == VAR || $token_type == GLOBAL_VAR | ||
| 68 | ) { | ||
| 69 | my $next_token = $tokens->[$i+1]; | ||
| 70 | if ( | ||
| 71 | ($token_data ne '$VERSION' && $token_data =~ /\A\$[A-Z0-9_]+\Z/) || | ||
| 72 | $next_token->{type} == NAMESPACE_RESOLVER | ||
| 73 | ) { | ||
| 74 | push @violations, { | ||
| 75 | filename => $file, | ||
| 76 | line => $token->{line}, | ||
| 77 | description => DESC, | ||
| 78 | explanation => EXPL, | ||
| 79 | policy => __PACKAGE__, | ||
| 80 | }; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | elsif ($token_type == STRING) { | ||
| 84 | if ($token_data =~ /\A\$(?:\S+::)+\S+\Z/) { | ||
| 85 | push @violations, { | ||
| 86 | filename => $file, | ||
| 87 | line => $token->{line}, | ||
| 88 | description => DESC, | ||
| 89 | explanation => EXPL, | ||
| 90 | policy => __PACKAGE__, | ||
| 91 | }; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | elsif ($token_type == REG_DOUBLE_QUOTE) { | ||
| 95 | $i++; # skip reg delimiter | ||
| 96 | $token = $tokens->[++$i]; | ||
| 97 | if ($token->{data} =~ /\A\$(?:\S+::)+\S+\Z/) { | ||
| 98 | push @violations, { | ||
| 99 | filename => $file, | ||
| 100 | line => $token->{line}, | ||
| 101 | description => DESC, | ||
| 102 | explanation => EXPL, | ||
| 103 | policy => __PACKAGE__, | ||
| 104 | }; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | elsif ($token_type == NAMESPACE) { | ||
| 108 | $token = $tokens->[++$i]; | ||
| 109 | if ($token->{type} == NAMESPACE_RESOLVER) { | ||
| 110 | push @violations, { | ||
| 111 | filename => $file, | ||
| 112 | line => $token->{line}, | ||
| 113 | description => DESC, | ||
| 114 | explanation => EXPL, | ||
| 115 | policy => __PACKAGE__, | ||
| 116 | }; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | elsif ($token_type == LEFT_PAREN) { | ||
| 120 | my $left_paren_num = 1; | ||
| 121 | for ($i++; $token = $tokens->[$i]; $i++) { | ||
| 122 | $token_type = $token->{type}; | ||
| 123 | if ($token_type == LEFT_PAREN) { | ||
| 124 | $left_paren_num++; | ||
| 125 | } | ||
| 126 | elsif ($token_type == RIGHT_PAREN) { | ||
| 127 | if (--$left_paren_num <= 0) { | ||
| 128 | last; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | elsif ($token_type == NAMESPACE_RESOLVER) { | ||
| 132 | push @violations, { | ||
| 133 | filename => $file, | ||
| 134 | line => $token->{line}, | ||
| 135 | description => DESC, | ||
| 136 | explanation => EXPL, | ||
| 137 | policy => __PACKAGE__, | ||
| 138 | }; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | elsif ($token_type == DO || ($token_type == BUILTIN_FUNC && $token_data eq 'eval')) { | ||
| 143 | $token = $tokens->[++$i]; | ||
| 144 | if ($token->{type} == LEFT_BRACE) { | ||
| 145 | my $left_brace_num = 1; | ||
| 146 | for ($i++; $token = $tokens->[$i]; $i++) { | ||
| 147 | $token_type = $token->{type}; | ||
| 148 | if ($token_type == LEFT_BRACE) { | ||
| 149 | $left_brace_num++; | ||
| 150 | } | ||
| 151 | elsif ($token_type == RIGHT_BRACE) { | ||
| 152 | if (--$left_brace_num <= 0) { | ||
| 153 | last; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | elsif ($token_type == NAMESPACE_RESOLVER) { | ||
| 157 | push @violations, { | ||
| 158 | filename => $file, | ||
| 159 | line => $token->{line}, | ||
| 160 | description => DESC, | ||
| 161 | explanation => EXPL, | ||
| 162 | policy => __PACKAGE__, | ||
| 163 | }; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | } | ||
| 170 | elsif ($token_type == USED_NAME && $token_data eq 'version') { | ||
| 171 | if ($forbid_use_version) { | ||
| 172 | push @violations, { | ||
| 173 | filename => $file, | ||
| 174 | line => $token->{line}, | ||
| 175 | description => DESC, | ||
| 176 | explanation => EXPL, | ||
| 177 | policy => __PACKAGE__, | ||
| 178 | }; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | return \@violations; | ||
| 184 | } | ||
| 185 | |||
| 186 | 1; | ||
| 187 | |||