| 1 | package App::PRT::Command::AddUse; | ||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use PPI; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | # Internal command to add use statement to file | ||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 23 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 9 | 23 | my ($class) = @_; | |||||||||||||||||||||||||||||||||
| 10 | 23 | bless { | |||||||||||||||||||||||||||||||||
| 11 | namespace => undef, | ||||||||||||||||||||||||||||||||||
| 12 | }, $class; | ||||||||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | # register a method | ||||||||||||||||||||||||||||||||||
| 16 | # arguments: | ||||||||||||||||||||||||||||||||||
| 17 | # $namespace: package name to use | ||||||||||||||||||||||||||||||||||
| 18 | 22 | ▶ | sub register { | ||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 19 | 22 | my ($self, $namespace) = @_; | |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | 22 | $self->{namespace} = $namespace; | |||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 68 | ▶ | sub namespace { | ||||||||||||||||||||||||||||||||
|
Subroutine
namespace: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 25 | 68 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | 68 | $self->{namespace}; | |||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | # refactor a file | ||||||||||||||||||||||||||||||||||
| 31 | # argumensts: | ||||||||||||||||||||||||||||||||||
| 32 | # $file: filename for refactoring | ||||||||||||||||||||||||||||||||||
| 33 | 22 | ▶ | sub execute { | ||||||||||||||||||||||||||||||||
|
Subroutine
execute: called CC=9 SCAR=22.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 34 | 22 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | 22 | my $document = PPI::Document->new($file); | |||||||||||||||||||||||||||||||||
| 37 | 22 | ▶ | return unless $document; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | 22 | my $used = 0; | |||||||||||||||||||||||||||||||||
| 40 | 22 | my $last_use; | |||||||||||||||||||||||||||||||||
| 41 | 22 | my $include_statements = $document->find('PPI::Statement::Include'); | |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | 22 | ▶ | if ($include_statements) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 44 | 17 | for my $statement (@$include_statements) { | |||||||||||||||||||||||||||||||||
| 45 | 49 | ▶ | next unless defined $statement->module; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 46 | 49 | ▶ | if ($statement->module eq $self->namespace) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 5 | $used++; | |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||
| 49 | 49 | $last_use = $statement; | |||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||
| 52 | 22 | ▶ | return if $used; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 17 | ▶ | my $insert_to = $last_use || $document->find_first('PPI::Statement::Package') || $document->find_first('PPI::Statement'); | ||||||||||||||||||||||||||||||||
|
Condition50%3/6
$last_use || $document->find_first('PPI::Statement::Package') || $document->find_first('PPI::Statement')
$last_use or $document->find_first("PPI::Statement::Package")
MC/DC0%0/3
$last_use || $document->find_first('PPI::Statement::Package') || $document->find_first('PPI::Statement')
$last_use
$document->find_first("PPI::Statement::Package")
$document->find_first('PPI::Statement')
Truth table50%2/4
A or B || C
A= $last_useB= $document->find_first("PPI::Statement::Package")C= $document->find_first('PPI::Statement')
| |||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | 17 | my $tokens_to_insert = PPI::Document->new(\"\nuse @{[ $self->namespace ]};"); | |||||||||||||||||||||||||||||||||
| 57 | 17 | $insert_to->add_element($tokens_to_insert); | |||||||||||||||||||||||||||||||||
| 58 | 17 | $document->save($file); | |||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | 2 | 1; | |||||||||||||||||||||||||||||||||