File Coverage

blib/lib/App/PerlNitpick/Rule/AppendUnimportStatement.pm
Criterion Covered Total %
statement 28 29 96.5
branch 5 8 62.5
condition 3 5 60.0
subroutine 6 6 100.0
pod 0 3 0.0
total 42 51 82.3


line stmt bran cond sub pod time code
1             # ABSTRACT: Ensure a 'no Moose;' statement is there if 'use Moose;' is.
2              
3             =encoding UTF-8
4              
5             =head1 DESCRIPTION
6              
7             This nitpicking rule ensure a 'no Moose' statement is present in the file if a 'use Moose' is there.
8              
9             =cut
10              
11             use Moose;
12 1     1   205614 use PPI::Document;
  1         384946  
  1         6  
13 1     1   6828  
  1         90133  
  1         314  
14             my ($self, $doc) = @_;
15              
16 6     6 0 31039 for my $module ('Moose', 'Mouse', 'Moo', 'Moose::Role', 'Mouse::Role') {
17             if ($self->has_import_but_has_no_unimport($doc, $module)) {
18 6         13 $self->append_unimport($doc, $module);
19 30 100       111 }
20 5         14 }
21              
22             return $doc;
23             }
24 6         33  
25             my ($self, $doc, $module) = @_;
26             my $include_statements = $doc->find(sub { $_[1]->isa('PPI::Statement::Include') }) || [];
27              
28 30     30 0 50 my ($has_use, $has_no);
29 30   50 855   136 for my $st (@$include_statements) {
  855         7803  
30             next unless $st->module eq $module;
31 30         313 if ($st->type eq 'use') {
32 30         48 $has_use = 1;
33 40 100       260 } elsif ($st->type eq 'no') {
34 5 50       100 $has_no = 1;
    0          
35 5         86 }
36             }
37 0         0 return $has_use && !$has_no;
38             }
39              
40 30   66     553 my ($self, $doc, $module) = @_;
41              
42             my $doc2 = PPI::Document->new(\"no ${module};");
43             my $el = $doc2->find_first('PPI::Statement::Include');
44 5     5 0 10 $el->remove;
45              
46 5         19 my @child = $doc->schildren();
47 5         3334 $child[-1]->insert_before($el);
48 5         857 $child[-1]->insert_before(PPI::Token::Whitespace->new("\n"));
49              
50 5         171 return $doc;
51 5         90 }
52 5         202  
53             1;