KDBX/Iterator.pm

filter: Statementstmt 96.4% 136 / 141 Branchbran 82.6% 43 / 52 Conditioncond 59.5% 25 / 42 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 96.8% 31 / 32 Podpod 94.1% 16 / 17 totaltotal 83.5% 254 / 304 CC 43 SCAR 19.5
CC
43
Cov
87%
order_by
20.2
each
7.3
norder_by
5.1
CRAP 7.0
« KDBX/Loader.pm ↑ Summary KDBX/Object.pm »
1 package File::KDBX::Iterator;
2 # ABSTRACT: KDBX database iterator
3  
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use File::KDBX::Util qw(:class :load :search);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Iterator::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Module::Loaded;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Ref::Util qw(is_arrayref is_coderef is_ref is_scalarref);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 ▶BEGIN { mark_as_loaded('Iterator::Simple::Iterator') }
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 extends 'Iterator::Simple::Iterator';
16  
17 12 our $VERSION = '0.905'; # VERSION
18  
19  
20 sub new {
21 818 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
22 818 ▶ my $code = is_coderef($_[0]) ? shift : sub { undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_coderef($_[0]) ? : 810 8 818
23  
24 818 ▶ my $items = @_ == 1 && is_arrayref($_[0]) ? $_[0] : \@_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && Ref::Util::XS::is_arrayref($_[0]) ? : 0 818 818
Condition33%1/3
@_ == 1 and Ref::Util::XS::is_arrayref($_[0])
!ll&&!rl&&r
81800
MC/DC0%0/2
@_ == 1 and Ref::Util::XS::is_arrayref($_[0])
@_ == 1 Ref::Util::XS::is_arrayref($_[0])
Truth table33%1/3
A and B
A= @_ == 1B= Ref::Util::XS::is_arrayref($_[0])
ABresult
0X0
100
111
25 return $class->SUPER::new(sub {
26 1983 ▶ if (@_) { # put back
Subroutine __ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 25 1958 1983
27 25 ▶ if (@_ == 1 && is_arrayref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and Ref::Util::XS::is_arrayref($_[0])) { } 22 3 25
Condition100%3/3
@_ == 1 and Ref::Util::XS::is_arrayref($_[0])
!ll&&!rl&&r
1222
MC/DC100%2/2
@_ == 1 and Ref::Util::XS::is_arrayref($_[0])
@_ == 1 Ref::Util::XS::is_arrayref($_[0])
Truth table100%3/3
A and B
A= @_ == 1B= Ref::Util::XS::is_arrayref($_[0])
ABresult
0X0
100
111
28 22 $items = $_[0];
29 }
30 else {
31 3 unshift @$items, @_;
32 }
33 25 return;
34 }
35 else {
36 1958 my $next = shift @$items;
37 1958 ▶ return $next if defined $next;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next 99 1859 1958
38 1859 return $code->();
39 }
40 818 });
41 }
42  
43  
44 sub next {
45 1167 ▶ my $self = shift;
Subroutine next: called CC=4 SCAR=13.9
Pod documented
46 1167 ▶ my $code = shift or return $self->();
Branch: 1 branch
BranchTrueFalseTotal
my $code = shift() or return &$self() 838 329 1167
47  
48 329 $code = query_any($code, @_);
49  
50 329 ▶ while (defined (local $_ = $self->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(local $_ = &$self())) 331 311 642
51 331 ▶ return $_ if $code->($_);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($_) 18 313 331
52 }
53 311 return;
54 }
55  
56  
57 sub peek {
58 2 ▶ my $self = shift;
Subroutine peek: called CC=2 SCAR=6.9
Pod documented
59  
60 2 my $next = $self->();
61 2 ▶ $self->($next) if defined $next;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next 1 1 2
62 2 return $next;
63 }
64  
65  
66 sub unget {
67 1 ▶ my $self = shift; # Must shift in a statement before calling.
Subroutine unget: called CC=1 SCAR=0.0
Pod documented
68 1 $self->(@_);
69 }
70  
71  
72 sub each {
73 27 ▶ my $self = shift;
Subroutine each: called CC=6 SCAR=19.9
Pod documented
74 27 ▶ my $cb = shift or return @{$self->to_array};
Branch: 1 branch
BranchTrueFalseTotal
my $cb = shift() or return @{$self->to_array;} 4 23 27
75  
76 23 ▶ if (is_coderef($cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_coderef($cb)) { } 23 0 23
elsif (not Ref::Util::XS::is_ref($cb)) { } 0 0 0
77 23 my $count = 0;
78 23 ▶ $cb->($_, $count++, @_) while defined (local $_ = $self->());
Branch: 1 branch
BranchTrueFalseTotal
defined(local $_ = &$self()) and do { &$cb($_, $count++, @_) } 49 23 72
79 }
80 elsif (!is_ref($cb)) {
81 0 ▶ $_->$cb(@_) while defined (local $_ = $self->());
Branch: 1 branch
BranchTrueFalseTotal
defined(local $_ = &$self()) and do { $_->$cb(@_) } 0 0 0
82 }
83 23 return $self;
84 }
85  
86  
87 2 ▶sub where { shift->grep(@_) }
Subroutine where: called CC=1 SCAR=0.0
Pod documented
88  
89 sub grep {
90 30 ▶ my $self = shift;
Subroutine grep: called CC=1 SCAR=0.0
Pod documented
91 30 my $code = query_any(@_);
92  
93 ref($self)->new(sub {
94 35 ▶ while (defined (local $_ = $self->())) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (defined(local $_ = &$self())) 54 7 61
95 54 ▶ return $_ if $code->($_);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($_) 28 26 54
96 }
97 7 return;
98 30 });
99 }
100  
101  
102 sub map {
103 21 ▶ my $self = shift;
Subroutine map: called CC=1 SCAR=0.0
Pod documented
104 21 my $code = shift;
105  
106 ref($self)->new(sub {
107 112 ▶ local $_ = $self->();
Subroutine __ANON__: called CC=2 SCAR=6.9
108 112 ▶ return if !defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 19 93 112
109 93 return $code->();
110 21 });
111 }
112  
113  
114 sub order_by {
115 5 ▶ my $self = shift;
Subroutine order_by: called CC=17 SCAR=30.1
Pod documented
116 5 my $field = shift;
117 5 my %args = @_;
118  
119 5 ▶ my $ascending = delete $args{ascending} // !delete $args{descending} // 1;
Condition60%3/5
(delete $args{"ascending"} // !delete $args{"descending"}) // 1
l!l
50
delete $args{"ascending"} // !delete $args{"descending"}
l!l&&r!l&&!r
230
MC/DC0%0/2
(delete $args{"ascending"} // !delete $args{"descending"}) // 1
delete $args{"ascending"} !delete $args{"descending"}
Truth table66%2/3
A // not(B)
A= delete $args{"ascending"}B= !delete $args{"descending"}
ABresult
1X1
011
000
120 5 ▶ my $case = delete $args{case} // !delete $args{no_case} // 1;
Condition60%3/5
(delete $args{"case"} // !delete $args{"no_case"}) // 1
l!l
50
delete $args{"case"} // !delete $args{"no_case"}
l!l&&r!l&&!r
320
MC/DC0%0/2
(delete $args{"case"} // !delete $args{"no_case"}) // 1
delete $args{"case"} !delete $args{"no_case"}
Truth table66%2/3
A // not(B)
A= delete $args{"case"}B= !delete $args{"no_case"}
ABresult
1X1
011
000
121 5 ▶ my $collate = (delete $args{collate} // !delete $args{no_collate} // 1)
Condition62%5/8
((delete $args{'collate'} // !delete $args{'no_collate'}) // 1) && try_load_optional('Unicode::Collate')
!ll&&!rl&&r
401
(delete $args{"collate"} // !delete $args{"no_collate"}) // 1
l!l
50
delete $args{"collate"} // !delete $args{"no_collate"}
l!l&&r!l&&!r
410
MC/DC0%0/3
((delete $args{'collate'} // !delete $args{'no_collate'}) // 1) && try_load_optional('Unicode::Collate')
delete $args{"collate"} !delete $args{"no_collate"} try_load_optional('Unicode::Collate')
Truth table20%1/5
A // not(B) && C
A= delete $args{"collate"}B= !delete $args{"no_collate"}C= try_load_optional('Unicode::Collate')
ABCresult
00X0
1X00
0100
1X11
0111
122 && try_load_optional('Unicode::Collate');
123  
124 5 ▶ if ($collate && !$case) {
Branch: 1 branch
BranchTrueFalseTotal
if ($collate and not $case) 1 4 5
Condition66%2/3
$collate and not $case
!ll&&!rl&&r
401
MC/DC50%1/2
$collate and not $case
$collate not $case
Truth table66%2/3
A and not(B)
A= $collateB= not $case
ABresult
0X0
100
111
125 1 $case = 1;
126 # use a proper Unicode::Collate level to ignore case
127 1 ▶ $args{level} //= 2;
Condition50%1/2
$args{"level"} //= 2
l!l
01
MC/DC0%0/1
$args{"level"} //= 2
$args{"level"}
Truth table50%1/2
A
A= $args{"level"}
Aresult
11
00
128 }
129 5 ▶ $args{upper_before_lower} //= 1;
Condition50%1/2
$args{"upper_before_lower"} //= 1
l!l
05
MC/DC0%0/1
$args{"upper_before_lower"} //= 1
$args{"upper_before_lower"}
Truth table50%1/2
A
A= $args{"upper_before_lower"}
Aresult
11
00
130  
131 5 my $value = $field;
132 5 ▶ $value = $case ? sub { $_[0]->$field // '' } : sub { uc($_[0]->$field) // '' } if !is_coderef($value);
Subroutine __ANON__: called CC=2 SCAR=7.3
Branch: 2 branches
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($value) 5 0 5
$case ? : 3 2 5
Condition50%2/4
$_[0]->$field // ""
l!l
100
uc $_[0]->$field // ""
l!l
150
MC/DC0%0/1
$_[0]->$field // ""
$_[0]->$field
MC/DC0%0/1
uc $_[0]->$field // ""
uc $_[0]->$field
Truth table50%1/2
A
A= $_[0]->$field
Aresult
11
00
Truth table50%1/2
A
A= uc $_[0]->$field
Aresult
11
00
133 5 my @all = CORE::map { [$_, $value->($_)] } @{$self->to_array};
134  
135 5 ▶ if ($collate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($collate) { } 1 4 5
136 1 my $c = Unicode::Collate->new(%args);
137 1 ▶ if ($ascending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ascending) { } 1 0 1
138 1 @all = CORE::map { $_->[0] } CORE::sort { $c->cmp($a->[1], $b->[1]) } @all;
139 } else {
140 0 @all = CORE::map { $_->[0] } CORE::sort { $c->cmp($b->[1], $a->[1]) } @all;
141 }
142 } else {
143 4 ▶ if ($ascending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ascending) { } 2 2 4
144 2 @all = CORE::map { $_->[0] } CORE::sort { $a->[1] cmp $b->[1] } @all;
145 } else {
146 2 @all = CORE::map { $_->[0] } CORE::sort { $b->[1] cmp $a->[1] } @all;
147 }
148 }
149  
150 5 $self->(\@all);
151 5 return $self;
152 }
153  
154  
155 5 ▶sub sort_by { shift->order_by(@_) }
Subroutine sort_by: called CC=1 SCAR=0.0
Pod documented
156  
157  
158 sub norder_by {
159 2 ▶ my $self = shift;
Subroutine norder_by: called CC=5 SCAR=16.2
Pod documented
160 2 my $field = shift;
161 2 my %args = @_;
162  
163 2 ▶ my $ascending = $args{ascending} // !$args{descending} // 1;
Condition60%3/5
($args{"ascending"} // !$args{"descending"}) // 1
l!l
20
$args{"ascending"} // !$args{"descending"}
l!l&&r!l&&!r
110
MC/DC0%0/2
($args{"ascending"} // !$args{"descending"}) // 1
$args{"ascending"} !$args{"descending"}
Truth table66%2/3
A // not(B)
A= $args{"ascending"}B= !$args{"descending"}
ABresult
1X1
011
000
164  
165 2 my $value = $field;
166 2 ▶ $value = sub { $_[0]->$field // 0 } if !is_coderef($value);
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($value) 2 0 2
Condition50%1/2
$_[0]->$field // 0
l!l
100
MC/DC0%0/1
$_[0]->$field // 0
$_[0]->$field
Truth table50%1/2
A
A= $_[0]->$field
Aresult
11
00
167 2 my @all = CORE::map { [$_, $value->($_)] } @{$self->to_array};
168  
169 2 ▶ if ($ascending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ascending) { } 1 1 2
170 1 @all = CORE::map { $_->[0] } CORE::sort { $a->[1] <=> $b->[1] } @all;
171 } else {
172 1 @all = CORE::map { $_->[0] } CORE::sort { $b->[1] <=> $a->[1] } @all;
173 }
174  
175 2 $self->(\@all);
176 2 return $self;
177 }
178  
179  
180 2 ▶sub nsort_by { shift->norder_by(@_) }
Subroutine nsort_by: called CC=1 SCAR=0.0
Pod documented
181  
182  
183 1 ▶sub limit { shift->head(@_) }
Subroutine limit: called CC=1 SCAR=0.0
Pod documented
184  
185  
186 sub to_array {
187 45 ▶ my $self = shift;
Subroutine to_array: called CC=2 SCAR=6.9
Pod documented
188  
189 45 my @all;
190 45 ▶ push @all, $_ while defined (local $_ = $self->());
Branch: 1 branch
BranchTrueFalseTotal
defined(local $_ = &$self()) and do { push @all, $_ } 168 45 213
191 45 return \@all;
192 }
193  
194  
195 sub count {
196 14 ▶ my $self = shift;
Subroutine count: called CC=1 SCAR=0.0
Pod documented
197  
198 14 my $items = $self->to_array;
199 14 $self->($items);
200 14 return scalar @$items;
201 }
202  
203  
204 10 ▶sub size { shift->count }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
205  
206 ##############################################################################
207  
208 0 ▶sub TO_JSON { $_[0]->to_array }
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod undocumented
209  
210 12 1;
211  
212 __END__
« KDBX/Loader.pm ↑ Summary KDBX/Object.pm »