blib/lib/XML/Struct/Simple.pm

filter: Statementstmt 98.5% 67 / 68 Branchbran 88.8% 32 / 36 Conditioncond 85.7% 18 / 21 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 92.8% 142 / 153 SCAR 33.0
CC
27
Cov
94%
transform_content
18.1
transform
4.0
__ANON__
3.1
CRAP 27.2
« blib/lib/XML/Struct/Writer.pm ↑ Summary blib/lib/XML/Struct/Writer/Stream.pm »
1 package XML::Struct::Simple;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '0.25';
8  
9 has root => (
10 is => 'rw',
11 6 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
12 6 );
13  
14 has attributes => (
15 is => 'rw',
16 10 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 35 ▶ coerce => sub { !defined $_[0] or ($_[0] and $_[0] ne 'remove') },
Subroutine __ANON__: called CC=3 SCAR=11.3
Branch: 1 branch
BranchTrueFalseTotal
not defined $_[0] or $_[0] and $_[0] ne "remove" 35 0 35
Condition83%5/6
!defined($_[0]) || $_[0] && $_[0] ne 'remove'
l!l&&r!l&&!r
0323
$_[0] and $_[0] ne "remove"
!ll&&!rl&&r
2132
MC/DC66%2/3
!defined($_[0]) || $_[0] && $_[0] ne 'remove'
!defined($_[0]) $_[0] $_[0] ne "remove"
Truth table75%3/4
not(A) || B and C
A= !defined($_[0])B= $_[0]C= $_[0] ne "remove"
ABCresult
1XX1
0111
00X0
0100
18 6 );
19  
20 has content => (
21 is => 'rw',
22 12 ▶ default => sub { 'content' },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 6 );
24  
25 has depth => (
26 is => 'rw',
27 26 ▶ coerce => sub { (defined $_[0] and $_[0] >= 0) ? $_[0] : undef },
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && $_[0] >= 0 ? : 7 19 26
Condition66%2/3
defined $_[0] and $_[0] >= 0
!ll&&!rl&&r
1907
MC/DC50%1/2
defined $_[0] and $_[0] >= 0
defined $_[0] $_[0] >= 0
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] >= 0
ABresult
0X0
100
111
28 6 );
29  
30 sub transform {
31 35 ▶ my ($self, $element) = @_;
Subroutine transform: called CC=4 SCAR=13.9
Pod documented
32
33 35 my $simple = $self->transform_content($element,0);
34  
35 # enforce root for special case <root>text</root>
36 35 ▶ if ($self->root or !ref $simple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->root or not ref $simple) { } 20 15 35
Condition100%3/3
$self->root or not ref $simple
l!l&&r!l&&!r
19115
MC/DC100%2/2
$self->root or not ref $simple
$self->root not ref $simple
Truth table100%3/3
A or not(B)
A= $self->rootB= not ref $simple
ABresult
1X1
011
000
37 20 ▶ my $root = $self->root !~ /^[+-]?[0-9]+$/ ? $self->root : $element->[0];
Branch: 1 branch
BranchTrueFalseTotal
!($self->root =~ /^[+-]?[0-9]+$/) ? : 10 10 20
38 20 return { $root => $simple };
39 } else {
40 15 return $simple;
41 }
42 }
43  
44 # returns a (possibly empty) hash or a scalar
45 sub transform_content {
46 113 ▶ my ($self, $element, $depth) = @_;
Subroutine transform_content: called CC=18 SCAR=28.9
Pod documented
47 113 ▶ $depth = 0 if !defined $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 0 113 113
48  
49 113 ▶ if (defined $self->depth and $depth >= $self->depth) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->depth and $depth >= $self->depth) { } 14 99 113
elsif (@$element == 1) { } 0 99 99
Condition100%3/3
defined $self->depth and $depth >= $self->depth
!ll&&!rl&&r
91814
MC/DC100%2/2
defined $self->depth and $depth >= $self->depth
defined $self->depth $depth >= $self->depth
Truth table100%3/3
A and B
A= defined $self->depthB= $depth >= $self->depth
ABresult
0X0
100
111
50 14 return $element;
51 } elsif ( @$element == 1 ) { # empty tag
52 0 return { };
53 }
54  
55 99 my $attributes = {};
56 99 my $children;
57  
58 99 ▶ if ( reftype $element->[1] eq 'HASH' ) { # [ $tag, \%attributes, \@children ]
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($element->[1]) eq 'HASH') { } 93 6 99
59 93 ▶ $attributes = $element->[1] if $self->attributes;
Branch: 1 branch
BranchTrueFalseTotal
if $self->attributes 89 4 93
60 93 $children = $element->[2];
61 } else { # [ $tag, \@children ]
62 6 $children = $element->[1];
63 }
64
65 # no element children
66 99 ▶ unless ( first { ref $_ } @$children ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&first(sub { ref $_; } , @$children)) 54 45 99
67 54 my $content = join "", @$children;
68 54 ▶ if (!%$attributes) {
Branch: 2 branches
BranchTrueFalseTotal
if (not %$attributes) { } 39 15 54
elsif ($content eq '') { } 10 5 15
69 39 return $content;
70 } elsif ($content eq '') {
71 10 return { %$attributes };
72 } else {
73 5 return { %$attributes, $self->content => $content };
74 }
75 }
76  
77 45 my $simple = { map {$_ => [$attributes->{$_}] } keys %$attributes };
78  
79 45 foreach my $child ( @$children ) {
80 86 ▶ next unless ref $child; # skip mixed content text
Branch: 1 branch
BranchTrueFalseTotal
unless ref $child 8 78 86
81  
82 78 my $name = $child->[0];
83 78 my $content = $self->transform_content($child, $depth+1);
84  
85 78 ▶ if ( $simple->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($simple->{$name}) { } 14 64 78
86 14 push @{$simple->{$name}}, $content;
87 } else {
88 64 $simple->{$name} = [$content];
89 }
90 }
91  
92 45 foreach my $name (keys %$simple) {
93 70 ▶ next if @{$simple->{$name}} != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$simple->{$name};} != 1 13 57 70
94 57 my $c = $simple->{$name}->[0];
95 57 ▶ if (!ref $c or (!blessed $c and reftype $c eq 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $c or not builtin::blessed($c) and builtin::reftype($c) eq "HASH") 49 8 57
Condition83%5/6
not ref $c or not builtin::blessed($c) and builtin::reftype($c) eq "HASH"
l!l&&r!l&&!r
34158
not builtin::blessed($c) and builtin::reftype($c) eq "HASH"
!ll&&!rl&&r
0815
MC/DC66%2/3
not ref $c or not builtin::blessed($c) and builtin::reftype($c) eq "HASH"
not ref $c not builtin::blessed($c) builtin::reftype($c) eq "HASH"
Truth table75%3/4
not(A) or not(B) and C
A= not ref $cB= not builtin::blessed($c)C= builtin::reftype($c) eq "HASH"
ABCresult
1XX1
0111
00X0
0100
96 49 $simple->{$name} = $c;
97 }
98 }
99  
100 45 return $simple;
101 }
102  
103 sub removeXMLAttr {
104 4 ▶ my $node = shift;
Subroutine removeXMLAttr: called CC=3 SCAR=11.0
Pod documented
105 ref $node
106 ? ( $node->[2]
107 4 ▶ ? [ $node->[0], [ map { removeXMLAttr($_) } @{$node->[2]} ] ]
Branch: 2 branches
BranchTrueFalseTotal
ref $node ? : 2 2 4
$node->[2] ? : 2 0 2
108 : [ $node->[0] ] ) # empty element
109 : $node; # text node
110 }
111  
112  
113 6 1;
114 __END__
« blib/lib/XML/Struct/Writer.pm ↑ Summary blib/lib/XML/Struct/Writer/Stream.pm »