bb15c9cc952a9fe083f2567e78132b3507431f26
[kivitendo-erp.git] / t / structure / no_lexicals_in_postif.t
1 use strict;
2 use lib 't';
3 use Support::Files;
4 use Test::More;
5
6 if (eval { require PPI; 1 }) {
7   plan tests => scalar(@Support::Files::testitems);
8 } else {
9   plan skip_all => "PPI not installed";
10 }
11
12 my $fh;
13 {
14     local $^W = 0;  # Don't complain about non-existent filehandles
15     if (-e \*Test::More::TESTOUT) {
16         $fh = \*Test::More::TESTOUT;
17     } elsif (-e \*Test::Builder::TESTOUT) {
18         $fh = \*Test::Builder::TESTOUT;
19     } else {
20         $fh = \*STDOUT;
21     }
22 }
23
24 my @testitems = @Support::Files::testitems;
25
26 foreach my $file (@testitems) {
27   next unless -f $file;
28   my $clean = 1;
29   my $doc = PPI::Document->new($file) or do {
30     ok 0, "PPI error for file $file: " . PPI::Document::errstr();
31     next;
32   };
33   my $stmts = $doc->find('Statement::Variable');
34
35   for my $var (@{ $stmts || [] }) {
36     # local can have valid uses like this, and our is extremely uncommon
37     next unless $var->type eq 'my';
38
39     # no if? alright
40     next unless $var->find(sub { $_[1]->content eq 'if' });
41
42     # token "if" is not in the top level struvture - no problem
43     # most likely an anonymous sub or a complicated map/grep/reduce
44     next unless grep { $_->content eq 'if'  } $var->schildren;
45
46     $clean = 0;
47     print $fh "?: $var \n";
48   }
49
50   ok $clean, $file;
51 }