File Coverage

blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitUniversalCan.pm
Criterion Covered Total %
statement 30 30 100.0
branch 8 10 80.0
condition 5 9 55.5
subroutine 6 6 100.0
pod 0 1 0.0
total 49 56 87.5


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::BuiltinFunctions::ProhibitUniversalCan;
2 133     133   67983 use strict;
  133         185  
  133         3182  
3 133     133   428 use warnings;
  133         165  
  133         2494  
4 133     133   793 use Perl::Lint::Constants::Type;
  133         152  
  133         59357  
5 133     133   572 use parent "Perl::Lint::Policy";
  133         189  
  133         566  
6              
7             use constant {
8 133         27619 DESC => 'UNIVERSAL::can should not be used as a function',
9             EXPL => 'Use eval{$obj->can($pkg)} instead',
10 133     133   6424 };
  133         187  
11              
12             sub evaluate {
13 3     3 0 4 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 3         4 my @violations;
16 3         12 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
17 53         36 my $token_type = $token->{type};
18 53         36 my $token_data = $token->{data};
19              
20 53 100 66     179 if ($token_type == KEY && $token_data eq 'can') { # for can()
    100 66        
21 6         6 $token = $tokens->[++$i];
22 6 50       11 if ($token->{type} == LEFT_PAREN) {
23             push @violations, {
24             filename => $file,
25             line => $token->{line},
26 6         22 description => DESC,
27             explanation => EXPL,
28             policy => __PACKAGE__,
29             };
30             }
31             }
32             elsif ($token_type == NAMESPACE && $token_data eq 'UNIVERSAL') { # for UNIVERSAL::can()
33 3         3 $i += 2; # skip the name space resolver
34 3         4 $token = $tokens->[$i];
35 3 50 33     12 if ($token->{type} == NAMESPACE && $token->{data} eq 'can') {
36 3 100       9 if ($tokens->[++$i]->{type} == LEFT_PAREN) {
37             push @violations, {
38             filename => $file,
39             line => $token->{line},
40 1         5 description => DESC,
41             explanation => EXPL,
42             policy => __PACKAGE__,
43             };
44             }
45             }
46             }
47             }
48              
49 3         9 return \@violations;
50             }
51              
52             1;
53