File Coverage

blib/lib/Perl/Lint/Policy/InputOutput/RequireBracedFileHandleWithPrint.pm
Criterion Covered Total %
statement 41 41 100.0
branch 9 10 90.0
condition 18 21 85.7
subroutine 7 7 100.0
pod 0 1 0.0
total 75 80 93.7


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::InputOutput::RequireBracedFileHandleWithPrint;
2 134     134   72066 use strict;
  134         199  
  134         3312  
3 134     134   450 use warnings;
  134         168  
  134         2604  
4 134     134   816 use Perl::Lint::Constants::Type;
  134         181  
  134         63443  
5 134     134   954 use Perl::Lint::Constants::Kind;
  134         222  
  134         6591  
6 134     134   498 use parent "Perl::Lint::Policy";
  134         236  
  134         624  
7              
8             use constant {
9 134         36395 DESC => 'File handle for "print" or "printf" is not braced',
10             EXPL => [217],
11 134     134   6870 };
  134         198  
12              
13             sub evaluate {
14 11     11 0 16 my ($class, $file, $tokens, $src, $args) = @_;
15              
16 11         10 my @violations;
17 11         31 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
18 178         130 my $token_type = $token->{type};
19 178         132 my $token_data = $token->{data};
20              
21 178 50 66     453 if ($token_type == BUILTIN_FUNC && ($token_data eq 'print' || $token_data eq 'printf' || $token_data eq 'say')) {
      66        
22 121         70 my @function_args;
23 121         153 for ($i++; my $token = $tokens->[$i]; $i++) {
24 665         453 my $token_type = $token->{type};
25 665         389 my $token_kind = $token->{kind};
26              
27 665 100 100     1529 if ($token_type == SEMI_COLON) {
    100          
28 121         81 last;
29             }
30             elsif ($token_type == LEFT_PAREN || $token_type == RIGHT_PAREN) {
31 94         126 next;
32             }
33              
34 450         577 push @function_args, $token;
35             }
36              
37 121 100       161 if (scalar @function_args > 1) {
38 110         70 my $first_arg = $function_args[0];
39 110         69 my $first_arg_type = $first_arg->{type};
40 110         64 my $second_arg = $function_args[1];
41 110         77 my $second_arg_type = $second_arg->{type};
42 110         64 my $second_arg_kind = $second_arg->{kind};
43 110 100 100     739 if (
      100        
      100        
      66        
44             (
45             $first_arg_type == GLOBAL_VAR ||
46             $first_arg_type == LOCAL_VAR ||
47             $first_arg_type == VAR ||
48             $first_arg_type == KEY
49             ) &&
50             $second_arg_kind != KIND_OP &&
51             $second_arg_type != COMMA &&
52             $second_arg_type != STRING_ADD
53             ) {
54             push @violations, {
55             filename => $file,
56             line => $token->{line},
57 25         91 description => DESC,
58             explanation => EXPL,
59             policy => __PACKAGE__,
60             };
61             }
62             }
63             }
64              
65             }
66              
67 11         35 return \@violations;
68             }
69              
70             1;
71