| 1 | package App::PRT::Command::RenameClass; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use PPI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use App::PRT::Util::DestinationFile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Path::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 16 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 9 | 16 | my ($class) = @_; | |||||||||||||||||||||||||||
| 10 | 16 | bless { | |||||||||||||||||||||||||||
| 11 | rule => undef, | ||||||||||||||||||||||||||||
| 12 | }, $class; | ||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # parse arguments from CLI | ||||||||||||||||||||||||||||
| 16 | # arguments: | ||||||||||||||||||||||||||||
| 17 | # @arguments | ||||||||||||||||||||||||||||
| 18 | # returns: | ||||||||||||||||||||||||||||
| 19 | # @rest_arguments | ||||||||||||||||||||||||||||
| 20 | 3 | ▶ | sub parse_arguments { | ||||||||||||||||||||||||||
|
Subroutine
parse_arguments: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 3 | my ($self, @arguments) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | die "source and destination class are required" unless @arguments >= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | $self->register(shift @arguments => shift @arguments); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | @arguments; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # register a replacing rule | ||||||||||||||||||||||||||||
| 32 | # arguments: | ||||||||||||||||||||||||||||
| 33 | # $source: source class name | ||||||||||||||||||||||||||||
| 34 | # $dest: destination class name | ||||||||||||||||||||||||||||
| 35 | 13 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 13 | my ($self, $source_class_name, $destination_class_name) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 13 | $self->{source_class_name} = $source_class_name; | |||||||||||||||||||||||||||
| 39 | 13 | $self->{destination_class_name} = $destination_class_name; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2241 | ▶ | sub source_class_name { | ||||||||||||||||||||||||||
|
Subroutine
source_class_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 2241 | my ($self) = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2241 | $self->{source_class_name}; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 67 | ▶ | sub destination_class_name { | ||||||||||||||||||||||||||
|
Subroutine
destination_class_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 67 | my ($self) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 67 | $self->{destination_class_name}; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # refactor a file | ||||||||||||||||||||||||||||
| 55 | # argumensts: | ||||||||||||||||||||||||||||
| 56 | # $file: filename for refactoring | ||||||||||||||||||||||||||||
| 57 | 26 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 26 | my ($self, $file) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 26 | my $replaced = 0; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 26 | my $document = PPI::Document->new($file); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # When parse failed | ||||||||||||||||||||||||||||
| 65 | 26 | ▶ | return unless $document; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 25 | my $should_rename = $self->_try_rename_package_statement($document); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 25 | $replaced += $self->_try_rename_includes($document); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 25 | $replaced += $self->_try_rename_parent_class($document); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 25 | $replaced += $self->_try_rename_quotes($document); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 25 | $replaced += $self->_try_rename_tokens($document); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 25 | $replaced += $self->_try_rename_symbols($document); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 25 | ▶ | if ($should_rename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 7 | my $dest_file = App::PRT::Util::DestinationFile::destination_file($self->source_class_name, $self->destination_class_name, $file); | |||||||||||||||||||||||||||
| 81 | 7 | my $dest_dir = file($dest_file)->dir; | |||||||||||||||||||||||||||
| 82 | 7 | $dest_dir->mkpath; | |||||||||||||||||||||||||||
| 83 | 7 | $document->save($dest_file); | |||||||||||||||||||||||||||
| 84 | 7 | unlink($file); | |||||||||||||||||||||||||||
| 85 | 7 | $dest_file; | |||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||
| 87 | 18 | ▶ | return unless $replaced; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 10 | $document->save($file); | |||||||||||||||||||||||||||
| 89 | 10 | $file; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # returns: should rename this document? | ||||||||||||||||||||||||||||
| 94 | 25 | ▶ | sub _try_rename_package_statement { | ||||||||||||||||||||||||||
|
Subroutine
_try_rename_package_statement: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 95 | 25 | my ($self, $document) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 25 | my $package = $document->find_first('PPI::Statement::Package'); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 25 | ▶ | return unless $package; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 19 | ▶ | return unless $package->namespace eq $self->source_class_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 9 | my $namespace = $package->schild(1); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 9 | ▶ | return unless $namespace->isa('PPI::Token::Word'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 9 | $namespace->set_content($self->destination_class_name); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Rename this file when the first significant token is package (heuristic). | ||||||||||||||||||||||||||||
| 109 | # This skips comments and POD before the package statement. | ||||||||||||||||||||||||||||
| 110 | 9 | return $document->schild(0)->DOES('PPI::Statement::Package'); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 25 | ▶ | sub _try_rename_includes { | ||||||||||||||||||||||||||
|
Subroutine
_try_rename_includes: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 114 | 25 | my ($self, $document) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 25 | my $replaced = 0; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 25 | my $statements = $document->find('PPI::Statement::Include'); | |||||||||||||||||||||||||||
| 119 | 25 | ▶ | return 0 unless $statements; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 23 | for my $statement (@$statements) { | |||||||||||||||||||||||||||
| 122 | 59 | ▶ | next unless defined $statement->module; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 59 | ▶ | next unless $statement->module eq $self->source_class_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3 | my $module = $statement->schild(1); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 3 | ▶ | return unless $module->isa('PPI::Token::Word'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 3 | $module->set_content($self->destination_class_name); | |||||||||||||||||||||||||||
| 130 | 3 | $replaced++; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 23 | $replaced; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 25 | ▶ | sub _try_rename_quotes { | ||||||||||||||||||||||||||
|
Subroutine
_try_rename_quotes: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 137 | 25 | my ($self, $document) = @_; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 25 | my $replaced = 0; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 25 | my $quotes = $document->find('PPI::Token::Quote'); | |||||||||||||||||||||||||||
| 142 | 25 | ▶ | return 0 unless $quotes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 21 | for my $quote (@$quotes) { | |||||||||||||||||||||||||||
| 145 | 68 | ▶ | next unless $quote->string eq $self->source_class_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 10 | $quote->set_content("'@{[ $self->destination_class_name ]}'"); | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 10 | $replaced++; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 21 | $replaced; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # TODO: too complicated | ||||||||||||||||||||||||||||
| 155 | 25 | ▶ | sub _try_rename_parent_class { | ||||||||||||||||||||||||||
|
Subroutine
_try_rename_parent_class: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 156 | 25 | my ($self, $document) = @_; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 25 | my $replaced = 0; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 25 | my $includes = $document->find('PPI::Statement::Include'); | |||||||||||||||||||||||||||
| 161 | 25 | ▶ | return 0 unless $includes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 23 | for my $statement (@$includes) { | |||||||||||||||||||||||||||
| 164 | 59 | ▶ | next unless defined $statement->pragma; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 59 | ▶ | next unless $statement->pragma =~ /^parent|base$/; # only 'use parent' and 'use base' are supported | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # schild(2) is 'Foo' of use parent Foo | ||||||||||||||||||||||||||||
| 168 | 10 | my $parent = $statement->schild(2); | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 10 | ▶ | if ($parent->isa('PPI::Token::Quote')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 171 | # The 'literal' method is not implemented by ::Quote::Double or ::Quote::Interpolate. | ||||||||||||||||||||||||||||
| 172 | 6 | ▶ | my $string = $parent->can('literal') ? $parent->literal : $parent->string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 6 | ▶ | if ($string eq $self->source_class_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 5 | $parent->set_content("'@{[ $self->destination_class_name ]}'"); | |||||||||||||||||||||||||||
| 176 | 5 | $replaced++; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } elsif ($parent->isa('PPI::Token::QuoteLike::Words')) { | ||||||||||||||||||||||||||||
| 179 | # use parent qw(A B C) pattern | ||||||||||||||||||||||||||||
| 180 | # literal is array when QuoteLike::Words | ||||||||||||||||||||||||||||
| 181 | 4 | my $_replaced = 0; | |||||||||||||||||||||||||||
| 182 | my @new_literal = map { | ||||||||||||||||||||||||||||
| 183 | 4 | ▶ | if ($_ eq $self->source_class_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 1 | $_replaced++; | |||||||||||||||||||||||||||
| 185 | 1 | $self->destination_class_name; | |||||||||||||||||||||||||||
| 186 | } else { | ||||||||||||||||||||||||||||
| 187 | 5 | $_; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | } $parent->literal; | ||||||||||||||||||||||||||||
| 190 | 4 | ▶ | if ($_replaced) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 1 | $parent->set_content('qw(' . join(' ', @new_literal) . ')'); | |||||||||||||||||||||||||||
| 192 | 1 | $replaced++; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 23 | $replaced; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # discussions: | ||||||||||||||||||||||||||||
| 201 | # seems too wild | ||||||||||||||||||||||||||||
| 202 | 25 | ▶ | sub _try_rename_tokens { | ||||||||||||||||||||||||||
|
Subroutine
_try_rename_tokens: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 203 | 25 | my ($self, $document) = @_; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 25 | my $replaced = 0; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 25 | my $tokens = $document->find('PPI::Token'); | |||||||||||||||||||||||||||
| 208 | 25 | ▶ | return 0 unless $tokens; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 25 | for my $token (@$tokens) { | |||||||||||||||||||||||||||
| 211 | 2057 | ▶ | next unless $token->content eq $self->source_class_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 13 | $token->set_content($self->destination_class_name); | |||||||||||||||||||||||||||
| 213 | 13 | $replaced++; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 25 | $replaced; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | # e.g. | ||||||||||||||||||||||||||||
| 220 | # $Foo::Bar::GLOBAL_VAR | ||||||||||||||||||||||||||||
| 221 | # ~~~~~~~~ Rename here | ||||||||||||||||||||||||||||
| 222 | # | ||||||||||||||||||||||||||||
| 223 | # $Foo::Bar::Buz::GLOBAL_VAR <= Don't rename because it's not the same class | ||||||||||||||||||||||||||||
| 224 | 25 | ▶ | sub _try_rename_symbols { | ||||||||||||||||||||||||||
|
Subroutine
_try_rename_symbols: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 225 | 25 | my ($self, $document) = @_; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 25 | my $replaced = 0; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 25 | my $symbols = $document->find('PPI::Token::Symbol'); | |||||||||||||||||||||||||||
| 230 | 25 | ▶ | return 0 unless $symbols; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 17 | my $source_class_name = $self->source_class_name; | |||||||||||||||||||||||||||
| 233 | 17 | my $destination_class_name = $self->destination_class_name; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 17 | for my $symbol (@$symbols) { | |||||||||||||||||||||||||||
| 236 | 113 | my $content = $symbol->content; | |||||||||||||||||||||||||||
| 237 | 113 | my $sigil = substr $content, 0, 1, ''; | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 113 | ▶ | if ($content =~ s/\A${source_class_name}::// && scalar(split /::/, $content) == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$content =~ s/\A$source_class_name::// and scalar split(/::/, $content, 0) == 1
MC/DC100%2/2
$content =~ s/\A$source_class_name::// and scalar split(/::/, $content, 0) == 1
$content =~ s/\A$source_class_name:://
scalar split(/::/, $content, 0) == 1
Truth table100%3/3
A and B
A= $content =~ s/\A$source_class_name:://B= scalar split(/::/, $content, 0) == 1
| |||||||||||||||||||||||||||||
| 240 | 5 | $symbol->set_content($sigil . $destination_class_name . '::' . $content); | |||||||||||||||||||||||||||
| 241 | 5 | $replaced++; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 17 | $replaced; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 3 | 1; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||