blib/lib/XML/LibXML/Iterator.pm

filter: Statementstmt 74.4% 105 / 141 Branchbran 60.2% 41 / 68 Conditioncond 40.0% 8 / 20 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 59.0% 13 / 22 Podpod 100% 14 / 14 totaltotal 65.4% 182 / 278 SCAR 47.5
CC
45
Cov
67%
iterator_function
20.0
default_iterator
14.1
nextNode
10.9
CRAP 116.1
↑ Summary blib/lib/XML/LibXML/NodeList/Iterator.pm »
1 #
2 #
3 package XML::LibXML::Iterator;
4 3 $XML::LibXML::Iterator::VERSION = '1.05';
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use XML::NodeFilter qw(:results);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 $VERSION = '1.04';
12  
13 use overload
14 0 ▶ '++' => sub { $_[0]->nextNode(); $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 0 ▶ '--' => sub { $_[0]->previousNode(); $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 0 ▶ '<>' => sub { return wantarray ? $_[0]->_get_all() : $_[0]->nextNode(); },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
17 3 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub new
20 {
21 7 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
22 7 my $node = shift;
23  
24 7 ▶ return undef unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 7 7
25  
26 7 my $self = bless {}, $class;
27  
28 7 $self->{FIRST} = $node;
29  
30 # $self->first;
31  
32 7 $self->{CURRENT} = undef;
33 7 $self->{INDEX} = -1;
34  
35 7 $self->{ITERATOR} = \&default_iterator;
36  
37 7 $self->{FILTERS} = [];
38  
39 7 return $self;
40 }
41  
42 sub iterator_function
43 {
44 0 ▶ my $self = shift;
Subroutine iterator_function: not called CC=4 SCAR=30.0
Pod documented
45 0 my $func = shift;
46  
47 0 ▶ return if defined $func and ref($func) ne "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if defined $func and ref $func ne "CODE" 0 0 0
Condition0%0/3
defined $func and ref $func ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $func and ref $func ne "CODE"
defined $func ref $func ne "CODE"
Truth table0%0/3
A and B
A= defined $funcB= ref $func ne "CODE"
ABresult
0X0
100
111
48  
49 0 $self->first;
50 0 ▶ if ( defined $func )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $func) { } 0 0 0
51 {
52 0 $self->{ITERATOR} = $func;
53 }
54 else
55 {
56 0 $self->{ITERATOR} = \&default_iterator;
57 }
58 }
59  
60 sub set_filter
61 {
62 0 ▶ my $self = shift;
Subroutine set_filter: not called CC=1 SCAR=6.9
Pod documented
63 0 $self->{FILTERS} = [@_];
64 }
65  
66 sub add_filter
67 {
68 0 ▶ my $self = shift;
Subroutine add_filter: not called CC=1 SCAR=6.9
Pod documented
69 0 push @{ $self->{FILTERS} }, @_;
70 }
71  
72 18 ▶sub current { return $_[0]->{CURRENT}; }
Subroutine current: called CC=1 SCAR=0.0
Pod documented
73 0 ▶sub index { return $_[0]->{INDEX}; }
Subroutine index: not called CC=1 SCAR=6.9
Pod documented
74  
75 20 ▶sub next { return $_[0]->nextNode(); }
Subroutine next: called CC=1 SCAR=0.0
Pod documented
76 0 ▶sub previous { return $_[0]->previousNode(); }
Subroutine previous: not called CC=1 SCAR=6.9
Pod documented
77  
78 sub nextNode
79 {
80 41 ▶ my $self = shift;
Subroutine nextNode: called CC=10 SCAR=23.9
Pod documented
81 41 my @filters = @{ $self->{FILTERS} };
82 41 my $node = undef;
83  
84 41 ▶ if ( $self->{INDEX} != -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'INDEX'} != -1) { } 36 5 41
85 {
86 36 my $fv = FILTER_SKIP;
87 36 ▶ unless ( scalar @filters > 0 )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @filters > 0) 36 0 36
88 {
89 36 $fv = FILTER_DECLINED;
90 }
91  
92 36 while (1)
93 {
94 36 $node = $self->{ITERATOR}->( $self, 1 );
95 36 ▶ last unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 6 30 36
96 30 foreach my $f (@filters)
97 {
98 0 $fv = $f->accept_node($node);
99 0 ▶ last if $fv;
Branch: 1 branch
BranchTrueFalseTotal
if $fv 0 0 0
100 }
101 30 ▶ last if $fv == FILTER_ACCEPT or $fv == FILTER_DECLINED;
Branch: 1 branch
BranchTrueFalseTotal
if $fv == 1 or $fv == 0 30 0 30
Condition33%1/3
$fv == 1 or $fv == 0
l!l&&r!l&&!r
0300
MC/DC0%0/2
$fv == 1 or $fv == 0
$fv == 1 $fv == 0
Truth table33%1/3
A or B
A= $fv == 1B= $fv == 0
ABresult
1X1
011
000
102 }
103 }
104 else
105 {
106 5 $node = $self->first();
107 }
108  
109 41 ▶ if ( defined $node )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node) 35 6 41
110 {
111 35 $self->{CURRENT} = $node;
112 35 ▶ if ( $node->isSameNode( $self->{FIRST} ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isSameNode($self->{'FIRST'})) { } 5 30 35
113 {
114 5 $self->{INDEX} = 0;
115 }
116 else
117 {
118 30 $self->{INDEX}++;
119 }
120 }
121  
122 41 return $node;
123 }
124  
125 sub previousNode
126 {
127 12 ▶ my $self = shift;
Subroutine previousNode: called CC=8 SCAR=22.1
Pod documented
128 12 my @filters = @{ $self->{FILTERS} };
129 12 my $node = undef;
130 12 my $fv = FILTER_SKIP;
131 12 ▶ unless ( scalar @filters > 0 )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @filters > 0) 12 0 12
132 {
133 12 $fv = FILTER_DECLINED;
134 }
135 12 while (1)
136 {
137 12 $node = $self->{ITERATOR}->( $self, -1 );
138 12 ▶ last unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 2 10 12
139 10 foreach my $f (@filters)
140 {
141 0 $fv = $f->accept_node($node);
142 0 ▶ last if $fv;
Branch: 1 branch
BranchTrueFalseTotal
if $fv 0 0 0
143 }
144 10 ▶ last if $fv == FILTER_ACCEPT or $fv == FILTER_DECLINED;
Branch: 1 branch
BranchTrueFalseTotal
if $fv == 1 or $fv == 0 10 0 10
Condition33%1/3
$fv == 1 or $fv == 0
l!l&&r!l&&!r
0100
MC/DC0%0/2
$fv == 1 or $fv == 0
$fv == 1 $fv == 0
Truth table33%1/3
A or B
A= $fv == 1B= $fv == 0
ABresult
1X1
011
000
145 }
146  
147 12 ▶ if ( defined $node )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node) 10 2 12
148 {
149 10 $self->{CURRENT} = $node;
150 10 $self->{INDEX}--;
151 }
152  
153 12 return $node;
154 }
155  
156 sub first
157 {
158 8 ▶ my $self = shift;
Subroutine first: called CC=7 SCAR=23.2
Pod documented
159 8 ▶ if ( scalar @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 8 8
160 {
161 0 $self->{FIRST} = shift;
162 }
163  
164 8 $self->{CURRENT} = $self->{FIRST};
165  
166 # this logic is required if the node is not allowed to be shown
167 8 ▶ my @filters = @{ $self->{FILTERS} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"FILTERS"} or [] 0 8 8
168 8 my $fv = FILTER_DECLINED;
169  
170 8 foreach my $f (@filters)
171 {
172 0 $fv = $f->accept_node( $self->{CURRENT} );
173 0 ▶ last if $fv;
Branch: 1 branch
BranchTrueFalseTotal
if $fv 0 0 0
174 }
175  
176 8 ▶ $fv ||= FILTER_ACCEPT;
Condition50%1/2
$fv ||= 1
l!l
08
MC/DC0%0/1
$fv ||= 1
$fv
Truth table50%1/2
A
A= $fv
Aresult
11
00
177  
178 8 ▶ unless ( $fv == FILTER_ACCEPT )
Branch: 1 branch
BranchTrueFalseTotal
unless ($fv == 1) 0 8 8
179 {
180 0 return undef;
181 }
182  
183 8 $self->{INDEX} = 0;
184 8 return $self->current;
185 }
186  
187 sub last
188 {
189 2 ▶ my $self = shift;
Subroutine last: called CC=2 SCAR=6.9
Pod documented
190 2 ▶ while ( $self->next ) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->next) 12 2 14
191 2 return $self->current;
192 }
193  
194 sub iterate
195 {
196 1 ▶ my $self = shift;
Subroutine iterate: called CC=4 SCAR=14.1
Pod documented
197 1 my $function = shift;
198 1 ▶ return unless defined $function and ref($function) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $function and ref $function eq "CODE" 0 1 1
Condition33%1/3
defined $function and ref $function eq "CODE"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $function and ref $function eq "CODE"
defined $function ref $function eq "CODE"
Truth table33%1/3
A and B
A= defined $functionB= ref $function eq "CODE"
ABresult
0X0
100
111
199 1 my $rv;
200 1 my $node = $self->first;
201 1 ▶ while ($node)
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 6 1 7
202 {
203 6 $rv = $function->( $self, $node );
204 6 $node = $self->next;
205 }
206 1 return $rv;
207 }
208  
209 sub default_iterator
210 {
211 48 ▶ my $self = shift;
Subroutine default_iterator: called CC=13 SCAR=26.5
Pod documented
212 48 my $dir = shift;
213 48 my $node = undef;
214  
215 48 ▶ if ( $dir < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($dir < 0) { } 12 36 48
216 {
217 return undef
218 if $self->{CURRENT}->isSameNode( $self->{FIRST} )
219 12 ▶ and $self->{INDEX} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"CURRENT"}->isSameNode($self->{"FIRST"}) and $self->{"INDEX"} <= 0 2 10 12
Condition66%2/3
$self->{"CURRENT"}->isSameNode($self->{"FIRST"}) and $self->{"INDEX"} <= 0
!ll&&!rl&&r
1002
MC/DC50%1/2
$self->{"CURRENT"}->isSameNode($self->{"FIRST"}) and $self->{"INDEX"} <= 0
$self->{"CURRENT"}->isSameNode($self->{"FIRST"}) $self->{"INDEX"} <= 0
Truth table66%2/3
A and B
A= $self->{"CURRENT"}->isSameNode($self->{"FIRST"})B= $self->{"INDEX"} <= 0
ABresult
0X0
100
111
220  
221 10 $node = $self->{CURRENT}->previousSibling;
222 10 ▶ return $self->{CURRENT}->parentNode unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 4 6 10
223  
224 6 ▶ while ( $node->hasChildNodes )
Branch: 1 branch
BranchTrueFalseTotal
if ($node->hasChildNodes) 0 6 6
225 {
226 0 $node = $node->lastChild;
227 }
228 }
229 else
230 {
231 36 ▶ if ( defined $self->{CURRENT} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'CURRENT'}) { } 36 0 36
232 {
233 return undef
234 if $self->{CURRENT}->isSameNode( $self->{FIRST} )
235 36 ▶ and $self->{INDEX} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"CURRENT"}->isSameNode($self->{"FIRST"}) and $self->{"INDEX"} > 0 0 36 36
Condition66%2/3
$self->{"CURRENT"}->isSameNode($self->{"FIRST"}) and $self->{"INDEX"} > 0
!ll&&!rl&&r
3060
MC/DC0%0/2
$self->{"CURRENT"}->isSameNode($self->{"FIRST"}) and $self->{"INDEX"} > 0
$self->{"CURRENT"}->isSameNode($self->{"FIRST"}) $self->{"INDEX"} > 0
Truth table66%2/3
A and B
A= $self->{"CURRENT"}->isSameNode($self->{"FIRST"})B= $self->{"INDEX"} > 0
ABresult
0X0
100
111
236  
237 36 ▶ if ( $self->{CURRENT}->hasChildNodes )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'CURRENT'}->hasChildNodes) { } 12 24 36
238 {
239 12 $node = $self->{CURRENT}->firstChild;
240 }
241 else
242 {
243 24 $node = $self->{CURRENT}->nextSibling;
244 24 my $pnode = $self->{CURRENT}->parentNode;
245 24 ▶ while ( not defined $node )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node) 24 18 42
246 {
247 24 ▶ last unless defined $pnode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pnode 6 18 24
248 18 $node = $pnode->nextSibling;
249 18 ▶ $pnode = $pnode->parentNode unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 18 0 18
250 }
251 }
252 }
253 else
254 {
255 0 $self->{CURRENT} = $self->{FIRST};
256 0 $node = $self->{CURRENT};
257 }
258 }
259  
260 42 return $node;
261 }
262  
263 # helper function for the <> operator
264 # returns all nodes that have not yet been accessed
265 sub _get_all
266 {
267 0 ▶ my $self = shift;
Subroutine _get_all: not called CC=2 SCAR=17.9
268 0 my @retval = ();
269 0 my $node;
270 0 ▶ while ( $node = $self->next() )
Branch: 1 branch
BranchTrueFalseTotal
if ($node = $self->next) 0 0 0
271 {
272 0 push @retval, $node;
273 }
274 0 return @retval;
275 }
276  
277 3 1;
278  
279 __END__
↑ Summary blib/lib/XML/LibXML/NodeList/Iterator.pm »