KDBX/Object.pm

filter: Statementstmt 94.7% 289 / 305 Branchbran 78.6% 107 / 136 Conditioncond 73.5% 78 / 106 MC/DCmcdc 38.6% 22 / 57 Subroutinesub 88.8% 48 / 54 Podpod 87.5% 21 / 24 totaltotal 82.8% 565 / 682 SCAR 49.8
CC
114
Cov
87%
custom_data
13.5
clone
12.1
STORABLE_thaw
11.5
CRAP 144.9
« KDBX/Loader/XML.pm ↑ Summary KDBX/Loader.pm »
1 package File::KDBX::Object;
2 # ABSTRACT: A KDBX database object
3  
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 ▶use Devel::GlobalDestruction;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use File::KDBX::Constants qw(:bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use File::KDBX::Util qw(:uuid);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Hash::Util::FieldHash qw(fieldhashes);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use List::Util qw(any first);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Ref::Util qw(is_arrayref is_plain_arrayref is_plain_hashref is_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 14 our $VERSION = '0.904'; # VERSION
18  
19 14 fieldhashes \my (%KDBX, %PARENT, %TXNS, %REFS, %SIGNALS);
20  
21  
22 sub new {
23 237 ▶ my $class = shift;
Subroutine new: called CC=10 SCAR=23.2
Pod documented
24  
25 # copy constructor
26 237 ▶ return $_[0]->clone if @_ == 1 && blessed $_[0] && $_[0]->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and builtin::blessed($_[0]) and $_[0]->isa($class) 0 237 237
Condition83%5/6
@_ == 1 and builtin::blessed($_[0]) and $_[0]->isa($class)
!ll&&!rl&&r
22890
@_ == 1 and builtin::blessed($_[0])
!ll&&!rl&&r
22719
MC/DC0%0/3
@_ == 1 and builtin::blessed($_[0]) and $_[0]->isa($class)
@_ == 1 builtin::blessed($_[0]) $_[0]->isa($class)
Truth table75%3/4
A and B and C
A= @_ == 1B= builtin::blessed($_[0])C= $_[0]->isa($class)
ABCresult
0XX0
10X0
1100
1111
27  
28 237 my $data;
29 237 ▶ $data = shift if is_plain_hashref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_plain_hashref($_[0]) 73 164 237
30  
31 237 my $kdbx;
32 237 ▶ $kdbx = shift if @_ % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 == 1 115 122 237
33  
34 237 my %args = @_;
35 237 ▶ $args{kdbx} //= $kdbx if defined $kdbx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $kdbx 115 122 237
Condition33%1/3
$args{"kdbx"} //= $kdbx
l!l&&r!l&&!r
01150
MC/DC0%0/2
$args{"kdbx"} //= $kdbx
$args{"kdbx"} $kdbx
Truth table33%1/3
A //= B
A= $args{"kdbx"}B= $kdbx
ABresult
1X1
011
000
36  
37 237 ▶ my $self = bless $data // {}, $class;
Condition100%2/2
$data // {}
l!l
73164
MC/DC100%1/1
$data // {}
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
38 237 $self->init(%args);
39 237 ▶ $self->_set_nonlazy_attributes if !$data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 164 73 237
40 237 return $self;
41 }
42  
43 0 ▶sub _set_nonlazy_attributes { die 'Not implemented' }
Subroutine _set_nonlazy_attributes: not called CC=1 SCAR=6.9
44  
45  
46 sub init {
47 133 ▶ my $self = shift;
Subroutine init: called CC=3 SCAR=11.0
Pod documented
48 133 my %args = @_;
49  
50 133 ▶ while (my ($key, $val) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 331 133 464
51 331 ▶ if (my $method = $self->can($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can($key)) 331 0 331
52 331 $self->$method($val);
53 }
54 }
55  
56 133 return $self;
57 }
58  
59  
60 sub wrap {
61 798 ▶ my $class = shift;
Subroutine wrap: called CC=4 SCAR=13.9
Pod documented
62 798 my $object = shift;
63 798 ▶ return $object if blessed $object && $object->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($object) and $object->isa($class) 684 114 798
Condition66%2/3
builtin::blessed($object) and $object->isa($class)
!ll&&!rl&&r
1140684
MC/DC50%1/2
builtin::blessed($object) and $object->isa($class)
builtin::blessed($object) $object->isa($class)
Truth table66%2/3
A and B
A= builtin::blessed($object)B= $object->isa($class)
ABresult
0X0
100
111
64 114 ▶ return $class->new(@_, @$object) if is_arrayref($object);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_arrayref($object) 42 72 114
65 72 return $class->new($object, @_);
66 }
67  
68  
69 0 ▶sub label { die 'Not implemented' }
Subroutine label: not called CC=1 SCAR=6.9
Pod documented
70  
71  
72 14 my %CLONE = (entries => 1, groups => 1, history => 1);
73 sub clone {
74 31 ▶ my $self = shift;
Subroutine clone: called CC=12 SCAR=24.9
Pod documented
75 31 my %args = @_;
76  
77 31 ▶ local $CLONE{new_uuid} = $args{new_uuid} // $args{parent} // 0;
Condition100%5/5
($args{"new_uuid"} // $args{"parent"}) // 0
l!l
427
$args{"new_uuid"} // $args{"parent"}
l!l&&r!l&&!r
1327
MC/DC100%2/2
($args{"new_uuid"} // $args{"parent"}) // 0
$args{"new_uuid"} $args{"parent"}
Truth table100%3/3
A // B
A= $args{"new_uuid"}B= $args{"parent"}
ABresult
1X1
011
000
78 31 ▶ local $CLONE{entries} = $args{entries} // 1;
Condition100%2/2
$args{"entries"} // 1
l!l
1813
MC/DC100%1/1
$args{"entries"} // 1
$args{"entries"}
Truth table100%2/2
A
A= $args{"entries"}
Aresult
11
00
79 31 ▶ local $CLONE{groups} = $args{groups} // 1;
Condition100%2/2
$args{"groups"} // 1
l!l
1714
MC/DC100%1/1
$args{"groups"} // 1
$args{"groups"}
Truth table100%2/2
A
A= $args{"groups"}
Aresult
11
00
80 31 ▶ local $CLONE{history} = $args{history} // 1;
Condition100%2/2
$args{"history"} // 1
l!l
1912
MC/DC100%1/1
$args{"history"} // 1
$args{"history"}
Truth table100%2/2
A
A= $args{"history"}
Aresult
11
00
81 31 ▶ local $CLONE{reference_password} = $args{reference_password} // 0;
Condition50%1/2
$args{"reference_password"} // 0
l!l
031
MC/DC0%0/1
$args{"reference_password"} // 0
$args{"reference_password"}
Truth table50%1/2
A
A= $args{"reference_password"}
Aresult
11
00
82 31 ▶ local $CLONE{reference_username} = $args{reference_username} // 0;
Condition100%2/2
$args{"reference_username"} // 0
l!l
130
MC/DC100%1/1
$args{"reference_username"} // 0
$args{"reference_username"}
Truth table100%2/2
A
A= $args{"reference_username"}
Aresult
11
00
83  
84 31 require Storable;
85 31 my $copy = Storable::dclone($self);
86  
87 31 ▶ if ($args{relabel} and my $label = $self->label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"relabel"} and my $label = $self->label) 3 28 31
Condition66%2/3
$args{"relabel"} and my $label = $self->label
!ll&&!rl&&r
2803
MC/DC50%1/2
$args{"relabel"} and my $label = $self->label
$args{"relabel"} my $label = $self->label
Truth table66%2/3
A and B
A= $args{"relabel"}B= my $label = $self->label
ABresult
0X0
100
111
88 3 $copy->label("$label - Copy");
89 }
90 31 ▶ if ($args{parent} and my $parent = $self->group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"parent"} and my $parent = $self->group) 3 28 31
Condition66%2/3
$args{"parent"} and my $parent = $self->group
!ll&&!rl&&r
2803
MC/DC50%1/2
$args{"parent"} and my $parent = $self->group
$args{"parent"} my $parent = $self->group
Truth table66%2/3
A and B
A= $args{"parent"}B= my $parent = $self->group
ABresult
0X0
100
111
91 3 $parent->add_object($copy);
92 }
93  
94 31 return $copy;
95 }
96  
97 sub STORABLE_freeze {
98 64 ▶ my $self = shift;
Subroutine STORABLE_freeze: called CC=5 SCAR=16.1
Pod undocumented
99 64 my $cloning = shift;
100  
101 64 my $copy = {%$self};
102 64 ▶ delete $copy->{entries} if !$CLONE{entries};
Branch: 1 branch
BranchTrueFalseTotal
unless $CLONE{"entries"} 17 47 64
103 64 ▶ delete $copy->{groups} if !$CLONE{groups};
Branch: 1 branch
BranchTrueFalseTotal
unless $CLONE{"groups"} 18 46 64
104 64 ▶ delete $copy->{history} if !$CLONE{history};
Branch: 1 branch
BranchTrueFalseTotal
unless $CLONE{"history"} 23 41 64
105  
106 64 ▶ return ($cloning ? Hash::Util::FieldHash::id($self) : ''), $copy;
Branch: 1 branch
BranchTrueFalseTotal
$cloning ? : 64 0 64
107 }
108  
109 sub STORABLE_thaw {
110 64 ▶ my $self = shift;
Subroutine STORABLE_thaw: called CC=11 SCAR=24.4
Pod undocumented
111 64 my $cloning = shift;
112 64 my $addr = shift;
113 64 my $copy = shift;
114  
115 64 @$self{keys %$copy} = values %$copy;
116  
117 64 ▶ if ($cloning) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cloning) 64 0 64
118 64 my $kdbx = $KDBX{$addr};
119 64 ▶ $self->kdbx($kdbx) if $kdbx;
Branch: 1 branch
BranchTrueFalseTotal
if $kdbx 62 2 64
120 }
121  
122 64 ▶ if (defined $self->{uuid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"uuid"}) 64 0 64
123 64 ▶ if (($CLONE{reference_password} || $CLONE{reference_username}) && $self->can('strings')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CLONE{"reference_password"} || $CLONE{"reference_username"} and $self->can("strings")) 2 62 64
Condition66%4/6
$CLONE{"reference_password"} || $CLONE{"reference_username"} and $self->can("strings")
!ll&&!rl&&r
6202
$CLONE{"reference_password"} or $CLONE{"reference_username"}
l!l&&r!l&&!r
0262
MC/DC33%1/3
$CLONE{"reference_password"} || $CLONE{"reference_username"} and $self->can("strings")
$CLONE{"reference_password"} $CLONE{"reference_username"} $self->can("strings")
Truth table40%2/5
A or B and C
A= $CLONE{"reference_password"}B= $CLONE{"reference_username"}C= $self->can("strings")
ABCresult
00X0
1X00
0100
1X11
0111
124 2 my $uuid = format_uuid($self->{uuid});
125 2 my $clone_obj = do {
126 2 local $CLONE{new_uuid} = 0;
127 2 local $CLONE{entries} = 1;
128 2 local $CLONE{groups} = 1;
129 2 local $CLONE{history} = 1;
130 2 local $CLONE{reference_password} = 0;
131 2 local $CLONE{reference_username} = 0;
132 # Clone only the entry's data and manually bless to avoid infinite recursion.
133 2 bless Storable::dclone({%$copy}), 'File::KDBX::Entry';
134 };
135 2 my $txn = $self->begin_work(snapshot => $clone_obj);
136 2 ▶ if ($CLONE{reference_password}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CLONE{"reference_password"}) 0 2 2
137 0 $self->password("{REF:P\@I:$uuid}");
138 }
139 2 ▶ if ($CLONE{reference_username}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CLONE{"reference_username"}) 2 0 2
140 2 $self->username("{REF:U\@I:$uuid}");
141 }
142 2 $txn->commit;
143 }
144 64 ▶ $self->uuid(generate_uuid) if $CLONE{new_uuid};
Branch: 1 branch
BranchTrueFalseTotal
if $CLONE{"new_uuid"} 13 51 64
145 }
146  
147 # Dualvars aren't cloned as dualvars, so dualify the icon.
148 64 ▶ $self->icon_id($self->{icon_id}) if defined $self->{icon_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"icon_id"} 64 0 64
149 }
150  
151  
152 sub kdbx {
153 978 ▶ my $self = shift;
Subroutine kdbx: called CC=5 SCAR=16.4
Pod documented
154 978 ▶ $self = $self->new if !ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 978 978
155 978 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 310 668 978
156 310 ▶ if (my $kdbx = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $kdbx = shift()) { } 310 0 310
157 310 $KDBX{$self} = $kdbx;
158 310 weaken $KDBX{$self};
159 }
160 else {
161 0 delete $KDBX{$self};
162 }
163 }
164 978 ▶ $KDBX{$self} or throw 'Object is disconnected', object => $self;
Branch: 1 branch
BranchTrueFalseTotal
$KDBX{$self} or throw("Object is disconnected", "object", $self) 320 658 978
165 }
166  
167  
168 sub is_connected {
169 84 ▶ my $self = shift;
Subroutine is_connected: called CC=1 SCAR=0.0
Pod documented
170 84 return !!eval { $self->kdbx };
171 }
172  
173  
174 5 ▶sub id { format_uuid(shift->uuid, @_) }
Subroutine id: called CC=1 SCAR=0.0
Pod documented
175  
176  
177 sub group {
178 154 ▶ my $self = shift;
Subroutine group: called CC=8 SCAR=21.1
Pod documented
179  
180 154 ▶ if (my $new_group = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new_group = shift()) 1 153 154
181 1 my $old_group = $self->group;
182 1 ▶ return $new_group if Hash::Util::FieldHash::id($old_group) == Hash::Util::FieldHash::id($new_group);
Branch: 1 branch
BranchTrueFalseTotal
if &Hash::Util::FieldHash::id($old_group) == &Hash::Util::FieldHash::id($new_group) 0 1 1
183 # move to a new parent
184 1 ▶ $self->remove(signal => 0) if $old_group;
Branch: 1 branch
BranchTrueFalseTotal
if $old_group 1 0 1
185 1 $self->location_changed('now');
186 1 $new_group->add_object($self);
187 }
188  
189 154 my $id = Hash::Util::FieldHash::id($self);
190 154 ▶ if (my $group = $PARENT{$self}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $PARENT{$self}) 31 123 154
191 31 my $method = $self->_parent_container;
192 31 ▶ return $group if first { $id == Hash::Util::FieldHash::id($_) } @{$group->$method};
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $id == &Hash::Util::FieldHash::id($_); } , @{$group->$method;}) 31 0 31
193 0 delete $PARENT{$self};
194 }
195 # always get lineage from root to leaf because the other way requires parent, so it would be recursive
196 123 ▶ my $lineage = $self->kdbx->_trace_lineage($self) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $lineage = $self->kdbx->_trace_lineage($self) or return 122 1 123
197 1 ▶ my $group = pop @$lineage or return;
Branch: 1 branch
BranchTrueFalseTotal
my $group = pop @$lineage or return 0 1 1
198 1 $PARENT{$self} = $group; weaken $PARENT{$self};
199 1 return $group;
200 }
201  
202 sub _set_group {
203 110 ▶ my $self = shift;
Subroutine _set_group: called CC=2 SCAR=6.9
204 110 ▶ if (my $parent = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = shift()) { } 48 62 110
205 48 $PARENT{$self} = $parent;
206 48 weaken $PARENT{$self};
207 }
208 else {
209 62 delete $PARENT{$self};
210 }
211 110 return $self;
212 }
213  
214 ### Name of the parent attribute expected to contain the object
215 0 ▶sub _parent_container { die 'Not implemented' }
Subroutine _parent_container: not called CC=1 SCAR=6.9
216  
217  
218 sub lineage {
219 5 ▶ my $self = shift;
Subroutine lineage: called CC=7 SCAR=21.8
Pod documented
220 5 my $base = shift;
221  
222 5 ▶ my $base_addr = $base ? Hash::Util::FieldHash::id($base) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$base ? : 0 5 5
223  
224 # try leaf to root
225 5 my @path;
226 5 my $object = $self;
227 5 ▶ while ($object = $object->group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object = $object->group) 8 5 13
228 8 unshift @path, $object;
229 8 ▶ last if $base_addr == Hash::Util::FieldHash::id($object);
Branch: 1 branch
BranchTrueFalseTotal
if $base_addr == &Hash::Util::FieldHash::id($object) 0 8 8
230 }
231 5 ▶ return \@path if @path && ($base_addr == Hash::Util::FieldHash::id($path[0]) || $path[0]->is_root);
Branch: 1 branch
BranchTrueFalseTotal
if @path and $base_addr == &Hash::Util::FieldHash::id($path[0]) || $path[0]->is_root 5 0 5
Condition33%2/6
@path and $base_addr == &Hash::Util::FieldHash::id($path[0]) || $path[0]->is_root
!ll&&!rl&&r
005
$base_addr == &Hash::Util::FieldHash::id($path[0]) or $path[0]->is_root
l!l&&r!l&&!r
050
MC/DC0%0/3
@path and $base_addr == &Hash::Util::FieldHash::id($path[0]) || $path[0]->is_root
@path $base_addr == &Hash::Util::FieldHash::id($path[0]) $path[0]->is_root
Truth table25%1/4
A and B or C
A= @pathB= $base_addr == &Hash::Util::FieldHash::id($path[0])C= $path[0]->is_root
ABCresult
0XX0
1000
11X1
1011
232  
233 # try root to leaf
234 0 return $self->kdbx->_trace_lineage($self, $base);
235 }
236  
237  
238 sub remove {
239 55 ▶ my $self = shift;
Subroutine remove: called CC=2 SCAR=6.9
Pod documented
240 55 my $parent = $self->group;
241 55 ▶ $parent->remove_object($self, @_) if $parent;
Branch: 1 branch
BranchTrueFalseTotal
if $parent 7 48 55
242 55 $self->_set_group(undef);
243 55 return $self;
244 }
245  
246  
247 sub recycle {
248 1 ▶ my $self = shift;
Subroutine recycle: called CC=1 SCAR=0.0
Pod documented
249 1 return $self->group($self->kdbx->recycle_bin);
250 }
251  
252  
253 sub recycle_or_remove {
254 3 ▶ my $self = shift;
Subroutine recycle_or_remove: called CC=4 SCAR=13.9
Pod documented
255 3 my $kdbx = eval { $self->kdbx };
256 3 ▶ if ($kdbx && $kdbx->recycle_bin_enabled && !$self->is_recycled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kdbx and $kdbx->recycle_bin_enabled and not $self->is_recycled) { } 1 2 3
Condition83%5/6
$kdbx and $kdbx->recycle_bin_enabled and not $self->is_recycled
!ll&&!rl&&r
111
$kdbx and $kdbx->recycle_bin_enabled
!ll&&!rl&&r
012
MC/DC0%0/3
$kdbx and $kdbx->recycle_bin_enabled and not $self->is_recycled
$kdbx $kdbx->recycle_bin_enabled not $self->is_recycled
Truth table25%1/4
A and B and not(C)
A= $kdbxB= $kdbx->recycle_bin_enabledC= not $self->is_recycled
ABCresult
0XX0
10X0
1100
1111
257 1 $self->recycle;
258 }
259 else {
260 2 $self->remove;
261 }
262 }
263  
264  
265 sub is_recycled {
266 4 ▶ my $self = shift;
Subroutine is_recycled: called CC=3 SCAR=11.0
Pod documented
267 4 ▶ eval { $self->kdbx } or return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->kdbx } } or return !!0 0 4 4
268 4 ▶ return !!($self->group && any { $_->is_recycle_bin } @{$self->lineage});
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
$self->group && &any(sub { $_->is_recycle_bin; } , @{$self->lineage;})
!ll&&!rl&&r
112
MC/DC0%0/2
$self->group && &any(sub { $_->is_recycle_bin; } , @{$self->lineage;})
$self->group &any(sub { $_->is_recycle_bin; } , @{$self->lineage;})
Truth table33%1/3
A && B
A= $self->groupB= &any(sub { $_->is_recycle_bin; } , @{$self->lineage;})
ABresult
0X0
100
111
269 }
270  
271 ##############################################################################
272  
273  
274 sub tag_list {
275 0 ▶ my $self = shift;
Subroutine tag_list: not called CC=2 SCAR=17.9
Pod documented
276 0 ▶ return grep { $_ ne '' } split(/[,\.:;]|\s+/, trim($self->tags) // '');
Condition0%0/2
trim($self->tags) // ""
l!l
00
MC/DC0%0/1
trim($self->tags) // ""
trim($self->tags)
Truth table0%0/2
A
A= trim($self->tags)
Aresult
11
00
277 }
278  
279  
280 sub custom_icon {
281 7 ▶ my $self = shift;
Subroutine custom_icon: called CC=4 SCAR=13.9
Pod documented
282 7 my $kdbx = $self->kdbx;
283 7 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 3 7
284 4 my $img = shift;
285 4 ▶ my $uuid = defined $img ? $kdbx->add_custom_icon($img, @_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $img ? : 3 1 4
286 4 ▶ $self->icon_id(0) if $uuid;
Branch: 1 branch
BranchTrueFalseTotal
if $uuid 3 1 4
287 4 $self->custom_icon_uuid($uuid);
288 4 return $img;
289 }
290 3 return $kdbx->custom_icon_data($self->custom_icon_uuid);
291 }
292  
293  
294 sub custom_data {
295 517 ▶ my $self = shift;
Subroutine custom_data: called CC=13 SCAR=26.0
Pod documented
296 517 ▶ $self->{custom_data} = shift if @_ == 1 && is_plain_hashref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and Ref::Util::XS::is_plain_hashref($_[0]) 0 517 517
Condition66%2/3
@_ == 1 and Ref::Util::XS::is_plain_hashref($_[0])
!ll&&!rl&&r
51160
MC/DC0%0/2
@_ == 1 and Ref::Util::XS::is_plain_hashref($_[0])
@_ == 1 Ref::Util::XS::is_plain_hashref($_[0])
Truth table66%2/3
A and B
A= @_ == 1B= Ref::Util::XS::is_plain_hashref($_[0])
ABresult
0X0
100
111
297 517 ▶ return $self->{custom_data} //= {} if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 498 19 517
Condition100%2/2
$self->{"custom_data"} //= {}
l!l
324174
MC/DC100%1/1
$self->{"custom_data"} //= {}
$self->{"custom_data"}
Truth table100%2/2
A
A= $self->{"custom_data"}
Aresult
11
00
298  
299 19 ▶ my %args = @_ == 2 ? (key => shift, value => shift)
Branch: 2 branches
BranchTrueFalseTotal
@_ == 2 ? : 11 8 19
@_ % 2 == 1 ? : 6 2 8
300 : @_ % 2 == 1 ? (key => shift, @_) : @_;
301  
302 19 ▶ if (!$args{key} && !$args{value}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"key"} or $args{"value"}) 2 17 19
Condition66%2/3
$args{"key"} or $args{"value"}
l!l&&r!l&&!r
1702
MC/DC50%1/2
$args{"key"} or $args{"value"}
$args{"key"} $args{"value"}
Truth table66%2/3
A or B
A= $args{"key"}B= $args{"value"}
ABresult
1X1
011
000
303 2 my %standard = (key => 1, value => 1, last_modification_time => 1);
304 2 my @other_keys = grep { !$standard{$_} } keys %args;
305 2 ▶ if (@other_keys == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@other_keys == 1) 2 0 2
306 2 my $key = $args{key} = $other_keys[0];
307 2 $args{value} = delete $args{$key};
308 }
309 }
310  
311 19 ▶ my $key = $args{key} or throw 'Must provide a custom_data key to access';
Branch: 1 branch
BranchTrueFalseTotal
my $key = $args{"key"} or throw("Must provide a custom_data key to access") 0 19 19
312  
313 19 ▶ return $self->{custom_data}{$key} = $args{value} if is_plain_hashref($args{value});
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_plain_hashref($args{"value"}) 0 19 19
314  
315 19 ▶ while (my ($field, $value) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 34 19 53
316 34 $self->{custom_data}{$key}{$field} = $value;
317 }
318 19 return $self->{custom_data}{$key};
319 }
320  
321  
322 sub custom_data_value {
323 6 ▶ my $self = shift;
Subroutine custom_data_value: called CC=2 SCAR=7.1
Pod documented
324 6 ▶ my $data = $self->custom_data(@_) // return undef;
Condition50%1/2
$self->custom_data(@_) // (return undef)
l!l
60
MC/DC0%0/1
$self->custom_data(@_) // (return undef)
$self->custom_data(@_)
Truth table50%1/2
A
A= $self->custom_data(@_)
Aresult
11
00
325 6 return $data->{value};
326 }
327  
328 ##############################################################################
329  
330  
331 sub begin_work {
332 35 ▶ my $self = shift;
Subroutine begin_work: called CC=9 SCAR=22.3
Pod documented
333  
334 35 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 16 19 35
335 16 require File::KDBX::Transaction;
336 16 return File::KDBX::Transaction->new($self, @_);
337 }
338  
339 19 my %args = @_;
340 19 ▶ my $orig = $args{snapshot} // do {
Condition66%2/3
$args{"snapshot"} // do { my $c = $self->clone("entries", $args{"entries"} // 0, "groups", $args{"groups"} // 0, "history", $args{"history"} // 0); $c->{"entries"} = $self->{"entries"} unless $args{"entries"}; $c->{"groups"} = $self->{"groups"} unless $args{"groups"}; $c->{"history"} = $self->{"history"} unless $args{"history"}; $c }
l!l&&r!l&&!r
2170
MC/DC0%0/2
$args{"snapshot"} // do { my $c = $self->clone("entries", $args{"entries"} // 0, "groups", $args{"groups"} // 0, "history", $args{"history"} // 0); $c->{"entries"} = $self->{"entries"} unless $args{"entries"}; $c->{"groups"} = $self->{"groups"} unless $args{"groups"}; $c->{"history"} = $self->{"history"} unless $args{"history"}; $c }
$args{"snapshot"} do { my $c = $self->clone("entries", $args{"entries"} // 0, "groups", $args{"groups"} // 0, "history", $args{"history"} // 0); $c->{"entries"} = $self->{"entries"} unless $args{"entries"}; $c->{"groups"} = $self->{"groups"} unless $args{"groups"}; $c->{"history"} = $self->{"history"} unless $args{"history"}; $c }
Truth table66%2/3
A // B
A= $args{"snapshot"}B= do { my $c = $self->clone("entries", $args{"entries"} // 0, "groups", $args{"groups"} // 0, "history", $args{"history"} // 0); $c->{"entries"} = $self->{"entries"} unless $args{"entries"}; $c->{"groups"} = $self->{"groups"} unless $args{"groups"}; $c->{"history"} = $self->{"history"} unless $args{"history"}; $c }
ABresult
1X1
011
000
341 my $c = $self->clone(
342 entries => $args{entries} // 0,
343 groups => $args{groups} // 0,
344 17 ▶ history => $args{history} // 0,
Condition66%4/6
$args{"entries"} // 0
l!l
116
$args{"groups"} // 0
l!l
017
$args{"history"} // 0
l!l
017
MC/DC100%1/1
$args{"entries"} // 0
$args{"entries"}
MC/DC0%0/1
$args{"groups"} // 0
$args{"groups"}
MC/DC0%0/1
$args{"history"} // 0
$args{"history"}
Truth table100%2/2
A
A= $args{"entries"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"groups"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"history"}
Aresult
11
00
345 );
346 17 ▶ $c->{entries} = $self->{entries} if !$args{entries};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"entries"} 16 1 17
347 17 ▶ $c->{groups} = $self->{groups} if !$args{groups};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"groups"} 17 0 17
348 17 ▶ $c->{history} = $self->{history} if !$args{history};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"history"} 17 0 17
349 17 $c;
350 };
351  
352 19 my $id = Hash::Util::FieldHash::id($orig);
353 19 _save_references($id, $self, $orig);
354  
355 19 $self->_signal_begin_work;
356  
357 19 push @{$self->_txns}, $orig;
358 }
359  
360  
361 sub commit {
362 14 ▶ my $self = shift;
Subroutine commit: called CC=3 SCAR=11.0
Pod documented
363 14 ▶ my $orig = pop @{$self->_txns} or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $orig = pop @{$self->_txns;} or return $self 0 14 14
364 14 $self->_commit($orig);
365 14 my $signals = $self->_signal_commit;
366 14 ▶ $self->_signal_send($signals) if !$self->_in_txn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_in_txn 12 2 14
367 14 return $self;
368 }
369  
370  
371 sub rollback {
372 5 ▶ my $self = shift;
Subroutine rollback: called CC=2 SCAR=7.0
Pod documented
373  
374 5 ▶ my $orig = pop @{$self->_txns} or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $orig = pop @{$self->_txns;} or return $self 0 5 5
375  
376 5 my $id = Hash::Util::FieldHash::id($orig);
377 5 _restore_references($id, $orig);
378  
379 5 $self->_signal_rollback;
380  
381 5 return $self;
382 }
383  
384 # Get whether or not there is at least one pending transaction.
385 82 ▶sub _in_txn { scalar @{$_[0]->_txns} }
Subroutine _in_txn: called CC=1 SCAR=0.0
386  
387 # Get an array ref of pending transactions.
388 152 ▶sub _txns { $TXNS{$_[0]} //= [] }
Subroutine _txns: called CC=2 SCAR=6.9
Condition100%2/2
$TXNS{$_[0]} //= []
l!l
6983
MC/DC100%1/1
$TXNS{$_[0]} //= []
$TXNS{$_[0]}
Truth table100%2/2
A
A= $TXNS{$_[0]}
Aresult
11
00
389  
390 # The _commit hook notifies subclasses that a commit has occurred.
391 0 ▶sub _commit { die 'Not implemented' }
Subroutine _commit: not called CC=1 SCAR=6.9
392  
393 # Get a reference to an object that represents an object's committed state. If there is no pending
394 # transaction, this is just $self. If there is a transaction, this is the snapshot take before the transaction
395 # began. This method is private because it provides direct access to the actual snapshot. It is important that
396 # the snapshot not be changed or a rollback would roll back to an altered state.
397 # This is used by File::KDBX::Dumper::XML so as to not dump uncommitted changes.
398 sub _committed {
399 32 ▶ my $self = shift;
Subroutine _committed: called CC=2 SCAR=7.0
400 32 my ($orig) = @{$self->_txns};
401 32 ▶ return $orig // $self;
Condition66%2/3
$orig // $self
l!l&&r!l&&!r
1310
MC/DC0%0/2
$orig // $self
$orig $self
Truth table66%2/3
A // B
A= $origB= $self
ABresult
1X1
011
000
402 }
403  
404 # In addition to cloning an object when beginning work, we also keep track its hashrefs and arrayrefs
405 # internally so that we can restore to the very same structures in the case of a rollback.
406 sub _save_references {
407 1406 ▶ my $id = shift;
Subroutine _save_references: called CC=7 SCAR=19.5
408 1406 my $self = shift;
409 1406 my $orig = shift;
410  
411 1406 ▶ if (is_plain_arrayref($orig)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($orig)) { } 67 1339 1406
elsif (Ref::Util::XS::is_plain_hashref($orig) or builtin::blessed($orig) and $orig->isa(__PACKAGE__)) { } 349 1649 1998
412 67 ▶ for (my $i = 0; $i < @$orig; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$orig) 20 67 87
413 20 _save_references($id, $self->[$i], $orig->[$i]);
414 }
415 67 $REFS{$id}{Hash::Util::FieldHash::id($orig)} = $self;
416 }
417 ▶ elsif (is_plain_hashref($orig) || (blessed $orig && $orig->isa(__PACKAGE__))) {
Condition100%6/6
Ref::Util::XS::is_plain_hashref($orig) or builtin::blessed($orig) and $orig->isa(__PACKAGE__)
l!l&&r!l&&!r
31039990
builtin::blessed($orig) and $orig->isa(__PACKAGE__)
!ll&&!rl&&r
65933139
MC/DC100%3/3
Ref::Util::XS::is_plain_hashref($orig) or builtin::blessed($orig) and $orig->isa(__PACKAGE__)
Ref::Util::XS::is_plain_hashref($orig) builtin::blessed($orig) $orig->isa(__PACKAGE__)
Truth table100%4/4
A or B and C
A= Ref::Util::XS::is_plain_hashref($orig)B= builtin::blessed($orig)C= $orig->isa(__PACKAGE__)
ABCresult
1XX1
0111
00X0
0100
418 349 for my $key (keys %$orig) {
419 1367 _save_references($id, $self->{$key}, $orig->{$key});
420 }
421 349 $REFS{$id}{Hash::Util::FieldHash::id($orig)} = $self;
422 }
423 }
424  
425 # During a rollback, copy data from the snapshot back into the original internal structures.
426 sub _restore_references {
427 377 ▶ my $id = shift;
Subroutine _restore_references: called CC=9 SCAR=22.7
428 377 ▶ my $orig = shift // return;
Condition100%2/2
shift() // (return)
l!l
32948
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
429 329 ▶ my $self = delete $REFS{$id}{Hash::Util::FieldHash::id($orig) // ''} // return $orig;
Condition75%3/4
delete $REFS{$id}{&Hash::Util::FieldHash::id($orig) // ""} // (return $orig)
l!l
108221
&Hash::Util::FieldHash::id($orig) // ""
l!l
3290
MC/DC100%1/1
delete $REFS{$id}{&Hash::Util::FieldHash::id($orig) // ""} // (return $orig)
delete $REFS{$id}{&Hash::Util::FieldHash::id($orig) // ""}
MC/DC0%0/1
&Hash::Util::FieldHash::id($orig) // ""
&Hash::Util::FieldHash::id($orig)
Truth table100%2/2
A
A= delete $REFS{$id}{&Hash::Util::FieldHash::id($orig) // ""}
Aresult
11
00
Truth table50%1/2
A
A= &Hash::Util::FieldHash::id($orig)
Aresult
11
00
430  
431 108 ▶ if (is_plain_arrayref($orig)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($orig)) { } 19 89 108
elsif (Ref::Util::XS::is_plain_hashref($orig) or builtin::blessed($orig) and $orig->isa(__PACKAGE__)) { } 89 0 89
432 19 @$self = map { _restore_references($id, $_) } @$orig;
433 }
434 ▶ elsif (is_plain_hashref($orig) || (blessed $orig && $orig->isa(__PACKAGE__))) {
Condition50%3/6
Ref::Util::XS::is_plain_hashref($orig) or builtin::blessed($orig) and $orig->isa(__PACKAGE__)
l!l&&r!l&&!r
78110
builtin::blessed($orig) and $orig->isa(__PACKAGE__)
!ll&&!rl&&r
0011
MC/DC0%0/3
Ref::Util::XS::is_plain_hashref($orig) or builtin::blessed($orig) and $orig->isa(__PACKAGE__)
Ref::Util::XS::is_plain_hashref($orig) builtin::blessed($orig) $orig->isa(__PACKAGE__)
Truth table50%2/4
A or B and C
A= Ref::Util::XS::is_plain_hashref($orig)B= builtin::blessed($orig)C= $orig->isa(__PACKAGE__)
ABCresult
1XX1
0111
00X0
0100
435 89 for my $key (keys %$orig) {
436 # next if is_ref($orig->{$key}) &&
437 # (Hash::Util::FieldHash::id($self->{$key}) // 0) == Hash::Util::FieldHash::id($orig->{$key});
438 366 $self->{$key} = _restore_references($id, $orig->{$key});
439 }
440 }
441  
442 108 return $self;
443 }
444  
445 ##############################################################################
446  
447 sub _signal {
448 68 ▶ my $self = shift;
Subroutine _signal: called CC=2 SCAR=8.1
449 68 my $type = shift;
450  
451 68 ▶ if ($self->_in_txn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_txn) 0 68 68
452 0 my $stack = $self->_signal_stack;
453 0 my $queue = $stack->[-1];
454 0 push @$queue, [$type, @_];
455 }
456  
457 68 $self->_signal_send([[$type, @_]]);
458  
459 68 return $self;
460 }
461  
462 52 ▶sub _signal_stack { $SIGNALS{$_[0]} //= [] }
Subroutine _signal_stack: called CC=2 SCAR=6.9
Condition100%2/2
$SIGNALS{$_[0]} //= []
l!l
448
MC/DC100%1/1
$SIGNALS{$_[0]} //= []
$SIGNALS{$_[0]}
Truth table100%2/2
A
A= $SIGNALS{$_[0]}
Aresult
11
00
463  
464 sub _signal_begin_work {
465 19 ▶ my $self = shift;
Subroutine _signal_begin_work: called CC=1 SCAR=0.0
466 19 push @{$self->_signal_stack}, [];
467 }
468  
469 sub _signal_commit {
470 14 ▶ my $self = shift;
Subroutine _signal_commit: called CC=2 SCAR=6.9
471 14 my $signals = pop @{$self->_signal_stack};
472 14 ▶ my $previous = $self->_signal_stack->[-1] // [];
Condition100%2/2
$self->_signal_stack->[-1] // []
l!l
212
MC/DC100%1/1
$self->_signal_stack->[-1] // []
$self->_signal_stack->[-1]
Truth table100%2/2
A
A= $self->_signal_stack->[-1]
Aresult
11
00
473 14 push @$previous, @$signals;
474 14 return $previous;
475 }
476  
477 sub _signal_rollback {
478 5 ▶ my $self = shift;
Subroutine _signal_rollback: called CC=1 SCAR=0.0
479 5 pop @{$self->_signal_stack};
480 }
481  
482 sub _signal_send {
483 80 ▶ my $self = shift;
Subroutine _signal_send: called CC=4 SCAR=14.0
484 80 ▶ my $signals = shift // [];
Condition50%1/2
shift() // []
l!l
800
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
485  
486 80 ▶ my $kdbx = $KDBX{$self} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $kdbx = $KDBX{$self} or return 0 80 80
487  
488 # de-duplicate, keeping the most recent signal for each type
489 80 my %seen;
490 80 my @signals = grep { !$seen{$_->[0]}++ } reverse @$signals;
491  
492 80 for my $sig (reverse @signals) {
493 68 $kdbx->_handle_signal($self, @$sig);
494 }
495 }
496  
497 ##############################################################################
498  
499 sub _wrap_group {
500 54 ▶ my $self = shift;
Subroutine _wrap_group: called CC=1 SCAR=0.0
501 54 my $group = shift;
502 54 require File::KDBX::Group;
503 54 return File::KDBX::Group->wrap($group, $KDBX{$self});
504 }
505  
506 sub _wrap_entry {
507 59 ▶ my $self = shift;
Subroutine _wrap_entry: called CC=1 SCAR=0.0
508 59 my $entry = shift;
509 59 require File::KDBX::Entry;
510 59 return File::KDBX::Entry->wrap($entry, $KDBX{$self});
511 }
512  
513 0 ▶sub TO_JSON { +{%{$_[0]}} }
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod undocumented
514  
515 14 1;
516  
517 __END__
« KDBX/Loader/XML.pm ↑ Summary KDBX/Loader.pm »