| 1 | package App::PRT::Command::ReplaceToken; | ||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use PPI; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 33 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 7 | 33 | my ($class) = @_; | |||||||||||||||||||||||||||||||||
| 8 | 33 | bless { | |||||||||||||||||||||||||||||||||
| 9 | source_tokens => undef, | ||||||||||||||||||||||||||||||||||
| 10 | destination_tokens => undef, | ||||||||||||||||||||||||||||||||||
| 11 | statement_in => undef, | ||||||||||||||||||||||||||||||||||
| 12 | }, $class; | ||||||||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | # parse arguments from CLI | ||||||||||||||||||||||||||||||||||
| 16 | # arguments: | ||||||||||||||||||||||||||||||||||
| 17 | # @arguments | ||||||||||||||||||||||||||||||||||
| 18 | # returns: | ||||||||||||||||||||||||||||||||||
| 19 | # @rest_arguments | ||||||||||||||||||||||||||||||||||
| 20 | 9 | ▶ | sub parse_arguments { | ||||||||||||||||||||||||||||||||
|
Subroutine
parse_arguments: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 21 | 9 | my ($self, @arguments) = @_; | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | die "source and destination tokens required" unless @arguments >= 2; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | 8 | $self->register(shift @arguments => shift @arguments); | |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | 8 | ▶ | if (@arguments >= 2 && $arguments[0] eq '--in-statement') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@arguments >= 2 and $arguments[0] eq "--in-statement"
MC/DC100%2/2
@arguments >= 2 and $arguments[0] eq "--in-statement"
@arguments >= 2
$arguments[0] eq "--in-statement"
Truth table100%3/3
A and B
A= @arguments >= 2B= $arguments[0] eq "--in-statement"
| |||||||||||||||||||||||||||||||||||
| 28 | 1 | shift @arguments; | |||||||||||||||||||||||||||||||||
| 29 | 1 | $self->set_replace_only_statement_which_has_token(shift @arguments); | |||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | 8 | @arguments; | |||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | # register a replacing rule | ||||||||||||||||||||||||||||||||||
| 36 | # arguments: | ||||||||||||||||||||||||||||||||||
| 37 | # $source: source token | ||||||||||||||||||||||||||||||||||
| 38 | # $destination: destinationination token | ||||||||||||||||||||||||||||||||||
| 39 | # discussions: | ||||||||||||||||||||||||||||||||||
| 40 | # should consider utf-8 flag ? | ||||||||||||||||||||||||||||||||||
| 41 | 30 | ▶ | sub register { | ||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 42 | 30 | my ($self, $source, $destination) = @_; | |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | 30 | my $source_tokens = do { | |||||||||||||||||||||||||||||||||
| 45 | 30 | my $document = PPI::Document::Fragment->new(\$source); | |||||||||||||||||||||||||||||||||
| 46 | 30 | [ map { $_->content } @{ $document->find('PPI::Token') } ]; | |||||||||||||||||||||||||||||||||
| 47 | }; | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | 30 | my $destination_tokens = do { | |||||||||||||||||||||||||||||||||
| 50 | 30 | my $document = PPI::Document::Fragment->new(\$destination); | |||||||||||||||||||||||||||||||||
| 51 | 30 | [ map { $_->content } @{ $document->find('PPI::Token') } ]; | |||||||||||||||||||||||||||||||||
| 52 | }; | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 30 | $self->{source_tokens} = $source_tokens; | |||||||||||||||||||||||||||||||||
| 55 | 30 | $self->{destination_tokens} = $destination_tokens; | |||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | 5 | ▶ | sub set_replace_only_statement_which_has_token { | ||||||||||||||||||||||||||||||||
|
Subroutine
set_replace_only_statement_which_has_token: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 59 | 5 | my ($self, $in) = @_; | |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | 5 | $self->{statement_in} = $in; | |||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | 223 | ▶ | sub replace_only_statement_which_has_token { | ||||||||||||||||||||||||||||||||
|
Subroutine
replace_only_statement_which_has_token: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 65 | 223 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | 223 | $self->{statement_in}; | |||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | 922 | ▶ | sub source_tokens { | ||||||||||||||||||||||||||||||||
|
Subroutine
source_tokens: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 71 | 922 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | 922 | $self->{source_tokens}; | |||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | 23 | ▶ | sub destination_tokens { | ||||||||||||||||||||||||||||||||
|
Subroutine
destination_tokens: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 77 | 23 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | 23 | $self->{destination_tokens}; | |||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | # refactor a file | ||||||||||||||||||||||||||||||||||
| 83 | # argumensts: | ||||||||||||||||||||||||||||||||||
| 84 | # $file: filename for refactoring | ||||||||||||||||||||||||||||||||||
| 85 | 21 | ▶ | sub execute { | ||||||||||||||||||||||||||||||||
|
Subroutine
execute: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||
| 86 | 21 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||
| 88 | 21 | ▶ | return unless defined $self->source_tokens; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | 20 | my $document = PPI::Document->new($file); | |||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | # When parse failed | ||||||||||||||||||||||||||||||||||
| 93 | 20 | ▶ | return unless $document; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | 18 | my $replaced = 0; | |||||||||||||||||||||||||||||||||
| 96 | 18 | ▶ | if (defined $self->replace_only_statement_which_has_token) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 97 | 3 | $replaced += $self->_replace_in_statement($document); | |||||||||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||||||||
| 99 | 15 | $replaced += $self->_replace_all($document); | |||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | 18 | ▶ | $document->save($file) if $replaced; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | 15 | ▶ | sub _replace_all { | ||||||||||||||||||||||||||||||||
|
Subroutine
_replace_all: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 106 | 15 | my ($self, $document) = @_; | |||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | 15 | my $tokens = $document->find('PPI::Token'); | |||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | 15 | ▶ | return 0 unless $tokens; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 15 | my $replaced = 0; | |||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | 15 | for my $token (@$tokens) { | |||||||||||||||||||||||||||||||||
| 115 | 834 | $replaced += $self->_try_replace($token); | |||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | 15 | $replaced; | |||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | 891 | ▶ | sub _try_replace { | ||||||||||||||||||||||||||||||||
|
Subroutine
_try_replace: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 122 | 891 | my ($self, $token) = @_; | |||||||||||||||||||||||||||||||||
| 123 | 891 | my @matched = $self->_match($token); | |||||||||||||||||||||||||||||||||
| 124 | 891 | ▶ | return 0 unless @matched; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 125 | 13 | my $first = shift @matched; | |||||||||||||||||||||||||||||||||
| 126 | 13 | $first->set_content(join '', @{$self->destination_tokens}); | |||||||||||||||||||||||||||||||||
| 127 | 13 | $_->set_content('') for @matched; # removing `(` will delete (...). So set empty content. | |||||||||||||||||||||||||||||||||
| 128 | 13 | 1; | |||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||
| 131 | 891 | ▶ | sub _match { | ||||||||||||||||||||||||||||||||
|
Subroutine
_match: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 132 | 891 | my ($self, $token) = @_; | |||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | 891 | my @matched; | |||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | 891 | for my $source (@{$self->source_tokens}) { | |||||||||||||||||||||||||||||||||
| 137 | 936 | ▶ | if ($token->content eq $source) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 138 | 53 | push @matched, $token; | |||||||||||||||||||||||||||||||||
| 139 | 53 | $token = $token->next_token; | |||||||||||||||||||||||||||||||||
| 140 | ▶ | } elsif (@matched && $token->next_token && ref $token eq 'PPI::Token::Whitespace') { | |||||||||||||||||||||||||||||||||
|
Condition83%5/6
@matched and $token->next_token and ref $token eq "PPI::Token::Whitespace"
@matched and $token->next_token
MC/DC66%2/3
@matched and $token->next_token and ref $token eq "PPI::Token::Whitespace"
@matched
$token->next_token
ref $token eq "PPI::Token::Whitespace"
Truth table75%3/4
A and B and C
A= @matchedB= $token->next_tokenC= ref $token eq "PPI::Token::Whitespace"
| |||||||||||||||||||||||||||||||||||
| 141 | # ignore white spaces of source | ||||||||||||||||||||||||||||||||||
| 142 | 5 | push @matched, $token; | |||||||||||||||||||||||||||||||||
| 143 | 5 | $token = $token->next_token; | |||||||||||||||||||||||||||||||||
| 144 | 5 | redo; | |||||||||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||||||||
| 146 | 878 | return; | |||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | 13 | return @matched; | |||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | 3 | ▶ | sub _replace_in_statement { | ||||||||||||||||||||||||||||||||
|
Subroutine
_replace_in_statement: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||||
| 153 | 3 | my ($self, $document) = @_; | |||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||
| 155 | 3 | my $statements = $document->find('PPI::Statement'); | |||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | 3 | ▶ | return 0 unless $statements; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | 3 | my $replaced = 0; | |||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||
| 161 | 3 | for my $statement (@$statements) { | |||||||||||||||||||||||||||||||||
| 162 | 42 | ▶ | next if ref $statement eq 'PPI::Statement::Sub'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 163 | 40 | ▶ | next if ref $statement eq 'PPI::Statement::Compound'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 164 | 40 | ▶ | next if $statement->schild(0)->content eq 'do'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | 40 | my $found = 0; | |||||||||||||||||||||||||||||||||
| 167 | 40 | my $tokens = $statement->find('PPI::Token'); | |||||||||||||||||||||||||||||||||
| 168 | 40 | for my $token (@$tokens) { | |||||||||||||||||||||||||||||||||
| 169 | 201 | ▶ | if ($token->content eq $self->replace_only_statement_which_has_token) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 170 | 6 | $found++; | |||||||||||||||||||||||||||||||||
| 171 | 6 | last; | |||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||
| 174 | 40 | ▶ | next unless $found; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||
| 176 | 6 | for my $token (@$tokens) { | |||||||||||||||||||||||||||||||||
| 177 | 57 | $replaced += $self->_try_replace($token); | |||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||
| 181 | 3 | $replaced; | |||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | 3 | 1; | |||||||||||||||||||||||||||||||||