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   67603 use strict;
  133         188  
  133         3080  
3 133     133   420 use warnings;
  133         165  
  133         2388  
4 133     133   785 use Perl::Lint::Constants::Type;
  133         150  
  133         58530  
5 133     133   626 use parent "Perl::Lint::Policy";
  133         182  
  133         585  
6              
7             use constant {
8 133         27386 DESC => 'UNIVERSAL::can should not be used as a function',
9             EXPL => 'Use eval{$obj->can($pkg)} instead',
10 133     133   6436 };
  133         185  
11              
12             sub evaluate {
13 3     3 0 5 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 3         3 my @violations;
16 3         12 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
17 53         37 my $token_type = $token->{type};
18 53         34 my $token_data = $token->{data};
19              
20 53 100 66     183 if ($token_type == KEY && $token_data eq 'can') { # for can()
    100 66        
21 6         4 $token = $tokens->[++$i];
22 6 50       12 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