File Coverage

blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitSleepViaSelect.pm
Criterion Covered Total %
statement 55 56 98.2
branch 21 26 80.7
condition 20 27 74.0
subroutine 7 7 100.0
pod 0 1 0.0
total 103 117 88.0


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::BuiltinFunctions::ProhibitSleepViaSelect;
2 133     133   68938 use strict;
  133         183  
  133         3226  
3 133     133   430 use warnings;
  133         180  
  133         2519  
4 133     133   782 use Perl::Lint::Constants::Type;
  133         157  
  133         59743  
5 133     133   961 use Perl::Lint::Constants::Kind;
  133         171  
  133         6747  
6 133     133   480 use parent "Perl::Lint::Policy";
  133         178  
  133         589  
7              
8             use constant {
9 133         44621 DESC => '"select" used to emulate "sleep"',
10             EXPL => [168],
11 133     133   6538 };
  133         198  
12              
13             sub evaluate {
14 9     9 0 16 my ($class, $file, $tokens, $src, $args) = @_;
15              
16 9         7 my @violations;
17 9         26 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
18 74         58 my $token_type = $token->{type};
19 74         51 my $token_data = $token->{data};
20              
21 74 100 100     153 if ($token_type == BUILTIN_FUNC && $token_data eq 'select') {
22 12         12 my $left_paren_num = 1;
23 12         9 my $comma_num = 0;
24 12         5 my $is_on_some_IO = 0;
25 12         10 my $last_arg = '';
26              
27 12         14 $token = $tokens->[++$i];
28 12 100       21 if ($token->{type} == LEFT_PAREN) {
29 3         8 for ($i++; $token = $tokens->[$i]; $i++) {
30 26         20 $token_type = $token->{type};
31 26 50       41 if ($token_type == LEFT_PAREN) {
    100          
    100          
32 0         0 $left_paren_num++;
33             }
34             elsif ($token_type == RIGHT_PAREN) {
35 3 50       5 if (--$left_paren_num <= 0) {
36 3         4 my $last_arg_type = $last_arg->{type};
37 3 50 66     27 if (!$is_on_some_IO && $comma_num == 3 && ($last_arg_type == DOUBLE || ($last_arg_type != DEFAULT && $last_arg->{kind} == KIND_TERM))) {
      66        
      66        
38             push @violations, {
39             filename => $file,
40             line => $token->{line},
41 2         10 description => DESC,
42             explanation => EXPL,
43             policy => __PACKAGE__,
44             };
45             }
46 3         8 last;
47             }
48             }
49             elsif ($token_type == COMMA) {
50 9         15 $comma_num++;
51             }
52             else {
53 14 100 100     38 if ($comma_num < 3 && $token_type != DEFAULT) {
54 3         2 $is_on_some_IO = 1;
55             }
56 14         19 $last_arg = $token;
57             }
58             }
59             }
60             else {
61 9         18 for (; $token = $tokens->[$i]; $i++) {
62 72         54 $token_type = $token->{type};
63 72 100       84 if ($token_type == SEMI_COLON) {
    100          
64 9 50       13 if (--$left_paren_num <= 0) {
65 9         10 my $last_arg_type = $last_arg->{type};
66 9 50 66     30 if (!$is_on_some_IO && $comma_num == 3 && ($last_arg_type == DOUBLE || ($last_arg_type != DEFAULT && $last_arg->{kind} == KIND_TERM))) {
      33        
      66        
67             push @violations, {
68             filename => $file,
69             line => $token->{line},
70 6         17 description => DESC,
71             explanation => EXPL,
72             policy => __PACKAGE__,
73             };
74             }
75 9         20 last;
76             }
77             }
78             elsif ($token_type == COMMA) {
79 27         33 $comma_num++;
80             }
81             else {
82 36 100 100     81 if ($comma_num < 3 && $token_type != DEFAULT) {
83 3         6 $is_on_some_IO = 1;
84             }
85 36         47 $last_arg = $token;
86             }
87             }
88             }
89             }
90             }
91              
92 9         29 return \@violations;
93             }
94              
95             1;
96