| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::PRT::Command::RenameNameSpace; | 
| 2 | 2 |  |  | 2 |  | 5746 | use strict; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 360 |  | 
| 3 | 2 |  |  | 2 |  | 14 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 109 |  | 
| 4 | 2 |  |  | 2 |  | 959 | use PPI; | 
|  | 2 |  |  |  |  | 150044 |  | 
|  | 2 |  |  |  |  | 79 |  | 
| 5 | 2 |  |  | 2 |  | 10725 | use App::PRT::Command::RenameClass; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 1243 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | sub new { | 
| 8 | 7 |  |  | 7 | 0 | 25478 | my ($class) = @_; | 
| 9 | 7 |  |  |  |  | 53 | 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 |  |  |  |  |  |  | sub parse_arguments { | 
| 21 | 2 |  |  | 2 | 0 | 14 | my ($self, @arguments) = @_; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 2 | 50 |  |  |  | 11 | die "source and destination class are required" unless @arguments >= 2; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 2 |  |  |  |  | 10 | $self->register(shift @arguments => shift @arguments); | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 2 |  |  |  |  | 7 | @arguments; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | # register a replacing rule | 
| 32 |  |  |  |  |  |  | # arguments: | 
| 33 |  |  |  |  |  |  | #   $source: source name space | 
| 34 |  |  |  |  |  |  | #   $dest:   destination name space | 
| 35 |  |  |  |  |  |  | sub register { | 
| 36 | 6 |  |  | 6 | 0 | 26 | my ($self, $source_name_space, $destination_name_space) = @_; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 6 |  |  |  |  | 23 | $self->{source_name_space} = $source_name_space; | 
| 39 | 6 |  |  |  |  | 19 | $self->{destination_name_space} = $destination_name_space; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub source_name_space { | 
| 43 | 17 |  |  | 17 | 0 | 226 | my ($self) = @_; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 17 |  |  |  |  | 161 | $self->{source_name_space}; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub destination_name_space { | 
| 49 | 5 |  |  | 5 | 0 | 12 | my ($self) = @_; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 5 |  |  |  |  | 21 | $self->{destination_name_space}; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | # refactor files | 
| 55 |  |  |  |  |  |  | # argumensts: | 
| 56 |  |  |  |  |  |  | #   $files: [ filenames for refactoring ] | 
| 57 |  |  |  |  |  |  | sub execute_files { | 
| 58 | 1 |  |  | 1 |  | 24 | my ($self, $files) = @_; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 1 |  |  |  |  | 6 | my $target_classes = $self->_collect_target_classes($files); | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 1 |  |  |  |  | 4 | my $knowns = { map { $_ => 1 } @$target_classes }; | 
|  | 2 |  |  |  |  | 9 |  | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 1 |  |  |  |  | 4 | for my $target_class (@$target_classes) { | 
| 65 | 2 |  |  |  |  | 26 | my $rename_command = App::PRT::Command::RenameClass->new; | 
| 66 | 2 |  |  |  |  | 11 | $rename_command->register($target_class => $self->_destination_class_name($target_class)); | 
| 67 | 2 |  |  |  |  | 6 | for my $file (@$files) { | 
| 68 | 15 | 100 |  |  |  | 719 | next unless -f $file; | 
| 69 | 14 |  |  |  |  | 66 | my $file_after = $rename_command->execute($file); | 
| 70 | 14 | 100 | 100 |  |  | 9777 | if ($file_after && $file_after ne $file && !$knowns->{$file_after}) { | 
|  |  |  | 66 |  |  |  |  | 
| 71 | 2 |  |  |  |  | 9 | $knowns->{$file_after}++; | 
| 72 | 2 |  |  |  |  | 8 | push @$files, $file_after; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub _collect_target_classes { | 
| 79 | 2 |  |  | 2 |  | 6016 | my ($self, $files) = @_; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 7 |  |  |  |  | 1599 | [ grep { | 
| 82 | 10 |  |  |  |  | 5161 | $_ | 
| 83 |  |  |  |  |  |  | } map { | 
| 84 | 2 |  |  |  |  | 5 | $self->_is_target_class($_); | 
| 85 |  |  |  |  |  |  | } @$files ]; | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub _is_target_class { | 
| 89 | 10 |  |  | 10 |  | 24 | my ($self, $file) = @_; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 10 |  |  |  |  | 66 | my $document = PPI::Document->new($file); | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | # When parse failed | 
| 94 | 10 | 50 |  |  |  | 105927 | return unless $document; | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 10 |  |  |  |  | 56 | my $package = $document->find_first('PPI::Statement::Package'); | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 10 | 100 |  |  |  | 11165 | return unless $package; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 7 | 100 |  |  |  | 41 | if (index($package->namespace, $self->source_name_space) == 0) { | 
| 101 | 4 |  |  |  |  | 13 | $package->namespace; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub _destination_class_name { | 
| 106 | 5 |  |  | 5 |  | 15 | my ($self, $class_name) = @_; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 5 | 100 |  |  |  | 42 | return unless index($class_name, $self->source_name_space) == 0; | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 3 |  |  |  |  | 7 | my $dest = $class_name; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 3 |  |  |  |  | 10 | $self->destination_name_space . substr($class_name, length($self->source_name_space)); | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | 1; |