blib/lib/XML/Struct/Simple.pm

filter: Statementstmt 100% 68 / 68 Branchbran 91.6% 33 / 36 Conditioncond 85.7% 18 / 21 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 94.1% 144 / 153 SCAR 33.0
CC
27
Cov
95%
transform_content
18.0
transform
4.0
__ANON__
3.1
CRAP 27.1
« 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.26';
8  
9 has root => (
10 is => 'rw',
11 8 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
12 6 );
13  
14 has attributes => (
15 is => 'rw',
16 13 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 40 ▶ 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" 40 0 40
Condition83%5/6
!defined($_[0]) || $_[0] && $_[0] ne 'remove'
l!l&&r!l&&!r
0373
$_[0] and $_[0] ne "remove"
!ll&&!rl&&r
2137
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 15 ▶ default => sub { 'content' },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 6 );
24  
25 has depth => (
26 is => 'rw',
27 28 ▶ 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 21 28
Condition66%2/3
defined $_[0] and $_[0] >= 0
!ll&&!rl&&r
2107
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 40 ▶ my ($self, $element) = @_;
Subroutine transform: called CC=4 SCAR=13.9
Pod documented
32
33 40 my $simple = $self->transform_content($element,0);
34  
35 # enforce root for special case <root>text</root>
36 40 ▶ if ($self->root or !ref $simple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->root or not ref $simple) { } 23 17 40
Condition100%3/3
$self->root or not ref $simple
l!l&&r!l&&!r
22117
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 23 ▶ my $root = $self->root !~ /^[+-]?[0-9]+$/ ? $self->root : $element->[0];
Branch: 1 branch
BranchTrueFalseTotal
!($self->root =~ /^[+-]?[0-9]+$/) ? : 10 13 23
38 23 return { $root => $simple };
39 } else {
40 17 return $simple;
41 }
42 }
43  
44 # returns a (possibly empty) hash or a scalar
45 sub transform_content {
46 121 ▶ my ($self, $element, $depth) = @_;
Subroutine transform_content: called CC=18 SCAR=28.9
Pod documented
47 121 ▶ $depth = 0 if !defined $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 0 121 121
48  
49 121 ▶ if (defined $self->depth and $depth >= $self->depth) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->depth and $depth >= $self->depth) { } 14 107 121
elsif (@$element == 1) { } 2 105 107
Condition100%3/3
defined $self->depth and $depth >= $self->depth
!ll&&!rl&&r
99814
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 2 return { };
53 }
54  
55 105 my $attributes = {};
56 105 my $children;
57  
58 105 ▶ if ( reftype $element->[1] eq 'HASH' ) { # [ $tag, \%attributes, \@children ]
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($element->[1]) eq 'HASH') { } 97 8 105
59 97 ▶ $attributes = $element->[1] if $self->attributes;
Branch: 1 branch
BranchTrueFalseTotal
if $self->attributes 93 4 97
60 97 $children = $element->[2];
61 } else { # [ $tag, \@children ]
62 8 $children = $element->[1];
63 }
64
65 # no element children
66 105 ▶ unless ( first { ref $_ } @$children ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&first(sub { ref $_; } , @$children)) 57 48 105
67 57 my $content = join "", @$children;
68 57 ▶ if ($content eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($content eq '') { } 20 37 57
elsif (not %$attributes) { } 32 5 37
69 20 return { %$attributes };
70 } elsif (!%$attributes) {
71 32 return $content;
72 } else {
73 5 return { %$attributes, $self->content => $content };
74 }
75 }
76  
77 48 my $simple = { map {$_ => [$attributes->{$_}] } keys %$attributes };
78  
79 48 foreach my $child ( @$children ) {
80 89 ▶ next unless ref $child; # skip mixed content text
Branch: 1 branch
BranchTrueFalseTotal
unless ref $child 8 81 89
81  
82 81 my $name = $child->[0];
83 81 my $content = $self->transform_content($child, $depth+1);
84  
85 81 ▶ if ( $simple->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($simple->{$name}) { } 14 67 81
86 14 push @{$simple->{$name}}, $content;
87 } else {
88 67 $simple->{$name} = [$content];
89 }
90 }
91  
92 48 foreach my $name (keys %$simple) {
93 73 ▶ next if @{$simple->{$name}} != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$simple->{$name};} != 1 13 60 73
94 60 my $c = $simple->{$name}->[0];
95 60 ▶ 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") 52 8 60
Condition83%5/6
not ref $c or not builtin::blessed($c) and builtin::reftype($c) eq "HASH"
l!l&&r!l&&!r
27258
not builtin::blessed($c) and builtin::reftype($c) eq "HASH"
!ll&&!rl&&r
0825
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 52 $simple->{$name} = $c;
97 }
98 }
99  
100 48 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 »