PRT/Command/RenameNameSpace.pm

filter: Statementstmt 100% 54 / 54 Branchbran 85.7% 12 / 14 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 5 totaltotal 90.5% 86 / 95 CC 12 SCAR 12.3
CC
12
Cov
96%
execute_files
7.0
_is_target_class
4.0
parse_arguments
2.0
CRAP 3.4
« PRT/Command/AddMethod.pm ↑ Summary PRT/Collector/GitDirectory.pm »
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
BranchTrueFalseTotal
unless @arguments >= 2 0 2 2
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
BranchTrueFalseTotal
unless -f $file 1 14 15
69 14 my $file_after = $rename_command->execute($file);
70 14 ▶ if ($file_after && $file_after ne $file && !$knowns->{$file_after}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_after and $file_after ne $file and not $knowns->{$file_after}) 2 12 14
Condition83%5/6
$file_after and $file_after ne $file and not $knowns->{$file_after}
!ll&&!rl&&r
1202
$file_after and $file_after ne $file
!ll&&!rl&&r
842
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}
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
unless $document 0 10 10
95  
96 10 my $package = $document->find_first('PPI::Statement::Package');
97  
98 10 ▶ return unless $package;
Branch: 1 branch
BranchTrueFalseTotal
unless $package 3 7 10
99  
100 7 ▶ if (index($package->namespace, $self->source_name_space) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($package->namespace, $self->source_name_space) == 0) 4 3 7
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
BranchTrueFalseTotal
unless index($class_name, $self->source_name_space) == 0 2 3 5
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;
« PRT/Command/AddMethod.pm ↑ Summary PRT/Collector/GitDirectory.pm »