Tidy/Node.pm

filter: Statementstmt 98.6% 145 / 147 Branchbran 90.3% 47 / 52 Conditioncond 93.7% 30 / 32 MC/DCmcdc 90.0% 18 / 20 Subroutinesub 100% 43 / 43 Podpod 0.0% 0 / 2 totaltotal 95.6% 283 / 296 CC 54 SCAR 14.8
CC
54
Cov
96%
fix_lines
10.0
_move_columns
8.0
fix_lines
8.0
CRAP 4.4
« Tidy/Config.pm ↑ Summary
1 # ABSTRACT: yamltidy parse tree element
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use experimental qw/ signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package YAML::Tidy::Node;
8  
9 8 our $VERSION = '0.007'; # VERSION
10 8 ▶use overload '""' => \&_stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 ▶sub new($class, %args) {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
13 31844 my $self = {
14 %args,
15 };
16 31844 return bless $self, $class;
17 }
18  
19 ▶sub _stringify($self, @args) {
Subroutine _stringify: called CC=3 SCAR=11.0
20 46370 ▶ my $type = $self->{type} // '';
Condition100%2/2
$self->{"type"} // ""
l!l
448721498
MC/DC100%1/1
$self->{"type"} // ""
$self->{"type"}
Truth table100%2/2
A
A= $self->{"type"}
Aresult
11
00
21 46370 my $str = "($type)";
22 46370 ▶ if ($self->is_collection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_collection) { } 44872 1498 46370
23 44872 my $open = $self->open;
24 44872 my $close = $self->close;
25 $str .= sprintf " <L %d C %d> <L %d C %d> - <L %d C %d> <L %d C %d>",
26 $open->{start}->{line}, $open->{start}->{column},
27 $open->{end}->{line}, $open->{end}->{column},
28 $close->{start}->{line}, $close->{start}->{column},
29 $close->{end}->{line}, $close->{end}->{column},
30 44872 }
31 else {
32 1498 my $val = substr($self->{value}, 0, 20);
33 1498 local $Data::Dumper::Useqq = 1;
34 1498 $val = Data::Dumper->Dump([$val], ['val']);
35 1498 chomp $val;
36 $str .= sprintf " <L %d C %d> - <L %d C %d> | %s",
37 $self->start->{line}, $self->start->{column},
38 1498 $self->end->{line}, $self->end->{column}, $val
39 }
40 46370 return $str;
41 }
42  
43 ▶sub is_mapping_value($node, $parent) {
Subroutine is_mapping_value: called CC=2 SCAR=6.9
Pod undocumented
44 2359 ▶ return ($parent->{type} eq 'MAP' and not $node->{index} % 2);
Condition100%3/3
$parent->{'type'} eq 'MAP' && !($node->{'index'} % 2)
!ll&&!rl&&r
771839749
MC/DC100%2/2
$parent->{'type'} eq 'MAP' && !($node->{'index'} % 2)
$parent->{'type'} eq 'MAP' !($node->{'index'} % 2)
Truth table100%3/3
A && not(B)
A= $parent->{'type'} eq 'MAP'B= !($node->{'index'} % 2)
ABresult
0X0
100
111
45 }
46  
47 package YAML::Tidy::Node::Collection;
48 8 ▶use constant DEBUG => $ENV{YAML_TIDY_DEBUG} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'YAML_TIDY_DEBUG'} ? : 0 8 8
49  
50 8 ▶use base 'YAML::Tidy::Node';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 67903 ▶sub is_collection { 1 }
Subroutine is_collection: called CC=1 SCAR=0.0
53  
54 ▶sub indent($self) {
Subroutine indent: called CC=2 SCAR=6.9
55 35911 my $firstevent = $self->open;
56 35911 ▶ if ($firstevent->{name} eq 'document_start_event') {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstevent->{"name"} eq "document_start_event") 7460 28451 35911
57 7460 return 0;
58 }
59 28451 my $startcol = $firstevent->{end}->{column};
60 28451 return $startcol;
61 }
62  
63 131939 ▶sub open($self) { $self->{start} }
Subroutine open: called CC=1 SCAR=0.0
64 76146 ▶sub close($self) { $self->{end} }
Subroutine close: called CC=1 SCAR=0.0
65  
66 ▶sub sibling($self, $node, $diff) {
Subroutine sibling: called CC=5 SCAR=19.1
67 749 ▶ return unless $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless $diff 0 749 749
68 749 my $children = $self->{children};
69 # double check if $node is child of $parent
70 749 ▶ unless ("$children->[ $node->{index} - 1 ]" eq "$node") {
Branch: 1 branch
BranchTrueFalseTotal
unless (qq[$$children[$node->{"index"} - 1]] eq "$node") 0 749 749
71 0 die ">$node< is not a child of >$self<";
72 }
73 749 my $new_index = $node->{index} - 1 + $diff;
74 749 ▶ if ($new_index < 0 or $new_index > $#$children) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_index < 0 or $new_index > $children->$#*) 0 749 749
Condition33%1/3
$new_index < 0 or $new_index > $children->$#*
l!l&&r!l&&!r
00749
MC/DC0%0/2
$new_index < 0 or $new_index > $children->$#*
$new_index < 0 $new_index > $children->$#*
Truth table33%1/3
A or B
A= $new_index < 0B= $new_index > $children->$#*
ABresult
1X1
011
000
75 0 die "$new_index out of range for '$self'";
76 }
77 749 return $children->[ $new_index ];
78 }
79  
80  
81 ▶sub end($self) {
Subroutine end: called CC=1 SCAR=0.0
82 13954 return $self->close->{end};
83 }
84  
85 ▶sub realendline($self) {
Subroutine realendline: called CC=1 SCAR=0.0
86 588 $self->close->{end}->{line} - 1;
87 }
88  
89 ▶sub start($self) {
Subroutine start: called CC=1 SCAR=0.0
90 18828 return $self->open->{start};
91 }
92  
93 ▶sub line($self) {
Subroutine line: called CC=1 SCAR=0.0
94  
95 10917 my $contentstart = $self->contentstart;
96 10917 return $contentstart->{line};
97 }
98  
99 ▶sub contentstart($self) {
Subroutine contentstart: called CC=1 SCAR=0.0
100 10917 my $firstevent = $self->open;
101 10917 return $firstevent->{end};
102 }
103  
104 ▶sub fix_node_indent($self, $fix) {
Subroutine fix_node_indent: called CC=4 SCAR=13.9
105 786 for my $e ($self->open, $self->close) {
106 1572 for my $pos (@$e{qw/ start end /}) {
107 3144 $pos->{column} += $fix;
108 }
109 }
110 786 for my $c (@{ $self->{children} }) {
111 1931 $c->fix_node_indent($fix);
112 }
113 }
114  
115 ▶sub _move_columns($self, $line, $offset, $fix) {
Subroutine _move_columns: called CC=8 SCAR=20.8
116 # warn __PACKAGE__.':'.__LINE__.": _move_columns $self $line $offset $fix\n";
117 6629 ▶ return if $self->end->{line} < $line;
Branch: 1 branch
BranchTrueFalseTotal
if $self->end->{"line"} < $line 746 5883 6629
118 5883 ▶ return if $self->start->{line} > $line;
Branch: 1 branch
BranchTrueFalseTotal
if $self->start->{"line"} > $line 909 4974 5883
119 4974 for my $e ($self->open, $self->close) {
120 9948 for my $pos (@$e{qw/ start end /}) {
121 19896 ▶ if ($pos->{line} == $line and $pos->{column} >= $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos->{"line"} == $line and $pos->{"column"} >= $offset) 770 19126 19896
Condition100%3/3
$pos->{"line"} == $line and $pos->{"column"} >= $offset
!ll&&!rl&&r
158343292770
MC/DC100%2/2
$pos->{"line"} == $line and $pos->{"column"} >= $offset
$pos->{"line"} == $line $pos->{"column"} >= $offset
Truth table100%3/3
A and B
A= $pos->{"line"} == $lineB= $pos->{"column"} >= $offset
ABresult
0X0
100
111
122 770 $pos->{column} += $fix;
123 }
124 }
125 }
126 4974 for my $c (@{ $self->{children} }) {
127 13576 $c->_move_columns($line, $offset, $fix);
128 }
129 }
130  
131  
132 ▶sub _fix_flow_indent($self, %args) {
Subroutine _fix_flow_indent: called CC=4 SCAR=13.9
133 # warn __PACKAGE__.':'.__LINE__.": ========== _fix_flow_indent l=$line diff=$diff\n";
134 3668 my $line = $args{line};
135 3668 my $diff = $args{diff};
136 3668 my $start = $self->open;
137 3668 my $end = $self->close;
138 3668 for my $pos ($start->{start}, $start->{end}, $end->{start}, $end->{end}) {
139 14672 ▶ if ($pos->{line} == $line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos->{"line"} == $line) 3207 11465 14672
140 3207 $pos->{column} += $diff;
141 }
142 }
143 3668 for my $c (@{ $self->{children} }) {
144 7790 $c->_fix_flow_indent(%args);
145 }
146 }
147  
148 ▶sub fix_lines($self, $startline, $diff) {
Subroutine fix_lines: called CC=8 SCAR=20.8
149 3957 my $start = $self->open;
150 3957 DEBUG and warn __PACKAGE__.':'.__LINE__.": ======== fix_lines $startline $diff ($self)\n";
151 3957 my $end = $self->close;
152 3957 for my $pos ($start->{start}, $start->{end}) {
153 7914 ▶ if ($pos->{line} >= $startline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos->{"line"} >= $startline) 3336 4578 7914
154 3336 $pos->{line} += $diff;
155 }
156 }
157 3957 for my $pos ($end->{start}, $end->{end}) {
158 7914 ▶ if ($pos->{column} == 0 and $pos->{line} == $startline) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pos->{'column'} == 0 and $pos->{'line'} == $startline) { } 2922 4992 7914
elsif ($pos->{'line'} >= $startline) { } 3846 1146 4992
Condition100%3/3
$pos->{"column"} == 0 and $pos->{"line"} == $startline
!ll&&!rl&&r
106239302922
MC/DC100%2/2
$pos->{"column"} == 0 and $pos->{"line"} == $startline
$pos->{"column"} == 0 $pos->{"line"} == $startline
Truth table100%3/3
A and B
A= $pos->{"column"} == 0B= $pos->{"line"} == $startline
ABresult
0X0
100
111
159 }
160 elsif ($pos->{line} >= $startline) {
161 3846 $pos->{line} += $diff;
162 }
163 }
164 3957 for my $c (@{ $self->{children} }) {
165 7476 $c->fix_lines($startline, $diff);
166 }
167 }
168  
169 package YAML::Tidy::Node::Scalar;
170 8 ▶use constant DEBUG => $ENV{YAML_TIDY_DEBUG} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'YAML_TIDY_DEBUG'} ? : 0 8 8
171 8 use YAML::PP::Common qw/
172 YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE
173 YAML_DOUBLE_QUOTED_SCALAR_STYLE YAML_LITERAL_SCALAR_STYLE
174 YAML_FOLDED_SCALAR_STYLE
175 YAML_FLOW_SEQUENCE_STYLE YAML_FLOW_MAPPING_STYLE
176 8 ▶/;
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 8 ▶use base 'YAML::Tidy::Node';
Subroutine BEGIN: called CC=1 SCAR=0.0
179  
180 29217 ▶sub is_collection { 0 }
Subroutine is_collection: called CC=1 SCAR=0.0
181  
182 ▶sub is_quoted($self) {
Subroutine is_quoted: called CC=1 SCAR=0.0
183 707 return $self->{style} ne YAML_PLAIN_SCALAR_STYLE
184 }
185  
186 ▶sub indent($self) {
Subroutine indent: called CC=1 SCAR=0.0
187 18207 return $self->open->{column};
188 }
189  
190 ▶sub start($self) {
Subroutine start: called CC=1 SCAR=0.0
191 42551 return $self->open;
192 }
193  
194 133655 ▶sub open($self) { $self->{start} }
Subroutine open: called CC=1 SCAR=0.0
195 106228 ▶sub close($self) { $self->{end} }
Subroutine close: called CC=1 SCAR=0.0
196  
197 ▶sub end($self) {
Subroutine end: called CC=1 SCAR=0.0
198 21659 return $self->close;
199 }
200  
201 ▶sub realendline($self) {
Subroutine realendline: called CC=4 SCAR=13.9
202 13114 my $end = $self->close;
203 13114 ▶ if ($self->{style} == YAML_LITERAL_SCALAR_STYLE
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"style"} == 4 or $self->{"style"} == 5) 2494 10620 13114
Condition100%3/3
$self->{"style"} == 4 or $self->{"style"} == 5
l!l&&r!l&&!r
1421107310620
MC/DC100%2/2
$self->{"style"} == 4 or $self->{"style"} == 5
$self->{"style"} == 4 $self->{"style"} == 5
Truth table100%3/3
A or B
A= $self->{"style"} == 4B= $self->{"style"} == 5
ABresult
1X1
011
000
204 or $self->{style} == YAML_FOLDED_SCALAR_STYLE) {
205 2494 ▶ if ($end->{column} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end->{"column"} == 0) 2414 80 2494
206 2414 return $end->{line} - 1;
207 }
208 }
209 10700 $end->{line};
210 }
211  
212 ▶sub line($self) {
Subroutine line: called CC=1 SCAR=0.0
213 20670 my $contentstart = $self->contentstart;
214 20670 return $contentstart->{line};
215 }
216  
217 ▶sub contentstart($self) {
Subroutine contentstart: called CC=1 SCAR=0.0
218 20670 return $self->start;
219 }
220  
221 ▶sub multiline($self) {
Subroutine multiline: called CC=2 SCAR=6.9
222 22922 ▶ if ($self->open->{line} < $self->close->{line}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->open->{"line"} < $self->close->{"line"}) 2930 19992 22922
223 2930 return 1;
224 }
225 19992 return 0;
226 }
227  
228 ▶sub empty_scalar($self) {
Subroutine empty_scalar: called CC=3 SCAR=11.0
229 23549 my ($start, $end) = ($self->open, $self->close);
230 23549 ▶ if ($start->{line} == $end->{line} and $start->{column} == $end->{column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start->{"line"} == $end->{"line"} and $start->{"column"} == $end->{"column"}) 551 22998 23549
Condition100%3/3
$start->{"line"} == $end->{"line"} and $start->{"column"} == $end->{"column"}
!ll&&!rl&&r
318319815551
MC/DC100%2/2
$start->{"line"} == $end->{"line"} and $start->{"column"} == $end->{"column"}
$start->{"line"} == $end->{"line"} $start->{"column"} == $end->{"column"}
Truth table100%3/3
A and B
A= $start->{"line"} == $end->{"line"}B= $start->{"column"} == $end->{"column"}
ABresult
0X0
100
111
231 551 return 1;
232 }
233 22998 return 0;
234 }
235  
236 ▶sub fix_node_indent($self, $fix) {
Subroutine fix_node_indent: called CC=2 SCAR=6.9
237 1733 for my $pos ($self->open, $self->close) {
238 3466 $pos->{column} += $fix;
239 }
240 }
241  
242 ▶sub _move_columns($self, $line, $offset, $fix) {
Subroutine _move_columns: called CC=6 SCAR=17.9
243 # warn __PACKAGE__.':'.__LINE__.": _move_columns $self $line $offset $fix\n";
244 8266 ▶ return if $self->end->{line} < $line;
Branch: 1 branch
BranchTrueFalseTotal
if $self->end->{"line"} < $line 2964 5302 8266
245 5302 ▶ return if $self->start->{line} > $line;
Branch: 1 branch
BranchTrueFalseTotal
if $self->start->{"line"} > $line 2623 2679 5302
246 2679 for my $pos ($self->open, $self->close) {
247 5358 ▶ if ($pos->{line} == $line and $pos->{column} >= $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos->{"line"} == $line and $pos->{"column"} >= $offset) 1402 3956 5358
Condition100%3/3
$pos->{"line"} == $line and $pos->{"column"} >= $offset
!ll&&!rl&&r
15438021402
MC/DC100%2/2
$pos->{"line"} == $line and $pos->{"column"} >= $offset
$pos->{"line"} == $line $pos->{"column"} >= $offset
Truth table100%3/3
A and B
A= $pos->{"line"} == $lineB= $pos->{"column"} >= $offset
ABresult
0X0
100
111
248 1402 $pos->{column} += $fix;
249 }
250 }
251 }
252  
253 ▶sub _fix_flow_indent($self, %args) {
Subroutine _fix_flow_indent: called CC=3 SCAR=11.0
254 5236 my $line = $args{line};
255 5236 my $diff = $args{diff};
256 # warn __PACKAGE__.':'.__LINE__.": ========== _fix_flow_indent l=$line diff=$diff\n";
257 5236 for my $pos ($self->open, $self->close) {
258 10472 ▶ if ($pos->{line} == $line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos->{"line"} == $line) 2989 7483 10472
259 2989 $pos->{column} += $diff;
260 }
261 }
262 }
263  
264 ▶sub fix_lines($self, $startline, $diff) {
Subroutine fix_lines: called CC=10 SCAR=23.0
265 4500 DEBUG and warn __PACKAGE__.':'.__LINE__.": ======== fix_lines $startline $diff ($self)\n";
266 4500 for my $pos ($self->open) {
267 4500 ▶ if ($self->empty_scalar and $pos->{column} == 0 and $pos->{line} == $startline) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->empty_scalar and $pos->{'column'} == 0 and $pos->{'line'} == $startline) { } 15 4485 4500
elsif ($pos->{'line'} >= $startline) { } 2178 2307 4485
Condition100%6/6
$self->empty_scalar and $pos->{"column"} == 0 and $pos->{"line"} == $startline
!ll&&!rl&&r
43988715
$self->empty_scalar and $pos->{"column"} == 0
!ll&&!rl&&r
436236102
MC/DC100%3/3
$self->empty_scalar and $pos->{"column"} == 0 and $pos->{"line"} == $startline
$self->empty_scalar $pos->{"column"} == 0 $pos->{"line"} == $startline
Truth table100%4/4
A and B and C
A= $self->empty_scalarB= $pos->{"column"} == 0C= $pos->{"line"} == $startline
ABCresult
0XX0
10X0
1100
1111
268 }
269 elsif ($pos->{line} >= $startline) {
270 2178 $pos->{line} += $diff;
271 }
272 }
273 4500 for my $pos ($self->close) {
274 4500 ▶ if ($pos->{column} == 0 and $pos->{line} == $startline) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pos->{'column'} == 0 and $pos->{'line'} == $startline) { } 84 4416 4500
elsif ($pos->{'line'} >= $startline) { } 2178 2238 4416
Condition100%3/3
$pos->{"column"} == 0 and $pos->{"line"} == $startline
!ll&&!rl&&r
409532184
MC/DC100%2/2
$pos->{"column"} == 0 and $pos->{"line"} == $startline
$pos->{"column"} == 0 $pos->{"line"} == $startline
Truth table100%3/3
A and B
A= $pos->{"column"} == 0B= $pos->{"line"} == $startline
ABresult
0X0
100
111
275 }
276 elsif ($pos->{line} >= $startline) {
277 2178 $pos->{line} += $diff;
278 }
279 }
280 }
281  
282 8 1;
« Tidy/Config.pm ↑ Summary