PerlNitpick/Rule/AppendUnimportStatement.pm

filter: Statementstmt 96.6% 29 / 30 Branchbran 62.5% 5 / 8 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 3 totaltotal 80.0% 44 / 55 CC 9 SCAR 16.1
CC
9
Cov
86%
has_import_but_has_no_unimport
8.1
rewrite
3.0
__ANON__
1.0
CRAP 5.0
« PerlNitpick/Rule/DedupeIncludeStatements.pm ↑ Summary PerlNitpick/Nitpicker.pm »
1 package App::PerlNitpick::Rule::AppendUnimportStatement;
2 # ABSTRACT: Ensure a 'no Moose;' statement is there if 'use Moose;' is.
3  
4 =encoding UTF-8
5  
6 =head1 DESCRIPTION
7  
8 This nitpicking rule ensure a 'no Moose' statement is present in the file if a 'use Moose' is there.
9  
10 =cut
11  
12 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use PPI::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 ▶sub rewrite {
Subroutine rewrite: called CC=3 SCAR=11.0
Pod undocumented
16 6 my ($self, $doc) = @_;
17  
18 6 for my $module ('Moose', 'Mouse', 'Moo', 'Moose::Role', 'Mouse::Role') {
19 30 ▶ if ($self->has_import_but_has_no_unimport($doc, $module)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_import_but_has_no_unimport($doc, $module)) 5 25 30
20 5 $self->append_unimport($doc, $module);
21 }
22 }
23  
24 6 return $doc;
25 }
26  
27 30 ▶sub has_import_but_has_no_unimport {
Subroutine has_import_but_has_no_unimport: called CC=7 SCAR=21.0
Pod undocumented
28 30 my ($self, $doc, $module) = @_;
29 30 ▶ my $include_statements = $doc->find(sub { $_[1]->isa('PPI::Statement::Include') }) || [];
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition50%1/2
$doc->find(sub { $_[1]->isa('PPI::Statement::Include'); } ) || []
l!l
300
MC/DC0%0/1
$doc->find(sub { $_[1]->isa('PPI::Statement::Include'); } ) || []
$doc->find(sub { $_[1]->isa('PPI::Statement::Include'); } )
Truth table50%1/2
A
A= $doc->find(sub { $_[1]->isa('PPI::Statement::Include'); } )
Aresult
11
00
30  
31 30 my ($has_use, $has_no);
32 30 for my $st (@$include_statements) {
33 40 ▶ next unless $st->module eq $module;
Branch: 1 branch
BranchTrueFalseTotal
unless $st->module eq $module 35 5 40
34 5 ▶ if ($st->type eq 'use') {
Branch: 2 branches
BranchTrueFalseTotal
if ($st->type eq 'use') { } 5 0 5
elsif ($st->type eq 'no') { } 0 0 0
35 5 $has_use = 1;
36 } elsif ($st->type eq 'no') {
37 0 $has_no = 1;
38 }
39 }
40 30 ▶ return $has_use && !$has_no;
Condition66%2/3
$has_use && !$has_no
!ll&&!rl&&r
2505
MC/DC50%1/2
$has_use && !$has_no
$has_use !$has_no
Truth table66%2/3
A && not(B)
A= $has_useB= !$has_no
ABresult
0X0
100
111
41 }
42  
43 5 ▶sub append_unimport {
Subroutine append_unimport: called CC=1 SCAR=0.0
Pod undocumented
44 5 my ($self, $doc, $module) = @_;
45  
46 5 my $doc2 = PPI::Document->new(\"no ${module};");
47 5 my $el = $doc2->find_first('PPI::Statement::Include');
48 5 $el->remove;
49  
50 5 my @child = $doc->schildren();
51 5 $child[-1]->insert_before($el);
52 5 $child[-1]->insert_before(PPI::Token::Whitespace->new("\n"));
53  
54 5 return $doc;
55 }
56  
57 1 1;
« PerlNitpick/Rule/DedupeIncludeStatements.pm ↑ Summary PerlNitpick/Nitpicker.pm »