File Coverage

blib/lib/Perl/Lint/Policy/TestingAndDebugging/RequireUseWarnings.pm
Criterion Covered Total %
statement 57 57 100.0
branch 16 16 100.0
condition 7 8 87.5
subroutine 9 9 100.0
pod 0 1 0.0
total 89 91 97.8


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::TestingAndDebugging::RequireUseWarnings;
2 133     133   92757 use strict;
  133         254  
  133         4991  
3 133     133   591 use warnings;
  133         192  
  133         3752  
4 133     133   593 use List::Util qw/any/;
  133         194  
  133         8299  
5 133     133   1100 use Perl::Lint::Constants::Type;
  133         209  
  133         82944  
6 133     133   758 use parent "Perl::Lint::Policy";
  133         220  
  133         812  
7              
8 133     133   8295 use constant EQUIVALENT_PERL_VERSION => 5.005;
  133         223  
  133         8219  
9              
10             use constant {
11 133         59566 DESC => 'Code before warnings are enabled',
12             EXPL => [431],
13 133     133   639 };
  133         225  
14              
15             sub evaluate {
16 20     20 0 88 my ($class, $file, $tokens, $src, $args) = @_;
17              
18 20         43 my @marshals = ('warnings', 'Moose', 'Moose::Role', 'Moose::Util::TypeConstraints');
19 20 100       43 if (my $this_policies_arg = $args->{require_use_warnings}) {
20 1   50     5 push @marshals, split / /, ($this_policies_arg->{equivalent_modules} || '');
21             }
22              
23 20         18 my @violations;
24 20         26 my $token_num = scalar @$tokens;
25 20         22 my $is_used_strict = 0;
26 20         43 TOP: for (my $i = 0; $i < $token_num; $i++) {
27 23         26 my $token = $tokens->[$i];
28 23         24 my $token_type = $token->{type};
29              
30 23 100       45 if ($token_type == USE_DECL) {
31 11         14 my $next_token = $tokens->[$i+1];
32 11 100       22 if ($next_token->{type} == DOUBLE) {
33 2         4 (my $next_token_data = $next_token->{data}) =~ s/_//g;
34 2 100       8 if ($next_token_data == EQUIVALENT_PERL_VERSION) {
35 1         3 last;
36             }
37 1         3 next;
38             }
39              
40 9         11 my $used_module = '';
41 9         22 for ($i++; $i < $token_num; $i++) {
42 24         20 $token = $tokens->[$i];
43 24         20 my $token_type = $token->{type};
44 24         22 my $token_data = $token->{data};
45 24 100 100     94 if (
      100        
46             $token_type == USED_NAME ||
47             $token_type == NAMESPACE ||
48             $token_type == NAMESPACE_RESOLVER
49             ) {
50 15         34 $used_module .= $token_data;
51             }
52             else {
53 9 100   22   66 if (any {$_ eq $used_module} @marshals) {
  22         29  
54 8         16 last TOP;
55             }
56 1         6 next TOP;
57             }
58             }
59             }
60              
61 12 100       23 if ($token_type == PACKAGE) {
62 1         3 for ($i++; $i < $token_num; $i++) {
63 2         4 $token = $tokens->[$i];
64 2 100       4 if ($token->{type} == SEMI_COLON) {
65 1         3 next TOP;
66             }
67             }
68             }
69              
70 11         44 push @violations, {
71             filename => $file,
72             line => $token->{line},
73             description => DESC,
74             explanation => EXPL,
75             policy => __PACKAGE__,
76             };
77 11         21 last;
78             }
79              
80 20         84 return \@violations;
81             }
82              
83             1;
84