PRT/Command/MoveClassMethod.pm

filter: Statementstmt 100% 109 / 109 Branchbran 94.4% 17 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 10 totaltotal 91.5% 151 / 165 CC 12 SCAR 7.8
CC
12
Cov
98%
execute
5.0
register
4.1
parse_arguments
2.0
CRAP 2.2
« PRT/CLI.pm ↑ Summary PRT/Command/IntroduceVariables.pm »
1 package App::PRT::Command::MoveClassMethod;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use App::PRT::Command::ReplaceToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use App::PRT::Command::AddUse;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use App::PRT::Command::AddMethod;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use App::PRT::Command::DeleteMethod;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use App::PRT::Util::DestinationFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
13 12 my ($class) = @_;
14 12 bless {
15 source_class_name => undef,
16 source_method_name => undef,
17 destination_class_name => undef,
18 destination_method_name => undef,
19 }, $class;
20 }
21  
22 # parse arguments from CLI
23 # arguments:
24 # @arguments
25 # returns:
26 # @rest_arguments
27 2 ▶sub parse_arguments {
Subroutine parse_arguments: called CC=2 SCAR=6.9
Pod undocumented
28 2 my ($self, @arguments) = @_;
29  
30 2 ▶ die "source and destination are required" unless @arguments >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @arguments >= 2 1 1 2
31  
32 1 $self->register(shift @arguments => shift @arguments);
33  
34 1 @arguments;
35 }
36  
37  
38 # register a replacing rule
39 # arguments:
40 # $source: source class name and method name, joined by '#'
41 # $dest: destination class name and method name, joined by '#'
42 11 ▶sub register {
Subroutine register: called CC=4 SCAR=14.0
Pod undocumented
43 11 my ($self, $source, $destination) = @_;
44  
45 11 ▶ die 'invalid format' unless $source =~ qr/\A[^#]+#[^#]+\Z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $source =~ qr/\A[^#]+#[^#]+\Z/ 2 9 11
46 9 ▶ die 'invalid format' unless $destination;
Branch: 1 branch
BranchTrueFalseTotal
unless $destination 0 9 9
47  
48 9 ($self->{source_class_name}, $self->{source_method_name}) = split '#', $source;
49 9 ($self->{destination_class_name}, $self->{destination_method_name}) = split '#', $destination;
50 9 ▶ $self->{destination_method_name} //= $self->{source_method_name};
Condition66%2/3
$self->{"destination_method_name"} //= $self->{"source_method_name"}
l!l&&r!l&&!r
810
MC/DC0%0/2
$self->{"destination_method_name"} //= $self->{"source_method_name"}
$self->{"destination_method_name"} $self->{"source_method_name"}
Truth table66%2/3
A //= B
A= $self->{"destination_method_name"}B= $self->{"source_method_name"}
ABresult
1X1
011
000
51 }
52  
53 24 ▶sub source_class_name {
Subroutine source_class_name: called CC=1 SCAR=0.0
Pod undocumented
54 24 my ($self) = @_;
55  
56 24 $self->{source_class_name};
57 }
58  
59 24 ▶sub source_method_name {
Subroutine source_method_name: called CC=1 SCAR=0.0
Pod undocumented
60 24 my ($self) = @_;
61  
62 24 $self->{source_method_name};
63 }
64  
65 28 ▶sub destination_class_name {
Subroutine destination_class_name: called CC=1 SCAR=0.0
Pod undocumented
66 28 my ($self) = @_;
67  
68 28 $self->{destination_class_name};
69 }
70  
71 21 ▶sub destination_method_name {
Subroutine destination_method_name: called CC=1 SCAR=0.0
Pod undocumented
72 21 my ($self) = @_;
73  
74 21 $self->{destination_method_name};
75 }
76  
77 1 ▶sub source_method_body {
Subroutine source_method_body: called CC=1 SCAR=0.0
Pod undocumented
78 1 my ($self) = @_;
79  
80 1 $self->{source_method_body};
81 }
82  
83 5 ▶sub destination_method_body {
Subroutine destination_method_body: called CC=1 SCAR=0.0
Pod undocumented
84 5 my ($self) = @_;
85  
86 5 my $document = PPI::Document->new(\$self->{source_method_body});
87 5 my $sub = $document->find_first('PPI::Statement::Sub');
88 5 $sub->schild(1)->set_content($self->destination_method_name);
89 5 $document->content;
90 }
91  
92 # refactor a file
93 # argumensts:
94 # $file: filename for refactoring
95 9 ▶sub execute {
Subroutine execute: called CC=5 SCAR=16.1
Pod undocumented
96 9 my ($self, $file) = @_;
97  
98 9 my $replaced = $self->_try_replace_tokens($file);
99  
100 # TODO:
101 # - Move comment before method?
102 # - Move pod?
103 # - Move test code?
104  
105 9 my $document = PPI::Document->new($file);
106  
107 # When parse failed
108 9 ▶ return unless $document;
Branch: 1 branch
BranchTrueFalseTotal
unless $document 1 8 9
109  
110 8 my $method_body = $self->_try_delete_method_and_get_deleted_code($file);
111 8 ▶ if ($method_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body) 4 4 8
112 4 $self->{source_method_body} = $method_body;
113  
114 # replace $class->$method
115 4 $self->_try_replace_tokens_in_target_class($file);
116  
117 4 $document = PPI::Document->new($file);
118 4 my $destination_file = App::PRT::Util::DestinationFile::destination_file(
119 $self->source_class_name,
120 $self->destination_class_name,
121 $file
122 );
123  
124 4 ▶ unless (-e $destination_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $destination_file) 3 1 4
125 # prepare destination file
126 3 my $fh = file($destination_file)->open('w');
127 3 print $fh <<CODE;
128 3 package @{[ $self->destination_class_name ]};
129  
130 1;
131 CODE
132 3 $fh->close;
133 }
134  
135 {
136 # move method
137 4 my $command = App::PRT::Command::AddMethod->new;
138 4 $command->register($self->destination_method_body);
139 4 $command->execute($destination_file);
140 }
141  
142 # copy including modules
143 4 my $packages_in_source = [ map { $_->module } @{ $document->find('PPI::Statement::Include') } ];
144 4 for my $package (@$packages_in_source) {
145 14 my $command = App::PRT::Command::AddUse->new;
146 14 $command->register($package);
147 14 $command->execute($destination_file);
148 }
149 4 1;
150 }
151  
152 8 $replaced;
153 }
154  
155 9 ▶sub _try_replace_tokens {
Subroutine _try_replace_tokens: called CC=2 SCAR=6.9
156 9 my ($self, $file) = @_;
157  
158 9 my $command = App::PRT::Command::ReplaceToken->new;
159 9 $command->register(
160 9 "@{[ $self->source_class_name ]}->@{[ $self->source_method_name ]}",
161 9 "@{[ $self->destination_class_name ]}->@{[ $self->destination_method_name ]}"
162 );
163 9 ▶ if ($command->execute($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command->execute($file)) 4 5 9
164 4 $self->_try_add_use($file);
165 }
166 }
167  
168 4 ▶sub _try_replace_tokens_in_target_class {
Subroutine _try_replace_tokens_in_target_class: called CC=2 SCAR=6.9
169 4 my ($self, $file) = @_;
170  
171 4 my $command = App::PRT::Command::ReplaceToken->new;
172 4 $command->register(
173 4 "\$class->@{[ $self->source_method_name ]}",
174 4 "@{[ $self->destination_class_name ]}->@{[ $self->destination_method_name ]}"
175 );
176 4 ▶ if ($command->execute($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command->execute($file)) 1 3 4
177 1 $self->_try_add_use($file);
178 }
179 }
180  
181 5 ▶sub _try_add_use {
Subroutine _try_add_use: called CC=1 SCAR=0.0
182 5 my ($self, $file) = @_;
183  
184 5 my $command = App::PRT::Command::AddUse->new;
185 5 $command->register($self->destination_class_name);
186 5 $command->execute($file);
187 }
188  
189 8 ▶sub _try_delete_method_and_get_deleted_code {
Subroutine _try_delete_method_and_get_deleted_code: called CC=2 SCAR=6.9
190 8 my ($self, $file) = @_;
191  
192 8 my $command = App::PRT::Command::DeleteMethod->new;
193 8 $command->register($self->source_class_name, $self->source_method_name);
194 8 ▶ if ($command->execute($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command->execute($file)) 4 4 8
195 4 return $command->deleted_code;
196 }
197 }
198  
199 1 1;
« PRT/CLI.pm ↑ Summary PRT/Command/IntroduceVariables.pm »