PRT/Command/AddMethod.pm

filter: Statementstmt 100% 36 / 36 Branchbran 83.3% 5 / 6 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 4 totaltotal 88.7% 55 / 62 CC 8 SCAR 16.1
CC
8
Cov
96%
execute
8.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.0
« PRT/Command/ReplaceToken.pm ↑ Summary PRT/Command/RenameNameSpace.pm »
1 package App::PRT::Command::AddMethod;
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 method to file
7  
8 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 8 my ($class) = @_;
10 8 bless {
11 code => undef,
12 }, $class;
13 }
14  
15 # register a method
16 # arguments:
17 # $code: Source code string of method to add
18 7 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod undocumented
19 7 my ($self, $code) = @_;
20  
21 7 $self->{code} = $code;
22 }
23  
24 7 ▶sub code {
Subroutine code: called CC=1 SCAR=0.0
Pod undocumented
25 7 my ($self) = @_;
26  
27 7 $self->{code};
28 }
29  
30 # refactor a file
31 # argumensts:
32 # $file: filename for refactoring
33 6 ▶sub execute {
Subroutine execute: called CC=8 SCAR=20.8
Pod undocumented
34 6 my ($self, $file) = @_;
35  
36 6 my $document = PPI::Document->new($file);
37  
38 6 my $statements = $document->find('PPI::Statement');
39 6 ▶ die 'statement not found' unless $statements;
Branch: 1 branch
BranchTrueFalseTotal
unless $statements 0 6 6
40 6 my $after = $statements->[-1];
41  
42 6 my $code_document = PPI::Document->new(\$self->code);
43 6 my $code_statement = $code_document->find_first('PPI::Statement::Sub');
44  
45 6 my @comments;
46 6 my $cursor = $code_statement->first_token->previous_token;
47 6 ▶ while (defined $cursor && (ref $cursor eq 'PPI::Token::Comment' || ref $cursor eq 'PPI::Token::Whitespace')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cursor and ref $cursor eq "PPI::Token::Comment" || ref $cursor eq "PPI::Token::Whitespace") 5 6 11
Condition83%5/6
defined $cursor and ref $cursor eq "PPI::Token::Comment" || ref $cursor eq "PPI::Token::Whitespace"
!ll&&!rl&&r
065
ref $cursor eq "PPI::Token::Comment" or ref $cursor eq "PPI::Token::Whitespace"
l!l&&r!l&&!r
416
MC/DC66%2/3
defined $cursor and ref $cursor eq "PPI::Token::Comment" || ref $cursor eq "PPI::Token::Whitespace"
defined $cursor ref $cursor eq "PPI::Token::Comment" ref $cursor eq "PPI::Token::Whitespace"
Truth table75%3/4
A and B or C
A= defined $cursorB= ref $cursor eq "PPI::Token::Comment"C= ref $cursor eq "PPI::Token::Whitespace"
ABCresult
0XX0
1000
11X1
1011
48 5 unshift @comments, $cursor;
49 5 $cursor = $cursor->previous_token;
50 }
51  
52 6 ▶ while (ref $comments[0] eq 'PPI::Token::Whitespace') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $comments[0] eq "PPI::Token::Whitespace") 1 6 7
53 1 shift @comments;
54 }
55  
56 6 $after->insert_before($_) for @comments;
57  
58 6 $after->insert_before($code_statement);
59  
60 6 my $whitespaces_document = PPI::Document->new(\"\n\n");
61 6 $after->insert_before($_) for @{ $whitespaces_document->find('PPI::Token') };
62  
63 6 $document->save($file);
64 }
65  
66 2 1;
« PRT/Command/ReplaceToken.pm ↑ Summary PRT/Command/RenameNameSpace.pm »