MyParsePP/Rule.pm

filter: Statementstmt 87.9% 102 / 116 Branchbran 80.3% 45 / 56 Conditioncond 73.3% 11 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 76.1% 16 / 21 Podpod 0.0% 0 / 15 totaltotal 76.7% 178 / 232 CC 38 SCAR 20.8
CC
38
Cov
84%
shrink
15.0
getTables
12.5
extractInner
7.0
CRAP 8.0
« MyParsePP/Token.pm ↑ Summary MyParsePP/Parser.pm »
1 package DBIx::MyParsePP::Rule;
2 8 require Exporter;
3 8 @ISA = qw(Exporter);
4 8 @EXPORT = qw(MYPARSEPP_SHRINK_IDENTICAL MYPARSEPP_SHRINK_LEAFS MYPARSEPP_SHRINK_SINGLES MYPARSEPP_SHRINK_CHILDREN);
5  
6 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 1;
9  
10 8 ▶use constant RULE_NAME => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use constant MYPARSEPP_SHRINK_IDENTICAL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use constant MYPARSEPP_SHRINK_LEAFS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use constant MYPARSEPP_SHRINK_SINGLES => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use constant MYPARSEPP_SHRINK_CHILDREN => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 87 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
18 87 my $class = shift;
19 87 my $rule = bless (\@_, $class);
20 87 return $rule;
21 }
22  
23 0 ▶sub getName {
Subroutine getName: not called CC=1 SCAR=6.9
Pod undocumented
24 0 return $_[0]->[RULE_NAME];
25 }
26  
27 1587 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
28 1587 return $_[0]->[RULE_NAME];
29 }
30  
31 222 ▶sub children {
Subroutine children: called CC=1 SCAR=0.0
Pod undocumented
32 222 my @rule = @{$_[0]};
33 222 return @rule[1..$#rule];
34 }
35  
36 0 ▶sub getChildren {
Subroutine getChildren: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my @rule = @{$_[0]};
38 0 return @rule[1..$#rule];
39 }
40  
41 50 ▶sub toString {
Subroutine toString: called CC=2 SCAR=7.1
Pod undocumented
42 50 my $rule = shift;
43  
44 50 ▶ if ($#{$rule} > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$rule;} > -1) { } 50 0 50
45 return join('', map {
46 72 $rule->[$_]->toString();
47 50 } (1..$#{$rule}) );
48 } else {
49 0 return undef;
50 }
51 }
52  
53 545 ▶sub shrink {
Subroutine shrink: called CC=14 SCAR=27.1
Pod undocumented
54 545 my ($parent, $flags) = @_;
55  
56 545 ▶ $flags = MYPARSEPP_SHRINK_IDENTICAL | MYPARSEPP_SHRINK_LEAFS | MYPARSEPP_SHRINK_SINGLES | MYPARSEPP_SHRINK_CHILDREN if not defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 12 533 545
57
58 545 ▶ if (($#{$parent} == 0) && ($flags & MYPARSEPP_SHRINK_LEAFS)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($#{$parent;} == 0 and $flags & 2) { } 111 434 545
elsif ($#{$parent;} == 1 and $flags & 4) { } 308 126 434
elsif ($flags & 8) { } 85 41 126
Condition66%2/3
$#{$parent;} == 0 and $flags & 2
!ll&&!rl&&r
4340111
MC/DC50%1/2
$#{$parent;} == 0 and $flags & 2
$#{$parent;} == 0 $flags & 2
Truth table66%2/3
A and B
A= $#{$parent;} == 0B= $flags & 2
ABresult
0X0
100
111
59 111 return undef;
60 434 ▶ } elsif (($#{$parent} == 1) && ($flags & MYPARSEPP_SHRINK_SINGLES)) {
Condition66%2/3
$#{$parent;} == 1 and $flags & 4
!ll&&!rl&&r
1260308
MC/DC50%1/2
$#{$parent;} == 1 and $flags & 4
$#{$parent;} == 1 $flags & 4
Truth table66%2/3
A and B
A= $#{$parent;} == 1B= $flags & 4
ABresult
0X0
100
111
61 308 ▶ if ($flags & MYPARSEPP_SHRINK_CHILDREN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 8) { } 266 42 308
62 266 return $parent->[1]->shrink($flags);
63 } else {
64 42 return $parent->[1];
65 }
66 } elsif ($flags & MYPARSEPP_SHRINK_CHILDREN) {
67 85 my @new_children;
68  
69 85 foreach my $i (1..$#{$parent}) {
70 271 my $child = $parent->[$i]->shrink($flags);
71 271 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 1 and ref $child eq 'DBIx::MyParsePP::Rule' and $child->name eq $parent->name) { } 0 271 271
Condition66%4/6
$flags & 1 and ref $child eq "DBIx::MyParsePP::Rule" and $child->name eq $parent->name
!ll&&!rl&&r
229420
$flags & 1 and ref $child eq "DBIx::MyParsePP::Rule"
!ll&&!rl&&r
022942
MC/DC0%0/3
$flags & 1 and ref $child eq "DBIx::MyParsePP::Rule" and $child->name eq $parent->name
$flags & 1 ref $child eq "DBIx::MyParsePP::Rule" $child->name eq $parent->name
Truth table50%2/4
A and B and C
A= $flags & 1B= ref $child eq "DBIx::MyParsePP::Rule"C= $child->name eq $parent->name
ABCresult
0XX0
10X0
1100
1111
72 ($flags & MYPARSEPP_SHRINK_IDENTICAL) &&
73 (ref($child) eq 'DBIx::MyParsePP::Rule') &&
74 ($child->name() eq $parent->name())
75 ) {
76 0 ▶ push @new_children, $child->children() if defined $child->children();
Branch: 1 branch
BranchTrueFalseTotal
if defined $child->children 0 0 0
77 } else {
78 271 ▶ push @new_children, $child if defined $child;
Branch: 1 branch
BranchTrueFalseTotal
if defined $child 160 111 271
79 }
80 }
81  
82 85 my $new_rule = DBIx::MyParsePP::Rule->new($parent->name(), @new_children);
83 85 return $new_rule->shrink($flags & ~MYPARSEPP_SHRINK_CHILDREN);
84 } else {
85 41 return $parent;
86 }
87 }
88  
89  
90 522 ▶sub extractInner {
Subroutine extractInner: called CC=7 SCAR=19.5
Pod undocumented
91 522 my $rule = shift;
92 522 ▶ return undef if ($#{$rule} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $#{$rule;} == 0 122 400 522
93  
94 400 my @matches;
95 400 foreach my $i (1..$#{$rule}) {
96 599 my $extract = $rule->[$i]->extract(@_);
97 599 ▶ next if not defined $extract;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extract 234 365 599
98 365 ▶ if (ref($extract) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $extract eq 'ARRAY') { } 78 287 365
99 78 push @matches, @{$extract};
100 } else {
101 287 push @matches, $extract;
102 }
103 }
104  
105 400 ▶ if ($#matches == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($#matches == -1) { } 59 341 400
elsif ($#matches == 0) { } 258 83 341
106 59 return undef;
107 } elsif ($#matches == 0) {
108 258 return $matches[0];
109 } else {
110 83 return \@matches;
111 }
112 }
113  
114 1 ▶sub getFields {
Subroutine getFields: called CC=1 SCAR=0.0
Pod undocumented
115 1 return $_[0]->extract('simple_ident_q','table_ident','ident');
116 }
117  
118 0 ▶sub fields {
Subroutine fields: not called CC=1 SCAR=6.9
Pod undocumented
119 0 return $_[0]->getFields();
120 }
121  
122 0 ▶sub tables {
Subroutine tables: not called CC=1 SCAR=6.9
Pod undocumented
123 0 return $_[0]->getTables();
124 }
125  
126 1 ▶sub getTables {
Subroutine getTables: called CC=11 SCAR=25.3
Pod undocumented
127 1 my $rule = shift;
128  
129 1 my @tables_array;
130 my %tables_hash;
131  
132 1 my $idents = $rule->extract('table_wild','table_ident','simple_ident_q');
133 1 ▶ $idents = [$idents] if ref($idents) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $idents ne "ARRAY" 0 1 1
134
135 1 foreach my $ident (@{$idents}) {
136 6 my $shrinked_ident = $ident->shrink();
137 6 my @children = $shrinked_ident->children();
138  
139 6 my $table;
140  
141 6 ▶ if ($#children == -1) { # No children
Branch: 4 branches
BranchTrueFalseTotal
if ($#children == -1) { } 1 5 6
elsif ($#children == 0) { } 0 5 5
elsif ($#children == 2) { } 3 2 5
elsif ($#children == 4) { } 2 0 2
142 1 $table = $shrinked_ident;
143 } elsif ($#children == 0) { # One child
144 0 $table = $children[0];
145 } elsif ($#children == 2) {
146 3 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($shrinked_ident->name eq 'simple_ident_q' or $shrinked_ident->name eq 'table_wild') { } 2 1 3
elsif ($shrinked_ident->name eq 'table_ident') { } 1 0 1
Condition100%3/3
$shrinked_ident->name eq "simple_ident_q" or $shrinked_ident->name eq "table_wild"
l!l&&r!l&&!r
111
MC/DC100%2/2
$shrinked_ident->name eq "simple_ident_q" or $shrinked_ident->name eq "table_wild"
$shrinked_ident->name eq "simple_ident_q" $shrinked_ident->name eq "table_wild"
Truth table100%3/3
A or B
A= $shrinked_ident->name eq "simple_ident_q"B= $shrinked_ident->name eq "table_wild"
ABresult
1X1
011
000
147 ($shrinked_ident->name() eq 'simple_ident_q') ||
148 ($shrinked_ident->name() eq 'table_wild')
149 ) {
150 2 $table = $children[0]; # We have "database.table"
151 } elsif ($shrinked_ident->name() eq 'table_ident') {
152 1 $table = $children[2]; # We have "table.field"
153 } else {
154 0 print STDERR "Assertion: \$\#children == $#children but name() = ".$shrinked_ident->name()."\n";
155 0 return undef;
156 }
157 } elsif ($#children == 4) { # We have "database.table.field"
158 2 $table = DBIx::MyParsePP::Rule->new( $ident->name(), @children[0..2] );
159 } else {
160 0 print STDERR "Assertion: \$\#children == $#children\n";
161 0 return undef;
162 }
163  
164 6 ▶ if (not exists $tables_hash{$table->toString()}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $tables_hash{$table->toString}) 6 0 6
165 6 push @tables_array, $table;
166 6 $tables_hash{$table->toString()} = 1;
167 }
168 }
169  
170 1 return \@tables_array;
171
172 }
173  
174 551 ▶sub extract {
Subroutine extract: called CC=3 SCAR=11.0
Pod undocumented
175 551 my $rule = shift;
176 551 foreach my $match (@_) {
177 1193 ▶ return $rule if ($rule->name() eq $match);
Branch: 1 branch
BranchTrueFalseTotal
if $rule->name eq $match 29 1164 1193
178 }
179 522 return $rule->extractInner(@_);
180 }
181  
182 0 ▶sub print {
Subroutine print: not called CC=1 SCAR=6.9
Pod undocumented
183 0 return $_[0]->toString();
184 }
185  
186 107 ▶sub isEqual {
Subroutine isEqual: called CC=6 SCAR=18.0
Pod undocumented
187 107 ▶ return 0 if !$_[1]->isa( 'DBIx::MyParsePP::Rule' );
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("DBIx::MyParsePP::Rule") 0 107 107
188 107 ▶ return 0 if $_[0]->name() ne $_[1]->name();
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->name ne $_[1]->name 1 106 107
189  
190 106 my @left_children = $_[0]->children();
191 106 my @right_children = $_[1]->children();
192 106 ▶ return 0 if @left_children != @right_children;
Branch: 1 branch
BranchTrueFalseTotal
if @left_children != @right_children 0 106 106
193  
194 106 ▶ for( my $i = 0; $i < @left_children; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @left_children) 149 105 254
195 149 ▶ return 0 if !$left_children[$i]->isEqual( $right_children[$i] );
Branch: 1 branch
BranchTrueFalseTotal
unless $left_children[$i]->isEqual($right_children[$i]) 1 148 149
196 }
197  
198 105 return 1;
199 }
200  
201 8 1;
202  
203 __END__
« MyParsePP/Token.pm ↑ Summary MyParsePP/Parser.pm »