PRT/Command/DeleteMethod.pm

filter: Statementstmt 100% 49 / 49 Branchbran 87.5% 14 / 16 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 7 totaltotal 85.8% 79 / 92 CC 13 SCAR 19.7
CC
13
Cov
94%
execute
12.1
parse_arguments
2.0
BEGIN
1.0
CRAP 7.1
↑ Summary PRT/Command/AddUse.pm »
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
BranchTrueFalseTotal
unless @arguments >= 2 1 1 2
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
BranchTrueFalseTotal
unless $document 0 12 12
64  
65 12 my $package = $document->find_first('PPI::Statement::Package');
66  
67 12 ▶ return unless $package;
Branch: 1 branch
BranchTrueFalseTotal
unless $package 3 9 12
68 9 ▶ return unless $package->namespace eq $self->target_class_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $package->namespace eq $self->target_class_name 2 7 9
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
BranchTrueFalseTotal
unless $sub->name eq $self->target_method_name 7 7 14
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
BranchTrueFalseTotal
if (defined $cursor and ref $cursor eq "PPI::Token::Comment") 4 7 11
Condition66%2/3
defined $cursor and ref $cursor eq "PPI::Token::Comment"
!ll&&!rl&&r
074
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (defined $cursor and ref $cursor eq "PPI::Token::Whitespace") 14 7 21
Condition66%2/3
defined $cursor and ref $cursor eq "PPI::Token::Whitespace"
!ll&&!rl&&r
0714
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $replaced 7 0 7
101 }
102  
103 2 1;
↑ Summary PRT/Command/AddUse.pm »