File Coverage

blib/lib/Perl/Lint/Policy/TestingAndDebugging/RequireUseStrict.pm
Criterion Covered Total %
statement 56 57 98.2
branch 15 16 93.7
condition 7 8 87.5
subroutine 9 9 100.0
pod 0 1 0.0
total 87 91 95.6


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::TestingAndDebugging::RequireUseStrict;
2 133     133   68925 use strict;
  133         162  
  133         3131  
3 133     133   390 use warnings;
  133         135  
  133         2751  
4 133     133   401 use List::Util qw/any/;
  133         136  
  133         5698  
5 133     133   856 use Perl::Lint::Constants::Type;
  133         141  
  133         58356  
6 133     133   532 use parent "Perl::Lint::Policy";
  133         147  
  133         515  
7              
8 133     133   6162 use constant EQUIVALENT_PERL_VERSION => 5.011;
  133         174  
  133         6837  
9              
10             use constant {
11 133         44255 DESC => 'Code before strictures are enabled',
12             EXPL => [429],
13 133     133   433 };
  133         144  
14              
15             sub evaluate {
16 21     21 0 33 my ($class, $file, $tokens, $src, $args) = @_;
17              
18 21         42 my @marshals = ('strict', 'Moose', 'Moose::Role', 'Moose::Util::TypeConstraints');
19 21 100       49 if (my $this_policies_arg = $args->{require_use_strict}) {
20 1   50     9 push @marshals, split / /, ($this_policies_arg->{equivalent_modules} || '');
21             }
22              
23 21         20 my @violations;
24 21         23 my $token_num = scalar @$tokens;
25 21         22 my $is_used_strict = 0;
26 21         41 TOP: for (my $i = 0; $i < $token_num; $i++) {
27 23         22 my $token = $tokens->[$i];
28 23         25 my $token_type = $token->{type};
29              
30 23 100       40 if ($token_type == USE_DECL) {
31 11         17 my $next_token = $tokens->[$i+1];
32 11 100       24 if ($next_token->{type} == DOUBLE) {
33 2         4 (my $next_token_data = $next_token->{data}) =~ s/_//g;
34 2 50       10 if ($next_token_data >= EQUIVALENT_PERL_VERSION) {
35 2         4 last;
36             }
37 0         0 next;
38             }
39              
40 9         11 my $used_module = '';
41 9         18 for ($i++; $i < $token_num; $i++) {
42 24         23 $token = $tokens->[$i];
43 24         20 my $token_type = $token->{type};
44 24         24 my $token_data = $token->{data};
45 24 100 100     95 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         30  
54 8         17 last TOP;
55             }
56 1         6 next TOP;
57             }
58             }
59             }
60              
61 12 100       18 if ($token_type == PACKAGE) {
62 1         3 for ($i++; $i < $token_num; $i++) {
63 2         3 $token = $tokens->[$i];
64 2 100       5 if ($token->{type} == SEMI_COLON) {
65 1         4 next TOP;
66             }
67             }
68             }
69              
70             push @violations, {
71             filename => $file,
72             line => $token->{line},
73 11         46 description => DESC,
74             explanation => EXPL,
75             policy => __PACKAGE__,
76             };
77 11         18 last;
78             }
79              
80 21         85 return \@violations;
81             }
82              
83             1;
84