| 1 | package App::PRT::Command::DeleteMethod; | ||||||||||||||||||||||||||||
| 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 | 14 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 7 | 14 | my ($class) = @_; | |||||||||||||||||||||||||||
| 8 | 14 | bless {}, $class; | |||||||||||||||||||||||||||
| 9 | } | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # parse arguments from CLI | ||||||||||||||||||||||||||||
| 12 | # arguments: | ||||||||||||||||||||||||||||
| 13 | # @arguments | ||||||||||||||||||||||||||||
| 14 | # returns: | ||||||||||||||||||||||||||||
| 15 | # @rest_arguments | ||||||||||||||||||||||||||||
| 16 | 2 | ▶ | sub parse_arguments { | ||||||||||||||||||||||||||
|
Subroutine
parse_arguments: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 2 | my ($self, @arguments) = @_; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | die "class and method required" unless @arguments >= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | $self->register(shift @arguments => shift @arguments); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | @arguments; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # register a replacing rule | ||||||||||||||||||||||||||||
| 27 | # arguments: | ||||||||||||||||||||||||||||
| 28 | # $target_class_name: Target Class Name | ||||||||||||||||||||||||||||
| 29 | # $target_method_name: Target Method Name | ||||||||||||||||||||||||||||
| 30 | 13 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 13 | my ($self, $target_class_name, $target_method_name) = @_; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 13 | $self->{target_class_name} = $target_class_name; | |||||||||||||||||||||||||||
| 34 | 13 | $self->{target_method_name} = $target_method_name; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 11 | ▶ | sub target_class_name { | ||||||||||||||||||||||||||
|
Subroutine
target_class_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 11 | $self->{target_class_name}; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 16 | ▶ | sub target_method_name { | ||||||||||||||||||||||||||
|
Subroutine
target_method_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 16 | my ($self) = @_; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 16 | $self->{target_method_name}; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 7 | ▶ | sub deleted_code { | ||||||||||||||||||||||||||
|
Subroutine
deleted_code: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 7 | $self->{deleted_code}; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # refactor a file | ||||||||||||||||||||||||||||
| 56 | # argumensts: | ||||||||||||||||||||||||||||
| 57 | # $file: filename for refactoring | ||||||||||||||||||||||||||||
| 58 | 12 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 12 | my ($self, $file) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 12 | my $document = PPI::Document->new($file); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 12 | ▶ | return unless $document; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 12 | my $package = $document->find_first('PPI::Statement::Package'); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 12 | ▶ | return unless $package; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 9 | ▶ | return unless $package->namespace eq $self->target_class_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 7 | my $subs = $document->find('PPI::Statement::Sub'); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 7 | my $replaced = 0; | |||||||||||||||||||||||||||
| 73 | 7 | for my $sub (@$subs) { | |||||||||||||||||||||||||||
| 74 | 14 | ▶ | next unless $sub->name eq $self->target_method_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 7 | my @garbages; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # comment before method | ||||||||||||||||||||||||||||
| 78 | 7 | my $cursor = $sub->first_token->previous_token; | |||||||||||||||||||||||||||
| 79 | 7 | ▶ | while (defined $cursor && ref $cursor eq 'PPI::Token::Comment') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cursor and ref $cursor eq "PPI::Token::Comment"
MC/DC50%1/2
defined $cursor and ref $cursor eq "PPI::Token::Comment"
defined $cursor
ref $cursor eq "PPI::Token::Comment"
Truth table66%2/3
A and B
A= defined $cursorB= ref $cursor eq "PPI::Token::Comment"
| |||||||||||||||||||||||||||||
| 80 | 4 | unshift @garbages, $cursor; | |||||||||||||||||||||||||||
| 81 | 4 | $cursor = $cursor->previous_token; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # method body | ||||||||||||||||||||||||||||
| 85 | 7 | push @garbages, $sub; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # whitespace after method | ||||||||||||||||||||||||||||
| 88 | 7 | $cursor = $sub->last_token->next_token; | |||||||||||||||||||||||||||
| 89 | 7 | ▶ | while (defined $cursor && ref $cursor eq 'PPI::Token::Whitespace') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cursor and ref $cursor eq "PPI::Token::Whitespace"
MC/DC50%1/2
defined $cursor and ref $cursor eq "PPI::Token::Whitespace"
defined $cursor
ref $cursor eq "PPI::Token::Whitespace"
Truth table66%2/3
A and B
A= defined $cursorB= ref $cursor eq "PPI::Token::Whitespace"
| |||||||||||||||||||||||||||||
| 90 | 14 | push @garbages, $cursor; | |||||||||||||||||||||||||||
| 91 | 14 | $cursor = $cursor->next_token; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 7 | $self->{deleted_code} = join '', @garbages; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 7 | $_->remove for @garbages; | |||||||||||||||||||||||||||
| 97 | 7 | $replaced++; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 7 | ▶ | $document->save($file) if $replaced; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | 1; | |||||||||||||||||||||||||||