| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::PRT::Command::AddUse; | 
| 2 | 2 |  |  | 2 |  | 2716 | use strict; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 64 |  | 
| 3 | 2 |  |  | 2 |  | 11 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 53 |  | 
| 4 | 2 |  |  | 2 |  | 575 | use PPI; | 
|  | 2 |  |  |  |  | 107026 |  | 
|  | 2 |  |  |  |  | 606 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | # Internal command to add use statement to file | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | sub new { | 
| 9 | 23 |  |  | 23 | 0 | 21548 | my ($class) = @_; | 
| 10 | 23 |  |  |  |  | 114 | bless { | 
| 11 |  |  |  |  |  |  | namespace => undef, | 
| 12 |  |  |  |  |  |  | }, $class; | 
| 13 |  |  |  |  |  |  | } | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | # register a method | 
| 16 |  |  |  |  |  |  | # arguments: | 
| 17 |  |  |  |  |  |  | #   $namespace: package name to use | 
| 18 |  |  |  |  |  |  | sub register { | 
| 19 | 22 |  |  | 22 | 0 | 90 | my ($self, $namespace) = @_; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 22 |  |  |  |  | 69 | $self->{namespace} = $namespace; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub namespace { | 
| 25 | 68 |  |  | 68 | 0 | 1138 | my ($self) = @_; | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 68 |  |  |  |  | 261 | $self->{namespace}; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | # refactor a file | 
| 31 |  |  |  |  |  |  | # argumensts: | 
| 32 |  |  |  |  |  |  | #   $file: filename for refactoring | 
| 33 |  |  |  |  |  |  | sub execute { | 
| 34 | 22 |  |  | 22 | 0 | 11238 | my ($self, $file) = @_; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 22 |  |  |  |  | 120 | my $document = PPI::Document->new($file); | 
| 37 | 22 | 50 |  |  |  | 167734 | return unless $document; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 22 |  |  |  |  | 48 | my $used = 0; | 
| 40 | 22 |  |  |  |  | 38 | my $last_use; | 
| 41 | 22 |  |  |  |  | 100 | my $include_statements = $document->find('PPI::Statement::Include'); | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 22 | 100 |  |  |  | 39412 | if ($include_statements) { | 
| 44 | 17 |  |  |  |  | 55 | for my $statement (@$include_statements) { | 
| 45 | 49 | 50 |  |  |  | 141 | next unless defined $statement->module; | 
| 46 | 49 | 100 |  |  |  | 1233 | if ($statement->module eq $self->namespace) { | 
| 47 | 5 |  |  |  |  | 9 | $used++; | 
| 48 |  |  |  |  |  |  | } | 
| 49 | 49 |  |  |  |  | 89 | $last_use = $statement; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | } | 
| 52 | 22 | 100 |  |  |  | 74 | return if $used; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 17 |  | 33 |  |  | 91 | my $insert_to = $last_use || $document->find_first('PPI::Statement::Package') || $document->find_first('PPI::Statement'); | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 17 |  |  |  |  | 1088 | my $tokens_to_insert = PPI::Document->new(\"\nuse @{[ $self->namespace ]};"); | 
|  | 17 |  |  |  |  | 47 |  | 
| 57 | 17 |  |  |  |  | 17170 | $insert_to->add_element($tokens_to_insert); | 
| 58 | 17 |  |  |  |  | 421 | $document->save($file); | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | 1; |