| 1 | package App::PRT::Command::RenameNameSpace; | ||||||||||||||||||||||||||||||||||||||||||
| 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 | 2 | ▶ | use App::PRT::Command::RenameClass; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | my ($class) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | bless { | |||||||||||||||||||||||||||||||||||||||||
| 10 | source_name_space => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 11 | target_hname_space => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 12 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # parse arguments from CLI | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # @arguments | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # returns: | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # @rest_arguments | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | sub parse_arguments { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_arguments: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | my ($self, @arguments) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | die "source and destination class are required" unless @arguments >= 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | $self->register(shift @arguments => shift @arguments); | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | @arguments; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # register a replacing rule | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # $source: source name space | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # $dest: destination name space | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 6 | ▶ | sub register { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 6 | my ($self, $source_name_space, $destination_name_space) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 6 | $self->{source_name_space} = $source_name_space; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 6 | $self->{destination_name_space} = $destination_name_space; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 17 | ▶ | sub source_name_space { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
source_name_space: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 17 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 17 | $self->{source_name_space}; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | ▶ | sub destination_name_space { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
destination_name_space: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | $self->{destination_name_space}; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # refactor files | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # argumensts: | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # $files: [ filenames for refactoring ] | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub execute_files { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute_files: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | my ($self, $files) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | my $target_classes = $self->_collect_target_classes($files); | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | my $knowns = { map { $_ => 1 } @$target_classes }; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | for my $target_class (@$target_classes) { | |||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | my $rename_command = App::PRT::Command::RenameClass->new; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | $rename_command->register($target_class => $self->_destination_class_name($target_class)); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | for my $file (@$files) { | |||||||||||||||||||||||||||||||||||||||||
| 68 | 15 | ▶ | next unless -f $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 14 | my $file_after = $rename_command->execute($file); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 14 | ▶ | if ($file_after && $file_after ne $file && !$knowns->{$file_after}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$file_after and $file_after ne $file and not $knowns->{$file_after}
$file_after and $file_after ne $file
MC/DC66%2/3
$file_after and $file_after ne $file and not $knowns->{$file_after}
$file_after
$file_after ne $file
not $knowns->{$file_after}
Truth table75%3/4
A and B and not(C)
A= $file_afterB= $file_after ne $fileC= not $knowns->{$file_after}
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | $knowns->{$file_after}++; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | push @$files, $file_after; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | sub _collect_target_classes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_collect_target_classes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | my ($self, $files) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | [ grep { | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 7 | $_ | |||||||||||||||||||||||||||||||||||||||||
| 83 | } map { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | $self->_is_target_class($_); | |||||||||||||||||||||||||||||||||||||||||
| 85 | } @$files ]; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 10 | ▶ | sub _is_target_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_target_class: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 10 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 10 | my $document = PPI::Document->new($file); | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | # When parse failed | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 10 | ▶ | return unless $document; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 10 | my $package = $document->find_first('PPI::Statement::Package'); | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 10 | ▶ | return unless $package; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 7 | ▶ | if (index($package->namespace, $self->source_name_space) == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 4 | $package->namespace; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 5 | ▶ | sub _destination_class_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_destination_class_name: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | my ($self, $class_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 5 | ▶ | return unless index($class_name, $self->source_name_space) == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | my $dest = $class_name; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | $self->destination_name_space . substr($class_name, length($self->source_name_space)); | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||