PerlNitpick/Rule/RewriteWithAssignmentOperators.pm

filter: Statementstmt 100% 46 / 46 Branchbran 100% 4 / 4 Conditioncond 50.0% 16 / 32 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 73.0% 76 / 104 CC 15 SCAR 25.1
CC
15
Cov
80%
rewrite
18.6
_trim_whitespace
2.0
BEGIN
1.0
CRAP 12.3
« PerlNitpick/Rule/QuoteSimpleStringWithSingleQuote.pm ↑ Summary PerlNitpick/Rule/RemoveUnusedImport.pm »
1 package App::PerlNitpick::Rule::RewriteWithAssignmentOperators;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use PPI::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use PPI::Token::Whitespace;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use constant IS_REWRITABLE => { map { $_ => 1 } (
Subroutine BEGIN: called CC=1 SCAR=0.0
9 '*', '/', '%', 'x', '+', '-', '.',
10 '//', '||',
11 '|', '&', '^',
12 '|.', '&.', '^.',
13 '<<', '>>'
14 1 ) };
15  
16 8 ▶sub is_rewritable {
Subroutine is_rewritable: called CC=1 SCAR=0.0
Pod undocumented
17 8 my ($op) = @_;
18 8 return IS_REWRITABLE->{$op};
19 }
20  
21 5 ▶sub _trim_whitespace {
Subroutine _trim_whitespace: called CC=2 SCAR=6.9
22 5 my ($elem) = @_;
23  
24 5 my $e = $elem->next_sibling;
25 5 ▶ while (! $e->significant) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $e->significant) 2 5 7
26 2 $e->remove;
27 2 $e = $elem->next_sibling;
28 }
29 }
30  
31 8 ▶sub rewrite {
Subroutine rewrite: called CC=14 SCAR=29.2
Pod undocumented
32 8 my ($self, $document) = @_;
33  
34 my @found = grep {
35 # Find a statement that looks like $x = $x + $y;
36 8 my $c0 = $_->schild(0); # $x
37 8 my $c1 = $_->schild(1); # =
38 8 my $c2 = $_->schild(2); # $x
39 8 my $c3 = $_->schild(3); # +
40 8 my $c4 = $_->schild(4); # $y
41 8 my $c5 = $_->schild(5); # ;
42  
43 8 ▶ ($c1->isa('PPI::Token::Operator') && $c1->content eq '=') &&
Condition50%15/30
$c1->isa('PPI::Token::Operator') && $c1->content eq '=' && ($c5->isa('PPI::Token::Structure') && $c5->content eq ';') && ($c3->isa('PPI::Token::Operator') && is_rewritable($c3->content)) && ($c0->isa('PPI::Token::Symbol') && $c0->raw_type eq '$' && $c2->isa('PPI::Token::Symbol') && $c2->raw_type eq '$' && $c0->content eq $c2->content)
!ll&&!rl&&r
125
$c1->isa("PPI::Token::Operator") and $c1->content eq "=" and ($c5->isa("PPI::Token::Structure") and $c5->content eq ";") and ($c3->isa("PPI::Token::Operator") and is_rewritable($c3->content))
!ll&&!rl&&r
017
$c1->isa("PPI::Token::Operator") and $c1->content eq "=" and ($c5->isa("PPI::Token::Structure") and $c5->content eq ";")
!ll&&!rl&&r
008
$c1->isa("PPI::Token::Operator") and $c1->content eq "="
!ll&&!rl&&r
008
$c5->isa("PPI::Token::Structure") and $c5->content eq ";"
!ll&&!rl&&r
008
$c3->isa("PPI::Token::Operator") and is_rewritable($c3->content)
!ll&&!rl&&r
017
$c0->isa("PPI::Token::Symbol") and $c0->raw_type eq "\$" and $c2->isa("PPI::Token::Symbol") and $c2->raw_type eq "\$" and $c0->content eq $c2->content
!ll&&!rl&&r
025
$c0->isa("PPI::Token::Symbol") and $c0->raw_type eq "\$" and $c2->isa("PPI::Token::Symbol") and $c2->raw_type eq "\$"
!ll&&!rl&&r
007
$c0->isa("PPI::Token::Symbol") and $c0->raw_type eq "\$" and $c2->isa("PPI::Token::Symbol")
!ll&&!rl&&r
007
$c0->isa("PPI::Token::Symbol") and $c0->raw_type eq "\$"
!ll&&!rl&&r
007
MC/DC18%2/11
$c1->isa('PPI::Token::Operator') && $c1->content eq '=' && ($c5->isa('PPI::Token::Structure') && $c5->content eq ';') && ($c3->isa('PPI::Token::Operator') && is_rewritable($c3->content)) && ($c0->isa('PPI::Token::Symbol') && $c0->raw_type eq '$' && $c2->isa('PPI::Token::Symbol') && $c2->raw_type eq '$' && $c0->content eq $c2->content)
$c1->isa("PPI::Token::Operator") $c1->content eq "=" $c5->isa("PPI::Token::Structure") $c5->content eq ";" $c3->isa("PPI::Token::Operator") is_rewritable($c3->content) $c0->isa("PPI::Token::Symbol") $c0->raw_type eq "\$" $c2->isa("PPI::Token::Symbol") $c2->raw_type eq "\$" $c0->content eq $c2->content
Truth table25%3/12
A and B and C and D and E and F && G and H and I and J and K
A= $c1->isa("PPI::Token::Operator")B= $c1->content eq "="C= $c5->isa("PPI::Token::Structure")D= $c5->content eq ";"E= $c3->isa("PPI::Token::Operator")F= is_rewritable($c3->content)G= $c0->isa("PPI::Token::Symbol")H= $c0->raw_type eq "\$"I= $c2->isa("PPI::Token::Symbol")J= $c2->raw_type eq "\$"K= $c0->content eq $c2->content
ABCDEFGHIJKresult
0XXXXXXXXXX0
10XXXXXXXXX0
110XXXXXXXX0
1110XXXXXXX0
11110XXXXXX0
111110XXXXX0
1111110XXXX0
11111110XXX0
111111110XX0
1111111110X0
111111111100
111111111111
44 ($c5->isa('PPI::Token::Structure') && $c5->content eq ';') &&
45 ($c3->isa('PPI::Token::Operator') && is_rewritable($c3->content)) &&
46 ($c0->isa('PPI::Token::Symbol') && $c0->raw_type eq '$' &&
47 $c2->isa('PPI::Token::Symbol') && $c2->raw_type eq '$' &&
48 $c0->content eq $c2->content)
49 } grep {
50 9 $_->schildren == 6
51 8 ▶ } @{ $document->find('PPI::Statement') ||[] };
Condition50%1/2
$document->find('PPI::Statement') || []
l!l
80
MC/DC0%0/1
$document->find('PPI::Statement') || []
$document->find('PPI::Statement')
Truth table50%1/2
A
A= $document->find('PPI::Statement')
Aresult
11
00
52  
53 8 ▶ return $document unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 3 5 8
54  
55 5 for my $statement (@found) {
56 5 my @child = $statement->schildren;
57  
58 5 my $assigment_operator = PPI::Token::Operator->new($child[3]->content . $child[1]->content);
59  
60 5 $child[3]->remove;
61  
62 5 _trim_whitespace($child[2]);
63 5 $child[2]->remove;
64  
65 5 $child[2]->remove;
66 5 $child[1]->insert_after($assigment_operator);
67 5 $child[1]->remove;
68 }
69  
70 5 return $document;
71 }
72  
73 1 1;
74  
75 __END__
« PerlNitpick/Rule/QuoteSimpleStringWithSingleQuote.pm ↑ Summary PerlNitpick/Rule/RemoveUnusedImport.pm »