SQL/Abstract/Formatter.pm

filter: Statementstmt 100% 62 / 62 Branchbran 88.2% 30 / 34 Conditioncond 80.0% 16 / 20 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 88.7% 118 / 133 CC 26 SCAR 27.8
CC
26
Cov
93%
_fold_sql
21.0
_simplify
6.2
BEGIN
1.0
CRAP 16.1
« SQL/Abstract/Test.pm ↑ Summary SQL/Abstract/Plugin/BangOverrides.pm »
1 package SQL::Abstract::Formatter;
2  
3 1 our $VERSION = '2.000003';
4 1 ▶$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
Branch: 1 branch
BranchTrueFalseTotal
if $VERSION =~ /_/ 0 1 1
5  
6 1 require SQL::Abstract::Parts; # it loads us too, don't cross the streams
7  
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 has indent_by => (is => 'ro', default => ' ');
11 1 has max_width => (is => 'ro', default => 78);
12  
13 5277 ▶sub _join {
Subroutine _join: called CC=1 SCAR=0.0
14 5277 shift;
15 5277 return SQL::Abstract::Parts::stringify(\@_);
16 }
17  
18 962 ▶sub format {
Subroutine format: called CC=1 SCAR=0.0
Pod undocumented
19 962 my ($self, $join, @parts) = @_;
20 962 $self->_fold_sql('', '', @{$self->_simplify($join, @parts)});
21 }
22  
23 12404 ▶sub _simplify {
Subroutine _simplify: called CC=6 SCAR=18.2
24 12404 my ($self, $join, @parts) = @_;
25 12404 ▶ return '' unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 12404 12404
26 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
27 12404 ▶ return $self->_simplify(@{$parts[0]}) if @parts == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parts == 1 1154 11250 12404
28 11250 ▶ return [ $join, map ref() ? $self->_simplify(@$_) : $_, @parts ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 10288 16827 27115
29 }
30  
31 3176 ▶sub _fold_sql {
Subroutine _fold_sql: called CC=21 SCAR=30.5
32 3176 my ($self, $indent0, $indent, $join, @parts) = @_;
33 3176 my @res;
34 3176 my $w = $self->max_width;
35 3176 my $join_len = 0;
36 (s/, \z/,\n/ and $join_len = 1)
37 or s/\A /\n/
38 or $_ = "\n"
39 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
40 3176 my ($nl_pre, $nl_post) = split "\n", $line_join, -1;
41 3176 my $line = $indent0;
42 3176 my ($lead0) = $indent0 =~ /\A(\s*)/;
43 3176 my $fresh = 1; # $line holds no part of this level yet
44 3176 my $line_proto = $indent.$nl_post;
45 3176 PART: foreach my $idx (0..$#parts) {
46 8017 my $p = $parts[$idx];
47 8017 my $last = $idx == $#parts;
48 8017 ▶ my $pre = $fresh ? '' : $join;
Branch: 1 branch
BranchTrueFalseTotal
$fresh ? : 3692 4325 8017
49 8017 ▶ my $j_part = $pre.(my $j = ref($p) ? $self->_join(@$p) : $p);
Branch: 1 branch
BranchTrueFalseTotal
ref $p ? : 5277 2740 8017
50 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
51 4204 $line .= $j_part;
52 4204 $fresh = 0;
53 4204 next PART;
54 }
55 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
56 967 my ($lead) = $line =~ /\A(\s*)/;
57 967 my $innerdent = $lead.$self->indent_by;
58 967 push @res, $line.$pre.'('."\n";
59 967 my @inner = @{$p}[2 .. $#$p - 1];
60 my $folded = $self->_fold_sql($innerdent, $innerdent,
61 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
62 );
63 967 push @res, $folded."\n";
64 967 $line = $lead.')';
65 967 $fresh = 0;
66 967 next PART;
67 }
68 2846 ▶ my $innerdent = (@res ? $indent : $lead0).$self->indent_by;
Branch: 1 branch
BranchTrueFalseTotal
@res ? : 706 2140 2846
69 2846 ▶ my $start = $fresh ? $line : $line_proto;
Branch: 1 branch
BranchTrueFalseTotal
$fresh ? : 709 2137 2846
70 2846 ▶ push @res, $line.$nl_pre."\n" unless $fresh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fresh 2137 709 2846
71 2846 $line = $start.$j;
72 2846 $fresh = 0;
73 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
74 1247 my $folded = $self->_fold_sql($start, $innerdent, @$p);
75 1247 ▶ push @res, $folded.($last ? '' : $nl_pre."\n");
Branch: 1 branch
BranchTrueFalseTotal
$last ? : 731 516 1247
76 1247 ▶ $line = $last ? '' : $line_proto;
Branch: 1 branch
BranchTrueFalseTotal
$last ? : 731 516 1247
77 1247 $fresh = 1;
78 }
79 3176 return join '', @res, $line;
80 }
81  
82 1 1;
« SQL/Abstract/Test.pm ↑ Summary SQL/Abstract/Plugin/BangOverrides.pm »