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   93515 use strict;
  133         295  
  133         5317  
3 133     133   676 use warnings;
  133         225  
  133         3507  
4 133     133   1029 use Perl::Lint::Constants::Type;
  133         226  
  133         83745  
5 133     133   1241 use Perl::Lint::Constants::Kind;
  133         226  
  133         9162  
6 133     133   751 use parent "Perl::Lint::Policy";
  133         243  
  133         737  
7              
8             use constant {
9 133         60056 DESC => '"select" used to emulate "sleep"',
10             EXPL => [168],
11 133     133   8416 };
  133         249  
12              
13             sub evaluate {
14 9     9 0 14 my ($class, $file, $tokens, $src, $args) = @_;
15              
16 9         10 my @violations;
17 9         30 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
18 74         58 my $token_type = $token->{type};
19 74         60 my $token_data = $token->{data};
20              
21 74 100 100     200 if ($token_type == BUILTIN_FUNC && $token_data eq 'select') {
22 12         11 my $left_paren_num = 1;
23 12         10 my $comma_num = 0;
24 12         11 my $is_on_some_IO = 0;
25 12         12 my $last_arg = '';
26              
27 12         15 $token = $tokens->[++$i];
28 12 100       22 if ($token->{type} == LEFT_PAREN) {
29 3         10 for ($i++; $token = $tokens->[$i]; $i++) {
30 26         25 $token_type = $token->{type};
31 26 50       54 if ($token_type == LEFT_PAREN) {
    100          
    100          
32 0         0 $left_paren_num++;
33             }
34             elsif ($token_type == RIGHT_PAREN) {
35 3 50       7 if (--$left_paren_num <= 0) {
36 3         6 my $last_arg_type = $last_arg->{type};
37 3 50 66     28 if (!$is_on_some_IO && $comma_num == 3 && ($last_arg_type == DOUBLE || ($last_arg_type != DEFAULT && $last_arg->{kind} == KIND_TERM))) {
      66        
      66        
38 2         11 push @violations, {
39             filename => $file,
40             line => $token->{line},
41             description => DESC,
42             explanation => EXPL,
43             policy => __PACKAGE__,
44             };
45             }
46 3         12 last;
47             }
48             }
49             elsif ($token_type == COMMA) {
50 9         16 $comma_num++;
51             }
52             else {
53 14 100 100     41 if ($comma_num < 3 && $token_type != DEFAULT) {
54 3         4 $is_on_some_IO = 1;
55             }
56 14         25 $last_arg = $token;
57             }
58             }
59             }
60             else {
61 9         19 for (; $token = $tokens->[$i]; $i++) {
62 72         60 $token_type = $token->{type};
63 72 100       100 if ($token_type == SEMI_COLON) {
    100          
64 9 50       16 if (--$left_paren_num <= 0) {
65 9         13 my $last_arg_type = $last_arg->{type};
66 9 50 66     44 if (!$is_on_some_IO && $comma_num == 3 && ($last_arg_type == DOUBLE || ($last_arg_type != DEFAULT && $last_arg->{kind} == KIND_TERM))) {
      33        
      66        
67 6         21 push @violations, {
68             filename => $file,
69             line => $token->{line},
70             description => DESC,
71             explanation => EXPL,
72             policy => __PACKAGE__,
73             };
74             }
75 9         24 last;
76             }
77             }
78             elsif ($token_type == COMMA) {
79 27         43 $comma_num++;
80             }
81             else {
82 36 100 100     91 if ($comma_num < 3 && $token_type != DEFAULT) {
83 3         4 $is_on_some_IO = 1;
84             }
85 36         55 $last_arg = $token;
86             }
87             }
88             }
89             }
90             }
91              
92 9         36 return \@violations;
93             }
94              
95             1;
96