| 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
| |||||||||||||||||||||
| 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');
}
) || []
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');
}
)
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 34 | 5 | ▶ | if ($st->type eq 'use') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 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
MC/DC50%1/2
$has_use && !$has_no
$has_use
!$has_no
Truth table66%2/3
A && not(B)
A= $has_useB= !$has_no
| |||||||||||||||||||||
| 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; | |||||||||||||||||||