File Coverage

blib/lib/Perl/Lint/Policy/InputOutput/ProhibitTwoArgOpen.pm
Criterion Covered Total %
statement 62 63 98.4
branch 27 30 90.0
condition 36 42 85.7
subroutine 7 7 100.0
pod 0 1 0.0
total 132 143 92.3


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::InputOutput::ProhibitTwoArgOpen;
2 133     133   126595 use strict;
  133         271  
  133         4784  
3 133     133   606 use warnings;
  133         214  
  133         3294  
4 133     133   955 use Perl::Lint::Constants::Type;
  133         215  
  133         81965  
5 133     133   1277 use Perl::Lint::Constants::Kind;
  133         229  
  133         9027  
6 133     133   787 use parent "Perl::Lint::Policy";
  133         198  
  133         766  
7              
8             use constant {
9 133         86840 DESC => 'Two-argument "open" used',
10             EXPL => [207],
11 133     133   8955 };
  133         253  
12              
13             sub evaluate {
14 7     7 0 11 my ($class, $file, $tokens, $src, $args) = @_;
15              
16 7         7 my @violations;
17 7         21 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
18 125         103 my $token_type = $token->{type};
19 125         108 my $token_data = $token->{data};
20              
21 125 100 100     475 if ($token_type == USE_DECL) {
    100          
22             # Skip when `use (<=5.005)`
23 2         3 $i++;
24 2         3 my $token = $tokens->[$i];
25 2         4 my $token_type = $token->{type};
26 2         4 my $token_data = $token->{data};
27 2 50       5 if ($token_type == DOUBLE) {
28 2         2 $token_data =~ s/_//g;
29 2 100       10 if ($token_data <= 5.005) {
30 1         7 return [];
31             }
32             }
33             }
34             elsif ($token_type == BUILTIN_FUNC && $token_data eq 'open') {
35 55         38 $i++;
36 55         43 my $token = $tokens->[$i];
37 55         54 my $token_type = $token->{type};
38              
39 55 100       62 if ($token_type == LEFT_PAREN) {
40 31         27 my $left_paren_num = 1;
41 31         23 my @args;
42 31         52 for ($i++; my $token = $tokens->[$i]; $i++) {
43 177         146 my $token_type = $token->{type};
44 177         134 my $token_kind = $token->{kind};
45              
46 177 50 100     1038 if ($token_type == LEFT_PAREN) {
    100 100        
    100 100        
      100        
47 0         0 $left_paren_num++;
48             }
49             elsif ($token_type == RIGHT_PAREN) {
50 31 50       46 last if --$left_paren_num <= 0;
51             }
52             elsif (
53             $token_type != COMMA &&
54             $token_type != REG_DOUBLE_QUOTE &&
55             $token_type != REG_QUOTE &&
56             $token_type != REG_DELIM &&
57             $token_kind != KIND_DECL
58             ) {
59 83         174 push @args, $token->{data};
60             }
61             }
62 31 100       66 if (scalar @args < 3) {
63 16         13 my $second = $args[1];
64 16 100 66     98 if (
      33        
65             $second &&
66             (
67             $second eq '-|' || $second eq '|-' ||
68             $second =~ /STD(?:OUT|ERR|IN)\Z/
69             )
70             ) {
71 4         11 next;
72             }
73              
74 12         50 push @violations, {
75             filename => $file,
76             line => $token->{line},
77             description => DESC,
78             explanation => EXPL,
79             policy => __PACKAGE__,
80             };
81             }
82             }
83             else {
84 24         19 my @args;
85 24         38 for (; my $token = $tokens->[$i]; $i++) {
86 139         117 my $token_type = $token->{type};
87 139         100 my $token_kind = $token->{kind};
88 139 100 100     947 if ($token_kind == KIND_STMT_END || $token_kind == KIND_OP) {
    100 100        
      100        
      100        
      100        
89 24         26 last;
90             }
91             elsif (
92             $token_type != COMMA &&
93             $token_type != REG_DOUBLE_QUOTE &&
94             $token_type != REG_QUOTE &&
95             $token_type != REG_DELIM &&
96             $token_kind != KIND_DECL
97             ) {
98 55         111 push @args, $token->{data};
99             }
100             }
101 24 100       49 if (scalar @args < 3) {
102 17         17 my $second = $args[1];
103 17 100 66     87 if (
      33        
104             $second &&
105             (
106             $second eq '-|' || $second eq '|-' ||
107             $second =~ /STD(?:OUT|ERR|IN)\Z/
108             )
109             ) {
110 5         11 next;
111             }
112 12         65 push @violations, {
113             filename => $file,
114             line => $token->{line},
115             description => DESC,
116             explanation => EXPL,
117             policy => __PACKAGE__,
118             };
119             }
120             }
121             }
122             }
123              
124 6         26 return \@violations;
125             }
126              
127             1;
128