| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::PRT::Command::AddMethod; | 
| 2 | 2 |  |  | 2 |  | 3546 | use strict; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 91 |  | 
| 3 | 2 |  |  | 2 |  | 22 | use warnings; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 58 |  | 
| 4 | 2 |  |  | 2 |  | 1053 | use PPI; | 
|  | 2 |  |  |  |  | 171808 |  | 
|  | 2 |  |  |  |  | 847 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | # Internal command to add method to file | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | sub new { | 
| 9 | 8 |  |  | 8 | 0 | 29125 | my ($class) = @_; | 
| 10 | 8 |  |  |  |  | 64 | bless { | 
| 11 |  |  |  |  |  |  | code => undef, | 
| 12 |  |  |  |  |  |  | }, $class; | 
| 13 |  |  |  |  |  |  | } | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | # register a method | 
| 16 |  |  |  |  |  |  | # arguments: | 
| 17 |  |  |  |  |  |  | #   $code: Source code string of method to add | 
| 18 |  |  |  |  |  |  | sub register { | 
| 19 | 7 |  |  | 7 | 0 | 1759 | my ($self, $code) = @_; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 7 |  |  |  |  | 48 | $self->{code} = $code; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub code { | 
| 25 | 7 |  |  | 7 | 0 | 21 | my ($self) = @_; | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 7 |  |  |  |  | 48 | $self->{code}; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | # refactor a file | 
| 31 |  |  |  |  |  |  | # argumensts: | 
| 32 |  |  |  |  |  |  | #   $file: filename for refactoring | 
| 33 |  |  |  |  |  |  | sub execute { | 
| 34 | 6 |  |  | 6 | 0 | 2210 | my ($self, $file) = @_; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 6 |  |  |  |  | 52 | my $document = PPI::Document->new($file); | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 6 |  |  |  |  | 48056 | my $statements = $document->find('PPI::Statement'); | 
| 39 | 6 | 50 |  |  |  | 14973 | die 'statement not found' unless $statements; | 
| 40 | 6 |  |  |  |  | 17 | my $after = $statements->[-1]; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 6 |  |  |  |  | 34 | my $code_document = PPI::Document->new(\$self->code); | 
| 43 | 6 |  |  |  |  | 26773 | my $code_statement = $code_document->find_first('PPI::Statement::Sub'); | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 6 |  |  |  |  | 1948 | my @comments; | 
| 46 | 6 |  |  |  |  | 47 | my $cursor = $code_statement->first_token->previous_token; | 
| 47 | 6 |  | 100 |  |  | 681 | while (defined $cursor && (ref $cursor eq 'PPI::Token::Comment' || ref $cursor eq 'PPI::Token::Whitespace')) { | 
|  |  |  | 33 |  |  |  |  | 
| 48 | 5 |  |  |  |  | 97 | unshift @comments, $cursor; | 
| 49 | 5 |  |  |  |  | 31 | $cursor = $cursor->previous_token; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 6 |  |  |  |  | 129 | while (ref $comments[0] eq 'PPI::Token::Whitespace') { | 
| 53 | 1 |  |  |  |  | 4 | shift @comments; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 6 |  |  |  |  | 29 | $after->insert_before($_) for @comments; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 6 |  |  |  |  | 259 | $after->insert_before($code_statement); | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 6 |  |  |  |  | 371 | my $whitespaces_document = PPI::Document->new(\"\n\n"); | 
| 61 | 6 |  |  |  |  | 2272 | $after->insert_before($_) for @{ $whitespaces_document->find('PPI::Token') }; | 
|  | 6 |  |  |  |  | 36 |  | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 6 |  |  |  |  | 2083 | $document->save($file); | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | 1; |