SQL/Abstract/Formatter.pm

filter: Statementstmt 100% 60 / 60 Branchbran 90.6% 29 / 32 Conditioncond 80.0% 16 / 20 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 89.1% 115 / 129 CC 26 SCAR 27.8
CC
26
Cov
94%
_fold_sql
21.0
_simplify
6.2
BEGIN
1.0
CRAP 16.1
« SQL/Abstract/Test.pm ↑ Summary SQL/Abstract.pm »
1 package SQL::Abstract::Formatter;
2  
3 1 require SQL::Abstract::Parts; # it loads us too, don't cross the streams
4  
5 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 has indent_by => (is => 'ro', default => ' ');
8 1 has max_width => (is => 'ro', default => 78);
9  
10 5277 ▶sub _join {
Subroutine _join: called CC=1 SCAR=0.0
11 5277 shift;
12 5277 return SQL::Abstract::Parts::stringify(\@_);
13 }
14  
15 962 ▶sub format {
Subroutine format: called CC=1 SCAR=0.0
Pod undocumented
16 962 my ($self, $join, @parts) = @_;
17 962 $self->_fold_sql('', '', @{$self->_simplify($join, @parts)});
18 }
19  
20 12404 ▶sub _simplify {
Subroutine _simplify: called CC=6 SCAR=18.2
21 12404 my ($self, $join, @parts) = @_;
22 12404 ▶ return '' unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 12404 12404
23 12404 ▶ return $parts[0] if @parts == 1 and !ref($parts[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @parts == 1 and not ref $parts[0] 0 12404 12404
Condition66%2/3
@parts == 1 and not ref $parts[0]
!ll&&!rl&&r
1125011540
MC/DC0%0/2
@parts == 1 and not ref $parts[0]
@parts == 1 not ref $parts[0]
Truth table66%2/3
A and not(B)
A= @parts == 1B= not ref $parts[0]
ABresult
0X0
100
111
24 12404 ▶ return $self->_simplify(@{$parts[0]}) if @parts == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parts == 1 1154 11250 12404
25 11250 ▶ return [ $join, map ref() ? $self->_simplify(@$_) : $_, @parts ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 10288 16827 27115
26 }
27  
28 3176 ▶sub _fold_sql {
Subroutine _fold_sql: called CC=21 SCAR=30.5
29 3176 my ($self, $indent0, $indent, $join, @parts) = @_;
30 3176 my @res;
31 3176 my $w = $self->max_width;
32 3176 my $join_len = 0;
33 (s/, \z/,\n/ and $join_len = 1)
34 or s/\A /\n/
35 or $_ = "\n"
36 3176 ▶ for my $line_join = $join;
Branch: 1 branch
BranchTrueFalseTotal
s/, \z/,\n/ and $join_len = 1 or s/\A /\n/ or $_ = "\n" 3176 0 3176
Condition80%4/5
s/, \z/,\n/ and $join_len = 1 or s/\A /\n/
l!l&&r!l&&!r
20529710
s/, \z/,\n/ and $join_len = 1
!ll
2971205
MC/DC0%0/2
s/, \z/,\n/ and $join_len = 1 or s/\A /\n/
s/, \z/,\n/ s/\A /\n/
Truth table66%2/3
A or B
A= s/, \z/,\n/B= s/\A /\n/
ABresult
1X1
011
000
37 3176 my ($nl_pre, $nl_post) = split "\n", $line_join, -1;
38 3176 my $line = $indent0;
39 3176 my ($lead0) = $indent0 =~ /\A(\s*)/;
40 3176 my $fresh = 1; # $line holds no part of this level yet
41 3176 my $line_proto = $indent.$nl_post;
42 3176 PART: foreach my $idx (0..$#parts) {
43 8017 my $p = $parts[$idx];
44 8017 my $last = $idx == $#parts;
45 8017 ▶ my $pre = $fresh ? '' : $join;
Branch: 1 branch
BranchTrueFalseTotal
$fresh ? : 3692 4325 8017
46 8017 ▶ my $j_part = $pre.(my $j = ref($p) ? $self->_join(@$p) : $p);
Branch: 1 branch
BranchTrueFalseTotal
ref $p ? : 5277 2740 8017
47 8017 ▶ if (length($j_part) + length($line) + $join_len <= $w) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($j_part) + length($line) + $join_len <= $w) 4204 3813 8017
48 4204 $line .= $j_part;
49 4204 $fresh = 0;
50 4204 next PART;
51 }
52 3813 ▶ if (ref($p) and $p->[1] eq '(' and $p->[-1] eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p and $p->[1] eq "(" and $p->[-1] eq ")") 967 2846 3813
Condition83%5/6
ref $p and $p->[1] eq "(" and $p->[-1] eq ")"
!ll&&!rl&&r
28460967
ref $p and $p->[1] eq "("
!ll&&!rl&&r
10321814967
MC/DC66%2/3
ref $p and $p->[1] eq "(" and $p->[-1] eq ")"
ref $p $p->[1] eq "(" $p->[-1] eq ")"
Truth table75%3/4
A and B and C
A= ref $pB= $p->[1] eq "("C= $p->[-1] eq ")"
ABCresult
0XX0
10X0
1100
1111
53 967 my ($lead) = $line =~ /\A(\s*)/;
54 967 my $innerdent = $lead.$self->indent_by;
55 967 push @res, $line.$pre.'('."\n";
56 967 my @inner = @{$p}[2 .. $#$p - 1];
57 my $folded = $self->_fold_sql($innerdent, $innerdent,
58 967 ▶ (@inner == 1 and ref($inner[0])) ? @{$inner[0]} : ($p->[0], @inner)
Branch: 1 branch
BranchTrueFalseTotal
@inner == 1 && ref $inner[0] ? : 899 68 967
Condition66%2/3
@inner == 1 and ref $inner[0]
!ll&&!rl&&r
680899
MC/DC50%1/2
@inner == 1 and ref $inner[0]
@inner == 1 ref $inner[0]
Truth table66%2/3
A and B
A= @inner == 1B= ref $inner[0]
ABresult
0X0
100
111
59 );
60 967 push @res, $folded."\n";
61 967 $line = $lead.')';
62 967 $fresh = 0;
63 967 next PART;
64 }
65 2846 ▶ my $innerdent = (@res ? $indent : $lead0).$self->indent_by;
Branch: 1 branch
BranchTrueFalseTotal
@res ? : 706 2140 2846
66 2846 ▶ my $start = $fresh ? $line : $line_proto;
Branch: 1 branch
BranchTrueFalseTotal
$fresh ? : 709 2137 2846
67 2846 ▶ push @res, $line.$nl_pre."\n" unless $fresh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fresh 2137 709 2846
68 2846 $line = $start.$j;
69 2846 $fresh = 0;
70 2846 ▶ next PART if !ref($p) or length($line) <= $w;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $p or length $line <= $w 1599 1247 2846
Condition100%3/3
not ref $p or length $line <= $w
l!l&&r!l&&!r
10325671247
MC/DC100%2/2
not ref $p or length $line <= $w
not ref $p length $line <= $w
Truth table100%3/3
not(A) or B
A= not ref $pB= length $line <= $w
ABresult
1X1
011
000
71 1247 my $folded = $self->_fold_sql($start, $innerdent, @$p);
72 1247 ▶ push @res, $folded.($last ? '' : $nl_pre."\n");
Branch: 1 branch
BranchTrueFalseTotal
$last ? : 731 516 1247
73 1247 ▶ $line = $last ? '' : $line_proto;
Branch: 1 branch
BranchTrueFalseTotal
$last ? : 731 516 1247
74 1247 $fresh = 1;
75 }
76 3176 return join '', @res, $line;
77 }
78  
79 1 1;
« SQL/Abstract/Test.pm ↑ Summary SQL/Abstract.pm »