assign/0.pm

filter: Statementstmt 95.9% 94 / 98 Branchbran 72.7% 16 / 22 Conditioncond 83.3% 35 / 42 MC/DCmcdc 58.8% 10 / 17 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 11 totaltotal 83.3% 175 / 210 CC 31 SCAR 15.5
CC
31
Cov
90%
__ANON__
9.3
__ANON__
7.2
debug
6.6
CRAP 4.7
« assign/Struct.pm ↑ Summary assign/Hash.pm »
1 6 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package assign::0;
3  
4 6 our $VERSION = '0.0.17';
5  
6 6 ▶use assign::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use assign::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Filter::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use XXX;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 our $var_prefix = '___';
14 6 our $var_id = 1000;
15 6 our $var_suffix = '';
16  
17 23 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
18 23 my $class = shift;
19 23 my $self = bless { @_ }, $class;
20 my $code = $self->{code}
21 23 ▶ or die "$class\->new requires 'code' string";
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->{"code"} or die "$class->new requires 'code' string" 23 0 23
22 23 ▶ $self->{line} //= 0;
Condition100%2/2
$self->{"line"} //= 0
l!l
716
MC/DC100%1/1
$self->{"line"} //= 0
$self->{"line"}
Truth table100%2/2
A
A= $self->{"line"}
Aresult
11
00
23 23 $self->{doc} = PPI::Document->new(\$code);
24 23 $self->{doc}->index_locations;
25 23 return $self;
26 }
27  
28 # FILTER_ONLY code_no_comments => \&filter;
29 6 FILTER_ONLY all => \&filter;
30  
31 7 ▶sub filter {
Subroutine filter: called CC=1 SCAR=0.0
Pod undocumented
32 7 my ($class) = @_;
33 7 $_ = $class->new(
34 code => $_,
35 line => ([caller(4)])->[2],
36 )->transform();
37 };
38  
39 1 ▶sub debug {
Subroutine debug: called CC=5 SCAR=18.9
Pod undocumented
40 1 my ($class, $code) = @_;
41 1 ▶ if (ref($code) eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $code eq 'SCALAR') { } 0 1 1
elsif (not ref $code) { } 1 0 1
42 0 $code = $$code;
43 } elsif (not ref($code)) {
44 1 ▶ if (not -f $code) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $code) 0 1 1
45 0 XXX $code;
46 0 die "Argument to $class\->debug() is not a valid file.\n" .
47 "Code strings need to be passed as scalar refs.";
48 }
49 1 ▶ open my $fh, $code or die "Can't open '$code' for input";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $code or die "Can't open '${code}' for input" 1 0 1
50 1 $code = do { local $/; <$fh> };
51 } else {
52 0 die "Invalid arguments for $class->debug(...)";
53 }
54 1 $class->new(code => $code)->transform;
55 }
56  
57 23 ▶sub transform {
Subroutine transform: called CC=1 SCAR=0.0
Pod undocumented
58 23 my ($self) = @_;
59  
60 # Call the various possible assignment transformations:
61 23 $self->transform_assignment_statements_with_decl;
62 23 $self->transform_assignment_statements_no_decl;
63 # ... more to come ...
64  
65 23 $self->{doc}->serialize;
66 }
67  
68 23 ▶sub transform_assignment_statements_with_decl {
Subroutine transform_assignment_statements_with_decl: called CC=2 SCAR=6.9
Pod undocumented
69 23 my ($self) = @_;
70  
71 23 for my $node ($self->find_assignment_statements_with_decl) {
72 24 my ($decl, $lhs, $oper, @rhs) = $node->schildren;
73 24 my $rhs = join '', map $_->content, @rhs;
74 24 $self->transform_assignment_statement(
75 $node, $decl, $lhs, $oper, $rhs,
76 );
77 }
78 }
79  
80 23 ▶sub transform_assignment_statements_no_decl {
Subroutine transform_assignment_statements_no_decl: called CC=2 SCAR=6.9
Pod undocumented
81 23 my ($self) = @_;
82  
83 23 for my $node ($self->find_assignment_statements_no_decl) {
84 2 my $decl = '';
85 2 my ($lhs, $oper, @rhs) = $node->schildren;
86 2 my $rhs = join '', map $_->content, @rhs;
87 2 $self->transform_assignment_statement(
88 $node, $decl, $lhs, $oper, $rhs,
89 );
90 }
91 }
92  
93 23 ▶sub find_assignment_statements_with_decl {
Subroutine find_assignment_statements_with_decl: called CC=2 SCAR=7.0
Pod undocumented
94 23 my ($self) = @_;
95  
96 23 ▶ map { $_ ||= []; @$_ }
Condition100%2/2
$_ ||= []
l!l
176
MC/DC100%1/1
$_ ||= []
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
97 2799 ▶ $self->{doc}->find(sub {
Subroutine __ANON__: called CC=9 SCAR=22.3
98 2799 my $n = $_[1];
99 return 0 unless
100 $n->isa('PPI::Statement::Variable') and
101 2799 ▶ @{[$n->schildren]} >= 5 and
Branch: 1 branch
BranchTrueFalseTotal
unless $n->isa("PPI::Statement::Variable") and @{[$n->schildren];} >= 5 and $n->schild(0)->isa("PPI::Token::Word") and $n->schild(0)->content =~ /^(my|our|local)$/ and $n->schild(1)->isa("PPI::Structure::Constructor") || $n->schild(1)->isa("PPI::Structure::Block") and $n->schild(2)->isa("PPI::Token::Operator") and $n->schild(2)->content eq "=" 2775 24 2799
Condition80%17/21
$n->isa("PPI::Statement::Variable") and @{[$n->schildren];} >= 5 and $n->schild(0)->isa("PPI::Token::Word") and $n->schild(0)->content =~ /^(my|our|local)$/ and $n->schild(1)->isa("PPI::Structure::Constructor") || $n->schild(1)->isa("PPI::Structure::Block") and $n->schild(2)->isa("PPI::Token::Operator") and $n->schild(2)->content eq "="
!ll&&!rl&&r
2775024
$n->isa("PPI::Statement::Variable") and @{[$n->schildren];} >= 5 and $n->schild(0)->isa("PPI::Token::Word") and $n->schild(0)->content =~ /^(my|our|local)$/ and $n->schild(1)->isa("PPI::Structure::Constructor") || $n->schild(1)->isa("PPI::Structure::Block") and $n->schild(2)->isa("PPI::Token::Operator")
!ll&&!rl&&r
2775024
$n->isa("PPI::Statement::Variable") and @{[$n->schildren];} >= 5 and $n->schild(0)->isa("PPI::Token::Word") and $n->schild(0)->content =~ /^(my|our|local)$/ and $n->schild(1)->isa("PPI::Structure::Constructor") || $n->schild(1)->isa("PPI::Structure::Block")
!ll&&!rl&&r
27423324
$n->isa("PPI::Statement::Variable") and @{[$n->schildren];} >= 5 and $n->schild(0)->isa("PPI::Token::Word") and $n->schild(0)->content =~ /^(my|our|local)$/
!ll&&!rl&&r
2742057
$n->isa("PPI::Statement::Variable") and @{[$n->schildren];} >= 5 and $n->schild(0)->isa("PPI::Token::Word")
!ll&&!rl&&r
2742057
$n->isa("PPI::Statement::Variable") and @{[$n->schildren];} >= 5
!ll&&!rl&&r
2736657
$n->schild(1)->isa("PPI::Structure::Constructor") or $n->schild(1)->isa("PPI::Structure::Block")
l!l&&r!l&&!r
21333
MC/DC50%4/8
$n->isa("PPI::Statement::Variable") and @{[$n->schildren];} >= 5 and $n->schild(0)->isa("PPI::Token::Word") and $n->schild(0)->content =~ /^(my|our|local)$/ and $n->schild(1)->isa("PPI::Structure::Constructor") || $n->schild(1)->isa("PPI::Structure::Block") and $n->schild(2)->isa("PPI::Token::Operator") and $n->schild(2)->content eq "="
$n->isa("PPI::Statement::Variable") @{[$n->schildren];} >= 5 $n->schild(0)->isa("PPI::Token::Word") $n->schild(0)->content =~ /^(my|our|local)$/ $n->schild(1)->isa("PPI::Structure::Constructor") $n->schild(1)->isa("PPI::Structure::Block") $n->schild(2)->isa("PPI::Token::Operator") $n->schild(2)->content eq "="
Truth table45%5/11
A and B and C and D and E or F and G and H
A= $n->isa("PPI::Statement::Variable")B= @{[$n->schildren];} >= 5C= $n->schild(0)->isa("PPI::Token::Word")D= $n->schild(0)->content =~ /^(my|our|local)$/E= $n->schild(1)->isa("PPI::Structure::Constructor")F= $n->schild(1)->isa("PPI::Structure::Block")G= $n->schild(2)->isa("PPI::Token::Operator")H= $n->schild(2)->content eq "="
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
111100XX0
11111X0X0
1111010X0
11111X100
111101100
11111X111
111101111
102 $n->schild(0)->isa('PPI::Token::Word') and
103 $n->schild(0)->content =~ /^(my|our|local)$/ and
104 (
105 $n->schild(1)->isa('PPI::Structure::Constructor') or
106 $n->schild(1)->isa('PPI::Structure::Block')
107 ) and
108 # or PPI::Structure::Block
109 $n->schild(2)->isa('PPI::Token::Operator') and
110 $n->schild(2)->content eq '=';
111 23 });
112 }
113  
114 23 ▶sub find_assignment_statements_no_decl {
Subroutine find_assignment_statements_no_decl: called CC=2 SCAR=7.0
Pod undocumented
115 23 my ($self) = @_;
116  
117 23 ▶ map { $_ ||= []; @$_ }
Condition100%2/2
$_ ||= []
l!l
221
MC/DC100%1/1
$_ ||= []
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
118 3738 ▶ $self->{doc}->find(sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
119 3738 my $n = $_[1];
120 return 0 unless
121 ref($n) eq 'PPI::Statement' and
122 3738 ▶ @{[$n->schildren]} >= 4 and
Branch: 1 branch
BranchTrueFalseTotal
unless ref $n eq "PPI::Statement" and @{[$n->schildren];} >= 4 and $n->schild(0)->isa("PPI::Structure::Constructor") || $n->schild(0)->isa("PPI::Structure::Block") and $n->schild(1)->isa("PPI::Token::Operator") and $n->schild(1)->content eq "=" 3736 2 3738
Condition80%12/15
ref $n eq "PPI::Statement" and @{[$n->schildren];} >= 4 and $n->schild(0)->isa("PPI::Structure::Constructor") || $n->schild(0)->isa("PPI::Structure::Block") and $n->schild(1)->isa("PPI::Token::Operator") and $n->schild(1)->content eq "="
!ll&&!rl&&r
373602
ref $n eq "PPI::Statement" and @{[$n->schildren];} >= 4 and $n->schild(0)->isa("PPI::Structure::Constructor") || $n->schild(0)->isa("PPI::Structure::Block") and $n->schild(1)->isa("PPI::Token::Operator")
!ll&&!rl&&r
373602
ref $n eq "PPI::Statement" and @{[$n->schildren];} >= 4 and $n->schild(0)->isa("PPI::Structure::Constructor") || $n->schild(0)->isa("PPI::Structure::Block")
!ll&&!rl&&r
36151212
ref $n eq "PPI::Statement" and @{[$n->schildren];} >= 4
!ll&&!rl&&r
359520123
$n->schild(0)->isa("PPI::Structure::Constructor") or $n->schild(0)->isa("PPI::Structure::Block")
l!l&&r!l&&!r
20121
MC/DC50%3/6
ref $n eq "PPI::Statement" and @{[$n->schildren];} >= 4 and $n->schild(0)->isa("PPI::Structure::Constructor") || $n->schild(0)->isa("PPI::Structure::Block") and $n->schild(1)->isa("PPI::Token::Operator") and $n->schild(1)->content eq "="
ref $n eq "PPI::Statement" @{[$n->schildren];} >= 4 $n->schild(0)->isa("PPI::Structure::Constructor") $n->schild(0)->isa("PPI::Structure::Block") $n->schild(1)->isa("PPI::Token::Operator") $n->schild(1)->content eq "="
Truth table44%4/9
A and B and C or D and E and F
A= ref $n eq "PPI::Statement"B= @{[$n->schildren];} >= 4C= $n->schild(0)->isa("PPI::Structure::Constructor")D= $n->schild(0)->isa("PPI::Structure::Block")E= $n->schild(1)->isa("PPI::Token::Operator")F= $n->schild(1)->content eq "="
ABCDEFresult
0XXXXX0
10XXXX0
1100XX0
111X0X0
11010X0
111X100
1101100
111X111
1101111
123 (
124 $n->schild(0)->isa('PPI::Structure::Constructor') or
125 $n->schild(0)->isa('PPI::Structure::Block')
126 ) and
127 $n->schild(1)->isa('PPI::Token::Operator') and
128 $n->schild(1)->content eq '=';
129 23 });
130 }
131  
132 26 ▶sub transform_assignment_statement {
Subroutine transform_assignment_statement: called CC=5 SCAR=16.1
Pod undocumented
133 26 my ($self, $node, $decl, $lhs, $oper, $rhs) = @_;
134  
135 26 ▶ $decl = $decl ? $decl->{content} . ' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$decl ? : 24 2 26
136 26 $oper = $oper->{content};
137  
138 26 ▶ my $class =
Branch: 2 branches
BranchTrueFalseTotal
$lhs->start->content eq '[' ? : 23 3 26
$lhs->start->content eq '{' ? : 3 0 3
139 $lhs->start->content eq '[' ? 'assign::Array' :
140 $lhs->start->content eq '{' ? 'assign::Hash' :
141 ZZZ $node, "Unsupported statement";
142  
143 26 my $from;
144 26 my $init = [];
145 26 ▶ if ($rhs =~ /^(\$\w+);/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs =~ /^(\$\w+);/) { } 16 10 26
146 16 $from = $1;
147 } else {
148 10 $from = $self->gen_var;
149 10 push @$init, "my $from = $rhs";
150 }
151  
152 26 my $code = $class->new(
153 node => $lhs,
154 )->parse->gen_code($decl, $oper, $from, $init);
155  
156 26 $self->replace_statement_node($node, $code);
157  
158 26 return;
159 }
160  
161 10 ▶sub gen_var {
Subroutine gen_var: called CC=1 SCAR=0.0
Pod undocumented
162 10 $var_id++;
163 10 return "\$$var_prefix$var_id$var_suffix";
164 }
165  
166 26 ▶sub replace_statement_node {
Subroutine replace_statement_node: called CC=3 SCAR=11.0
Pod undocumented
167 26 my ($self, $node, $code) = @_;
168 26 my $line_number = $node->last_token->logical_line_number + $self->{line};
169 $node->insert_after($_->remove)
170 26 for reverse PPI::Document->new(\"\n#line $line_number")->elements;
171 $node->insert_after($_->remove)
172 26 for reverse PPI::Document->new(\$code)->elements;
173 26 $node->remove;
174 26 return;
175 }
176  
177 6 1;
« assign/Struct.pm ↑ Summary assign/Hash.pm »