Indent/Block.pm

filter: Statementstmt 79.7% 59 / 74 Branchbran 66.6% 16 / 24 Conditioncond 33.3% 4 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 70.8% 90 / 127 SCAR 31.6
CC
17
Cov
72%
_compare
33.0
indent
9.1
BEGIN
1.0
CRAP 23.5
« Indent/String.pm ↑ Summary Indent/Word.pm »
1 package Indent::Block;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Indent::Utils qw(line_size_check string_len);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Constants.
11 4 Readonly::Scalar my $EMPTY_STR => q{};
12 4 Readonly::Scalar my $LINE_SIZE => 79;
13  
14 4 our $VERSION = 0.08;
15  
16 # Constructor.
17 sub new {
18 5 ▶ my ($class, @params) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
19 5 my $self = bless {}, $class;
20  
21 # Line size.
22 5 $self->{'line_size'} = $LINE_SIZE;
23  
24 # Next indent.
25 5 $self->{'next_indent'} = "\t";
26  
27 # Output.
28 5 $self->{'output_separator'} = "\n";
29  
30 # Strict mode - without white space optimalization.
31 5 $self->{'strict'} = 1;
32  
33 # Process params.
34 5 set_params($self, @params);
35  
36 # 'line_size' check.
37 3 line_size_check($self);
38  
39 # Save current piece.
40 3 $self->{'_current'} = $EMPTY_STR;
41  
42 # Object.
43 3 return $self;
44 }
45  
46 # Parses tag to indented data.
47 sub indent {
48 5 ▶ my ($self, $data_ar, $act_indent, $non_indent) = @_;
Subroutine indent: called CC=9 SCAR=22.1
Pod documented
49  
50 # Undef indent.
51 5 ▶ if (! $act_indent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($act_indent) 4 1 5
52 4 $act_indent = $EMPTY_STR;
53 }
54  
55 # Input data.
56 5 my @input = @{$data_ar};
57  
58 # If non_indent data, than return.
59 5 ▶ if ($non_indent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($non_indent) 1 4 5
60 1 return $act_indent.join($EMPTY_STR, @input);
61 }
62  
63 # Indent.
64 4 my @data = ();
65 4 my ($first, $second);
66 4 $first = shift @input;
67 4 my $tmp_indent = $act_indent;
68 4 ▶ while (@input) {
Branch: 1 branch
BranchTrueFalseTotal
if (@input) 4 4 8
69 4 $second = shift @input;
70 4 ▶ if ($self->_compare($first, $second, $tmp_indent)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_compare($first, $second, $tmp_indent)) { } 2 2 4
71 2 push @data, $self->{'_current'};
72 2 $first = $second;
73 2 $second = $EMPTY_STR;
74 2 $tmp_indent = $act_indent.$self->{'next_indent'};
75 } else {
76 2 $first .= $second;
77 }
78 }
79  
80 # Add other data to @data array.
81 4 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 4 0 4
82  
83 # White space optimalization.
84 4 ▶ if (! $self->{'strict'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"strict"}) 0 4 4
85 0 $first =~ s/^\s*//ms;
86 0 $first =~ s/\s*$//ms;
87 }
88 4 ▶ if ($first ne $EMPTY_STR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first ne $EMPTY_STR) 4 0 4
89 4 push @data, $tmp_indent.$first;
90 }
91 }
92  
93 # Return as array or one line with output separator between its.
94 4 ▶ return wantarray ? @data : join($self->{'output_separator'}, @data);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 1 4
95 }
96  
97 # Compare strings with 'line_size' and save right current string.
98 sub _compare {
99 4 ▶ my ($self, $first, $second, $act_indent) = @_;
Subroutine _compare: called CC=9 SCAR=35.0
100  
101 # Without optimalization.
102 4 ▶ if ($self->{'strict'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'strict'}) { } 4 0 4
103 4 ▶ if (length $first > 0
Branch: 1 branch
BranchTrueFalseTotal
if (length $first > 0 and string_len($act_indent . $first) >= $self->{'line_size'} || string_len($act_indent . $first . $second) > $self->{'line_size'}) { } 2 2 4
Condition66%4/6
length $first > 0 and string_len($act_indent . $first) >= $self->{"line_size"} || string_len($act_indent . $first . $second) > $self->{"line_size"}
!ll&&!rl&&r
022
string_len($act_indent . $first) >= $self->{"line_size"} or string_len($act_indent . $first . $second) > $self->{"line_size"}
l!l&&r!l&&!r
202
MC/DC33%1/3
length $first > 0 and string_len($act_indent . $first) >= $self->{"line_size"} || string_len($act_indent . $first . $second) > $self->{"line_size"}
length $first > 0 string_len($act_indent . $first) >= $self->{"line_size"} string_len($act_indent . $first . $second) > $self->{"line_size"}
Truth table50%2/4
A and B or C
A= length $first > 0B= string_len($act_indent . $first) >= $self->{"line_size"}C= string_len($act_indent . $first . $second) > $self->{"line_size"}
ABCresult
0XX0
1000
11X1
1011
104 && (string_len($act_indent.$first)
105 >= $self->{'line_size'}
106 || string_len($act_indent.$first.$second)
107 > $self->{'line_size'})) {
108  
109 2 $self->{'_current'} = $act_indent.$first;
110 2 return 1;
111 } else {
112 2 return 0;
113 }
114  
115 # With optimalizaton.
116 # TODO Rewrite.
117 } else {
118 0 my $tmp1 = $first;
119 0 $tmp1 =~ s/^\s*//ms;
120 0 $tmp1 =~ s/\s*$//ms;
121 0 ▶ if (length $tmp1 > 0
Branch: 1 branch
BranchTrueFalseTotal
if (length $tmp1 > 0 and string_len($act_indent . $tmp1) >= $self->{'line_size'}) { } 0 0 0
Condition0%0/3
length $tmp1 > 0 and string_len($act_indent . $tmp1) >= $self->{"line_size"}
!ll&&!rl&&r
000
MC/DC0%0/2
length $tmp1 > 0 and string_len($act_indent . $tmp1) >= $self->{"line_size"}
length $tmp1 > 0 string_len($act_indent . $tmp1) >= $self->{"line_size"}
Truth table0%0/3
A and B
A= length $tmp1 > 0B= string_len($act_indent . $tmp1) >= $self->{"line_size"}
ABresult
0X0
100
111
122 && string_len($act_indent.$tmp1)
123 >= $self->{'line_size'}) {
124  
125 0 $self->{'_current'} = $act_indent.$tmp1;
126 0 return 1;
127 } else {
128 0 my $tmp2 = $first.$second;
129 0 $tmp2 =~ s/^\s*//ms;
130 0 $tmp2 =~ s/\s*$//ms;
131 0 ▶ if (length $tmp1 > 0
Branch: 1 branch
BranchTrueFalseTotal
if (length $tmp1 > 0 and string_len($act_indent . $tmp2) > $self->{'line_size'}) { } 0 0 0
Condition0%0/3
length $tmp1 > 0 and string_len($act_indent . $tmp2) > $self->{"line_size"}
!ll&&!rl&&r
000
MC/DC0%0/2
length $tmp1 > 0 and string_len($act_indent . $tmp2) > $self->{"line_size"}
length $tmp1 > 0 string_len($act_indent . $tmp2) > $self->{"line_size"}
Truth table0%0/3
A and B
A= length $tmp1 > 0B= string_len($act_indent . $tmp2) > $self->{"line_size"}
ABresult
0X0
100
111
132 && string_len($act_indent.$tmp2)
133 > $self->{'line_size'}) {
134  
135 0 $self->{'_current'} = $act_indent.$tmp1;
136 0 return 1;
137 } else {
138 0 return 0;
139 }
140 }
141 }
142 }
143  
144 4 1;
145  
146 __END__
« Indent/String.pm ↑ Summary Indent/Word.pm »