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   69183 use strict;
  133         176  
  133         2942  
3 133     133   391 use warnings;
  133         147  
  133         2768  
4 133     133   374 use List::Util qw/any/;
  133         128  
  133         5711  
5 133     133   817 use Perl::Lint::Constants::Type;
  133         136  
  133         58828  
6 133     133   528 use parent "Perl::Lint::Policy";
  133         137  
  133         530  
7              
8 133     133   6078 use constant EQUIVALENT_PERL_VERSION => 5.005;
  133         144  
  133         6672  
9              
10             use constant {
11 133         44961 DESC => 'Code before warnings are enabled',
12             EXPL => [431],
13 133     133   459 };
  133         153  
14              
15             sub evaluate {
16 20     20 0 29 my ($class, $file, $tokens, $src, $args) = @_;
17              
18 20         42 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     7 push @marshals, split / /, ($this_policies_arg->{equivalent_modules} || '');
21             }
22              
23 20         18 my @violations;
24 20         21 my $token_num = scalar @$tokens;
25 20         17 my $is_used_strict = 0;
26 20         40 TOP: for (my $i = 0; $i < $token_num; $i++) {
27 23         21 my $token = $tokens->[$i];
28 23         32 my $token_type = $token->{type};
29              
30 23 100       37 if ($token_type == USE_DECL) {
31 11         13 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         2 last;
36             }
37 1         4 next;
38             }
39              
40 9         11 my $used_module = '';
41 9         19 for ($i++; $i < $token_num; $i++) {
42 24         22 $token = $tokens->[$i];
43 24         22 my $token_type = $token->{type};
44 24         21 my $token_data = $token->{data};
45 24 100 100     89 if (
      100        
46             $token_type == USED_NAME ||
47             $token_type == NAMESPACE ||
48             $token_type == NAMESPACE_RESOLVER
49             ) {
50 15         44 $used_module .= $token_data;
51             }
52             else {
53 9 100   22   56 if (any {$_ eq $used_module} @marshals) {
  22         29  
54 8         18 last TOP;
55             }
56 1         4 next TOP;
57             }
58             }
59             }
60              
61 12 100       17 if ($token_type == PACKAGE) {
62 1         4 for ($i++; $i < $token_num; $i++) {
63 2         3 $token = $tokens->[$i];
64 2 100       5 if ($token->{type} == SEMI_COLON) {
65 1         3 next TOP;
66             }
67             }
68             }
69              
70             push @violations, {
71             filename => $file,
72             line => $token->{line},
73 11         45 description => DESC,
74             explanation => EXPL,
75             policy => __PACKAGE__,
76             };
77 11         18 last;
78             }
79              
80 20         80 return \@violations;
81             }
82              
83             1;
84