PRT/Command/AddUse.pm

filter: Statementstmt 100% 34 / 34 Branchbran 80.0% 8 / 10 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 4 totaltotal 81.2% 52 / 64 CC 9 SCAR 17.8
CC
9
Cov
90%
execute
9.3
BEGIN
1.0
BEGIN
1.0
CRAP 6.0
« PRT/Command/DeleteMethod.pm ↑ Summary PRT/Command/RenameClass.pm »
1 package App::PRT::Command::AddUse;
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  
6 # Internal command to add use statement to file
7  
8 23 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 23 my ($class) = @_;
10 23 bless {
11 namespace => undef,
12 }, $class;
13 }
14  
15 # register a method
16 # arguments:
17 # $namespace: package name to use
18 22 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod undocumented
19 22 my ($self, $namespace) = @_;
20  
21 22 $self->{namespace} = $namespace;
22 }
23  
24 68 ▶sub namespace {
Subroutine namespace: called CC=1 SCAR=0.0
Pod undocumented
25 68 my ($self) = @_;
26  
27 68 $self->{namespace};
28 }
29  
30 # refactor a file
31 # argumensts:
32 # $file: filename for refactoring
33 22 ▶sub execute {
Subroutine execute: called CC=9 SCAR=22.3
Pod undocumented
34 22 my ($self, $file) = @_;
35  
36 22 my $document = PPI::Document->new($file);
37 22 ▶ return unless $document;
Branch: 1 branch
BranchTrueFalseTotal
unless $document 0 22 22
38  
39 22 my $used = 0;
40 22 my $last_use;
41 22 my $include_statements = $document->find('PPI::Statement::Include');
42  
43 22 ▶ if ($include_statements) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include_statements) 17 5 22
44 17 for my $statement (@$include_statements) {
45 49 ▶ next unless defined $statement->module;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $statement->module 0 49 49
46 49 ▶ if ($statement->module eq $self->namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement->module eq $self->namespace) 5 44 49
47 5 $used++;
48 }
49 49 $last_use = $statement;
50 }
51 }
52 22 ▶ return if $used;
Branch: 1 branch
BranchTrueFalseTotal
if $used 5 17 22
53  
54 17 ▶ my $insert_to = $last_use || $document->find_first('PPI::Statement::Package') || $document->find_first('PPI::Statement');
Condition50%3/6
$last_use || $document->find_first('PPI::Statement::Package') || $document->find_first('PPI::Statement')
l!l&&r!l&&!r
1700
$last_use or $document->find_first("PPI::Statement::Package")
l!l&&r!l&&!r
1250
MC/DC0%0/3
$last_use || $document->find_first('PPI::Statement::Package') || $document->find_first('PPI::Statement')
$last_use $document->find_first("PPI::Statement::Package") $document->find_first('PPI::Statement')
Truth table50%2/4
A or B || C
A= $last_useB= $document->find_first("PPI::Statement::Package")C= $document->find_first('PPI::Statement')
ABCresult
1XX1
01X1
0011
0000
55  
56 17 my $tokens_to_insert = PPI::Document->new(\"\nuse @{[ $self->namespace ]};");
57 17 $insert_to->add_element($tokens_to_insert);
58 17 $document->save($file);
59 }
60  
61 2 1;
« PRT/Command/DeleteMethod.pm ↑ Summary PRT/Command/RenameClass.pm »