Iterator.pm

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