lib/Text/JSContact.pm

filter: Statementstmt 95.3% 1462 / 1533 Branchbran 75.2% 596 / 792 Conditioncond 61.5% 384 / 624 MC/DCmcdc 37.8% 133 / 351 Subroutinesub 96.9% 128 / 132 Podpod 0.0% 0 / 3 totaltotal 78.6% 2703 / 3435 CC 757 SCAR 29.2
CC
757
Cov
83%
_patch_kind
132.0
_patch_speak_to_as
56.0
_to_v3
55.8
CRAP 18.6
↑ Summary
1 #!/usr/bin/perl -cw
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Text::JSContact;
7  
8 19 our $VERSION = '0.06';
9  
10 # vCard <=> JSContact (RFC 9553) conversion, writing JSContact version 2.0
11 # (RFC 9982); follows RFC 9555 for vCard mapping and RFC 9554 for extensions
12  
13 19 ▶use Text::VCardFast qw(vcard2hash);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 19 ▶use Encode qw(decode_utf8 encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 19 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 19 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶use Scalar::Util qw(looks_like_number refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 19 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 19 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 19 our @EXPORT_OK = qw(vcard_to_jscontact jscontact_to_vcard patch_vcard);
22  
23 ###############################################################################
24 # HELPERS
25 ###############################################################################
26  
27 3295 ▶sub _first_prop {
Subroutine _first_prop: called CC=3 SCAR=11.0
28 3295 my ($props, $name) = @_;
29 3295 my $items = $props->{$name};
30 3295 ▶ return undef unless $items && @$items;
Branch: 1 branch
BranchTrueFalseTotal
unless $items and @$items 2101 1194 3295
Condition66%2/3
$items and @$items
!ll&&!rl&&r
210101194
MC/DC50%1/2
$items and @$items
$items @$items
Truth table66%2/3
A and B
A= $itemsB= @$items
ABresult
0X0
100
111
31 1194 return $items->[0];
32 }
33  
34 3042 ▶sub _first_value {
Subroutine _first_value: called CC=2 SCAR=6.9
35 3042 my ($props, $name) = @_;
36 3042 my $prop = _first_prop($props, $name);
37 3042 ▶ return undef unless $prop;
Branch: 1 branch
BranchTrueFalseTotal
unless $prop 2037 1005 3042
38 1005 return $prop->{value};
39 }
40  
41 # vCard 4 parameters that a vCard 3 card carries as X- (see _to_v3)
42 19 my %X_FALLBACK = map { $_ => 1 } qw(label geo tz cc mediatype sort-as level
43 index created author author-name);
44  
45 13300 ▶sub _param {
Subroutine _param: called CC=5 SCAR=16.1
46 13300 my ($prop, $name) = @_;
47 13300 my $val = $prop->{params}{$name};
48 13300 ▶ $val = $prop->{params}{"x-$name"} if !defined $val && $X_FALLBACK{$name};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $val and $X_FALLBACK{$name} 1750 11550 13300
Condition100%3/3
not defined $val and $X_FALLBACK{$name}
!ll&&!rl&&r
235791931750
MC/DC100%2/2
not defined $val and $X_FALLBACK{$name}
not defined $val $X_FALLBACK{$name}
Truth table100%3/3
not(A) and B
A= not defined $valB= $X_FALLBACK{$name}
ABresult
0X0
100
111
49 13300 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 10943 2357 13300
50 2357 ▶ return ref $val eq 'ARRAY' ? $val->[0] : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 2357 0 2357
51 }
52  
53 3558 ▶sub _params_list {
Subroutine _params_list: called CC=3 SCAR=11.0
54 3558 my ($prop, $name) = @_;
55 3558 my $val = $prop->{params}{$name};
56 3558 ▶ return () unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1439 2119 3558
57 # Split comma-separated values (e.g. TYPE=work,voice)
58 2119 ▶ return map { split /,/ } (ref $val eq 'ARRAY' ? @$val : ($val));
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 2119 0 2119
59 }
60  
61 ###############################################################################
62 # CONVERSION CONTEXT
63 ###############################################################################
64  
65 # Everything one conversion allocates lives in a context made for that
66 # call: the ids given to vCard properties without a PROP-ID, and the
67 # itemN groups that tie an X-ABLabel to its property. Each allocator is
68 # seeded with what the vCard already uses, so an allocated id never
69 # equals an explicit PROP-ID and a new group never takes an existing one.
70 613 ▶sub _new_context {
Subroutine _new_context: called CC=1 SCAR=0.0
71 613 my (%args) = @_;
72 return {
73 613 next_id => 0,
74 used_ids => {}, # PROP-IDs present in the vCard
75 next_group => 0,
76 used_groups => {}, # groups present in the vCard, lower-cased
77 %args,
78 };
79 }
80  
81 # Seed a context with the PROP-IDs and groups of parsed vCard properties
82 253 ▶sub _seed_context {
Subroutine _seed_context: called CC=6 SCAR=18.0
83 253 my ($ctx, $props) = @_;
84 253 for my $item (map { @$_ } values %$props) {
85 5619 my $pid = _param($item, 'prop-id');
86 5619 ▶ $ctx->{used_ids}{$pid} = 1 if defined $pid && $pid ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $pid and $pid ne "" 617 5002 5619
Condition66%2/3
defined $pid and $pid ne ""
!ll&&!rl&&r
50020617
MC/DC50%1/2
defined $pid and $pid ne ""
defined $pid $pid ne ""
Truth table66%2/3
A and B
A= defined $pidB= $pid ne ""
ABresult
0X0
100
111
87 5619 ▶ $ctx->{used_groups}{lc $item->{group}} = 1 if defined $item->{group} && $item->{group} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $item->{"group"} and $item->{"group"} ne "" 1503 4116 5619
Condition66%2/3
defined $item->{"group"} and $item->{"group"} ne ""
!ll&&!rl&&r
411601503
MC/DC50%1/2
defined $item->{"group"} and $item->{"group"} ne ""
defined $item->{"group"} $item->{"group"} ne ""
Truth table66%2/3
A and B
A= defined $item->{"group"}B= $item->{"group"} ne ""
ABresult
0X0
100
111
88 }
89 253 return $ctx;
90 }
91  
92 # TODO(registry): an allocated id is a position in reading order, across
93 # the whole card. A vCard without PROP-IDs (every vCard 3 card, which
94 # patch_vcard keeps free of them, and many vCard 4 ones) therefore keeps
95 # its ids only while no entry before them is added or removed: patching
96 # one entry out renumbers every entry read after it. Counting per map
97 # would confine that to the one map; stable ids need either PROP-IDs
98 # written on every line or ids the registry derives some other way.
99 2089 ▶sub _alloc_id {
Subroutine _alloc_id: called CC=2 SCAR=6.9
100 2089 my ($ctx) = @_;
101 2089 my $id;
102 2089 ▶ do { $id = '' . ++$ctx->{next_id} } while $ctx->{used_ids}{$id};
Branch: 1 branch
BranchTrueFalseTotal
$ctx->{"used_ids"}{$id} and do { do { $id = '' . ++$ctx->{"next_id"} } } 1 2089 2090
103 2089 return $id;
104 }
105  
106 168 ▶sub _alloc_group {
Subroutine _alloc_group: called CC=2 SCAR=6.9
107 168 my ($ctx) = @_;
108 168 my $group;
109 168 ▶ do { $group = 'item' . ++$ctx->{next_group} } while $ctx->{used_groups}{$group};
Branch: 1 branch
BranchTrueFalseTotal
$ctx->{"used_groups"}{$group} and do { do { $group = 'item' . ++$ctx->{"next_group"} } } 22 168 190
110 168 $ctx->{used_groups}{$group} = 1;
111 168 return $group;
112 }
113  
114 2706 ▶sub _prop_id {
Subroutine _prop_id: called CC=3 SCAR=11.0
115 2706 my ($prop, $ctx) = @_;
116 2706 my $pid = _param($prop, 'prop-id');
117 2706 ▶ $pid = _alloc_id($ctx) unless defined $pid && $pid ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid and $pid ne "" 2089 617 2706
Condition66%2/3
defined $pid and $pid ne ""
!ll&&!rl&&r
20890617
MC/DC50%1/2
defined $pid and $pid ne ""
defined $pid $pid ne ""
Truth table66%2/3
A and B
A= defined $pidB= $pid ne ""
ABresult
0X0
100
111
118 2706 push @{$ctx->{pending}}, [$pid, $prop];
119 2706 return $pid;
120 }
121  
122 # A vCard read from a CardDAV server is UTF-8 bytes (RFC 6350 S3.1).
123 # Decode it once, before parsing, so every value is characters; one that
124 # is not valid UTF-8 is read as Latin-1, a character per byte. A string
125 # that is characters already is left alone.
126 256 ▶sub _decode_input {
Subroutine _decode_input: called CC=5 SCAR=16.2
127 256 my ($s) = @_;
128 256 ▶ return $s if !defined $s || utf8::is_utf8($s) || $s !~ /[\x80-\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $s or utf8::is_utf8($s) or not $s =~ /[\x80-\xff]/ 207 49 256
Condition83%5/6
not defined $s or utf8::is_utf8($s) or not $s =~ /[\x80-\xff]/
l!l&&r!l&&!r
320449
not defined $s or utf8::is_utf8($s)
l!l&&r!l&&!r
03253
MC/DC66%2/3
not defined $s or utf8::is_utf8($s) or not $s =~ /[\x80-\xff]/
not defined $s utf8::is_utf8($s) not $s =~ /[\x80-\xff]/
Truth table75%3/4
not(A) or B or not(C)
A= not defined $sB= utf8::is_utf8($s)C= not $s =~ /[\x80-\xff]/
ABCresult
1XX1
01X1
0011
0000
129 49 my $bytes = $s;
130 49 my $chars = eval { Encode::decode('UTF-8', $bytes, Encode::FB_CROAK) };
131 49 ▶ return defined $chars ? $chars : $s;
Branch: 1 branch
BranchTrueFalseTotal
defined $chars ? : 49 0 49
132 }
133  
134 1887 ▶sub _make_contexts {
Subroutine _make_contexts: called CC=5 SCAR=16.1
135 1887 my @types = @_;
136 1887 my %ctx;
137 1887 for my $t (@types) {
138 1584 my $lt = lc $t;
139 1584 ▶ if ($lt eq 'home') { $ctx{private} = JSON::true }
Branch: 2 branches
BranchTrueFalseTotal
if ($lt eq 'home') { } 315 1269 1584
elsif ($lt eq 'work') { } 435 834 1269
140 435 elsif ($lt eq 'work') { $ctx{work} = JSON::true }
141 }
142 1887 ▶ return %ctx ? \%ctx : undef;
Branch: 1 branch
BranchTrueFalseTotal
%ctx ? : 750 1137 1887
143 }
144  
145 1881 ▶sub _make_pref {
Subroutine _make_pref: called CC=5 SCAR=16.1
146 1881 my $prop = shift;
147 # vCard 4: PREF parameter
148 1881 my $pref = _param($prop, 'pref');
149 1881 ▶ if (defined $pref && looks_like_number($pref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pref and &looks_like_number($pref)) 358 1523 1881
Condition66%2/3
defined $pref and &looks_like_number($pref)
!ll&&!rl&&r
15230358
MC/DC50%1/2
defined $pref and &looks_like_number($pref)
defined $pref &looks_like_number($pref)
Truth table66%2/3
A and B
A= defined $prefB= &looks_like_number($pref)
ABresult
0X0
100
111
150 358 return $pref + 0;
151 }
152 # vCard 3: TYPE=pref
153 1523 for my $t (_params_list($prop, 'type')) {
154 1417 ▶ return 1 if lc $t eq 'pref';
Branch: 1 branch
BranchTrueFalseTotal
if lc $t eq "pref" 232 1185 1417
155 }
156 1291 return undef;
157 }
158  
159 153 ▶sub _to_utc_datetime {
Subroutine _to_utc_datetime: called CC=3 SCAR=12.0
160 153 my $val = shift;
161 153 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 153 153
162 # Normalize compact timestamps (e.g. 19940930T143510Z -> 1994-09-30T14:35:10Z)
163 153 ▶ if ($val =~ /^(\d{4})-?(\d{2})-?(\d{2})T(\d{2}):?(\d{2}):?(\d{2})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(\d{4})-?(\d{2})-?(\d{2})T(\d{2}):?(\d{2}):?(\d{2})/) 153 0 153
164 153 return "$1-$2-$3T$4:$5:$6Z";
165 }
166 0 return $val;
167 }
168  
169 1518 ▶sub _set_if {
Subroutine _set_if: called CC=5 SCAR=16.3
170 1518 my ($hash, $key, $val, $transform) = @_;
171 1518 ▶ return unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 960 558 1518
Condition66%2/3
defined $val and $val ne ""
!ll&&!rl&&r
9600558
MC/DC50%1/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table66%2/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
172 558 ▶ $val = $transform->($val) if $transform;
Branch: 1 branch
BranchTrueFalseTotal
if $transform 186 372 558
173 558 ▶ $hash->{$key} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 558 0 558
174 }
175  
176 # Collect X-ABLabel associations (Apple extension)
177 3476 ▶sub _collect_labels {
Subroutine _collect_labels: called CC=6 SCAR=18.0
178 3476 my $props = shift;
179 3476 my %labels;
180 3476 ▶ for my $lp (@{$props->{'x-ablabel'} // []}) {
Condition100%2/2
$props->{"x-ablabel"} // []
l!l
13782098
MC/DC100%1/1
$props->{"x-ablabel"} // []
$props->{"x-ablabel"}
Truth table100%2/2
A
A= $props->{"x-ablabel"}
Aresult
11
00
181 9433 ▶ my $group = $lp->{group} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $group = $lp->{"group"} or next 9433 0 9433
182 9433 ▶ my $val = $lp->{value} // '';
Condition50%1/2
$lp->{"value"} // ""
l!l
94330
MC/DC0%0/1
$lp->{"value"} // ""
$lp->{"value"}
Truth table50%1/2
A
A= $lp->{"value"}
Aresult
11
00
183 9433 ▶ $val = $1 if $val =~ m{^_\$\!<([^>]*)>\!\$_$};
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^_\$\!<([^>]*)>\!\$_$/ 3600 5833 9433
184 9433 $labels{$group} = $val;
185 }
186 3476 return \%labels;
187 }
188  
189 1630 ▶sub _apply_label {
Subroutine _apply_label: called CC=3 SCAR=11.0
190 1630 my ($obj, $prop, $labels) = @_;
191 1630 ▶ if (my $group = $prop->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $prop->{"group"}) 481 1149 1630
192 481 ▶ if (my $label = $labels->{$group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $label = $labels->{$group}) 332 149 481
193 332 $obj->{label} = $label;
194 }
195 }
196 }
197  
198 299 ▶sub _convert_vcard_date {
Subroutine _convert_vcard_date: called CC=6 SCAR=19.6
199 299 my $val = shift;
200 299 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 299 299
201  
202 # vCard 4 compact: YYYYMMDD or --MMDD
203 299 ▶ if ($val =~ /^(\d{4}|--)-?(\d{2})-?(\d{2})(?:T|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(\d{4}|--)-?(\d{2})-?(\d{2})(?:T|$)/) 299 0 299
204 299 my ($y, $m, $d) = ($1, $2, $3);
205 299 ▶ $y = '0000' if $y eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if $y eq "--" 31 268 299
206 299 ▶ $y = '0000' if $y eq '1604'; # iOS "no year" magic
Branch: 1 branch
BranchTrueFalseTotal
if $y eq 1604 37 262 299
207 299 return "$y-$m-$d";
208 }
209  
210 # Already ISO format
211 0 ▶ return $val if $val =~ /^\d{4}-\d{2}-\d{2}/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^\d{4}-\d{2}-\d{2}/ 0 0 0
212  
213 0 return $val;
214 }
215  
216 1068 ▶sub _adr_values {
Subroutine _adr_values: called CC=5 SCAR=19.1
217 1068 my ($values, $idx) = @_;
218 1068 my $val = $values->[$idx];
219 1068 ▶ return () unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 1068 1068
220 1068 ▶ if (ref $val eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY") 0 1068 1068
221 0 ▶ return grep { defined $_ && $_ ne '' } @$val;
Condition0%0/3
defined $_ && $_ ne ''
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table0%0/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
222 }
223 1068 ▶ return ($val) if $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $val ne "" 924 144 1068
224 144 return ();
225 }
226  
227 ###############################################################################
228 # VCARD -> JSCONTACT
229 ###############################################################################
230  
231 202 ▶sub vcard_to_jscontact {
Subroutine vcard_to_jscontact: called CC=1 SCAR=0.0
Pod undocumented
232 202 my ($vcard_string) = @_;
233 202 my ($card) = _read_vcard(_decode_input($vcard_string));
234 202 return $card;
235 }
236  
237 # The converters that fill a map of the Card, each with the key it fills:
238 # the ids a converter allocates are recorded against that key, so a patch
239 # knows which vCard property each entry came from
240 19 my @MAP_CONVERTERS = (
241 [ nicknames => \&_convert_nicknames ],
242 [ emails => \&_convert_emails ],
243 [ phones => \&_convert_phones ],
244 [ addresses => \&_convert_addresses ],
245 [ organizations => \&_convert_organizations ],
246 [ titles => \&_convert_titles ],
247 [ anniversaries => \&_convert_anniversaries ],
248 [ notes => \&_convert_notes ],
249 [ onlineServices => \&_convert_online_services ],
250 [ media => \&_convert_media ],
251 [ links => \&_convert_links ],
252 [ calendars => \&_convert_calendars ],
253 [ schedulingAddresses => \&_convert_scheduling_addresses ],
254 [ cryptoKeys => \&_convert_crypto_keys ],
255 [ directories => \&_convert_directories ],
256 [ preferredLanguages => \&_convert_preferred_languages ],
257 [ personalInfo => \&_convert_personal_info ],
258 );
259  
260 # Read a vCard given as characters. Returns the Card, the conversion
261 # context (whose {sources} maps each Card entry to the vCard properties it
262 # was read from) and the parsed properties; nothing if it does not parse.
263 256 ▶sub _read_vcard {
Subroutine _read_vcard: called CC=8 SCAR=20.9
264 256 my ($vcard_string) = @_;
265  
266 256 my $parsed = eval { vcard2hash($vcard_string, multival => [qw(n adr org)]) };
267 256 ▶ return unless $parsed && $parsed->{objects} && @{$parsed->{objects}};
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed and $parsed->{"objects"} and @{$parsed->{"objects"};} 3 253 256
Condition83%5/6
$parsed and $parsed->{"objects"} and @{$parsed->{"objects"};}
!ll&&!rl&&r
30253
$parsed and $parsed->{"objects"}
!ll&&!rl&&r
21253
MC/DC66%2/3
$parsed and $parsed->{"objects"} and @{$parsed->{"objects"};}
$parsed $parsed->{"objects"} @{$parsed->{"objects"};}
Truth table75%3/4
A and B and C
A= $parsedB= $parsed->{"objects"}C= @{$parsed->{"objects"};}
ABCresult
0XX0
10X0
1100
1111
268  
269 253 my $vcard = $parsed->{objects}[0];
270 253 ▶ return unless $vcard && $vcard->{type} eq 'vcard';
Branch: 1 branch
BranchTrueFalseTotal
unless $vcard and $vcard->{"type"} eq "vcard" 0 253 253
Condition33%1/3
$vcard and $vcard->{"type"} eq "vcard"
!ll&&!rl&&r
00253
MC/DC0%0/2
$vcard and $vcard->{"type"} eq "vcard"
$vcard $vcard->{"type"} eq "vcard"
Truth table33%1/3
A and B
A= $vcardB= $vcard->{"type"} eq "vcard"
ABresult
0X0
100
111
271  
272 253 my $props = $vcard->{properties};
273 253 my $ctx = _seed_context(_new_context(), $props);
274  
275 253 my $card = {
276 '@type' => 'Card',
277 version => '2.0',
278 };
279  
280 # UID: optional since JSContact 2.0 (RFC 9982), which forbids making one
281 # up for a vCard that has none -- a protocol that needs a uid (CardDAV,
282 # JMAP Contacts) assigns it itself
283 253 _set_if($card, 'uid', _first_value($props, 'uid'));
284  
285 # Simple scalar properties
286 _set_if($card, 'kind', _first_value($props, 'kind')
287 // _first_value($props, 'x-addressbookserver-kind'),
288 253 ▶ sub { lc $_[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
_first_value($props, "kind") // _first_value($props, "x-addressbookserver-kind")
l!l&&r!l&&!r
4520188
MC/DC100%2/2
_first_value($props, "kind") // _first_value($props, "x-addressbookserver-kind")
_first_value($props, "kind") _first_value($props, "x-addressbookserver-kind")
Truth table100%3/3
A // B
A= _first_value($props, "kind")B= _first_value($props, "x-addressbookserver-kind")
ABresult
1X1
011
000
289 253 _set_if($card, 'prodId', _first_value($props, 'prodid'));
290 253 _set_if($card, 'updated', _first_value($props, 'rev'), \&_to_utc_datetime);
291 253 _set_if($card, 'created', _first_value($props, 'created'), \&_to_utc_datetime);
292 253 _set_if($card, 'language', _first_value($props, 'language'));
293  
294 # Complex properties
295 253 _convert_name($card, $props);
296 253 for my $conv (@MAP_CONVERTERS) {
297 4301 my ($key, $fn) = @$conv;
298 4301 $fn->($card, $props, $ctx);
299 4301 _claim_sources($ctx, $key);
300 }
301 253 _convert_keywords($card, $props);
302 253 _convert_members($card, $props, $ctx);
303 253 _convert_related($card, $props, $ctx);
304 253 _convert_speak_to_as($card, $props, $ctx);
305 253 _claim_sources($ctx, 'speakToAs');
306  
307 253 return ($card, $ctx, $props);
308 }
309  
310 # The ids handed out since the last claim belong to $key
311 4554 ▶sub _claim_sources {
Subroutine _claim_sources: called CC=3 SCAR=11.0
312 4554 my ($ctx, $key) = @_;
313 4554 ▶ for my $pair (@{$ctx->{pending} // []}) {
Condition100%2/2
$ctx->{"pending"} // []
l!l
4397157
MC/DC100%1/1
$ctx->{"pending"} // []
$ctx->{"pending"}
Truth table100%2/2
A
A= $ctx->{"pending"}
Aresult
11
00
314 2706 my ($id, $item) = @$pair;
315 2706 push @{$ctx->{sources}{$key}{$id}}, $item;
316 }
317 4554 $ctx->{pending} = [];
318 }
319  
320 ###############################################################################
321 # INDIVIDUAL VCARD -> JSCONTACT CONVERTERS
322 ###############################################################################
323  
324 # FN + N -> name
325 253 ▶sub _convert_name {
Subroutine _convert_name: called CC=25 SCAR=33.7
326 253 my ($card, $props) = @_;
327 253 my $name = {};
328  
329 253 my $fn = _first_value($props, 'fn');
330 253 ▶ $name->{full} = $fn if defined $fn;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fn 251 2 253
331  
332 253 my $n_prop = _first_prop($props, 'n');
333 253 ▶ if ($n_prop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_prop) 189 64 253
334 189 ▶ my $values = $n_prop->{values} || [];
Condition50%1/2
$n_prop->{'values'} || []
l!l
1890
MC/DC0%0/1
$n_prop->{'values'} || []
$n_prop->{'values'}
Truth table50%1/2
A
A= $n_prop->{'values'}
Aresult
11
00
335 # N component order: surname, given, given2, title(prefix), credential(suffix), surname2, generation
336 189 my @kind_map = qw(surname given given2 title credential surname2 generation);
337  
338 189 my @components;
339 189 for my $idx (0..$#kind_map) {
340 1323 my $raw = $values->[$idx];
341 1323 ▶ next unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 420 903 1323
342 # Handle both arrayref and string; split comma-separated values
343 903 my @vals;
344 903 ▶ for my $v (ref $raw eq 'ARRAY' ? @$raw : ($raw)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $raw eq 'ARRAY' ? : 0 903 903
345 903 push @vals, split /,/, $v;
346 }
347 903 for my $v (@vals) {
348 653 ▶ next unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 0 653 653
Condition33%1/3
defined $v and $v ne ""
!ll&&!rl&&r
00653
MC/DC0%0/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table33%1/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
349 653 push @components, {
350 '@type' => 'NameComponent',
351 kind => $kind_map[$idx],
352 value => $v,
353 };
354 }
355 }
356  
357 189 ▶ $name->{components} = \@components if @components;
Branch: 1 branch
BranchTrueFalseTotal
if @components 168 21 189
358  
359 # SORT-AS parameter
360 189 my $sort_as_param = _param($n_prop, 'sort-as');
361 189 ▶ if ($sort_as_param) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_as_param) 32 157 189
362 32 my @sa = split /,/, $sort_as_param;
363 32 my %sort_map;
364 32 for my $i (0..$#sa) {
365 64 ▶ $sort_map{$kind_map[$i]} = $sa[$i]
Branch: 1 branch
BranchTrueFalseTotal
if $i <= $#kind_map and defined $sa[$i] and $sa[$i] ne "" 64 0 64
Condition33%2/6
$i <= $#kind_map and defined $sa[$i] and $sa[$i] ne ""
!ll&&!rl&&r
0064
$i <= $#kind_map and defined $sa[$i]
!ll&&!rl&&r
0064
MC/DC0%0/3
$i <= $#kind_map and defined $sa[$i] and $sa[$i] ne ""
$i <= $#kind_map defined $sa[$i] $sa[$i] ne ""
Truth table25%1/4
A and B and C
A= $i <= $#kind_mapB= defined $sa[$i]C= $sa[$i] ne ""
ABCresult
0XX0
10X0
1100
1111
366 if $i <= $#kind_map && defined $sa[$i] && $sa[$i] ne '';
367 }
368 32 ▶ $name->{sortAs} = \%sort_map if %sort_map;
Branch: 1 branch
BranchTrueFalseTotal
if %sort_map 32 0 32
369 }
370 }
371  
372 # Apple X-PHONETIC-*-NAME -> phonetic name components
373 253 my $phonFirst = _first_value($props, 'x-phonetic-first-name');
374 253 my $phonLast = _first_value($props, 'x-phonetic-last-name');
375 253 my $phonMid = _first_value($props, 'x-phonetic-middle-name');
376 253 ▶ if ($phonFirst || $phonLast || $phonMid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phonFirst or $phonLast or $phonMid) 38 215 253
Condition66%4/6
$phonFirst or $phonLast or $phonMid
l!l&&r!l&&!r
380215
$phonFirst or $phonLast
l!l&&r!l&&!r
380215
MC/DC33%1/3
$phonFirst or $phonLast or $phonMid
$phonFirst $phonLast $phonMid
Truth table50%2/4
A or B or C
A= $phonFirstB= $phonLastC= $phonMid
ABCresult
1XX1
01X1
0011
0000
377 38 my @phon;
378 38 ▶ push @phon, { '@type' => 'NameComponent', kind => 'given', value => $phonFirst, phonetic => 'ipa' } if $phonFirst;
Branch: 1 branch
BranchTrueFalseTotal
if $phonFirst 38 0 38
379 38 ▶ push @phon, { '@type' => 'NameComponent', kind => 'given2', value => $phonMid, phonetic => 'ipa' } if $phonMid;
Branch: 1 branch
BranchTrueFalseTotal
if $phonMid 37 1 38
380 38 ▶ push @phon, { '@type' => 'NameComponent', kind => 'surname', value => $phonLast, phonetic => 'ipa' } if $phonLast;
Branch: 1 branch
BranchTrueFalseTotal
if $phonLast 38 0 38
381 38 $name->{phoneticComponents} = \@phon;
382 }
383  
384 253 ▶ $card->{name} = $name if %$name;
Branch: 1 branch
BranchTrueFalseTotal
if %$name 251 2 253
385 }
386  
387 # NICKNAME -> nicknames
388 253 ▶sub _convert_nicknames {
Subroutine _convert_nicknames: called CC=8 SCAR=21.1
389 253 my ($card, $props, $ctx) = @_;
390 253 ▶ my $items = $props->{nickname} || return;
Condition100%2/2
$props->{'nickname'} || (return)
l!l
96157
MC/DC100%1/1
$props->{'nickname'} || (return)
$props->{'nickname'}
Truth table100%2/2
A
A= $props->{'nickname'}
Aresult
11
00
391  
392 96 my %map;
393 96 for my $item (@$items) {
394 97 my $val = $item->{value};
395 97 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 97 97
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0097
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
396 97 my $id = _prop_id($item, $ctx);
397 97 my $obj = { '@type' => 'Nickname', name => $val };
398 97 ▶ if (my $ctx = _make_contexts(_params_list($item, 'type'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(_params_list($item, "type"))) 18 79 97
399 18 $obj->{contexts} = $ctx;
400 }
401 97 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 1 96 97
402 97 $map{$id} = $obj;
403 }
404  
405 96 ▶ $card->{nicknames} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 96 0 96
406 }
407  
408 # EMAIL -> emails
409 253 ▶sub _convert_emails {
Subroutine _convert_emails: called CC=8 SCAR=21.0
410 253 my ($card, $props, $ctx) = @_;
411 253 ▶ my $items = $props->{email} || return;
Condition100%2/2
$props->{'email'} || (return)
l!l
15796
MC/DC100%1/1
$props->{'email'} || (return)
$props->{'email'}
Truth table100%2/2
A
A= $props->{'email'}
Aresult
11
00
412 157 my $labels = _collect_labels($props);
413  
414 157 my %map;
415 157 for my $item (@$items) {
416 303 my $val = $item->{value};
417 303 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 303 303
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00303
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
418 303 my $id = _prop_id($item, $ctx);
419 303 my $obj = { '@type' => 'EmailAddress', address => $val };
420 303 ▶ if (my $ctx = _make_contexts(_params_list($item, 'type'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(_params_list($item, "type"))) 219 84 303
421 219 $obj->{contexts} = $ctx;
422 }
423 303 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 100 203 303
424 303 _apply_label($obj, $item, $labels);
425 303 $map{$id} = $obj;
426 }
427  
428 157 ▶ $card->{emails} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 157 0 157
429 }
430  
431 # TEL -> phones
432 253 ▶sub _convert_phones {
Subroutine _convert_phones: called CC=11 SCAR=24.1
433 253 my ($card, $props, $ctx) = @_;
434 253 ▶ my $items = $props->{tel} || return;
Condition100%2/2
$props->{'tel'} || (return)
l!l
144109
MC/DC100%1/1
$props->{'tel'} || (return)
$props->{'tel'}
Truth table100%2/2
A
A= $props->{'tel'}
Aresult
11
00
435 144 my $labels = _collect_labels($props);
436  
437 144 my %tel_feature_map = (
438 cell => 'mobile', voice => 'voice', fax => 'fax',
439 video => 'video', pager => 'pager', text => 'text',
440 textphone => 'textphone',
441 );
442  
443 144 my %map;
444 144 for my $item (@$items) {
445 351 my $val = $item->{value};
446 351 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 351 351
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00351
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
447 351 my $id = _prop_id($item, $ctx);
448 351 my $obj = { '@type' => 'Phone', number => $val };
449  
450 351 my @types = _params_list($item, 'type');
451 351 ▶ if (my $ctx = _make_contexts(@types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 175 176 351
452 175 $obj->{contexts} = $ctx;
453 }
454  
455 351 my %features;
456 351 for my $t (@types) {
457 555 my $feat = $tel_feature_map{lc $t};
458 555 ▶ $features{$feat} = JSON::true if $feat;
Branch: 1 branch
BranchTrueFalseTotal
if $feat 321 234 555
459 }
460 351 ▶ $obj->{features} = \%features if %features;
Branch: 1 branch
BranchTrueFalseTotal
if %features 225 126 351
461  
462 351 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 85 266 351
463 351 _apply_label($obj, $item, $labels);
464 351 $map{$id} = $obj;
465 }
466  
467 144 ▶ $card->{phones} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 144 0 144
468 }
469  
470 # ADR -> addresses (+ GEO, TZ by group association)
471 253 ▶sub _convert_addresses {
Subroutine _convert_addresses: called CC=34 SCAR=35.7
472 253 my ($card, $props, $ctx) = @_;
473 253 ▶ my $items = $props->{adr} || return;
Condition100%2/2
$props->{'adr'} || (return)
l!l
139114
MC/DC100%1/1
$props->{'adr'} || (return)
$props->{'adr'}
Truth table100%2/2
A
A= $props->{'adr'}
Aresult
11
00
474 139 my $labels = _collect_labels($props);
475  
476 # Collect grouped GEO/TZ for association with ADR
477 139 my %geo_by_group;
478 my %tz_by_group;
479 139 ▶ for my $geo (@{$props->{geo} // []}) {
Condition100%2/2
$props->{"geo"} // []
l!l
12127
MC/DC100%1/1
$props->{"geo"} // []
$props->{"geo"}
Truth table100%2/2
A
A= $props->{"geo"}
Aresult
11
00
480 12 ▶ $geo_by_group{$geo->{group}} = $geo->{value} if $geo->{group};
Branch: 1 branch
BranchTrueFalseTotal
if $geo->{"group"} 0 12 12
481 }
482 139 ▶ for my $tz (@{$props->{tz} // []}) {
Condition100%2/2
$props->{"tz"} // []
l!l
12127
MC/DC100%1/1
$props->{"tz"} // []
$props->{"tz"}
Truth table100%2/2
A
A= $props->{"tz"}
Aresult
11
00
483 12 ▶ $tz_by_group{$tz->{group}} = $tz->{value} if $tz->{group};
Branch: 1 branch
BranchTrueFalseTotal
if $tz->{"group"} 0 12 12
484 }
485  
486 139 my %map;
487 139 for my $item (@$items) {
488 178 my $id = _prop_id($item, $ctx);
489 178 ▶ my $values = $item->{values} || [];
Condition50%1/2
$item->{'values'} || []
l!l
1780
MC/DC0%0/1
$item->{'values'} || []
$item->{'values'}
Truth table50%1/2
A
A= $item->{'values'}
Aresult
11
00
490 178 my $obj = { '@type' => 'Address' };
491  
492 # Build components from ADR structured value
493 # Indices: 0=POBox 1=Extended 2=Street 3=Locality 4=Region 5=PostCode 6=Country
494 178 my @components;
495 178 for my $v (_adr_values($values, 2)) {
496 177 push @components, { '@type' => 'AddressComponent', kind => 'name', value => $v };
497 }
498 178 for my $v (_adr_values($values, 1)) {
499 68 push @components, { '@type' => 'AddressComponent', kind => 'apartment', value => $v };
500 }
501 178 for my $v (_adr_values($values, 3)) {
502 178 push @components, { '@type' => 'AddressComponent', kind => 'locality', value => $v };
503 }
504 178 for my $v (_adr_values($values, 4)) {
505 152 push @components, { '@type' => 'AddressComponent', kind => 'region', value => $v };
506 }
507 178 for my $v (_adr_values($values, 5)) {
508 174 push @components, { '@type' => 'AddressComponent', kind => 'postcode', value => $v };
509 }
510 178 for my $v (_adr_values($values, 6)) {
511 175 push @components, { '@type' => 'AddressComponent', kind => 'country', value => $v };
512 }
513 178 ▶ $obj->{components} = \@components if @components;
Branch: 1 branch
BranchTrueFalseTotal
if @components 178 0 178
514  
515 # Parameters
516 178 my $label_param = _param($item, 'label');
517 178 ▶ $obj->{full} = $label_param if defined $label_param;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label_param 18 160 178
518  
519 178 my $geo = _param($item, 'geo');
520 178 ▶ $geo //= $geo_by_group{$item->{group}} if $item->{group};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"group"} 96 82 178
Condition33%1/3
$geo //= $geo_by_group{$item->{"group"}}
l!l&&r!l&&!r
0096
MC/DC0%0/2
$geo //= $geo_by_group{$item->{"group"}}
$geo $geo_by_group{$item->{"group"}}
Truth table33%1/3
A //= B
A= $geoB= $geo_by_group{$item->{"group"}}
ABresult
1X1
011
000
521 178 ▶ $obj->{coordinates} = $geo if defined $geo;
Branch: 1 branch
BranchTrueFalseTotal
if defined $geo 17 161 178
522  
523 178 my $tz = _param($item, 'tz');
524 178 ▶ $tz //= $tz_by_group{$item->{group}} if $item->{group};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"group"} 96 82 178
Condition33%1/3
$tz //= $tz_by_group{$item->{"group"}}
l!l&&r!l&&!r
0096
MC/DC0%0/2
$tz //= $tz_by_group{$item->{"group"}}
$tz $tz_by_group{$item->{"group"}}
Truth table33%1/3
A //= B
A= $tzB= $tz_by_group{$item->{"group"}}
ABresult
1X1
011
000
525 178 ▶ $obj->{timeZone} = $tz if defined $tz;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tz 17 161 178
526  
527 178 my $cc = _param($item, 'cc');
528 # Apple X-ABADR grouped with ADR
529 178 ▶ if (!$cc && $item->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cc and $item->{"group"}) 96 82 178
Condition100%3/3
not $cc and $item->{"group"}
!ll&&!rl&&r
354796
MC/DC100%2/2
not $cc and $item->{"group"}
not $cc $item->{"group"}
Truth table100%3/3
not(A) and B
A= not $ccB= $item->{"group"}
ABresult
0X0
100
111
530 96 ▶ for my $xabadr (@{$props->{'x-abadr'} // []}) {
Condition50%1/2
$props->{"x-abadr"} // []
l!l
960
MC/DC0%0/1
$props->{"x-abadr"} // []
$props->{"x-abadr"}
Truth table50%1/2
A
A= $props->{"x-abadr"}
Aresult
11
00
531 133 ▶ if (($xabadr->{group} // '') eq $item->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (($xabadr->{"group"} // "") eq $item->{"group"}) 96 37 133
Condition50%1/2
$xabadr->{"group"} // ""
l!l
1330
MC/DC0%0/1
$xabadr->{"group"} // ""
$xabadr->{"group"}
Truth table50%1/2
A
A= $xabadr->{"group"}
Aresult
11
00
532 96 $cc = $xabadr->{value};
533 96 last;
534 }
535 }
536 }
537 178 ▶ $obj->{countryCode} = $cc if defined $cc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cc 131 47 178
538  
539 178 my @types = _params_list($item, 'type');
540 178 ▶ if (my $ctx = _make_contexts(@types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 175 3 178
541 175 $obj->{contexts} = $ctx;
542 }
543 178 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 79 99 178
544 178 _apply_label($obj, $item, $labels);
545  
546 178 $map{$id} = $obj;
547 }
548  
549 139 ▶ $card->{addresses} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 139 0 139
550 }
551  
552 # ORG -> organizations
553 253 ▶sub _convert_organizations {
Subroutine _convert_organizations: called CC=22 SCAR=34.1
554 253 my ($card, $props, $ctx) = @_;
555 253 ▶ my $items = $props->{org} || return;
Condition100%2/2
$props->{'org'} || (return)
l!l
138115
MC/DC100%1/1
$props->{'org'} || (return)
$props->{'org'}
Truth table100%2/2
A
A= $props->{'org'}
Aresult
11
00
556  
557 138 my %map;
558 138 for my $item (@$items) {
559 138 my $id = _prop_id($item, $ctx);
560 138 ▶ my $values = $item->{values} || [];
Condition50%1/2
$item->{'values'} || []
l!l
1380
MC/DC0%0/1
$item->{'values'} || []
$item->{'values'}
Truth table50%1/2
A
A= $item->{'values'}
Aresult
11
00
561 138 my $obj = { '@type' => 'Organization' };
562  
563 # First component -> name
564 0 ▶ my @name_vals = ref $values->[0] eq 'ARRAY' ? @{$values->[0]} : ($values->[0] // ());
Branch: 1 branch
BranchTrueFalseTotal
ref $values->[0] eq 'ARRAY' ? : 0 138 138
Condition33%1/3
$values->[0] // ()
l!l&&r!l&&!r
13800
MC/DC0%0/2
$values->[0] // ()
$values->[0] ()
Truth table33%1/3
A // B
A= $values->[0]B= ()
ABresult
1X1
011
000
565 138 ▶ my $org_name = join(' ', grep { defined $_ && $_ ne '' } @name_vals);
Condition33%1/3
defined $_ && $_ ne ''
!ll&&!rl&&r
00138
MC/DC0%0/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table33%1/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
566 138 ▶ $obj->{name} = $org_name if $org_name ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $org_name ne "" 138 0 138
567  
568 # Subsequent components -> units
569 138 my @units;
570 138 for my $i (1..$#$values) {
571 0 ▶ my @vals = ref $values->[$i] eq 'ARRAY' ? @{$values->[$i]} : ($values->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
ref $values->[$i] eq 'ARRAY' ? : 0 167 167
572 167 for my $v (@vals) {
573 167 ▶ next unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 0 167 167
Condition33%1/3
defined $v and $v ne ""
!ll&&!rl&&r
00167
MC/DC0%0/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table33%1/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
574 167 push @units, { '@type' => 'OrgUnit', name => $v };
575 }
576 }
577 138 ▶ $obj->{units} = \@units if @units;
Branch: 1 branch
BranchTrueFalseTotal
if @units 97 41 138
578  
579 # SORT-AS
580 138 my $sort_as = _param($item, 'sort-as');
581 138 ▶ if (defined $sort_as) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sort_as) 32 106 138
582 32 my @sa = split /,/, $sort_as;
583 32 ▶ $obj->{sortAs} = $sa[0] if defined $sa[0] && $sa[0] ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $sa[0] and $sa[0] ne "" 32 0 32
Condition33%1/3
defined $sa[0] and $sa[0] ne ""
!ll&&!rl&&r
0032
MC/DC0%0/2
defined $sa[0] and $sa[0] ne ""
defined $sa[0] $sa[0] ne ""
Truth table33%1/3
A and B
A= defined $sa[0]B= $sa[0] ne ""
ABresult
0X0
100
111
584 32 for my $i (0..$#units) {
585 64 ▶ $units[$i]{sortAs} = $sa[$i+1] if defined $sa[$i+1] && $sa[$i+1] ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $sa[$i + 1] and $sa[$i + 1] ne "" 1 63 64
Condition66%2/3
defined $sa[$i + 1] and $sa[$i + 1] ne ""
!ll&&!rl&&r
6301
MC/DC50%1/2
defined $sa[$i + 1] and $sa[$i + 1] ne ""
defined $sa[$i + 1] $sa[$i + 1] ne ""
Truth table66%2/3
A and B
A= defined $sa[$i + 1]B= $sa[$i + 1] ne ""
ABresult
0X0
100
111
586 }
587 }
588  
589 138 my @types = _params_list($item, 'type');
590 138 ▶ if (my $ctx = _make_contexts(@types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 14 124 138
591 14 $obj->{contexts} = $ctx;
592 }
593  
594 138 $map{$id} = $obj;
595 }
596  
597 138 ▶ $card->{organizations} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 138 0 138
598 }
599  
600 # TITLE + ROLE -> titles
601 253 ▶sub _convert_titles {
Subroutine _convert_titles: called CC=7 SCAR=19.7
602 253 my ($card, $props, $ctx) = @_;
603  
604 253 my %map;
605 253 for my $spec (['title', 'title'], ['role', 'role']) {
606 506 my ($propname, $kind) = @$spec;
607 506 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
128378
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
608 142 my $val = $item->{value};
609 142 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 142 142
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00142
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
610 142 my $id = _prop_id($item, $ctx);
611 142 $map{$id} = { '@type' => 'Title', kind => $kind, name => $val };
612 }
613 }
614  
615 253 ▶ $card->{titles} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 109 144 253
616 }
617  
618 # BDAY, ANNIVERSARY, DEATHDATE, X-ABDATE -> anniversaries
619 253 ▶sub _convert_anniversaries {
Subroutine _convert_anniversaries: called CC=15 SCAR=28.2
620 253 my ($card, $props, $ctx) = @_;
621 253 my $labels = _collect_labels($props);
622  
623 253 my %map;
624 253 for my $spec (['bday', 'birth'], ['anniversary', 'wedding'], ['deathdate', 'death']) {
625 759 my ($propname, $kind) = @$spec;
626 759 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
180579
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
627 183 my $val = $item->{value};
628 183 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 183 183
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00183
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
629 183 my $id = _prop_id($item, $ctx);
630 183 $map{$id} = {
631 '@type' => 'Anniversary',
632 kind => $kind,
633 date => _convert_vcard_date($val),
634 };
635 }
636 }
637  
638 # Apple X-ABDATE + X-ABLabel -> anniversaries
639 253 ▶ for my $item (@{$props->{'x-abdate'} // []}) {
Condition100%2/2
$props->{"x-abdate"} // []
l!l
58195
MC/DC100%1/1
$props->{"x-abdate"} // []
$props->{"x-abdate"}
Truth table100%2/2
A
A= $props->{"x-abdate"}
Aresult
11
00
640 116 my $val = $item->{value};
641 116 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 116 116
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00116
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
642 116 my $id = _prop_id($item, $ctx);
643  
644 116 my $kind = 'other';
645 116 ▶ if (my $group = $item->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $item->{"group"}) 116 0 116
646 116 my $label = $labels->{$group};
647 116 ▶ if ($label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label) 116 0 116
648 116 my $lt = lc $label;
649 116 ▶ if ($lt eq 'anniversary') { $kind = 'wedding' }
Branch: 2 branches
BranchTrueFalseTotal
if ($lt eq 'anniversary') { } 58 58 116
elsif ($lt eq 'other') { } 0 58 58
650 0 elsif ($lt eq 'other') { $kind = 'other' }
651 58 else { $kind = $lt }
652 }
653 }
654  
655 116 $map{$id} = {
656 '@type' => 'Anniversary',
657 kind => $kind,
658 date => _convert_vcard_date($val),
659 };
660 }
661  
662 253 ▶ $card->{anniversaries} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 116 137 253
663 }
664  
665 # NOTE -> notes
666 253 ▶sub _convert_notes {
Subroutine _convert_notes: called CC=11 SCAR=24.4
667 253 my ($card, $props, $ctx) = @_;
668 253 ▶ my $items = $props->{note} || return;
Condition100%2/2
$props->{'note'} || (return)
l!l
141112
MC/DC100%1/1
$props->{'note'} || (return)
$props->{'note'}
Truth table100%2/2
A
A= $props->{'note'}
Aresult
11
00
669  
670 141 my %map;
671 141 for my $item (@$items) {
672 156 my $val = $item->{value};
673 156 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 156 156
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00156
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
674 156 my $id = _prop_id($item, $ctx);
675 156 my $obj = { '@type' => 'Note', note => $val };
676  
677 156 my $created = _param($item, 'created');
678 156 ▶ $obj->{created} = _to_utc_datetime($created) if defined $created;
Branch: 1 branch
BranchTrueFalseTotal
if defined $created 32 124 156
679  
680 156 my $author_name = _param($item, 'author-name');
681 156 my $author_uri = _param($item, 'author');
682 156 ▶ if ($author_name || $author_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($author_name or $author_uri) 33 123 156
Condition66%2/3
$author_name or $author_uri
l!l&&r!l&&!r
330123
MC/DC50%1/2
$author_name or $author_uri
$author_name $author_uri
Truth table66%2/3
A or B
A= $author_nameB= $author_uri
ABresult
1X1
011
000
683 33 my $author = {};
684 33 ▶ $author->{name} = $author_name if $author_name;
Branch: 1 branch
BranchTrueFalseTotal
if $author_name 33 0 33
685 33 ▶ $author->{uri} = $author_uri if $author_uri;
Branch: 1 branch
BranchTrueFalseTotal
if $author_uri 1 32 33
686 33 $obj->{author} = $author;
687 }
688  
689 156 $map{$id} = $obj;
690 }
691  
692 141 ▶ $card->{notes} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 141 0 141
693 }
694  
695 # IMPP, SOCIALPROFILE, X-SOCIALPROFILE -> onlineServices
696 253 ▶sub _convert_online_services {
Subroutine _convert_online_services: called CC=40 SCAR=39.6
697 253 my ($card, $props, $ctx) = @_;
698 253 my $labels = _collect_labels($props);
699  
700 253 my %map;
701  
702 253 ▶ for my $item (@{$props->{impp} // []}) {
Condition100%2/2
$props->{"impp"} // []
l!l
99154
MC/DC100%1/1
$props->{"impp"} // []
$props->{"impp"}
Truth table100%2/2
A
A= $props->{"impp"}
Aresult
11
00
703 110 my $val = $item->{value};
704 110 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 110 110
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00110
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
705 110 my $id = _prop_id($item, $ctx);
706 110 my $obj = { '@type' => 'OnlineService' };
707 # RFC 9554 SERVICE-TYPE and USERNAME, as _unconvert_online_services
708 # writes them, or the older x- forms
709 110 ▶ my $service = _param($item, 'service-type') // _param($item, 'x-service-type');
Condition100%3/3
_param($item, "service-type") // _param($item, "x-service-type")
l!l&&r!l&&!r
343838
MC/DC100%2/2
_param($item, "service-type") // _param($item, "x-service-type")
_param($item, "service-type") _param($item, "x-service-type")
Truth table100%3/3
A // B
A= _param($item, "service-type")B= _param($item, "x-service-type")
ABresult
1X1
011
000
710 110 ▶ $obj->{service} = $service if defined $service;
Branch: 1 branch
BranchTrueFalseTotal
if defined $service 72 38 110
711 110 ▶ my $user = _param($item, 'username') // _param($item, 'x-user');
Condition66%2/3
_param($item, "username") // _param($item, "x-user")
l!l&&r!l&&!r
29081
MC/DC50%1/2
_param($item, "username") // _param($item, "x-user")
_param($item, "username") _param($item, "x-user")
Truth table66%2/3
A // B
A= _param($item, "username")B= _param($item, "x-user")
ABresult
1X1
011
000
712 110 ▶ $obj->{user} = $user if defined $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user 29 81 110
713 # a service with only a user name is written with that name as the value
714 110 ▶ $obj->{uri} = $val unless defined $user && $val eq $user && $val !~ /^[a-z][a-z0-9+.-]*:/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user and $val eq $user and not $val =~ /^[a-z][a-z0-9+.-]*:/i 103 7 110
Condition83%5/6
defined $user and $val eq $user and not $val =~ /^[a-z][a-z0-9+.-]*:/i
!ll&&!rl&&r
10307
defined $user and $val eq $user
!ll&&!rl&&r
81227
MC/DC66%2/3
defined $user and $val eq $user and not $val =~ /^[a-z][a-z0-9+.-]*:/i
defined $user $val eq $user not $val =~ /^[a-z][a-z0-9+.-]*:/i
Truth table75%3/4
A and B and not(C)
A= defined $userB= $val eq $userC= not $val =~ /^[a-z][a-z0-9+.-]*:/i
ABCresult
0XX0
10X0
1100
1111
715 110 my @types = _params_list($item, 'type');
716 110 ▶ if (my $ctx = _make_contexts(@types)) { $obj->{contexts} = $ctx }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 2 108 110
717 110 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 56 54 110
718 110 _apply_label($obj, $item, $labels);
719 110 $map{$id} = $obj;
720 }
721  
722 253 for my $propname (qw(socialprofile x-socialprofile)) {
723 506 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
67439
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
724 105 my $val = $item->{value};
725 105 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 105 105
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00105
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
726 105 my $id = _prop_id($item, $ctx);
727 105 my $obj = { '@type' => 'OnlineService' };
728  
729 105 ▶ if ($val =~ m{://}) { $obj->{uri} = $val }
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ m[://]) { } 105 0 105
730 0 else { $obj->{user} = $val }
731  
732 105 ▶ my $service = _param($item, 'service-type') // _param($item, 'x-service-type');
Condition66%2/3
_param($item, "service-type") // _param($item, "x-service-type")
l!l&&r!l&&!r
29076
MC/DC50%1/2
_param($item, "service-type") // _param($item, "x-service-type")
_param($item, "service-type") _param($item, "x-service-type")
Truth table66%2/3
A // B
A= _param($item, "service-type")B= _param($item, "x-service-type")
ABresult
1X1
011
000
733 105 ▶ unless ($service) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service) 76 29 105
734 76 for my $t (_params_list($item, 'type')) {
735 76 my $lt = lc $t;
736 76 ▶ if ($lt ne 'home' && $lt ne 'work' && $lt ne 'pref') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lt ne "home" and $lt ne "work" and $lt ne "pref") 76 0 76
Condition33%2/6
$lt ne "home" and $lt ne "work" and $lt ne "pref"
!ll&&!rl&&r
0076
$lt ne "home" and $lt ne "work"
!ll&&!rl&&r
0076
MC/DC0%0/3
$lt ne "home" and $lt ne "work" and $lt ne "pref"
$lt ne "home" $lt ne "work" $lt ne "pref"
Truth table25%1/4
A and B and C
A= $lt ne "home"B= $lt ne "work"C= $lt ne "pref"
ABCresult
0XX0
10X0
1100
1111
737 76 $service = $lt;
738 76 last;
739 }
740 }
741 }
742 105 ▶ $obj->{service} = $service if defined $service;
Branch: 1 branch
BranchTrueFalseTotal
if defined $service 105 0 105
743  
744 105 ▶ my $user = _param($item, 'x-user') // _param($item, 'username');
Condition66%2/3
_param($item, "x-user") // _param($item, "username")
l!l&&r!l&&!r
76029
MC/DC50%1/2
_param($item, "x-user") // _param($item, "username")
_param($item, "x-user") _param($item, "username")
Truth table66%2/3
A // B
A= _param($item, "x-user")B= _param($item, "username")
ABresult
1X1
011
000
745 105 ▶ $obj->{user} = $user if defined $user && !$obj->{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $user and not $obj->{"user"} 76 29 105
Condition66%2/3
defined $user and not $obj->{"user"}
!ll&&!rl&&r
29076
MC/DC50%1/2
defined $user and not $obj->{"user"}
defined $user not $obj->{"user"}
Truth table66%2/3
A and not(B)
A= defined $userB= not $obj->{"user"}
ABresult
0X0
100
111
746  
747 105 my @types = _params_list($item, 'type');
748 0 ▶ if (my $ctx = _make_contexts(@types)) { $obj->{contexts} = $ctx }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 0 105 105
749 0 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 0 105 105
750 105 _apply_label($obj, $item, $labels);
751 105 $map{$id} = $obj;
752 }
753 }
754  
755 # Legacy X-service IM properties (Apple/others)
756 253 my %xservice_map = (
757 'x-aim' => 'AIM',
758 'x-icq' => 'ICQ',
759 'x-msn' => 'MSN',
760 'x-yahoo' => 'Yahoo',
761 'x-jabber' => 'Jabber',
762 'x-skype' => 'Skype',
763 'x-skype-username' => 'Skype',
764 'x-twitter' => 'Twitter',
765 'x-google-talk' => 'GoogleTalk',
766 );
767 253 for my $propname (sort keys %xservice_map) {
768 2277 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
392238
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
769 39 my $val = $item->{value};
770 39 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 39 39
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0039
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
771 39 my $id = _prop_id($item, $ctx);
772 39 my $obj = { '@type' => 'OnlineService', user => $val, service => $xservice_map{$propname} };
773 39 my @types = _params_list($item, 'type');
774 39 ▶ if (my $ctx = _make_contexts(@types)) { $obj->{contexts} = $ctx }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 37 2 39
775 0 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 0 39 39
776 39 _apply_label($obj, $item, $labels);
777 39 $map{$id} = $obj;
778 }
779 }
780  
781 253 ▶ $card->{onlineServices} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 100 153 253
782 }
783  
784 # PHOTO, LOGO, SOUND -> media
785 253 ▶sub _convert_media {
Subroutine _convert_media: called CC=17 SCAR=29.5
786 253 my ($card, $props, $ctx) = @_;
787 253 my $labels = _collect_labels($props);
788  
789 253 my %map;
790 253 for my $spec (['photo', 'photo'], ['logo', 'logo'], ['sound', 'sound']) {
791 759 my ($propname, $kind) = @$spec;
792 759 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
131628
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
793 132 my $val = $item->{value};
794 132 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 132 132
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00132
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
795 132 my $id = _prop_id($item, $ctx);
796 132 my $obj = { '@type' => 'Media', kind => $kind };
797  
798 132 my $encoding = _param($item, 'encoding');
799 132 ▶ if ($encoding && lc($encoding) eq 'b') {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding and lc $encoding eq 'b') { } 39 93 132
Condition66%2/3
$encoding and lc $encoding eq "b"
!ll&&!rl&&r
93039
MC/DC50%1/2
$encoding and lc $encoding eq "b"
$encoding lc $encoding eq "b"
Truth table66%2/3
A and B
A= $encodingB= lc $encoding eq "b"
ABresult
0X0
100
111
800 # vCard 3 inline data (RFC 2426 S3.1.4): TYPE is the format,
801 # e.g. TYPE=PNG; media types are case-insensitive, lower-case
802 # them as RFC 2045 writes them
803 39 ▶ my $mediatype = _param($item, 'mediatype') // _param($item, 'type');
Condition33%1/3
_param($item, "mediatype") // _param($item, "type")
l!l&&r!l&&!r
0390
MC/DC0%0/2
_param($item, "mediatype") // _param($item, "type")
_param($item, "mediatype") _param($item, "type")
Truth table33%1/3
A // B
A= _param($item, "mediatype")B= _param($item, "type")
ABresult
1X1
011
000
804 # TYPE is a list (RFC 2426 S3.1.4 allows one format, but servers
805 # merge: Cyrus writes TYPE=PNG,PNG for a vCard 4 PHOTO that had both
806 # a MEDIATYPE and a data: URI); the format is its first value
807 39 ▶ ($mediatype) = grep { length } split /\s*,\s*/, $mediatype if defined $mediatype;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mediatype 39 0 39
808 39 ▶ $mediatype = ($kind eq 'sound' ? 'audio/' : 'image/') . $mediatype
Branch: 2 branches
BranchTrueFalseTotal
if $mediatype and not $mediatype =~ m[/] 39 0 39
$kind eq 'sound' ? : 0 39 39
Condition33%1/3
$mediatype and not $mediatype =~ m[/]
!ll&&!rl&&r
0039
MC/DC0%0/2
$mediatype and not $mediatype =~ m[/]
$mediatype not $mediatype =~ m[/]
Truth table33%1/3
A and not(B)
A= $mediatypeB= not $mediatype =~ m[/]
ABresult
0X0
100
111
809 if $mediatype && $mediatype !~ m{/};
810 39 ▶ $mediatype = lc($mediatype // 'application/octet-stream');
Condition50%1/2
$mediatype // "application/octet-stream"
l!l
390
MC/DC0%0/1
$mediatype // "application/octet-stream"
$mediatype
Truth table50%1/2
A
A= $mediatype
Aresult
11
00
811 # the value is the base64 text already: Text::VCardFast does
812 # not decode it
813 39 (my $b64 = $val) =~ s/\s+//g;
814 39 $obj->{uri} = "data:$mediatype;base64,$b64";
815 39 $obj->{mediaType} = $mediatype;
816 } else {
817 93 $obj->{uri} = $val;
818 93 my $mt = _param($item, 'mediatype');
819 93 ▶ $obj->{mediaType} = $mt if defined $mt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mt 24 69 93
820 }
821  
822 132 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 1 131 132
823 132 _apply_label($obj, $item, $labels);
824 132 $map{$id} = $obj;
825 }
826 }
827  
828 253 ▶ $card->{media} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 101 152 253
829 }
830  
831 # Generic resource converter for URI-based properties
832 2024 ▶sub _convert_resource_to {
Subroutine _convert_resource_to: called CC=11 SCAR=24.0
833 2024 my ($card, $key, $props, $propname, $kind, $ctx, $type) = @_;
834 2024 my $labels = _collect_labels($props);
835  
836 2024 my %map;
837 2024 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
3141710
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
838 412 my $val = $item->{value};
839 412 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 412 412
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00412
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
840 412 my $id = _prop_id($item, $ctx);
841 412 my $obj = { '@type' => $type, uri => $val };
842 412 ▶ $obj->{kind} = $kind if defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
if defined $kind 122 290 412
843 412 my $mt = _param($item, 'mediatype');
844 412 ▶ $obj->{mediaType} = $mt if defined $mt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mt 63 349 412
845 412 my @types = _params_list($item, 'type');
846 412 ▶ if (my $ctx = _make_contexts(@types)) { $obj->{contexts} = $ctx }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 47 365 412
847 412 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 130 282 412
848 412 _apply_label($obj, $item, $labels);
849 412 $map{$id} = $obj;
850 }
851  
852 2024 ▶ if (%map) {
Branch: 1 branch
BranchTrueFalseTotal
if (%map) 314 1710 2024
853 314 ▶ $card->{$key} = { %{$card->{$key} // {}}, %map };
Condition100%2/2
$card->{$key} // {}
l!l
61253
MC/DC100%1/1
$card->{$key} // {}
$card->{$key}
Truth table100%2/2
A
A= $card->{$key}
Aresult
11
00
854 }
855 }
856  
857 # URL, FBURL, CONTACT-URI -> links
858 253 ▶sub _convert_links {
Subroutine _convert_links: called CC=1 SCAR=0.0
859 253 my ($card, $props, $ctx) = @_;
860 253 _convert_resource_to($card, 'links', $props, 'url', undef, $ctx, 'Link');
861 253 _convert_resource_to($card, 'links', $props, 'fburl', 'freeBusy', $ctx, 'Link');
862 253 _convert_resource_to($card, 'links', $props, 'contact-uri', 'contact', $ctx, 'Link');
863 }
864  
865 # CALURI -> calendars
866 253 ▶sub _convert_calendars {
Subroutine _convert_calendars: called CC=1 SCAR=0.0
867 253 my ($card, $props, $ctx) = @_;
868 253 _convert_resource_to($card, 'calendars', $props, 'caluri', undef, $ctx, 'Calendar');
869 }
870  
871 # CALADRURI -> schedulingAddresses
872 253 ▶sub _convert_scheduling_addresses {
Subroutine _convert_scheduling_addresses: called CC=1 SCAR=0.0
873 253 my ($card, $props, $ctx) = @_;
874 253 _convert_resource_to($card, 'schedulingAddresses', $props, 'caladruri', undef, $ctx, 'SchedulingAddress');
875 }
876  
877 # KEY -> cryptoKeys
878 253 ▶sub _convert_crypto_keys {
Subroutine _convert_crypto_keys: called CC=1 SCAR=0.0
879 253 my ($card, $props, $ctx) = @_;
880 253 _convert_resource_to($card, 'cryptoKeys', $props, 'key', undef, $ctx, 'CryptoKey');
881 }
882  
883 # SOURCE, ORG-DIRECTORY -> directories
884 253 ▶sub _convert_directories {
Subroutine _convert_directories: called CC=1 SCAR=0.0
885 253 my ($card, $props, $ctx) = @_;
886 253 _convert_resource_to($card, 'directories', $props, 'source', 'entry', $ctx, 'Directory');
887 253 _convert_resource_to($card, 'directories', $props, 'org-directory', 'directory', $ctx, 'Directory');
888 }
889  
890 # LANG -> preferredLanguages
891 253 ▶sub _convert_preferred_languages {
Subroutine _convert_preferred_languages: called CC=8 SCAR=21.0
892 253 my ($card, $props, $ctx) = @_;
893 253 ▶ my $items = $props->{lang} || return;
Condition100%2/2
$props->{'lang'} || (return)
l!l
45208
MC/DC100%1/1
$props->{'lang'} || (return)
$props->{'lang'}
Truth table100%2/2
A
A= $props->{'lang'}
Aresult
11
00
894  
895 45 my %map;
896 45 for my $item (@$items) {
897 105 my $val = $item->{value};
898 105 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 105 105
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00105
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
899 105 my $id = _prop_id($item, $ctx);
900 105 my $obj = { '@type' => 'LanguagePref', language => $val };
901 105 my @types = _params_list($item, 'type');
902 105 ▶ if (my $ctx = _make_contexts(@types)) { $obj->{contexts} = $ctx }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 62 43 105
903 105 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 90 15 105
904 105 $map{$id} = $obj;
905 }
906  
907 45 ▶ $card->{preferredLanguages} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 45 0 45
908 }
909  
910 # EXPERTISE, HOBBY, INTEREST -> personalInfo
911 253 ▶sub _convert_personal_info {
Subroutine _convert_personal_info: called CC=10 SCAR=23.1
912 253 my ($card, $props, $ctx) = @_;
913  
914 253 my %level_map = (beginner => 'low', average => 'medium', expert => 'high');
915 253 my %map;
916  
917 253 for my $spec (['expertise', 'expertise'], ['hobby', 'hobby'], ['interest', 'interest']) {
918 759 my ($propname, $kind) = @$spec;
919 759 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
47712
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
920 90 my $val = $item->{value};
921 90 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 90 90
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0090
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
922 90 my $id = _prop_id($item, $ctx);
923 90 my $obj = { '@type' => 'PersonalInfo', kind => $kind, value => $val };
924  
925 90 my $level = _param($item, 'level');
926 90 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 89 1 90
927 89 ▶ $obj->{level} = $level_map{lc $level} // lc $level;
Condition100%2/2
$level_map{lc $level} // lc $level
l!l
3752
MC/DC100%1/1
$level_map{lc $level} // lc $level
$level_map{lc $level}
Truth table100%2/2
A
A= $level_map{lc $level}
Aresult
11
00
928 }
929  
930 90 my $index = _param($item, 'index');
931 90 ▶ $obj->{listAs} = $index + 0 if defined $index;
Branch: 1 branch
BranchTrueFalseTotal
if defined $index 88 2 90
932  
933 90 $map{$id} = $obj;
934 }
935 }
936  
937 253 ▶ $card->{personalInfo} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 17 236 253
938 }
939  
940 # CATEGORIES -> keywords
941 253 ▶sub _convert_keywords {
Subroutine _convert_keywords: called CC=7 SCAR=19.7
942 253 my ($card, $props) = @_;
943 253 ▶ my $items = $props->{categories} || return;
Condition100%2/2
$props->{'categories'} || (return)
l!l
94159
MC/DC100%1/1
$props->{'categories'} || (return)
$props->{'categories'}
Truth table100%2/2
A
A= $props->{'categories'}
Aresult
11
00
944  
945 94 my %keywords;
946 94 for my $item (@$items) {
947 94 my $val = $item->{value};
948 94 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 94 94
949 94 for my $cat (split /,/, $val) {
950 240 $cat =~ s/^\s+//;
951 240 $cat =~ s/\s+$//;
952 240 ▶ $keywords{$cat} = JSON::true if $cat ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $cat ne "" 240 0 240
953 }
954 }
955  
956 94 ▶ $card->{keywords} = \%keywords if %keywords;
Branch: 1 branch
BranchTrueFalseTotal
if %keywords 94 0 94
957 }
958  
959 # MEMBER / X-ADDRESSBOOKSERVER-MEMBER -> members
960 253 ▶sub _convert_members {
Subroutine _convert_members: called CC=8 SCAR=21.3
961 253 my ($card, $props, $ctx) = @_;
962  
963 253 my %members;
964 253 for my $propname (qw(member x-addressbookserver-member)) {
965 506 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
38468
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
966 94 my $val = $item->{value};
967 94 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 94 94
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0094
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
968 94 $members{$val} = JSON::true;
969 94 ▶ push @{$ctx->{sources}{members}{$val}}, $item if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 94 0 94
970 }
971 }
972  
973 253 ▶ $card->{members} = \%members if %members;
Branch: 1 branch
BranchTrueFalseTotal
if %members 38 215 253
974 }
975  
976 # RELATED / X-ABRELATEDNAMES -> relatedTo
977 253 ▶sub _convert_related {
Subroutine _convert_related: called CC=19 SCAR=30.3
978 253 my ($card, $props, $ctx) = @_;
979 253 my $labels = _collect_labels($props);
980  
981 253 my %related;
982  
983 # Standard RELATED property
984 253 ▶ for my $item (@{$props->{related} // []}) {
Condition100%2/2
$props->{"related"} // []
l!l
37216
MC/DC100%1/1
$props->{"related"} // []
$props->{"related"}
Truth table100%2/2
A
A= $props->{"related"}
Aresult
11
00
985 72 my $val = $item->{value};
986 72 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 72 72
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0072
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
987  
988 72 my $rel = { '@type' => 'Relation' };
989 72 my %relation;
990 72 for my $t (_params_list($item, 'type')) {
991 55 my $lt = lc $t;
992 55 ▶ $relation{$lt} = JSON::true
Branch: 1 branch
BranchTrueFalseTotal
if $lt =~ /^(acquaintance|agent|child|co-resident|co-worker|colleague|contact|crush|date|emergency|friend|kin|me|met|muse|neighbor|parent|sibling|spouse|sweetheart)$/ 55 0 55
993 if $lt =~ /^(acquaintance|agent|child|co-resident|co-worker|colleague|contact|crush|date|emergency|friend|kin|me|met|muse|neighbor|parent|sibling|spouse|sweetheart)$/;
994 }
995 72 ▶ $rel->{relation} = \%relation if %relation;
Branch: 1 branch
BranchTrueFalseTotal
if %relation 54 18 72
996 72 $related{$val} = $rel;
997 72 ▶ push @{$ctx->{sources}{relatedTo}{$val}}, $item if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 72 0 72
998 }
999  
1000 # Apple X-ABRELATEDNAMES + X-ABLabel -> relatedTo
1001 253 my %ablabel_relation = (
1002 'mother' => 'parent', 'father' => 'parent', 'parent' => 'parent',
1003 'brother' => 'sibling', 'sister' => 'sibling',
1004 'child' => 'child',
1005 'friend' => 'friend',
1006 'spouse' => 'spouse', 'partner' => 'spouse',
1007 'assistant' => 'colleague', 'manager' => 'colleague',
1008 );
1009 253 ▶ for my $item (@{$props->{'x-abrelatednames'} // []}) {
Condition100%2/2
$props->{"x-abrelatednames"} // []
l!l
65188
MC/DC100%1/1
$props->{"x-abrelatednames"} // []
$props->{"x-abrelatednames"}
Truth table100%2/2
A
A= $props->{"x-abrelatednames"}
Aresult
11
00
1010 141 my $val = $item->{value};
1011 141 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 141 141
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00141
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
1012  
1013 141 my $rel = { '@type' => 'Relation' };
1014 141 my %relation;
1015  
1016 # Get relation type from X-ABLabel
1017 my $label;
1018 141 ▶ if (my $group = $item->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $item->{"group"}) 141 0 141
1019 141 $label = $labels->{$group};
1020 }
1021 141 ▶ if ($label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label) 141 0 141
1022 141 my $lt = lc $label;
1023 141 ▶ if (my $mapped = $ablabel_relation{$lt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mapped = $ablabel_relation{$lt}) 104 37 141
1024 104 $relation{$mapped} = JSON::true;
1025 }
1026 }
1027 141 ▶ $rel->{relation} = \%relation if %relation;
Branch: 1 branch
BranchTrueFalseTotal
if %relation 104 37 141
1028 141 $related{$val} = $rel;
1029 141 ▶ push @{$ctx->{sources}{relatedTo}{$val}}, $item if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 141 0 141
1030 }
1031  
1032 253 ▶ $card->{relatedTo} = \%related if %related;
Branch: 1 branch
BranchTrueFalseTotal
if %related 102 151 253
1033 }
1034  
1035 # GRAMGENDER, PRONOUNS (RFC 9554) -> speakToAs
1036 253 ▶sub _convert_speak_to_as {
Subroutine _convert_speak_to_as: called CC=10 SCAR=23.2
1037 253 my ($card, $props, $ctx) = @_;
1038  
1039 253 my $speak = {};
1040  
1041 253 ▶ if (my $gg = _first_value($props, 'gramgender')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $gg = _first_value($props, "gramgender")) 32 221 253
1042 32 $speak->{grammaticalGender} = lc $gg;
1043 }
1044  
1045 253 ▶ if (my $items = $props->{pronouns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $items = $props->{"pronouns"}) 33 220 253
1046 33 my %pronouns;
1047 33 for my $item (@$items) {
1048 49 my $val = $item->{value};
1049 49 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 49 49
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0049
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
1050 49 my $id = _prop_id($item, $ctx);
1051 49 my $obj = { '@type' => 'Pronouns', pronouns => $val };
1052 49 ▶ if (my $ctx = _make_contexts(_params_list($item, 'type'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(_params_list($item, "type"))) 1 48 49
1053 1 $obj->{contexts} = $ctx;
1054 }
1055 49 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 48 1 49
1056 49 $pronouns{$id} = $obj;
1057 }
1058 33 ▶ $speak->{pronouns} = \%pronouns if %pronouns;
Branch: 1 branch
BranchTrueFalseTotal
if %pronouns 33 0 33
1059 }
1060  
1061 253 ▶ $card->{speakToAs} = $speak if %$speak;
Branch: 1 branch
BranchTrueFalseTotal
if %$speak 33 220 253
1062 }
1063  
1064 ###############################################################################
1065 # JSCONTACT -> VCARD
1066 ###############################################################################
1067  
1068 # jscontact_to_vcard($card) writes the Card as a new vCard 4.0.
1069 #
1070 # jscontact_to_vcard($card, original => $vcard) writes it as a patch of
1071 # $vcard, the vCard the Card is an edit of (as fetched from the server,
1072 # bytes or characters): the Card is compared with what $vcard itself
1073 # reads as, and only the entries that differ are rewritten, so whatever
1074 # else $vcard holds is kept (see patch_vcard). The caller need not keep
1075 # the Card it first read, and may have changed anything in it. If
1076 # $vcard cannot be parsed, the Card is written as a new vCard instead.
1077 #
1078 # Either way the result is a character string.
1079 385 ▶sub jscontact_to_vcard {
Subroutine jscontact_to_vcard: called CC=10 SCAR=23.0
Pod undocumented
1080 385 my ($card, %opts) = @_;
1081  
1082 385 ▶ if (defined $opts{original}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"original"}) 28 357 385
1083 28 my $patched = _patch_vcard($opts{original}, $card);
1084 28 ▶ return $patched if defined $patched;
Branch: 1 branch
BranchTrueFalseTotal
if defined $patched 25 3 28
1085 }
1086  
1087 360 my $ctx = _new_context();
1088 360 my @props;
1089  
1090 # VERSION (always 4.0)
1091 360 push @props, { name => 'version', value => '4.0' };
1092  
1093 # UID
1094 360 ▶ push @props, { name => 'uid', value => $card->{uid} } if $card->{uid};
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"uid"} 294 66 360
1095  
1096 # KIND
1097 push @props, { name => 'kind', value => $card->{kind} }
1098 360 ▶ if $card->{kind} && $card->{kind} ne 'individual';
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"kind"} and $card->{"kind"} ne "individual" 65 295 360
Condition100%3/3
$card->{"kind"} and $card->{"kind"} ne "individual"
!ll&&!rl&&r
2286765
MC/DC100%2/2
$card->{"kind"} and $card->{"kind"} ne "individual"
$card->{"kind"} $card->{"kind"} ne "individual"
Truth table100%3/3
A and B
A= $card->{"kind"}B= $card->{"kind"} ne "individual"
ABresult
0X0
100
111
1099  
1100 # PRODID
1101 360 ▶ push @props, { name => 'prodid', value => $card->{prodId} } if $card->{prodId};
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"prodId"} 165 195 360
1102  
1103 # REV
1104 360 ▶ push @props, { name => 'rev', value => $card->{updated} } if $card->{updated};
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"updated"} 132 228 360
1105  
1106 # CREATED (RFC 9554)
1107 360 ▶ push @props, { name => 'created', value => $card->{created} } if $card->{created};
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"created"} 67 293 360
1108  
1109 # LANGUAGE (RFC 9554)
1110 360 ▶ push @props, { name => 'language', value => $card->{language} } if $card->{language};
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"language"} 99 261 360
1111  
1112 # Name -> N + FN
1113 360 _unconvert_name(\@props, $card, $ctx);
1114  
1115 # All map-based properties
1116 360 _unconvert_nicknames(\@props, $card, $ctx);
1117 360 _unconvert_emails(\@props, $card, $ctx);
1118 360 _unconvert_phones(\@props, $card, $ctx);
1119 360 _unconvert_addresses(\@props, $card, $ctx);
1120 360 _unconvert_organizations(\@props, $card, $ctx);
1121 360 _unconvert_titles(\@props, $card, $ctx);
1122 360 _unconvert_anniversaries(\@props, $card, $ctx);
1123 360 _unconvert_notes(\@props, $card, $ctx);
1124 360 _unconvert_online_services(\@props, $card, $ctx);
1125 360 _unconvert_media(\@props, $card, $ctx);
1126 360 _unconvert_links(\@props, $card, $ctx);
1127 360 _unconvert_calendars(\@props, $card, $ctx);
1128 360 _unconvert_scheduling_addresses(\@props, $card, $ctx);
1129 360 _unconvert_crypto_keys(\@props, $card, $ctx);
1130 360 _unconvert_directories(\@props, $card, $ctx);
1131 360 _unconvert_preferred_languages(\@props, $card, $ctx);
1132 360 _unconvert_personal_info(\@props, $card, $ctx);
1133 360 _unconvert_keywords(\@props, $card, $ctx);
1134 360 _unconvert_members(\@props, $card, $ctx);
1135 360 _unconvert_related(\@props, $card, $ctx);
1136 360 _unconvert_speak_to_as(\@props, $card, $ctx);
1137  
1138 360 return _write_vcard(\@props);
1139 }
1140  
1141 ###############################################################################
1142 # INDIVIDUAL JSCONTACT -> VCARD CONVERTERS
1143 ###############################################################################
1144  
1145 1144 ▶sub _contexts_to_types {
Subroutine _contexts_to_types: called CC=5 SCAR=16.5
1146 1144 my $contexts = shift;
1147 1144 ▶ return () unless $contexts && ref $contexts eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $contexts and ref $contexts eq "HASH" 0 1144 1144
Condition33%1/3
$contexts and ref $contexts eq "HASH"
!ll&&!rl&&r
001144
MC/DC0%0/2
$contexts and ref $contexts eq "HASH"
$contexts ref $contexts eq "HASH"
Truth table33%1/3
A and B
A= $contextsB= ref $contexts eq "HASH"
ABresult
0X0
100
111
1148 1144 my @types;
1149 1144 ▶ push @types, 'home' if $contexts->{private};
Branch: 1 branch
BranchTrueFalseTotal
if $contexts->{"private"} 404 740 1144
1150 1144 ▶ push @types, 'work' if $contexts->{work};
Branch: 1 branch
BranchTrueFalseTotal
if $contexts->{"work"} 739 405 1144
1151 1144 return @types;
1152 }
1153  
1154 2878 ▶sub _add_pref_params {
Subroutine _add_pref_params: called CC=2 SCAR=6.9
1155 2878 my ($params, $pref) = @_;
1156 2878 ▶ $params->{pref} = [$pref] if defined $pref;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pref 977 1901 2878
1157 }
1158  
1159 881 ▶sub _add_label {
Subroutine _add_label: called CC=4 SCAR=13.9
1160 881 my ($out, $prop, $obj, $ctx) = @_;
1161 881 ▶ return if $ctx->{no_labels};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"no_labels"} 14 867 881
1162 867 my $label = $obj->{label};
1163 867 ▶ return unless defined $label && $label ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label and $label ne "" 701 166 867
Condition66%2/3
defined $label and $label ne ""
!ll&&!rl&&r
7010166
MC/DC50%1/2
defined $label and $label ne ""
defined $label $label ne ""
Truth table66%2/3
A and B
A= defined $labelB= $label ne ""
ABresult
0X0
100
111
1164  
1165 166 my $group = _alloc_group($ctx);
1166 166 $prop->{group} = $group;
1167 166 push @$out, {
1168 name => 'x-ablabel',
1169 value => $label,
1170 params => {},
1171 group => $group,
1172 };
1173 }
1174  
1175 2868 ▶sub _add_context_params {
Subroutine _add_context_params: called CC=3 SCAR=11.0
1176 2868 my ($params, $obj) = @_;
1177 2868 ▶ if ($obj->{contexts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"contexts"}) 1144 1724 2868
1178 1144 my @types = _contexts_to_types($obj->{contexts});
1179 1144 ▶ push @{$params->{type}}, @types if @types;
Branch: 1 branch
BranchTrueFalseTotal
if @types 1143 1 1144
1180 }
1181 }
1182  
1183 368 ▶sub _unconvert_name {
Subroutine _unconvert_name: called CC=23 SCAR=36.0
1184 368 my ($out, $card, $ctx) = @_;
1185 368 ▶ my $name = $card->{name} || return;
Condition100%2/2
$card->{'name'} || (return)
l!l
3608
MC/DC100%1/1
$card->{'name'} || (return)
$card->{'name'}
Truth table100%2/2
A
A= $card->{'name'}
Aresult
11
00
1186  
1187 # FN
1188 360 ▶ if (my $full = $name->{full}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $full = $name->{"full"}) 360 0 360
1189 360 push @$out, { name => 'fn', value => $full, params => {} };
1190 }
1191  
1192 # N from components
1193 360 ▶ if (my $components = $name->{components}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $components = $name->{"components"}) 270 90 360
1194 270 my %by_kind;
1195 270 for my $c (@$components) {
1196 1081 push @{$by_kind{$c->{kind}}}, $c->{value};
1197 }
1198  
1199 # N order: surname, given, given2(additional), title(prefix), credential(suffix), surname2, generation
1200 270 my @n_values;
1201 270 for my $kind (qw(surname given given2 title credential surname2 generation)) {
1202 1890 my $vals = $by_kind{$kind};
1203 # several values of one kind are a list (RFC 6350 S6.2.2)
1204 1890 ▶ push @n_values, $vals ? [ grep { defined $_ } @$vals ] : '';
Branch: 1 branch
BranchTrueFalseTotal
$vals ? : 919 971 1890
1205 }
1206  
1207 # Trim trailing empty components
1208 270 ▶ pop @n_values while @n_values && !ref $n_values[-1] && $n_values[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
@n_values and not ref $n_values[-1] and $n_values[-1] eq "" and do { pop @n_values } 874 270 1144
Condition66%4/6
@n_values and not ref $n_values[-1] and $n_values[-1] eq ""
!ll&&!rl&&r
2700874
@n_values and not ref $n_values[-1]
!ll&&!rl&&r
0270874
MC/DC33%1/3
@n_values and not ref $n_values[-1] and $n_values[-1] eq ""
@n_values not ref $n_values[-1] $n_values[-1] eq ""
Truth table50%2/4
A and not(B) and C
A= @n_valuesB= not ref $n_values[-1]C= $n_values[-1] eq ""
ABCresult
0XX0
10X0
1100
1111
1209  
1210 270 my $params = {};
1211 270 ▶ if ($name->{sortAs} && ref $name->{sortAs} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name->{"sortAs"} and ref $name->{"sortAs"} eq "HASH") 67 203 270
Condition66%2/3
$name->{"sortAs"} and ref $name->{"sortAs"} eq "HASH"
!ll&&!rl&&r
203067
MC/DC50%1/2
$name->{"sortAs"} and ref $name->{"sortAs"} eq "HASH"
$name->{"sortAs"} ref $name->{"sortAs"} eq "HASH"
Truth table66%2/3
A and B
A= $name->{"sortAs"}B= ref $name->{"sortAs"} eq "HASH"
ABresult
0X0
100
111
1212 67 my @sa;
1213 67 for my $kind (qw(surname given given2 title credential surname2 generation)) {
1214 469 ▶ push @sa, $name->{sortAs}{$kind} // '';
Condition100%2/2
$name->{"sortAs"}{$kind} // ""
l!l
134335
MC/DC100%1/1
$name->{"sortAs"}{$kind} // ""
$name->{"sortAs"}{$kind}
Truth table100%2/2
A
A= $name->{"sortAs"}{$kind}
Aresult
11
00
1215 }
1216 67 ▶ pop @sa while @sa && $sa[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
@sa and $sa[-1] eq "" and do { pop @sa } 335 67 402
Condition66%2/3
@sa and $sa[-1] eq ""
!ll&&!rl&&r
067335
MC/DC50%1/2
@sa and $sa[-1] eq ""
@sa $sa[-1] eq ""
Truth table66%2/3
A and B
A= @saB= $sa[-1] eq ""
ABresult
0X0
100
111
1217 67 ▶ $params->{'sort-as'} = [join(',', @sa)] if @sa;
Branch: 1 branch
BranchTrueFalseTotal
if @sa 67 0 67
1218 }
1219  
1220 270 push @$out, { name => 'n', values => \@n_values, params => $params };
1221 }
1222  
1223 # Generate FN from components if no full name given
1224 360 ▶ if (!$name->{full} && $name->{components}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $name->{"full"} and $name->{"components"}) 0 360 360
Condition33%1/3
not $name->{"full"} and $name->{"components"}
!ll&&!rl&&r
36000
MC/DC0%0/2
not $name->{"full"} and $name->{"components"}
not $name->{"full"} $name->{"components"}
Truth table33%1/3
not(A) and B
A= not $name->{"full"}B= $name->{"components"}
ABresult
0X0
100
111
1225 0 my @parts;
1226 0 for my $c (@{$name->{components}}) {
1227 0 ▶ next if ($c->{kind} // '') eq 'separator';
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"kind"} // "") eq "separator" 0 0 0
Condition0%0/2
$c->{"kind"} // ""
l!l
00
MC/DC0%0/1
$c->{"kind"} // ""
$c->{"kind"}
Truth table0%0/2
A
A= $c->{"kind"}
Aresult
11
00
1228 0 push @parts, $c->{value};
1229 }
1230 0 ▶ push @$out, { name => 'fn', value => join(' ', @parts), params => {} } if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 0 0 0
1231 }
1232 }
1233  
1234 360 ▶sub _unconvert_nicknames {
Subroutine _unconvert_nicknames: called CC=3 SCAR=11.0
1235 360 my ($out, $card, $ctx) = @_;
1236 360 ▶ my $nicknames = $card->{nicknames} || return;
Condition100%2/2
$card->{'nicknames'} || (return)
l!l
134226
MC/DC100%1/1
$card->{'nicknames'} || (return)
$card->{'nicknames'}
Truth table100%2/2
A
A= $card->{'nicknames'}
Aresult
11
00
1237  
1238 134 for my $id (sort keys %$nicknames) {
1239 136 my $nn = $nicknames->{$id};
1240 136 my $params = { 'prop-id' => [$id] };
1241 136 _add_context_params($params, $nn);
1242 136 _add_pref_params($params, $nn->{pref});
1243 136 push @$out, { name => 'nickname', value => $nn->{name}, params => $params };
1244 }
1245 }
1246  
1247 372 ▶sub _unconvert_emails {
Subroutine _unconvert_emails: called CC=3 SCAR=11.0
1248 372 my ($out, $card, $ctx) = @_;
1249 372 ▶ my $emails = $card->{emails} || return;
Condition100%2/2
$card->{'emails'} || (return)
l!l
245127
MC/DC100%1/1
$card->{'emails'} || (return)
$card->{'emails'}
Truth table100%2/2
A
A= $card->{'emails'}
Aresult
11
00
1250  
1251 245 for my $id (sort keys %$emails) {
1252 413 my $em = $emails->{$id};
1253 413 my $params = { 'prop-id' => [$id] };
1254 413 _add_context_params($params, $em);
1255 413 _add_pref_params($params, $em->{pref});
1256 413 my $prop = { name => 'email', value => $em->{address}, params => $params };
1257 413 _add_label($out, $prop, $em, $ctx);
1258 413 push @$out, $prop;
1259 }
1260 }
1261  
1262 362 ▶sub _unconvert_phones {
Subroutine _unconvert_phones: called CC=7 SCAR=19.5
1263 362 my ($out, $card, $ctx) = @_;
1264 362 ▶ my $phones = $card->{phones} || return;
Condition100%2/2
$card->{'phones'} || (return)
l!l
203159
MC/DC100%1/1
$card->{'phones'} || (return)
$card->{'phones'}
Truth table100%2/2
A
A= $card->{'phones'}
Aresult
11
00
1265  
1266 203 my %feature_to_type = (
1267 mobile => 'cell', voice => 'voice', fax => 'fax',
1268 video => 'video', pager => 'pager', text => 'text',
1269 textphone => 'textphone',
1270 );
1271  
1272 203 for my $id (sort keys %$phones) {
1273 468 my $ph = $phones->{$id};
1274 468 my $params = { 'prop-id' => [$id] };
1275 468 _add_context_params($params, $ph);
1276 468 _add_pref_params($params, $ph->{pref});
1277  
1278 468 ▶ if ($ph->{features}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ph->{"features"}) 303 165 468
1279 303 for my $feat (sort keys %{$ph->{features}}) {
1280 470 my $type = $feature_to_type{$feat};
1281 470 ▶ push @{$params->{type}}, $type if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 469 1 470
1282 }
1283 }
1284  
1285 # TEL is TEXT unless it says it is a URI (RFC 6350 S6.4.1); a URI
1286 # escaped as TEXT is no longer the URI
1287 468 ▶ $params->{value} = ['uri'] if _looks_like_uri($ph->{number});
Branch: 1 branch
BranchTrueFalseTotal
if _looks_like_uri($ph->{"number"}) 198 270 468
1288  
1289 468 my $prop = { name => 'tel', value => $ph->{number}, params => $params };
1290 468 _add_label($out, $prop, $ph, $ctx);
1291 468 push @$out, $prop;
1292 }
1293 }
1294  
1295 361 ▶sub _unconvert_addresses {
Subroutine _unconvert_addresses: called CC=13 SCAR=25.7
1296 361 my ($out, $card, $ctx) = @_;
1297 361 ▶ my $addresses = $card->{addresses} || return;
Condition100%2/2
$card->{'addresses'} || (return)
l!l
202159
MC/DC100%1/1
$card->{'addresses'} || (return)
$card->{'addresses'}
Truth table100%2/2
A
A= $card->{'addresses'}
Aresult
11
00
1298  
1299 202 my %kind_to_idx = (
1300 name => 2, apartment => 1,
1301 locality => 3, region => 4, postcode => 5, country => 6,
1302 );
1303  
1304 202 for my $id (sort keys %$addresses) {
1305 236 my $adr = $addresses->{$id};
1306 236 my $params = { 'prop-id' => [$id] };
1307 236 _add_context_params($params, $adr);
1308 236 _add_pref_params($params, $adr->{pref});
1309  
1310 236 ▶ $params->{label} = [$adr->{full}] if $adr->{full};
Branch: 1 branch
BranchTrueFalseTotal
if $adr->{"full"} 36 200 236
1311 236 ▶ $params->{geo} = [$adr->{coordinates}] if $adr->{coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if $adr->{"coordinates"} 35 201 236
1312 236 ▶ $params->{tz} = [$adr->{timeZone}] if $adr->{timeZone};
Branch: 1 branch
BranchTrueFalseTotal
if $adr->{"timeZone"} 35 201 236
1313 236 ▶ $params->{cc} = [$adr->{countryCode}] if $adr->{countryCode};
Branch: 1 branch
BranchTrueFalseTotal
if $adr->{"countryCode"} 165 71 236
1314  
1315 # Build ADR values (7 standard components)
1316 236 my @values = ('') x 7;
1317  
1318 236 ▶ if (my $components = $adr->{components}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $components = $adr->{"components"}) 236 0 236
1319 236 my %by_idx;
1320 236 for my $c (@$components) {
1321 1232 ▶ next if ($c->{kind} // '') eq 'separator';
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"kind"} // "") eq "separator" 0 1232 1232
Condition50%1/2
$c->{"kind"} // ""
l!l
12320
MC/DC0%0/1
$c->{"kind"} // ""
$c->{"kind"}
Truth table50%1/2
A
A= $c->{"kind"}
Aresult
11
00
1322 1232 my $idx = $kind_to_idx{$c->{kind}};
1323 1232 ▶ push @{$by_idx{$idx}}, $c->{value} if defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $idx 1231 1 1232
1324 }
1325 # several values of one component are a list (RFC 6350 S6.3.1)
1326 236 for my $idx (keys %by_idx) {
1327 1230 $values[$idx] = $by_idx{$idx};
1328 }
1329 }
1330  
1331 236 push @$out, { name => 'adr', values => \@values, params => $params };
1332 }
1333 }
1334  
1335 360 ▶sub _unconvert_organizations {
Subroutine _unconvert_organizations: called CC=13 SCAR=25.8
1336 360 my ($out, $card, $ctx) = @_;
1337 360 ▶ my $orgs = $card->{organizations} || return;
Condition100%2/2
$card->{'organizations'} || (return)
l!l
198162
MC/DC100%1/1
$card->{'organizations'} || (return)
$card->{'organizations'}
Truth table100%2/2
A
A= $card->{'organizations'}
Aresult
11
00
1338  
1339 198 for my $id (sort keys %$orgs) {
1340 198 my $org = $orgs->{$id};
1341 198 my $params = { 'prop-id' => [$id] };
1342 198 _add_context_params($params, $org);
1343  
1344 198 ▶ my @values = ($org->{name} // '');
Condition50%1/2
$org->{"name"} // ""
l!l
1980
MC/DC0%0/1
$org->{"name"} // ""
$org->{"name"}
Truth table50%1/2
A
A= $org->{"name"}
Aresult
11
00
1345 198 ▶ if ($org->{units}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($org->{"units"}) 134 64 198
1346 134 for my $unit (@{$org->{units}}) {
1347 233 push @values, $unit->{name};
1348 }
1349 }
1350  
1351 198 ▶ if (defined $org->{sortAs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $org->{"sortAs"}) 67 131 198
1352 67 my @sa = ($org->{sortAs});
1353 67 ▶ if ($org->{units}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($org->{"units"}) 67 0 67
1354 67 for my $unit (@{$org->{units}}) {
1355 134 ▶ push @sa, $unit->{sortAs} // '';
Condition100%2/2
$unit->{"sortAs"} // ""
l!l
2132
MC/DC100%1/1
$unit->{"sortAs"} // ""
$unit->{"sortAs"}
Truth table100%2/2
A
A= $unit->{"sortAs"}
Aresult
11
00
1356 }
1357 }
1358 67 ▶ pop @sa while @sa && $sa[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
@sa and $sa[-1] eq "" and do { pop @sa } 132 67 199
Condition66%2/3
@sa and $sa[-1] eq ""
!ll&&!rl&&r
067132
MC/DC50%1/2
@sa and $sa[-1] eq ""
@sa $sa[-1] eq ""
Truth table66%2/3
A and B
A= @saB= $sa[-1] eq ""
ABresult
0X0
100
111
1359 67 ▶ $params->{'sort-as'} = [join(',', @sa)] if @sa;
Branch: 1 branch
BranchTrueFalseTotal
if @sa 67 0 67
1360 }
1361  
1362 198 push @$out, { name => 'org', values => \@values, params => $params };
1363 }
1364 }
1365  
1366 360 ▶sub _unconvert_titles {
Subroutine _unconvert_titles: called CC=5 SCAR=16.1
1367 360 my ($out, $card, $ctx) = @_;
1368 360 ▶ my $titles = $card->{titles} || return;
Condition100%2/2
$card->{'titles'} || (return)
l!l
167193
MC/DC100%1/1
$card->{'titles'} || (return)
$card->{'titles'}
Truth table100%2/2
A
A= $card->{'titles'}
Aresult
11
00
1369  
1370 167 for my $id (sort keys %$titles) {
1371 236 my $t = $titles->{$id};
1372 236 ▶ my $propname = ($t->{kind} // 'title') eq 'role' ? 'role' : 'title';
Branch: 1 branch
BranchTrueFalseTotal
($t->{'kind'} // 'title') eq 'role' ? : 36 200 236
Condition50%1/2
$t->{"kind"} // "title"
l!l
2360
MC/DC0%0/1
$t->{"kind"} // "title"
$t->{"kind"}
Truth table50%1/2
A
A= $t->{"kind"}
Aresult
11
00
1373 236 my $params = { 'prop-id' => [$id] };
1374 236 push @$out, { name => $propname, value => $t->{name}, params => $params };
1375 }
1376 }
1377  
1378 361 ▶sub _unconvert_anniversaries {
Subroutine _unconvert_anniversaries: called CC=7 SCAR=19.5
1379 361 my ($out, $card, $ctx) = @_;
1380 361 ▶ my $anns = $card->{anniversaries} || return;
Condition100%2/2
$card->{'anniversaries'} || (return)
l!l
168193
MC/DC100%1/1
$card->{'anniversaries'} || (return)
$card->{'anniversaries'}
Truth table100%2/2
A
A= $card->{'anniversaries'}
Aresult
11
00
1381  
1382 168 my %kind_to_prop = (birth => 'bday', wedding => 'anniversary', death => 'deathdate');
1383  
1384 168 for my $id (sort keys %$anns) {
1385 433 my $ann = $anns->{$id};
1386 433 ▶ my $kind = $ann->{kind} // '';
Condition50%1/2
$ann->{"kind"} // ""
l!l
4330
MC/DC0%0/1
$ann->{"kind"} // ""
$ann->{"kind"}
Truth table50%1/2
A
A= $ann->{"kind"}
Aresult
11
00
1387 433 ▶ my $propname = $kind_to_prop{$kind} // 'anniversary';
Condition100%2/2
$kind_to_prop{$kind} // "anniversary"
l!l
36766
MC/DC100%1/1
$kind_to_prop{$kind} // "anniversary"
$kind_to_prop{$kind}
Truth table100%2/2
A
A= $kind_to_prop{$kind}
Aresult
11
00
1388 433 my $params = { 'prop-id' => [$id] };
1389  
1390 433 ▶ my $date = $ann->{date} // '';
Condition50%1/2
$ann->{"date"} // ""
l!l
4330
MC/DC0%0/1
$ann->{"date"} // ""
$ann->{"date"}
Truth table50%1/2
A
A= $ann->{"date"}
Aresult
11
00
1391 # Convert unknown-year dates to vCard 4 format
1392 433 ▶ $date = "--$1$2" if $date =~ /^0000-(\d{2})-(\d{2})/;
Branch: 1 branch
BranchTrueFalseTotal
if $date =~ /^0000-(\d{2})-(\d{2})/ 99 334 433
1393  
1394 433 push @$out, { name => $propname, value => $date, params => $params };
1395 }
1396 }
1397  
1398 360 ▶sub _unconvert_notes {
Subroutine _unconvert_notes: called CC=7 SCAR=19.5
1399 360 my ($out, $card, $ctx) = @_;
1400 360 ▶ my $notes = $card->{notes} || return;
Condition100%2/2
$card->{'notes'} || (return)
l!l
201159
MC/DC100%1/1
$card->{'notes'} || (return)
$card->{'notes'}
Truth table100%2/2
A
A= $card->{'notes'}
Aresult
11
00
1401  
1402 201 for my $id (sort keys %$notes) {
1403 236 my $note = $notes->{$id};
1404 236 my $params = { 'prop-id' => [$id] };
1405  
1406 236 ▶ if ($note->{created}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($note->{"created"}) 67 169 236
1407 67 $params->{created} = [$note->{created}];
1408 }
1409 236 ▶ if ($note->{author}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($note->{"author"}) 68 168 236
1410 68 ▶ $params->{'author-name'} = [$note->{author}{name}] if $note->{author}{name};
Branch: 1 branch
BranchTrueFalseTotal
if $note->{"author"}{"name"} 68 0 68
1411 68 ▶ $params->{author} = [$note->{author}{uri}] if $note->{author}{uri};
Branch: 1 branch
BranchTrueFalseTotal
if $note->{"author"}{"uri"} 2 66 68
1412 }
1413  
1414 236 push @$out, { name => 'note', value => $note->{note}, params => $params };
1415 }
1416 }
1417  
1418 360 ▶sub _unconvert_online_services {
Subroutine _unconvert_online_services: called CC=7 SCAR=19.5
1419 360 my ($out, $card, $ctx) = @_;
1420 360 ▶ my $services = $card->{onlineServices} || return;
Condition100%2/2
$card->{'onlineServices'} || (return)
l!l
139221
MC/DC100%1/1
$card->{'onlineServices'} || (return)
$card->{'onlineServices'}
Truth table100%2/2
A
A= $card->{'onlineServices'}
Aresult
11
00
1421  
1422 139 for my $id (sort keys %$services) {
1423 308 my $svc = $services->{$id};
1424 308 my $params = { 'prop-id' => [$id] };
1425 308 _add_context_params($params, $svc);
1426 308 _add_pref_params($params, $svc->{pref});
1427  
1428 308 ▶ $params->{'service-type'} = [$svc->{service}] if $svc->{service};
Branch: 1 branch
BranchTrueFalseTotal
if $svc->{"service"} 242 66 308
1429 308 ▶ $params->{username} = [$svc->{user}] if $svc->{user};
Branch: 1 branch
BranchTrueFalseTotal
if $svc->{"user"} 141 167 308
1430  
1431 308 ▶ my $uri = $svc->{uri} // $svc->{user} // '';
Condition60%3/5
($svc->{"uri"} // $svc->{"user"}) // ""
l!l
3080
$svc->{"uri"} // $svc->{"user"}
l!l&&r!l&&!r
269390
MC/DC0%0/2
($svc->{"uri"} // $svc->{"user"}) // ""
$svc->{"uri"} $svc->{"user"}
Truth table66%2/3
A // B
A= $svc->{"uri"}B= $svc->{"user"}
ABresult
1X1
011
000
1432 308 push @$out, { name => 'impp', value => $uri, params => $params };
1433 }
1434 }
1435  
1436 362 ▶sub _unconvert_media {
Subroutine _unconvert_media: called CC=8 SCAR=20.9
1437 362 my ($out, $card, $ctx) = @_;
1438 362 ▶ my $media = $card->{media} || return;
Condition100%2/2
$card->{'media'} || (return)
l!l
139223
MC/DC100%1/1
$card->{'media'} || (return)
$card->{'media'}
Truth table100%2/2
A
A= $card->{'media'}
Aresult
11
00
1439  
1440 139 my %kind_to_prop = (photo => 'photo', logo => 'logo', sound => 'sound');
1441  
1442 139 for my $id (sort keys %$media) {
1443 209 my $m = $media->{$id};
1444 # A Media given only by blobId: the caller resolves the blob to a
1445 # data: URI first; this module fetches nothing, and an empty PHOTO
1446 # is no picture at all.
1447 209 ▶ next unless defined $m->{uri} && $m->{uri} ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->{"uri"} and $m->{"uri"} ne "" 1 208 209
Condition66%2/3
defined $m->{"uri"} and $m->{"uri"} ne ""
!ll&&!rl&&r
10208
MC/DC50%1/2
defined $m->{"uri"} and $m->{"uri"} ne ""
defined $m->{"uri"} $m->{"uri"} ne ""
Truth table66%2/3
A and B
A= defined $m->{"uri"}B= $m->{"uri"} ne ""
ABresult
0X0
100
111
1448 208 ▶ my $propname = $kind_to_prop{$m->{kind} // 'photo'} // 'photo';
Condition50%2/4
$kind_to_prop{$m->{"kind"} // "photo"} // "photo"
l!l
2080
$m->{"kind"} // "photo"
l!l
2080
MC/DC0%0/1
$kind_to_prop{$m->{"kind"} // "photo"} // "photo"
$kind_to_prop{$m->{"kind"} // "photo"}
MC/DC0%0/1
$m->{"kind"} // "photo"
$m->{"kind"}
Truth table50%1/2
A
A= $kind_to_prop{$m->{"kind"} // "photo"}
Aresult
11
00
Truth table50%1/2
A
A= $m->{"kind"}
Aresult
11
00
1449 208 my $params = { 'prop-id' => [$id] };
1450 208 _add_pref_params($params, $m->{pref});
1451 208 ▶ $params->{mediatype} = [$m->{mediaType}] if $m->{mediaType};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"mediaType"} 75 133 208
1452 208 push @$out, { name => $propname, value => $m->{uri}, params => $params };
1453 }
1454 }
1455  
1456 361 ▶sub _unconvert_links {
Subroutine _unconvert_links: called CC=7 SCAR=19.5
1457 361 my ($out, $card, $ctx) = @_;
1458 361 ▶ my $links = $card->{links} || return;
Condition100%2/2
$card->{'links'} || (return)
l!l
169192
MC/DC100%1/1
$card->{'links'} || (return)
$card->{'links'}
Truth table100%2/2
A
A= $card->{'links'}
Aresult
11
00
1459  
1460 169 for my $id (sort keys %$links) {
1461 334 my $link = $links->{$id};
1462 334 ▶ my $kind = $link->{kind} // '';
Condition100%2/2
$link->{"kind"} // ""
l!l
133201
MC/DC100%1/1
$link->{"kind"} // ""
$link->{"kind"}
Truth table100%2/2
A
A= $link->{"kind"}
Aresult
11
00
1463 334 ▶ my $propname = $kind eq 'freeBusy' ? 'fburl'
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'freeBusy' ? : 99 235 334
$kind eq 'contact' ? : 34 201 235
1464 : $kind eq 'contact' ? 'contact-uri'
1465 : 'url';
1466 334 my $params = { 'prop-id' => [$id] };
1467 334 _add_context_params($params, $link);
1468 334 _add_pref_params($params, $link->{pref});
1469 334 ▶ $params->{mediatype} = [$link->{mediaType}] if $link->{mediaType};
Branch: 1 branch
BranchTrueFalseTotal
if $link->{"mediaType"} 67 267 334
1470 334 push @$out, { name => $propname, value => $link->{uri}, params => $params };
1471 }
1472 }
1473  
1474 1080 ▶sub _unconvert_resource {
Subroutine _unconvert_resource: called CC=4 SCAR=13.9
1475 1080 my ($out, $ctx, $card, $key, $propname) = @_;
1476 1080 ▶ my $resources = $card->{$key} || return;
Condition100%2/2
$card->{$key} || (return)
l!l
237843
MC/DC100%1/1
$card->{$key} || (return)
$card->{$key}
Truth table100%2/2
A
A= $card->{$key}
Aresult
11
00
1477  
1478 237 for my $id (sort keys %$resources) {
1479 308 my $r = $resources->{$id};
1480 308 my $params = { 'prop-id' => [$id] };
1481 308 _add_context_params($params, $r);
1482 308 _add_pref_params($params, $r->{pref});
1483 308 ▶ $params->{mediatype} = [$r->{mediaType}] if $r->{mediaType};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"mediaType"} 69 239 308
1484 308 push @$out, { name => $propname, value => $r->{uri}, params => $params };
1485 }
1486 }
1487  
1488 360 ▶sub _unconvert_calendars {
Subroutine _unconvert_calendars: called CC=1 SCAR=0.0
1489 360 _unconvert_resource($_[0], $_[2], $_[1], 'calendars', 'caluri');
1490 }
1491  
1492 360 ▶sub _unconvert_scheduling_addresses {
Subroutine _unconvert_scheduling_addresses: called CC=1 SCAR=0.0
1493 360 _unconvert_resource($_[0], $_[2], $_[1], 'schedulingAddresses', 'caladruri');
1494 }
1495  
1496 360 ▶sub _unconvert_crypto_keys {
Subroutine _unconvert_crypto_keys: called CC=1 SCAR=0.0
1497 360 _unconvert_resource($_[0], $_[2], $_[1], 'cryptoKeys', 'key');
1498 }
1499  
1500 360 ▶sub _unconvert_directories {
Subroutine _unconvert_directories: called CC=6 SCAR=18.0
1501 360 my ($out, $card, $ctx) = @_;
1502 360 ▶ my $dirs = $card->{directories} || return;
Condition100%2/2
$card->{'directories'} || (return)
l!l
69291
MC/DC100%1/1
$card->{'directories'} || (return)
$card->{'directories'}
Truth table100%2/2
A
A= $card->{'directories'}
Aresult
11
00
1503  
1504 69 for my $id (sort keys %$dirs) {
1505 135 my $dir = $dirs->{$id};
1506 135 ▶ my $kind = $dir->{kind} // '';
Condition50%1/2
$dir->{"kind"} // ""
l!l
1350
MC/DC0%0/1
$dir->{"kind"} // ""
$dir->{"kind"}
Truth table50%1/2
A
A= $dir->{"kind"}
Aresult
11
00
1507 135 ▶ my $propname = $kind eq 'entry' ? 'source' : 'org-directory';
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'entry' ? : 68 67 135
1508 135 my $params = { 'prop-id' => [$id] };
1509 135 _add_context_params($params, $dir);
1510 135 _add_pref_params($params, $dir->{pref});
1511 135 ▶ $params->{mediatype} = [$dir->{mediaType}] if $dir->{mediaType};
Branch: 1 branch
BranchTrueFalseTotal
if $dir->{"mediaType"} 0 135 135
1512 135 push @$out, { name => $propname, value => $dir->{uri}, params => $params };
1513 }
1514 }
1515  
1516 360 ▶sub _unconvert_preferred_languages {
Subroutine _unconvert_preferred_languages: called CC=3 SCAR=11.0
1517 360 my ($out, $card, $ctx) = @_;
1518 360 ▶ my $langs = $card->{preferredLanguages} || return;
Condition100%2/2
$card->{'preferredLanguages'} || (return)
l!l
99261
MC/DC100%1/1
$card->{'preferredLanguages'} || (return)
$card->{'preferredLanguages'}
Truth table100%2/2
A
A= $card->{'preferredLanguages'}
Aresult
11
00
1519  
1520 99 for my $id (sort keys %$langs) {
1521 230 my $lp = $langs->{$id};
1522 230 my $params = { 'prop-id' => [$id] };
1523 230 _add_context_params($params, $lp);
1524 230 _add_pref_params($params, $lp->{pref});
1525 230 push @$out, { name => 'lang', value => $lp->{language}, params => $params };
1526 }
1527 }
1528  
1529 360 ▶sub _unconvert_personal_info {
Subroutine _unconvert_personal_info: called CC=7 SCAR=19.6
1530 360 my ($out, $card, $ctx) = @_;
1531 360 ▶ my $info = $card->{personalInfo} || return;
Condition100%2/2
$card->{'personalInfo'} || (return)
l!l
36324
MC/DC100%1/1
$card->{'personalInfo'} || (return)
$card->{'personalInfo'}
Truth table100%2/2
A
A= $card->{'personalInfo'}
Aresult
11
00
1532  
1533 36 my %level_map = (low => 'beginner', medium => 'average', high => 'expert');
1534  
1535 36 for my $id (sort keys %$info) {
1536 200 my $pi = $info->{$id};
1537 200 ▶ my $propname = $pi->{kind} // 'expertise';
Condition50%1/2
$pi->{"kind"} // "expertise"
l!l
2000
MC/DC0%0/1
$pi->{"kind"} // "expertise"
$pi->{"kind"}
Truth table50%1/2
A
A= $pi->{"kind"}
Aresult
11
00
1538 200 my $params = { 'prop-id' => [$id] };
1539 200 ▶ if ($pi->{level}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pi->{"level"}) 199 1 200
1540 199 ▶ $params->{level} = [$level_map{$pi->{level}} // $pi->{level}];
Condition33%1/3
$level_map{$pi->{"level"}} // $pi->{"level"}
l!l&&r!l&&!r
19900
MC/DC0%0/2
$level_map{$pi->{"level"}} // $pi->{"level"}
$level_map{$pi->{"level"}} $pi->{"level"}
Truth table33%1/3
A // B
A= $level_map{$pi->{"level"}}B= $pi->{"level"}
ABresult
1X1
011
000
1541 }
1542 200 ▶ if (defined $pi->{listAs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pi->{"listAs"}) 196 4 200
1543 196 $params->{index} = [$pi->{listAs}];
1544 }
1545 200 push @$out, { name => $propname, value => $pi->{value}, params => $params };
1546 }
1547 }
1548  
1549 360 ▶sub _unconvert_keywords {
Subroutine _unconvert_keywords: called CC=4 SCAR=14.6
1550 360 my ($out, $card, $ctx) = @_;
1551 360 ▶ my $keywords = $card->{keywords} || return;
Condition100%2/2
$card->{'keywords'} || (return)
l!l
134226
MC/DC100%1/1
$card->{'keywords'} || (return)
$card->{'keywords'}
Truth table100%2/2
A
A= $card->{'keywords'}
Aresult
11
00
1552 134 ▶ return unless ref $keywords eq 'HASH' && %$keywords;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keywords eq "HASH" and %$keywords 0 134 134
Condition33%1/3
ref $keywords eq "HASH" and %$keywords
!ll&&!rl&&r
00134
MC/DC0%0/2
ref $keywords eq "HASH" and %$keywords
ref $keywords eq "HASH" %$keywords
Truth table33%1/3
A and B
A= ref $keywords eq "HASH"B= %$keywords
ABresult
0X0
100
111
1553 # a list: one value, comma-separated (RFC 6350 S6.7.1)
1554 134 push @$out, { name => 'categories', values => [[ sort keys %$keywords ]], params => {} };
1555 }
1556  
1557 360 ▶sub _unconvert_members {
Subroutine _unconvert_members: called CC=3 SCAR=11.0
1558 360 my ($out, $card, $ctx) = @_;
1559 360 ▶ my $members = $card->{members} || return;
Condition100%2/2
$card->{'members'} || (return)
l!l
68292
MC/DC100%1/1
$card->{'members'} || (return)
$card->{'members'}
Truth table100%2/2
A
A= $card->{'members'}
Aresult
11
00
1560 68 for my $uid (sort keys %$members) {
1561 165 push @$out, { name => 'member', value => $uid, params => {} };
1562 }
1563 }
1564  
1565 2 ▶sub _unconvert_members_apple {
Subroutine _unconvert_members_apple: called CC=4 SCAR=14.2
1566 2 my ($out, $card, $ctx) = @_;
1567 2 ▶ my $members = $card->{members} || return;
Condition50%1/2
$card->{'members'} || (return)
l!l
20
MC/DC0%0/1
$card->{'members'} || (return)
$card->{'members'}
Truth table50%1/2
A
A= $card->{'members'}
Aresult
11
00
1568 2 for my $uid (sort keys %$members) {
1569 # Apple uses urn:uuid: prefix for member UIDs
1570 2 my $val = $uid;
1571 2 ▶ $val = "urn:uuid:$val" unless $val =~ /^urn:uuid:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^urn:uuid:/ 0 2 2
1572 2 push @$out, { name => 'x-addressbookserver-member', value => $val, params => {} };
1573 }
1574 }
1575  
1576 361 ▶sub _unconvert_related {
Subroutine _unconvert_related: called CC=7 SCAR=19.5
1577 361 my ($out, $card, $ctx) = @_;
1578 361 ▶ my $related = $card->{relatedTo} || return;
Condition100%2/2
$card->{'relatedTo'} || (return)
l!l
136225
MC/DC100%1/1
$card->{'relatedTo'} || (return)
$card->{'relatedTo'}
Truth table100%2/2
A
A= $card->{'relatedTo'}
Aresult
11
00
1579  
1580 136 for my $uid (sort keys %$related) {
1581 270 my $rel = $related->{$uid};
1582 270 my $params = {};
1583 270 ▶ if ($rel->{relation} && ref $rel->{relation} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"relation"} and ref $rel->{"relation"} eq "HASH") 200 70 270
Condition66%2/3
$rel->{"relation"} and ref $rel->{"relation"} eq "HASH"
!ll&&!rl&&r
700200
MC/DC50%1/2
$rel->{"relation"} and ref $rel->{"relation"} eq "HASH"
$rel->{"relation"} ref $rel->{"relation"} eq "HASH"
Truth table66%2/3
A and B
A= $rel->{"relation"}B= ref $rel->{"relation"} eq "HASH"
ABresult
0X0
100
111
1584 200 my @types = sort keys %{$rel->{relation}};
1585 200 ▶ $params->{type} = \@types if @types;
Branch: 1 branch
BranchTrueFalseTotal
if @types 200 0 200
1586 }
1587 # RELATED is a URI unless VALUE=text says otherwise (RFC 6350 S6.6.6)
1588 270 ▶ $params->{value} = ['text'] unless _looks_like_uri($uid);
Branch: 1 branch
BranchTrueFalseTotal
unless _looks_like_uri($uid) 166 104 270
1589 270 push @$out, { name => 'related', value => $uid, params => $params };
1590 }
1591 }
1592  
1593 360 ▶sub _unconvert_speak_to_as {
Subroutine _unconvert_speak_to_as: called CC=5 SCAR=16.1
1594 360 my ($out, $card, $ctx) = @_;
1595 360 ▶ my $sta = $card->{speakToAs} || return;
Condition100%2/2
$card->{'speakToAs'} || (return)
l!l
68292
MC/DC100%1/1
$card->{'speakToAs'} || (return)
$card->{'speakToAs'}
Truth table100%2/2
A
A= $card->{'speakToAs'}
Aresult
11
00
1596  
1597 68 ▶ if ($sta->{grammaticalGender}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sta->{"grammaticalGender"}) 67 1 68
1598 67 push @$out, { name => 'gramgender', value => $sta->{grammaticalGender}, params => {} };
1599 }
1600  
1601 68 ▶ if ($sta->{pronouns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sta->{"pronouns"}) 68 0 68
1602 68 for my $id (sort keys %{$sta->{pronouns}}) {
1603 102 my $p = $sta->{pronouns}{$id};
1604 102 my $params = { 'prop-id' => [$id] };
1605 102 _add_context_params($params, $p);
1606 102 _add_pref_params($params, $p->{pref});
1607 102 push @$out, { name => 'pronouns', value => $p->{pronouns}, params => $params };
1608 }
1609 }
1610 }
1611  
1612 ###############################################################################
1613 # PATCH: Apply JSContact changes to an existing vCard with minimal disruption
1614 ###############################################################################
1615  
1616 # top-level Card property => the sub that patches it (filled in below)
1617 19 my %PATCHERS;
1618  
1619 # patch_vcard($original, $old_card, $new_card) applies the change from
1620 # $old_card to $new_card to the original vCard, line by line: a line for
1621 # an entry the change does not touch keeps its bytes, its group, its
1622 # folding and its place, and so does every line JSContact cannot say
1623 # anything about. Returns $original itself when nothing changed; else
1624 # the patched vCard, as bytes if $original was bytes (as read from a
1625 # CardDAV server) and as characters if it was characters. An original
1626 # that cannot be parsed gives the whole Card written afresh.
1627 47 ▶sub patch_vcard {
Subroutine patch_vcard: called CC=4 SCAR=14.0
Pod undocumented
1628 47 my ($original_vcard, $old_card, $new_card) = @_;
1629  
1630 # Determine which top-level JSContact properties changed
1631 47 my %changed = _diff_cards($old_card, $new_card);
1632 47 ▶ return $original_vcard unless %changed;
Branch: 1 branch
BranchTrueFalseTotal
unless %changed 21 26 47
1633  
1634 26 ▶ my $out = _patch_vcard($original_vcard, $new_card, \%changed)
Condition33%1/3
_patch_vcard($original_vcard, $new_card, \%changed) // jscontact_to_vcard($new_card)
l!l&&r!l&&!r
2600
MC/DC0%0/2
_patch_vcard($original_vcard, $new_card, \%changed) // jscontact_to_vcard($new_card)
_patch_vcard($original_vcard, $new_card, \%changed) jscontact_to_vcard($new_card)
Truth table33%1/3
A // B
A= _patch_vcard($original_vcard, $new_card, \%changed)B= jscontact_to_vcard($new_card)
ABresult
1X1
011
000
1635 // jscontact_to_vcard($new_card);
1636 26 ▶ return utf8::is_utf8($original_vcard) ? $out : encode_utf8($out);
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($original_vcard) ? : 1 25 26
1637 }
1638  
1639 # The engine behind patch_vcard and jscontact_to_vcard(original => ...).
1640 # Reads $original (bytes or characters) itself, so the Card it diffs
1641 # against is exactly what the original says, with the vCard properties
1642 # each entry came from. $only limits the patch to those top-level Card
1643 # properties. Returns characters, or undef if the original cannot be
1644 # patched (it does not parse, or its lines cannot be matched to what
1645 # Text::VCardFast parsed).
1646 54 ▶sub _patch_vcard {
Subroutine _patch_vcard: called CC=11 SCAR=24.0
1647 54 my ($original, $new_card, $only) = @_;
1648  
1649 54 my $text = _decode_input($original);
1650 54 my ($old_card, $ctx, $props) = _read_vcard($text);
1651 54 ▶ return undef unless $old_card;
Branch: 1 branch
BranchTrueFalseTotal
unless $old_card 3 51 54
1652  
1653 51 ▶ my $P = _split_vcard($text, $props) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $P = _split_vcard($text, $props) or return undef 51 0 51
1654 51 $P->{ctx} = $ctx;
1655 51 $P->{props} = $props;
1656 # vCard 3 (RFC 2426) or 2.1: lines patched in must be valid there
1657 51 ▶ $P->{v3} = ((_first_value($props, 'version') // '4.0') =~ /^[23]\./) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(_first_value($props, 'version') // '4.0') =~ /^[23]\./ ? : 34 17 51
Condition50%1/2
_first_value($props, "version") // "4.0"
l!l
510
MC/DC0%0/1
_first_value($props, "version") // "4.0"
_first_value($props, "version")
Truth table50%1/2
A
A= _first_value($props, "version")
Aresult
11
00
1658  
1659 51 ▶ my %changed = $only ? %$only : _diff_cards($old_card, $new_card);
Branch: 1 branch
BranchTrueFalseTotal
$only ? : 26 25 51
1660 51 for my $key (sort keys %changed) {
1661 36 ▶ my $patcher = $PATCHERS{$key} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $patcher = $PATCHERS{$key} or next 35 1 36
1662 35 $patcher->($P, $key, $old_card, $new_card);
1663 }
1664 51 _drop_orphan_groups($P);
1665  
1666 return join '', map {
1667 1526 ▶ (@{$_->{before} // []}, ($_->{deleted} ? () : $_->{raw}), @{$_->{after} // []})
Branch: 1 branch
BranchTrueFalseTotal
$_->{'deleted'} ? : 5 1521 1526
Condition100%4/4
$_->{"before"} // []
l!l
21524
$_->{"after"} // []
l!l
71519
MC/DC100%1/1
$_->{"before"} // []
$_->{"before"}
MC/DC100%1/1
$_->{"after"} // []
$_->{"after"}
Truth table100%2/2
A
A= $_->{"before"}
Aresult
11
00
Truth table100%2/2
A
A= $_->{"after"}
Aresult
11
00
1668 51 } @{$P->{lines}};
1669 }
1670  
1671 # --- The original, as lines ----------------------------------------------
1672  
1673 # Split a vCard into content lines, each keeping its raw text (folding and
1674 # line ending included), and match each property line of the first vCard
1675 # to the property Text::VCardFast parsed from it
1676 51 ▶sub _split_vcard {
Subroutine _split_vcard: called CC=22 SCAR=31.7
1677 51 my ($text, $props) = @_;
1678 51 ▶ return undef unless $text =~ /\n/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\n/ 0 51 51
1679 51 ▶ my $eol = $text =~ /\r\n/ ? "\r\n" : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\r\n/ ? : 4 47 51
1680  
1681 51 my @lines;
1682 51 for my $phys (split /(?<=\n)/, $text) {
1683 1566 ▶ if (@lines && $phys =~ /^[ \t]/) { $lines[-1]{raw} .= $phys }
Branch: 1 branch
BranchTrueFalseTotal
if (@lines and $phys =~ /^[ \t]/) { } 40 1526 1566
Condition100%3/3
@lines and $phys =~ /^[ \t]/
!ll&&!rl&&r
51147540
MC/DC100%2/2
@lines and $phys =~ /^[ \t]/
@lines $phys =~ /^[ \t]/
Truth table100%3/3
A and B
A= @linesB= $phys =~ /^[ \t]/
ABresult
0X0
100
111
1684 1526 else { push @lines, { raw => $phys } }
1685 }
1686  
1687 51 my ($depth, $end, %by_name) = (0);
1688 51 for my $i (0..$#lines) {
1689 1526 my $line = $lines[$i];
1690 1526 $line->{index} = $i;
1691 1526 (my $u = $line->{raw}) =~ s/\r?\n[ \t]//g;
1692 1526 $u =~ s/\r?\n\z//;
1693 1526 $line->{text} = $u;
1694 1526 ▶ next if $u =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $u =~ /^\s*$/ 0 1526 1526
1695 1526 ▶ if ($u =~ /^BEGIN:/i) { $depth++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($u =~ /^BEGIN:/i) 51 1475 1526
1696 1475 ▶ if ($u =~ /^END:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u =~ /^END:/i) 51 1424 1475
1697 51 ▶ $end //= $i if $depth == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 1 51 0 51
Condition33%1/3
$end //= $i
l!l&&r!l&&!r
0510
MC/DC0%0/2
$end //= $i
$end $i
Truth table33%1/3
A //= B
A= $endB= $i
ABresult
1X1
011
000
1698 51 $depth--;
1699 51 next;
1700 }
1701 1424 ▶ next unless $depth == 1 && !defined $end;
Branch: 1 branch
BranchTrueFalseTotal
unless $depth == 1 and not defined $end 0 1424 1424
Condition33%1/3
$depth == 1 and not defined $end
!ll&&!rl&&r
001424
MC/DC0%0/2
$depth == 1 and not defined $end
$depth == 1 not defined $end
Truth table33%1/3
A and not(B)
A= $depth == 1B= not defined $end
ABresult
0X0
100
111
1702 1424 my ($name) = $u =~ /^([^;:]*)/;
1703 1424 my $lname = lc $name;
1704 # as Text::VCardFast: the group is everything before the last dot
1705 1424 ▶ if ($lname =~ s/^(.+)\.(.*?)$/$2/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lname =~ s/^(.+)\.(.*?)$/$2/) 509 915 1424
1706 509 ($line->{group}) = $name =~ /^(.+)\./;
1707 }
1708 1424 $line->{name} = $lname;
1709 1424 push @{$by_name{$lname}}, $line;
1710 }
1711 51 ▶ return undef unless defined $end;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end 0 51 51
1712  
1713 51 my %line_of;
1714 51 for my $name (keys %$props) {
1715 995 my $items = $props->{$name};
1716 995 ▶ my $ls = $by_name{$name} // [];
Condition50%1/2
$by_name{$name} // []
l!l
9950
MC/DC0%0/1
$by_name{$name} // []
$by_name{$name}
Truth table50%1/2
A
A= $by_name{$name}
Aresult
11
00
1717 995 ▶ return undef unless @$ls == @$items;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ls == @$items 0 995 995
1718 995 for my $i (0..$#$items) {
1719 1424 $line_of{refaddr $items->[$i]} = $ls->[$i];
1720 1424 $ls->[$i]{item} = $items->[$i];
1721 }
1722 }
1723 51 for my $name (keys %by_name) {
1724 995 ▶ return undef unless $props->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $props->{$name} 0 995 995
1725 }
1726  
1727 return {
1728 51 lines => \@lines,
1729 eol => $eol,
1730 end => $lines[$end],
1731 by_name => \%by_name,
1732 line_of => \%line_of,
1733 };
1734 }
1735  
1736 70 ▶sub _lines_of_items {
Subroutine _lines_of_items: called CC=3 SCAR=11.6
1737 70 my ($P, $items) = @_;
1738 30 return sort { $a->{index} <=> $b->{index} }
1739 92 ▶ grep { defined && !$_->{deleted} }
Condition33%1/3
defined $_ && !$_->{'deleted'}
!ll&&!rl&&r
0092
MC/DC0%0/2
defined $_ && !$_->{'deleted'}
defined $_ !$_->{'deleted'}
Truth table33%1/3
A && not(B)
A= defined $_B= !$_->{'deleted'}
ABresult
0X0
100
111
1740 70 ▶ map { $P->{line_of}{refaddr $_} } @{$items // []};
Condition50%1/2
$items // []
l!l
700
MC/DC0%0/1
$items // []
$items
Truth table50%1/2
A
A= $items
Aresult
11
00
1741 }
1742  
1743 13 ▶sub _lines_named {
Subroutine _lines_named: called CC=2 SCAR=6.9
1744 13 my ($P, @names) = @_;
1745 7 return sort { $a->{index} <=> $b->{index} }
1746 17 grep { !$_->{deleted} }
1747 13 ▶ map { @{$P->{by_name}{$_} // []} } @names;
Condition100%2/2
$P->{"by_name"}{$_} // []
l!l
125
MC/DC100%1/1
$P->{"by_name"}{$_} // []
$P->{"by_name"}{$_}
Truth table100%2/2
A
A= $P->{"by_name"}{$_}
Aresult
11
00
1748 }
1749  
1750 6 ▶sub _group_lines {
Subroutine _group_lines: called CC=3 SCAR=11.0
1751 6 my ($P, $group) = @_;
1752 330 ▶ return grep { defined $_->{group} && lc $_->{group} eq lc $group && !$_->{deleted} }
Condition100%6/6
defined $_->{'group'} && lc $_->{'group'} eq lc $group && !$_->{'deleted'}
!ll&&!rl&&r
318210
defined $_->{"group"} and lc $_->{"group"} eq lc $group
!ll&&!rl&&r
17414412
MC/DC100%3/3
defined $_->{'group'} && lc $_->{'group'} eq lc $group && !$_->{'deleted'}
defined $_->{"group"} lc $_->{"group"} eq lc $group !$_->{'deleted'}
Truth table100%4/4
A and B && not(C)
A= defined $_->{"group"}B= lc $_->{"group"} eq lc $groupC= !$_->{'deleted'}
ABCresult
0XX0
10X0
1100
1111
1753 6 @{$P->{lines}};
1754 }
1755  
1756 # --- Editing lines -------------------------------------------------------
1757  
1758 25 ▶sub _set_line {
Subroutine _set_line: called CC=2 SCAR=6.9
1759 25 my ($P, $line, $text) = @_;
1760 25 ▶ return if $text eq $line->{text};
Branch: 1 branch
BranchTrueFalseTotal
if $text eq $line->{"text"} 1 24 25
1761 24 $line->{text} = $text;
1762 24 $line->{raw} = _fold($text, $P->{eol});
1763 }
1764  
1765 5 ▶sub _delete_line {
Subroutine _delete_line: called CC=3 SCAR=11.0
1766 5 my ($P, $line) = @_;
1767 5 ▶ return unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 5 5
1768 5 $line->{deleted} = 1;
1769 5 ▶ $P->{touched_groups}{lc $line->{group}} = 1 if defined $line->{group};
Branch: 1 branch
BranchTrueFalseTotal
if defined $line->{"group"} 4 1 5
1770 }
1771  
1772 # Insert content lines after $anchor (and the lines of its group that
1773 # follow it), or before END:VCARD
1774 8 ▶sub _insert {
Subroutine _insert: called CC=7 SCAR=19.5
1775 8 my ($P, $anchor, @texts) = @_;
1776 8 my $at = $P->{end};
1777 8 my $where = 'before';
1778 8 ▶ if ($anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anchor) 6 2 8
1779 6 my $i = $anchor->{index};
1780 6 ▶ my $g = $anchor->{group} // $anchor->{new_group};
Condition66%2/3
$anchor->{"group"} // $anchor->{"new_group"}
l!l&&r!l&&!r
402
MC/DC50%1/2
$anchor->{"group"} // $anchor->{"new_group"}
$anchor->{"group"} $anchor->{"new_group"}
Truth table66%2/3
A // B
A= $anchor->{"group"}B= $anchor->{"new_group"}
ABresult
1X1
011
000
1781 6 my $lines = $P->{lines};
1782 6 ▶ while (defined $g && $i < $#$lines && defined $lines->[$i+1]{group}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $g and $i < $lines->$#* and defined $$lines[$i + 1]{"group"} and lc $$lines[$i + 1]{"group"} eq lc $g) 4 6 10
Condition88%8/9
defined $g and $i < $lines->$#* and defined $$lines[$i + 1]{"group"} and lc $$lines[$i + 1]{"group"} eq lc $g
!ll&&!rl&&r
514
defined $g and $i < $lines->$#* and defined $$lines[$i + 1]{"group"}
!ll&&!rl&&r
235
defined $g and $i < $lines->$#*
!ll&&!rl&&r
208
MC/DC75%3/4
defined $g and $i < $lines->$#* and defined $$lines[$i + 1]{"group"} and lc $$lines[$i + 1]{"group"} eq lc $g
defined $g $i < $lines->$#* defined $$lines[$i + 1]{"group"} lc $$lines[$i + 1]{"group"} eq lc $g
Truth table80%4/5
A and B and C and D
A= defined $gB= $i < $lines->$#*C= defined $$lines[$i + 1]{"group"}D= lc $$lines[$i + 1]{"group"} eq lc $g
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1783 && lc $lines->[$i+1]{group} eq lc $g) {
1784 4 $i++;
1785 }
1786 6 ($at, $where) = ($lines->[$i], 'after');
1787 }
1788 8 push @{$at->{$where}}, map { _fold($_, $P->{eol}) } @texts;
1789 }
1790  
1791 # A group left with nothing but annotations (a label, an Apple country
1792 # code, a GEO or TZ for an address) once its property is deleted goes too
1793 19 my %ANNOTATION = map { $_ => 1 } qw(x-ablabel x-abadr geo tz);
1794  
1795 51 ▶sub _drop_orphan_groups {
Subroutine _drop_orphan_groups: called CC=5 SCAR=16.1
1796 51 my ($P) = @_;
1797 51 ▶ for my $g (sort keys %{$P->{touched_groups} // {}}) {
Condition100%2/2
$P->{"touched_groups"} // {}
l!l
150
MC/DC100%1/1
$P->{"touched_groups"} // {}
$P->{"touched_groups"}
Truth table100%2/2
A
A= $P->{"touched_groups"}
Aresult
11
00
1798 2 my @live = _group_lines($P, $g);
1799 2 ▶ next if grep { !$ANNOTATION{$_->{name}} } @live;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $ANNOTATION{$_->{"name"}};} @live 0 2 2
1800 2 _delete_line($P, $_) for @live;
1801 }
1802 }
1803  
1804 # --- Writing an entry into a line ----------------------------------------
1805  
1806 # A content line split into its name (group included, as written), its
1807 # parameters (each as written, with its values decoded) and its value
1808 25 ▶sub _parse_line {
Subroutine _parse_line: called CC=6 SCAR=19.8
1809 25 my ($text) = @_;
1810 25 $text =~ /\G([^;:]*)/gc;
1811 25 my $name = $1;
1812 25 my @tokens;
1813 25 ▶ while ($text =~ /\G(;([^;:=]*)(?:=((?:"[^"]*"|[^";:])*))?)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G(;([^;:=]*)(?:=((?:"[^"]*"|[^";:])*))?)/cg) 28 25 53
1814 28 my ($raw, $pname, $pval) = ($1, $2, $3);
1815 28 my $tok = { raw => $raw, rawname => $pname };
1816 28 ▶ if (defined $pval) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pval) { } 28 0 28
1817 28 $tok->{name} = lc $pname;
1818 $tok->{values} = [ map {
1819 28 my $r = $_;
1820 29 (my $v = $r) =~ s/^"(.*)"$/$1/s;
1821 0 ▶ $v =~ s/\^([n^'])/$1 eq 'n' ? "\n" : $1 eq '^' ? '^' : '"'/ge;
Branch: 2 branches
BranchTrueFalseTotal
$1 eq 'n' ? : 0 0 0
$1 eq '^' ? : 0 0 0
1822 29 { raw => $r, val => $v };
1823 } $pval =~ /((?:"[^"]*"|[^,"])+)/g ];
1824 }
1825 else {
1826 # vCard 2.1: a bare TYPE value
1827 0 $tok->{name} = 'type';
1828 0 $tok->{bare} = 1;
1829 0 $tok->{values} = [ { raw => $pname, val => $pname } ];
1830 }
1831 28 push @tokens, $tok;
1832 }
1833 25 ▶ return undef unless $text =~ /\G:/gc;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\G:/cg 0 25 25
1834 25 return { name => $name, tokens => \@tokens, value => substr($text, pos $text) };
1835 }
1836  
1837 # The parameters the reader interprets. Any other parameter on a line is
1838 # kept as it is; so is a TYPE value the reader does not interpret.
1839 589 my %KNOWN_PARAM = map { $_ => 1 } qw(
1840 prop-id value type pref label geo tz cc mediatype service-type username
1841 x-service-type x-user level index created author author-name sort-as
1842 encoding
1843 19 ), map { "x-$_" } qw(label geo tz cc mediatype sort-as level index created
1844 author author-name);
1845 19 my %KNOWN_TYPE = map { $_ => 1 } qw(home work pref cell voice fax video pager
1846 text textphone);
1847 # properties whose every TYPE value is the reader's (a media format, a
1848 # relation)
1849 19 my %ALL_TYPES_KNOWN = map { $_ => 1 } qw(photo logo sound related);
1850  
1851 44 ▶sub _param_key {
Subroutine _param_key: called CC=2 SCAR=6.9
1852 44 my ($name, $val) = @_;
1853 44 $name = lc $name;
1854 44 ▶ return "$name\0" . ($name =~ /^(?:type|value|encoding)$/ ? lc $val : $val);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^(?:type|value|encoding)$/ ? : 37 7 44
1855 }
1856  
1857 # The line for $prop written over the original $line of the same name:
1858 # the original's name, group and parameters stay as written, except the
1859 # known parameters the new property no longer has
1860 23 ▶sub _merged_text {
Subroutine _merged_text: called CC=23 SCAR=31.6
1861 23 my ($P, $line, $prop) = @_;
1862 23 ▶ my $parsed = _parse_line($line->{text}) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = _parse_line($line->{"text"}) or return undef 23 0 23
1863 23 ▶ my $params = $prop->{params} // {};
Condition50%1/2
$prop->{"params"} // {}
l!l
230
MC/DC0%0/1
$prop->{"params"} // {}
$prop->{"params"}
Truth table50%1/2
A
A= $prop->{"params"}
Aresult
11
00
1864 23 my $lname = lc $prop->{name};
1865  
1866 23 my (@want, %want);
1867 23 for my $n (_param_order($params)) {
1868 14 my $vals = $params->{$n};
1869 14 ▶ for my $v (ref $vals eq 'ARRAY' ? @$vals : ($vals)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $vals eq 'ARRAY' ? : 14 0 14
1870 17 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 17 17
1871 17 my $k = _param_key($n, _clean_vcard_text($v));
1872 17 push @want, [$n, $v, $k];
1873 17 $want{$k}++;
1874 }
1875 }
1876  
1877 23 my $out = $parsed->{name};
1878 23 ▶ $out = "$line->{new_group}.$out" if $line->{new_group};
Branch: 1 branch
BranchTrueFalseTotal
if $line->{"new_group"} 1 22 23
1879 23 for my $tok (@{$parsed->{tokens}}) {
1880 28 my $n = $tok->{name};
1881 my $keep_all = !$KNOWN_PARAM{$n}
1882 # a VALUE=date (say) stays unless the new value says otherwise
1883 || ($n eq 'value' && !$params->{value}
1884 28 ▶ && !grep { $_->{val} =~ /^(?:uri|text)$/i } @{$tok->{values}});
Condition77%7/9
!$KNOWN_PARAM{$n} || $n eq 'value' && !$params->{'value'} && !grep({$_->{'val'} =~ /^(?:uri|text)$/i;} @{$$tok{'values'};})
l!l&&r!l&&!r
1126
$n eq "value" and not $params->{"value"} and not grep({$_->{"val"} =~ /^(?:uri|text)$/i;} @{$$tok{"values"};})
!ll&&!rl&&r
2601
$n eq "value" and not $params->{"value"}
!ll&&!rl&&r
2601
MC/DC50%2/4
!$KNOWN_PARAM{$n} || $n eq 'value' && !$params->{'value'} && !grep({$_->{'val'} =~ /^(?:uri|text)$/i;} @{$$tok{'values'};})
!$KNOWN_PARAM{$n} $n eq "value" not $params->{"value"} not grep({$_->{"val"} =~ /^(?:uri|text)$/i;} @{$$tok{"values"};})
Truth table60%3/5
not(A) || B and not(C) and not(D)
A= !$KNOWN_PARAM{$n}B= $n eq "value"C= not $params->{"value"}D= not grep({$_->{"val"} =~ /^(?:uri|text)$/i;} @{$$tok{"values"};})
ABCDresult
1XXX1
01111
00XX0
010X0
01100
1885 28 ▶ if ($keep_all) { $out .= $tok->{raw}; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_all) 2 26 28
1886 26 my (@kept, $dropped);
1887 26 for my $v (@{$tok->{values}}) {
1888 27 my $k = _param_key($n, $v->{val});
1889 27 ▶ if ($want{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($want{$k}) { } 15 12 27
elsif ($n eq 'type' and not $ALL_TYPES_KNOWN{$lname} and not $KNOWN_TYPE{lc $v->{'val'}}) { } 9 3 12
1890 15 $want{$k}--;
1891 15 push @kept, $v;
1892 }
1893 ▶ elsif ($n eq 'type' && !$ALL_TYPES_KNOWN{$lname} && !$KNOWN_TYPE{lc $v->{val}}) {
Condition100%6/6
$n eq "type" and not $ALL_TYPES_KNOWN{$lname} and not $KNOWN_TYPE{lc $v->{"val"}}
!ll&&!rl&&r
219
$n eq "type" and not $ALL_TYPES_KNOWN{$lname}
!ll&&!rl&&r
1110
MC/DC100%3/3
$n eq "type" and not $ALL_TYPES_KNOWN{$lname} and not $KNOWN_TYPE{lc $v->{"val"}}
$n eq "type" not $ALL_TYPES_KNOWN{$lname} not $KNOWN_TYPE{lc $v->{"val"}}
Truth table100%4/4
A and not(B) and not(C)
A= $n eq "type"B= not $ALL_TYPES_KNOWN{$lname}C= not $KNOWN_TYPE{lc $v->{"val"}}
ABCresult
0XX0
10X0
1100
1111
1894 9 push @kept, $v;
1895 }
1896 else {
1897 3 $dropped = 1;
1898 }
1899 }
1900 26 ▶ if (!$dropped) { $out .= $tok->{raw} }
Branch: 2 branches
BranchTrueFalseTotal
if (not $dropped) { } 23 3 26
elsif (@kept) { } 1 2 3
1901 elsif (@kept) {
1902 0 $out .= $tok->{bare} ? join('', map { ";$_->{raw}" } @kept)
1903 1 ▶ : ";$tok->{rawname}=" . join(',', map { $_->{raw} } @kept);
Branch: 1 branch
BranchTrueFalseTotal
$tok->{'bare'} ? : 0 1 1
1904 }
1905 }
1906 23 for my $w (@want) {
1907 17 my ($n, $v, $k) = @$w;
1908 17 ▶ next unless $want{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $want{$k} 15 2 17
1909 2 $want{$k}--;
1910 2 $out .= ';' . uc($n) . '=' . _param_value($v);
1911 }
1912 23 return $out . ':' . _value_text($prop);
1913 }
1914  
1915 # Write $prop over the original $line: merged when it is the same vCard
1916 # property, else replaced, keeping the line's group (and so its label)
1917 23 ▶sub _replace_line {
Subroutine _replace_line: called CC=5 SCAR=23.1
1918 23 my ($P, $line, $prop) = @_;
1919 23 my $text;
1920 23 ▶ $text = _merged_text($P, $line, $prop) if lc $prop->{name} eq $line->{name};
Branch: 1 branch
BranchTrueFalseTotal
if lc $prop->{"name"} eq $line->{"name"} 23 0 23
1921 23 ▶ unless (defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 23 23
1922 0 ▶ $prop->{group} //= $line->{group} // $line->{new_group};
Condition0%0/6
$prop->{"group"} //= $line->{"group"} // $line->{"new_group"}
l!l&&r!l&&!r
000
$line->{"group"} // $line->{"new_group"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$prop->{"group"} //= $line->{"group"} // $line->{"new_group"}
$prop->{"group"} $line->{"group"} $line->{"new_group"}
Truth table0%0/4
A //= B // C
A= $prop->{"group"}B= $line->{"group"}C= $line->{"new_group"}
ABCresult
1XX1
01X1
0011
0000
1923 0 $text = _prop_line($prop);
1924 }
1925 23 _set_line($P, $line, $text);
1926 }
1927  
1928 # A vCard property that has an Apple or older form keeps the form the
1929 # original used
1930 19 my %KEEP_NAME = (
1931 'x-abdate' => { anniversary => 1 },
1932 'x-socialprofile' => { impp => 1 },
1933 'socialprofile' => { impp => 1 },
1934 );
1935  
1936 # Make a generated property fit the original: its property name, and the
1937 # vCard 3 syntax if the original is vCard 3
1938 31 ▶sub _fit {
Subroutine _fit: called CC=5 SCAR=17.2
1939 31 my ($P, $prop, $line) = @_;
1940 $prop->{name} = $line->{name}
1941 31 ▶ if $line && $KEEP_NAME{$line->{name}} && $KEEP_NAME{$line->{name}}{lc $prop->{name}};
Branch: 1 branch
BranchTrueFalseTotal
if $line and $KEEP_NAME{$line->{"name"}} and $KEEP_NAME{$line->{"name"}}{lc $prop->{"name"}} 0 31 31
Condition50%3/6
$line and $KEEP_NAME{$line->{"name"}} and $KEEP_NAME{$line->{"name"}}{lc $prop->{"name"}}
!ll&&!rl&&r
3100
$line and $KEEP_NAME{$line->{"name"}}
!ll&&!rl&&r
8230
MC/DC0%0/3
$line and $KEEP_NAME{$line->{"name"}} and $KEEP_NAME{$line->{"name"}}{lc $prop->{"name"}}
$line $KEEP_NAME{$line->{"name"}} $KEEP_NAME{$line->{"name"}}{lc $prop->{"name"}}
Truth table50%2/4
A and B and C
A= $lineB= $KEEP_NAME{$line->{"name"}}C= $KEEP_NAME{$line->{"name"}}{lc $prop->{"name"}}
ABCresult
0XX0
10X0
1100
1111
1942 31 ▶ _to_v3($prop) if $P->{v3};
Branch: 1 branch
BranchTrueFalseTotal
if $P->{"v3"} 26 5 31
1943 31 return $prop;
1944 }
1945  
1946 # vCard 4 parameters with no vCard 3 form keep their value as X- (RFC
1947 # 2426 S4 allows x-name parameters); the reader reads them back
1948 19 my @V4_ONLY_PARAM = qw(label geo tz cc mediatype sort-as level index created
1949 author author-name);
1950  
1951 # Rewrite a property as the vCard 4 writer made it into vCard 3 (RFC 2426)
1952 # syntax: no PROP-ID; PREF=1 becomes TYPE=pref; an inline data: URI is
1953 # ENCODING=b with the format as TYPE; RFC 9554 SERVICE-TYPE and USERNAME
1954 # are the X-SERVICE-TYPE and X-USER that Apple writes; KIND and MEMBER
1955 # are Apple's X-ADDRESSBOOKSERVER-*; a date without a year uses Apple's
1956 # 1604. A property defined only for vCard 4 (RELATED, GENDER, LANG, ...)
1957 # keeps its name: RFC 2425 allows any registered name, and a vCard 3
1958 # reader skips one it does not know.
1959 26 ▶sub _to_v3 {
Subroutine _to_v3: called CC=30 SCAR=40.2
1960 26 my ($prop) = @_;
1961 26 ▶ my $p = $prop->{params} //= {};
Condition50%1/2
$prop->{"params"} //= {}
l!l
260
MC/DC0%0/1
$prop->{"params"} //= {}
$prop->{"params"}
Truth table50%1/2
A
A= $prop->{"params"}
Aresult
11
00
1962 26 my $name = lc $prop->{name};
1963  
1964 26 delete $p->{'prop-id'};
1965 26 ▶ if (my $pref = delete $p->{pref}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = delete $p->{"pref"}) 4 22 26
1966 4 push @{$p->{type}}, 'pref'
1967 4 ▶ if grep { defined $_ && looks_like_number($_) && $_ == 1 } ref $pref ? @$pref : ($pref);
Branch: 2 branches
BranchTrueFalseTotal
if grep {defined $_ and &looks_like_number($_) and $_ == 1;} ref $pref ? @$pref : $pref 4 0 4
ref $pref ? : 4 0 4
Condition33%2/6
defined $_ && &looks_like_number($_) && $_ == 1
!ll&&!rl&&r
004
defined $_ and &looks_like_number($_)
!ll&&!rl&&r
004
MC/DC0%0/3
defined $_ && &looks_like_number($_) && $_ == 1
defined $_ &looks_like_number($_) $_ == 1
Truth table25%1/4
A and B && C
A= defined $_B= &looks_like_number($_)C= $_ == 1
ABCresult
0XX0
10X0
1100
1111
1968 }
1969  
1970 26 ▶ if ($name =~ /^(?:photo|logo|sound)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(?:photo|logo|sound)$/) 1 25 26
1971 1 my $mt = delete $p->{mediatype};
1972 1 ▶ if (($prop->{value} // '') =~ m{^data:([^;,]*)(?:;[^;,]*)*;base64,(.*)\z}si) {
Branch: 1 branch
BranchTrueFalseTotal
if (($prop->{'value'} // '') =~ /^data:([^;,]*)(?:;[^;,]*)*;base64,(.*)\z/is) { } 1 0 1
Condition50%1/2
$prop->{"value"} // ""
l!l
10
MC/DC0%0/1
$prop->{"value"} // ""
$prop->{"value"}
Truth table50%1/2
A
A= $prop->{"value"}
Aresult
11
00
1973 1 my ($type, $b64) = ($1, $2);
1974 1 ▶ $type = $mt->[0] if $type eq '' && $mt;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "" and $mt 0 1 1
Condition33%1/3
$type eq "" and $mt
!ll&&!rl&&r
100
MC/DC0%0/2
$type eq "" and $mt
$type eq "" $mt
Truth table33%1/3
A and B
A= $type eq ""B= $mt
ABresult
0X0
100
111
1975 1 ▶ $type =~ s{.*/}{} if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 1 0 1
1976 1 $p->{encoding} = ['b'];
1977 1 ▶ $p->{type} = [$type] if defined $type && $type ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and $type ne "" 1 0 1
Condition33%1/3
defined $type and $type ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $type and $type ne ""
defined $type $type ne ""
Truth table33%1/3
A and B
A= defined $typeB= $type ne ""
ABresult
0X0
100
111
1978 1 $prop->{value} = $b64;
1979 }
1980 else {
1981 0 $p->{value} = ['uri'];
1982 }
1983 }
1984 26 ▶ $p->{type} = [ map { uc } @{$p->{type}} ] if $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"type"} 10 16 26
1985  
1986 26 ▶ $p->{'x-service-type'} = delete $p->{'service-type'} if $p->{'service-type'};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"service-type"} 0 26 26
1987 26 ▶ $p->{'x-user'} = delete $p->{username} if $p->{username};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"username"} 0 26 26
1988 26 for my $n (@V4_ONLY_PARAM) {
1989 286 ▶ $p->{"x-$n"} = delete $p->{$n} if exists $p->{$n};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{$n} 0 286 286
1990 }
1991  
1992 26 ▶ if ($name eq 'tel' && $p->{value}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'tel' and $p->{'value'}) { } 1 25 26
elsif ($name eq 'related' and $p->{'value'}) { } 1 48 49
Condition100%3/3
$name eq "tel" and $p->{"value"}
!ll&&!rl&&r
2411
MC/DC100%2/2
$name eq "tel" and $p->{"value"}
$name eq "tel" $p->{"value"}
Truth table100%3/3
A and B
A= $name eq "tel"B= $p->{"value"}
ABresult
0X0
100
111
1993 # a vCard 3 TEL is a phone number, not a URI (RFC 2426 S3.3.1)
1994 1 delete $p->{value};
1995 1 $prop->{value} =~ s/^tel://i;
1996 }
1997 ▶ elsif ($name eq 'related' && $p->{value}) {
Condition66%2/3
$name eq "related" and $p->{"value"}
!ll&&!rl&&r
2401
MC/DC50%1/2
$name eq "related" and $p->{"value"}
$name eq "related" $p->{"value"}
Truth table66%2/3
A and B
A= $name eq "related"B= $p->{"value"}
ABresult
0X0
100
111
1998 # vCard 3 has no RELATED; RFC 2425 values are text unless they say
1999 # otherwise, so the vCard 4 VALUE=text is not needed
2000 1 delete $p->{value};
2001 }
2002  
2003 26 ▶ if ($name eq 'kind') {
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq 'kind') { } 0 26 26
elsif ($name eq 'member') { } 0 26 26
elsif ($name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/ and ($prop->{'value'} // '') =~ /^--(\d\d)(\d\d)$/) { } 1 50 51
2004 0 $prop->{name} = 'x-addressbookserver-kind';
2005 }
2006 elsif ($name eq 'member') {
2007 0 $prop->{name} = 'x-addressbookserver-member';
2008 0 ▶ $prop->{value} = "urn:uuid:$prop->{value}" unless $prop->{value} =~ /^urn:uuid:/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $prop->{"value"} =~ /^urn:uuid:/i 0 0 0
2009 }
2010 ▶ elsif ($name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/
Condition60%3/5
$name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/ and ($prop->{"value"} // "") =~ /^--(\d\d)(\d\d)$/
!ll&&!rl&&r
2501
$prop->{"value"} // ""
l!l
10
MC/DC50%1/2
$name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/ and ($prop->{"value"} // "") =~ /^--(\d\d)(\d\d)$/
$name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/ $prop->{"value"}
Truth table66%2/3
A and B
A= $name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/B= $prop->{"value"}
ABresult
0X0
100
111
2011 && ($prop->{value} // '') =~ /^--(\d\d)(\d\d)$/) {
2012 1 $prop->{value} = "1604-$1-$2";
2013 }
2014 26 return $prop;
2015 }
2016  
2017 # --- Labels ---------------------------------------------------------------
2018  
2019 2 ▶sub _label_text {
Subroutine _label_text: called CC=1 SCAR=0.0
2020 2 my ($group, $label) = @_;
2021 2 return "$group.X-ABLabel:" . _escape_text($label);
2022 }
2023  
2024 # Give $line the label $new (none if undef or empty): the X-ABLabel of its
2025 # group is rewritten, added (with a new group if the line has none) or
2026 # deleted
2027 2 ▶sub _set_label {
Subroutine _set_label: called CC=13 SCAR=29.4
2028 2 my ($P, $line, $prop, $new) = @_;
2029 2 ▶ $new = undef if defined $new && (ref $new || $new eq '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $new and ref $new || $new eq "" 0 2 2
Condition33%2/6
defined $new and ref $new || $new eq ""
!ll&&!rl&&r
020
ref $new or $new eq ""
l!l&&r!l&&!r
002
MC/DC0%0/3
defined $new and ref $new || $new eq ""
defined $new ref $new $new eq ""
Truth table25%1/4
A and B or C
A= defined $newB= ref $newC= $new eq ""
ABCresult
0XX0
1000
11X1
1011
2030 2 ▶ my $group = $line->{group} // $line->{new_group};
Condition66%2/3
$line->{"group"} // $line->{"new_group"}
l!l&&r!l&&!r
101
MC/DC50%1/2
$line->{"group"} // $line->{"new_group"}
$line->{"group"} $line->{"new_group"}
Truth table66%2/3
A // B
A= $line->{"group"}B= $line->{"new_group"}
ABresult
1X1
011
000
2031 my ($label_line) = defined $group
2032 2 ▶ ? grep { $_->{name} && $_->{name} eq 'x-ablabel' } _group_lines($P, $group) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $group ? : 1 1 2
Condition66%2/3
$_->{'name'} && $_->{'name'} eq 'x-ablabel'
!ll&&!rl&&r
011
MC/DC50%1/2
$_->{'name'} && $_->{'name'} eq 'x-ablabel'
$_->{'name'} $_->{'name'} eq 'x-ablabel'
Truth table66%2/3
A && B
A= $_->{'name'}B= $_->{'name'} eq 'x-ablabel'
ABresult
0X0
100
111
2033 2 ▶ if (!defined $new) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new) 0 2 2
2034 0 ▶ _delete_line($P, $label_line) if $label_line;
Branch: 1 branch
BranchTrueFalseTotal
if $label_line 0 0 0
2035 0 return;
2036 }
2037 2 ▶ if ($label_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label_line) 1 1 2
2038 1 my $pl = _parse_line($label_line->{text});
2039 0 ▶ my $head = $pl ? $pl->{name} . join('', map { $_->{raw} } @{$pl->{tokens}})
Branch: 1 branch
BranchTrueFalseTotal
$pl ? : 1 0 1
2040 : "$group.X-ABLabel";
2041 1 _set_line($P, $label_line, "$head:" . _escape_text($new));
2042 1 return;
2043 }
2044 1 ▶ unless (defined $group) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $group) 1 0 1
2045 1 $group = _alloc_group($P->{ctx});
2046 1 $line->{new_group} = $group;
2047 }
2048 1 ▶ $prop->{group} = $group if $prop;
Branch: 1 branch
BranchTrueFalseTotal
if $prop 1 0 1
2049 1 push @{$line->{after}}, _fold(_label_text($group, $new), $P->{eol});
2050 }
2051  
2052 14 ▶sub _patch_label {
Subroutine _patch_label: called CC=10 SCAR=23.4
2053 14 my ($P, $line, $prop, $old, $new) = @_;
2054 14 ▶ $_ = (defined $_ && !ref $_ && $_ ne '') ? $_ : undef for my ($o, $n) = ($old, $new);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && !ref($_) && $_ ne '' ? : 13 15 28
Condition66%4/6
defined $_ and not ref $_ and $_ ne ""
!ll&&!rl&&r
15013
defined $_ and not ref $_
!ll&&!rl&&r
15013
MC/DC33%1/3
defined $_ and not ref $_ and $_ ne ""
defined $_ not ref $_ $_ ne ""
Truth table50%2/4
A and not(B) and C
A= defined $_B= not ref $_C= $_ ne ""
ABCresult
0XX0
10X0
1100
1111
2055 14 ▶ return if !defined $o && !defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o or defined $n 7 7 14
Condition100%3/3
defined $o or defined $n
l!l&&r!l&&!r
617
MC/DC100%2/2
defined $o or defined $n
defined $o defined $n
Truth table100%3/3
A or B
A= defined $oB= defined $n
ABresult
1X1
011
000
2056 7 ▶ return if defined $o && defined $n && $o eq $n;
Branch: 1 branch
BranchTrueFalseTotal
if defined $o and defined $n and $o eq $n 6 1 7
Condition66%4/6
defined $o and defined $n and $o eq $n
!ll&&!rl&&r
106
defined $o and defined $n
!ll&&!rl&&r
106
MC/DC33%1/3
defined $o and defined $n and $o eq $n
defined $o defined $n $o eq $n
Truth table50%2/4
A and B and C
A= defined $oB= defined $nC= $o eq $n
ABCresult
0XX0
10X0
1100
1111
2057 1 _set_label($P, $line, $prop, $n);
2058 }
2059  
2060 # --- Patchers, one per top-level Card property ----------------------------
2061  
2062 62 ▶sub _same {
Subroutine _same: called CC=1 SCAR=0.0
2063 62 my ($x, $y) = @_;
2064 62 state $json = JSON->new->canonical->allow_nonref->allow_blessed->convert_blessed;
2065 62 return $json->encode([$x]) eq $json->encode([$y]);
2066 }
2067  
2068 # The maps whose entries the reader labels from an X-ABLabel
2069 19 my %LABELLED = map { $_ => 1 } qw(emails phones addresses onlineServices media
2070 links calendars schedulingAddresses cryptoKeys directories);
2071  
2072 19 my %MAP_WRITERS = (
2073 nicknames => \&_unconvert_nicknames,
2074 emails => \&_unconvert_emails,
2075 phones => \&_unconvert_phones,
2076 addresses => \&_unconvert_addresses,
2077 organizations => \&_unconvert_organizations,
2078 titles => \&_unconvert_titles,
2079 anniversaries => \&_unconvert_anniversaries,
2080 notes => \&_unconvert_notes,
2081 onlineServices => \&_unconvert_online_services,
2082 media => \&_unconvert_media,
2083 links => \&_unconvert_links,
2084 calendars => \&_unconvert_calendars,
2085 schedulingAddresses => \&_unconvert_scheduling_addresses,
2086 cryptoKeys => \&_unconvert_crypto_keys,
2087 directories => \&_unconvert_directories,
2088 preferredLanguages => \&_unconvert_preferred_languages,
2089 personalInfo => \&_unconvert_personal_info,
2090 );
2091  
2092 # Patch a map of the Card entry by entry: an unchanged entry keeps its
2093 # line; a changed one is written over its line; a removed one loses its
2094 # line; a new one is added after the lines of the same map. $gen makes
2095 # the vCard property of one entry (with no label: labels are patched
2096 # here); $hook may adjust it against the original line first.
2097 22 ▶sub _patch_map {
Subroutine _patch_map: called CC=21 SCAR=30.8
2098 22 my ($P, $key, $old, $new, $gen, $hook) = @_;
2099 22 ▶ $old = {} unless ref $old eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $old eq "HASH" 1 21 22
2100 22 ▶ $new = {} unless ref $new eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $new eq "HASH" 1 21 22
2101 22 ▶ my $sources = $P->{ctx}{sources}{$key} // {};
Condition100%2/2
$P->{"ctx"}{"sources"}{$key} // {}
l!l
211
MC/DC100%1/1
$P->{"ctx"}{"sources"}{$key} // {}
$P->{"ctx"}{"sources"}{$key}
Truth table100%2/2
A
A= $P->{"ctx"}{"sources"}{$key}
Aresult
11
00
2102 22 my $labelled = $LABELLED{$key};
2103  
2104 22 my @all = _lines_of_items($P, [ map { @$_ } values %$sources ]);
2105 22 my $anchor = $all[-1];
2106  
2107 22 my %placed;
2108 22 for my $id (sort keys %$old) {
2109 44 my @lines = _lines_of_items($P, $sources->{$id});
2110 44 ▶ next unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 44 44
2111 44 $placed{$id} = 1;
2112 44 my $entry = $new->{$id};
2113 44 ▶ if (ref $entry ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry ne "HASH") 3 41 44
2114 3 _delete_line($P, $_) for @lines;
2115 3 next;
2116 }
2117 41 ▶ next if _same($old->{$id}, $entry);
Branch: 1 branch
BranchTrueFalseTotal
if _same($old->{$id}, $entry) 26 15 41
2118 15 my $line = pop @lines;
2119 15 _delete_line($P, $_) for @lines;
2120 15 my ($prop) = $gen->($id, $entry);
2121 15 ▶ unless ($prop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prop) 0 15 15
2122 0 _delete_line($P, $line);
2123 0 next;
2124 }
2125 15 ▶ $hook->($P, $line, $prop, $old->{$id}, $entry) if $hook;
Branch: 1 branch
BranchTrueFalseTotal
if $hook 1 14 15
2126 15 _fit($P, $prop, $line);
2127 15 ▶ _patch_label($P, $line, $prop, $old->{$id}{label}, $entry->{label}) if $labelled;
Branch: 1 branch
BranchTrueFalseTotal
if $labelled 14 1 15
2128 15 _replace_line($P, $line, $prop);
2129 }
2130  
2131 22 for my $id (sort keys %$new) {
2132 45 ▶ next if $placed{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $placed{$id} 41 4 45
2133 4 my $entry = $new->{$id};
2134 4 ▶ next unless ref $entry eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 4 4
2135 4 ▶ my ($prop) = $gen->($id, $entry) or next;
Branch: 1 branch
BranchTrueFalseTotal
my($prop) = &$gen($id, $entry) or next 4 0 4
2136 4 _fit($P, $prop);
2137 4 ▶ my $label = $labelled ? $entry->{label} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$labelled ? : 4 0 4
2138 4 my @texts;
2139 4 ▶ if (defined $label && !ref $label && $label ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $label and not ref $label and $label ne '') { } 1 3 4
Condition66%4/6
defined $label and not ref $label and $label ne ""
!ll&&!rl&&r
301
defined $label and not ref $label
!ll&&!rl&&r
301
MC/DC33%1/3
defined $label and not ref $label and $label ne ""
defined $label not ref $label $label ne ""
Truth table50%2/4
A and not(B) and C
A= defined $labelB= not ref $labelC= $label ne ""
ABCresult
0XX0
10X0
1100
1111
2140 1 $prop->{group} = _alloc_group($P->{ctx});
2141 1 @texts = (_prop_line($prop), _label_text($prop->{group}, $label));
2142 }
2143 else {
2144 3 @texts = (_prop_line($prop));
2145 }
2146 4 _insert($P, $anchor, @texts);
2147 }
2148 }
2149  
2150 # One entry of a map as the writer makes it, without its label
2151 22 ▶sub _map_writer {
Subroutine _map_writer: called CC=1 SCAR=0.0
2152 22 my ($P, $key) = @_;
2153 22 my $ctx = $P->{ctx};
2154 19 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2155 19 my ($id, $entry) = @_;
2156 19 my @out;
2157 19 local $ctx->{no_labels} = 1;
2158 19 $MAP_WRITERS{$key}->(\@out, { $key => { $id => $entry } }, $ctx);
2159 19 return @out;
2160 22 };
2161 }
2162  
2163 # An Apple address keeps its country code, coordinates and time zone in
2164 # lines of its group (X-ABADR, GEO, TZ) rather than in parameters: those
2165 # lines are updated instead
2166 1 ▶sub _address_siblings {
Subroutine _address_siblings: called CC=14 SCAR=31.0
2167 1 my ($P, $line, $prop, $old, $new) = @_;
2168 1 ▶ my $group = $line->{group} // return;
Condition50%1/2
$line->{"group"} // (return)
l!l
10
MC/DC0%0/1
$line->{"group"} // (return)
$line->{"group"}
Truth table50%1/2
A
A= $line->{"group"}
Aresult
11
00
2169 1 ▶ my $item = $line->{item} // {};
Condition50%1/2
$line->{"item"} // {}
l!l
10
MC/DC0%0/1
$line->{"item"} // {}
$line->{"item"}
Truth table50%1/2
A
A= $line->{"item"}
Aresult
11
00
2170 1 for my $spec ([cc => 'x-abadr', 'countryCode'], [geo => 'geo', 'coordinates'],
2171 [tz => 'tz', 'timeZone']) {
2172 3 my ($param, $sib_name, $field) = @$spec;
2173 3 ▶ next if defined $item->{params}{$param};
Branch: 1 branch
BranchTrueFalseTotal
if defined $item->{"params"}{$param} 0 3 3
2174 3 ▶ my ($sib) = grep { $_->{name} && $_->{name} eq $sib_name } _group_lines($P, $group);
Condition66%2/3
$_->{'name'} && $_->{'name'} eq $sib_name
!ll&&!rl&&r
051
MC/DC50%1/2
$_->{'name'} && $_->{'name'} eq $sib_name
$_->{'name'} $_->{'name'} eq $sib_name
Truth table66%2/3
A && B
A= $_->{'name'}B= $_->{'name'} eq $sib_name
ABresult
0X0
100
111
2175 3 ▶ next unless $sib;
Branch: 1 branch
BranchTrueFalseTotal
unless $sib 2 1 3
2176 1 delete $prop->{params}{$param};
2177 1 ▶ next if _same($old->{$field}, $new->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if _same($old->{$field}, $new->{$field}) 0 1 1
2178 1 my $val = $new->{$field};
2179 1 ▶ if (!defined $val || ref $val || $val eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val or ref $val or $val eq "") 0 1 1
Condition33%2/6
not defined $val or ref $val or $val eq ""
l!l&&r!l&&!r
001
not defined $val or ref $val
l!l&&r!l&&!r
001
MC/DC0%0/3
not defined $val or ref $val or $val eq ""
not defined $val ref $val $val eq ""
Truth table25%1/4
not(A) or B or C
A= not defined $valB= ref $valC= $val eq ""
ABCresult
1XX1
01X1
0011
0000
2180 0 _delete_line($P, $sib);
2181 0 next;
2182 }
2183 1 ▶ my $pl = _parse_line($sib->{text}) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pl = _parse_line($sib->{"text"}) or next 1 0 1
2184 0 my $head = $pl->{name} . join('', map { $_->{raw} } @{$pl->{tokens}});
2185 1 ▶ my $text = $sib_name eq 'geo' && $val =~ /^[a-z][a-z0-9+.-]*:/i ? _uri_text($val) : _escape_text($val);
Branch: 1 branch
BranchTrueFalseTotal
$sib_name eq 'geo' && $val =~ /^[a-z][a-z0-9+.-]*:/i ? : 0 1 1
Condition33%1/3
$sib_name eq "geo" and $val =~ /^[a-z][a-z0-9+.-]*:/i
!ll&&!rl&&r
100
MC/DC0%0/2
$sib_name eq "geo" and $val =~ /^[a-z][a-z0-9+.-]*:/i
$sib_name eq "geo" $val =~ /^[a-z][a-z0-9+.-]*:/i
Truth table33%1/3
A and B
A= $sib_name eq "geo"B= $val =~ /^[a-z][a-z0-9+.-]*:/i
ABresult
0X0
100
111
2186 1 _set_line($P, $sib, "$head:$text");
2187 }
2188 }
2189  
2190 # One vCard property, by name: the first line is written over, or every
2191 # line deleted, or one added before END:VCARD. With $all, the other lines
2192 # of that name go too (the reader merges them all).
2193 9 ▶sub _patch_single {
Subroutine _patch_single: called CC=6 SCAR=19.1
2194 9 my ($P, $names, $prop, $all) = @_;
2195 9 my @lines = _lines_named($P, @$names);
2196 9 ▶ unless ($prop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prop) 0 9 9
2197 0 _delete_line($P, $_) for @lines;
2198 0 return;
2199 }
2200 9 my $first = shift @lines;
2201 9 _fit($P, $prop, $first);
2202 9 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) { } 8 1 9
2203 8 _replace_line($P, $first, $prop);
2204 0 ▶ if ($all) { _delete_line($P, $_) for @lines }
Branch: 1 branch
BranchTrueFalseTotal
if ($all) 0 8 8
2205 }
2206 else {
2207 1 _insert($P, undef, _prop_line($prop));
2208 }
2209 }
2210  
2211 8 ▶sub _patch_name {
Subroutine _patch_name: called CC=8 SCAR=23.9
2212 8 my ($P, $key, $old_card, $new_card) = @_;
2213 8 ▶ my $o = ref $old_card->{name} eq 'HASH' ? $old_card->{name} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $old_card->{'name'} eq 'HASH' ? : 8 0 8
2214 8 ▶ my $n = ref $new_card->{name} eq 'HASH' ? $new_card->{name} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $new_card->{'name'} eq 'HASH' ? : 8 0 8
2215  
2216 8 my @gen;
2217 8 _unconvert_name(\@gen, { name => $n }, $P->{ctx});
2218 8 my ($fn) = grep { $_->{name} eq 'fn' } @gen;
2219 8 my ($nn) = grep { $_->{name} eq 'n' } @gen;
2220  
2221 # FN is the full name, or made from the components when there is none
2222 _patch_single($P, ['fn'], $fn)
2223 if !_same($o->{full}, $n->{full})
2224 8 ▶ || (!defined $n->{full} && !_same($o->{components}, $n->{components}));
Branch: 1 branch
BranchTrueFalseTotal
unless _same($o->{"full"}, $n->{"full"}) and defined $n->{"full"} || _same($o->{"components"}, $n->{"components"}) 8 0 8
Condition16%1/6
_same($o->{"full"}, $n->{"full"}) and defined $n->{"full"} || _same($o->{"components"}, $n->{"components"})
!ll&&!rl&&r
800
defined $n->{"full"} or _same($o->{"components"}, $n->{"components"})
l!l&&r!l&&!r
000
MC/DC0%0/3
_same($o->{"full"}, $n->{"full"}) and defined $n->{"full"} || _same($o->{"components"}, $n->{"components"})
_same($o->{"full"}, $n->{"full"}) defined $n->{"full"} _same($o->{"components"}, $n->{"components"})
Truth table25%1/4
A and B or C
A= _same($o->{"full"}, $n->{"full"})B= defined $n->{"full"}C= _same($o->{"components"}, $n->{"components"})
ABCresult
0XX0
1000
11X1
1011
2225 _patch_single($P, ['n'], $nn)
2226 8 ▶ unless _same([$o->{components}, $o->{sortAs}], [$n->{components}, $n->{sortAs}]);
Branch: 1 branch
BranchTrueFalseTotal
unless _same([$o->{"components"}, $o->{"sortAs"}], [$n->{"components"}, $n->{"sortAs"}]) 0 8 8
2227  
2228 # TODO(registry): phoneticComponents are read from X-PHONETIC-*-NAME but
2229 # the writer has no form for them yet (_unconvert_name), so a change to
2230 # them is not patched; the X-PHONETIC-* lines are left as they are.
2231 8 ▶ _patch_single($P, [qw(x-phonetic-first-name x-phonetic-middle-name x-phonetic-last-name)], undef)
Branch: 1 branch
BranchTrueFalseTotal
unless %$n 0 8 8
2232 unless %$n;
2233 }
2234  
2235 0 ▶sub _patch_keywords {
Subroutine _patch_keywords: not called CC=1 SCAR=6.9
2236 0 my ($P, $key, $old_card, $new_card) = @_;
2237 0 my @gen;
2238 0 _unconvert_keywords(\@gen, $new_card, $P->{ctx});
2239 0 _patch_single($P, ['categories'], $gen[0], 1);
2240 }
2241  
2242 2 ▶sub _patch_members {
Subroutine _patch_members: called CC=12 SCAR=26.7
2243 2 my ($P, $key, $old_card, $new_card) = @_;
2244 2 ▶ my $o = ref $old_card->{members} eq 'HASH' ? $old_card->{members} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $old_card->{'members'} eq 'HASH' ? : 2 0 2
2245 2 ▶ my $n = ref $new_card->{members} eq 'HASH' ? $new_card->{members} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $new_card->{'members'} eq 'HASH' ? : 2 0 2
2246 2 ▶ my $sources = $P->{ctx}{sources}{members} // {};
Condition50%1/2
$P->{"ctx"}{"sources"}{"members"} // {}
l!l
20
MC/DC0%0/1
$P->{"ctx"}{"sources"}{"members"} // {}
$P->{"ctx"}{"sources"}{"members"}
Truth table50%1/2
A
A= $P->{"ctx"}{"sources"}{"members"}
Aresult
11
00
2247 2 ▶ my $apple = $P->{v3} || _lines_named($P, 'x-addressbookserver-member');
Condition33%1/3
$P->{'v3'} || _lines_named($P, 'x-addressbookserver-member')
l!l&&r!l&&!r
200
MC/DC0%0/2
$P->{'v3'} || _lines_named($P, 'x-addressbookserver-member')
$P->{'v3'} _lines_named($P, 'x-addressbookserver-member')
Truth table33%1/3
A || B
A= $P->{'v3'}B= _lines_named($P, 'x-addressbookserver-member')
ABresult
1X1
011
000
2248 2 my @all = _lines_named($P, qw(member x-addressbookserver-member));
2249 2 my $anchor = $all[-1];
2250  
2251 2 for my $m (sort keys %$o) {
2252 5 ▶ next if $n->{$m};
Branch: 1 branch
BranchTrueFalseTotal
if $n->{$m} 5 0 5
2253 0 _delete_line($P, $_) for _lines_of_items($P, $sources->{$m});
2254 }
2255 2 for my $m (sort keys %$n) {
2256 7 ▶ next if $o->{$m} || !$n->{$m};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{$m} or not $n->{$m} 5 2 7
Condition66%2/3
$o->{$m} or not $n->{$m}
l!l&&r!l&&!r
502
MC/DC50%1/2
$o->{$m} or not $n->{$m}
$o->{$m} not $n->{$m}
Truth table66%2/3
A or not(B)
A= $o->{$m}B= not $n->{$m}
ABresult
1X1
011
000
2257 2 my @gen;
2258 2 ▶ my $writer = $apple ? \&_unconvert_members_apple : \&_unconvert_members;
Branch: 1 branch
BranchTrueFalseTotal
$apple ? : 2 0 2
2259 2 $writer->(\@gen, { members => { $m => JSON::true } }, $P->{ctx});
2260 2 _insert($P, $anchor, map { _prop_line(_fit($P, $_)) } @gen);
2261 }
2262 }
2263  
2264 # The X-ABLabel that says a relation to Apple (the reader maps it back)
2265 19 my %RELATION_LABEL = (spouse => 'Spouse', parent => 'Parent', child => 'Child',
2266 friend => 'Friend');
2267  
2268 2 ▶sub _patch_related {
Subroutine _patch_related: called CC=18 SCAR=31.8
2269 2 my ($P, $key, $old_card, $new_card) = @_;
2270 2 ▶ my $o = ref $old_card->{relatedTo} eq 'HASH' ? $old_card->{relatedTo} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $old_card->{'relatedTo'} eq 'HASH' ? : 2 0 2
2271 2 ▶ my $n = ref $new_card->{relatedTo} eq 'HASH' ? $new_card->{relatedTo} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $new_card->{'relatedTo'} eq 'HASH' ? : 2 0 2
2272 2 ▶ my $sources = $P->{ctx}{sources}{relatedTo} // {};
Condition50%1/2
$P->{"ctx"}{"sources"}{"relatedTo"} // {}
l!l
20
MC/DC0%0/1
$P->{"ctx"}{"sources"}{"relatedTo"} // {}
$P->{"ctx"}{"sources"}{"relatedTo"}
Truth table50%1/2
A
A= $P->{"ctx"}{"sources"}{"relatedTo"}
Aresult
11
00
2273 2 my @all = _lines_named($P, qw(related x-abrelatednames));
2274 2 my $anchor = $all[-1];
2275 1 ▶ my $gen = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2276 1 my ($k, $rel) = @_;
2277 1 my @out;
2278 1 _unconvert_related(\@out, { relatedTo => { $k => $rel } }, $P->{ctx});
2279 1 return $out[0];
2280 2 };
2281  
2282 2 my %placed;
2283 2 for my $k (sort keys %$o) {
2284 4 my @lines = _lines_of_items($P, $sources->{$k});
2285 4 ▶ next unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 4 4
2286 4 $placed{$k} = 1;
2287 4 ▶ if (ref $n->{$k} ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $n->{$k} ne "HASH") 0 4 4
2288 0 _delete_line($P, $_) for @lines;
2289 0 next;
2290 }
2291 4 ▶ next if _same($o->{$k}, $n->{$k});
Branch: 1 branch
BranchTrueFalseTotal
if _same($o->{$k}, $n->{$k}) 3 1 4
2292 1 my $line = pop @lines;
2293 1 _delete_line($P, $_) for @lines;
2294 1 ▶ if ($line->{name} eq 'x-abrelatednames') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"name"} eq "x-abrelatednames") 1 0 1
2295 # Apple says the relation in the label
2296 1 ▶ my @rel = grep { $n->{$k}{relation}{$_} } sort keys %{$n->{$k}{relation} // {}};
Condition50%1/2
$n->{$k}{"relation"} // {}
l!l
10
MC/DC0%0/1
$n->{$k}{"relation"} // {}
$n->{$k}{"relation"}
Truth table50%1/2
A
A= $n->{$k}{"relation"}
Aresult
11
00
2297 0 ▶ if (!@rel) { _set_label($P, $line, undef, undef); next }
Branch: 1 branch
BranchTrueFalseTotal
unless (@rel) 0 1 1
2298 1 ▶ if (@rel == 1 && $RELATION_LABEL{$rel[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rel == 1 and $RELATION_LABEL{$rel[0]}) 1 0 1
Condition33%1/3
@rel == 1 and $RELATION_LABEL{$rel[0]}
!ll&&!rl&&r
001
MC/DC0%0/2
@rel == 1 and $RELATION_LABEL{$rel[0]}
@rel == 1 $RELATION_LABEL{$rel[0]}
Truth table33%1/3
A and B
A= @rel == 1B= $RELATION_LABEL{$rel[0]}
ABresult
0X0
100
111
2299 1 _set_label($P, $line, undef, "_\$!<$RELATION_LABEL{$rel[0]}>!\$_");
2300 1 next;
2301 }
2302 }
2303 0 _replace_line($P, $line, _fit($P, $gen->($k, $n->{$k}), $line));
2304 }
2305 2 for my $k (sort keys %$n) {
2306 5 ▶ next if $placed{$k} || ref $n->{$k} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $placed{$k} or ref $n->{$k} ne "HASH" 4 1 5
Condition66%2/3
$placed{$k} or ref $n->{$k} ne "HASH"
l!l&&r!l&&!r
401
MC/DC50%1/2
$placed{$k} or ref $n->{$k} ne "HASH"
$placed{$k} ref $n->{$k} ne "HASH"
Truth table66%2/3
A or B
A= $placed{$k}B= ref $n->{$k} ne "HASH"
ABresult
1X1
011
000
2307 1 _insert($P, $anchor, _prop_line(_fit($P, $gen->($k, $n->{$k}))));
2308 }
2309 }
2310  
2311 0 ▶sub _patch_speak_to_as {
Subroutine _patch_speak_to_as: not called CC=7 SCAR=40.3
2312 0 my ($P, $key, $old_card, $new_card) = @_;
2313 0 ▶ my $o = ref $old_card->{speakToAs} eq 'HASH' ? $old_card->{speakToAs} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $old_card->{'speakToAs'} eq 'HASH' ? : 0 0 0
2314 0 ▶ my $n = ref $new_card->{speakToAs} eq 'HASH' ? $new_card->{speakToAs} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $new_card->{'speakToAs'} eq 'HASH' ? : 0 0 0
2315 0 ▶ unless (_same($o->{grammaticalGender}, $n->{grammaticalGender})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_same($o->{"grammaticalGender"}, $n->{"grammaticalGender"})) 0 0 0
2316 0 my $gg = $n->{grammaticalGender};
2317 0 ▶ _patch_single($P, ['gramgender'],
Branch: 1 branch
BranchTrueFalseTotal
defined $gg && !ref($gg) && $gg ne '' ? : 0 0 0
Condition0%0/6
defined $gg and not ref $gg and $gg ne ""
!ll&&!rl&&r
000
defined $gg and not ref $gg
!ll&&!rl&&r
000
MC/DC0%0/3
defined $gg and not ref $gg and $gg ne ""
defined $gg not ref $gg $gg ne ""
Truth table0%0/4
A and not(B) and C
A= defined $ggB= not ref $ggC= $gg ne ""
ABCresult
0XX0
10X0
1100
1111
2318 defined $gg && !ref $gg && $gg ne '' ? { name => 'gramgender', value => $gg, params => {} } : undef);
2319 }
2320 0 my $ctx = $P->{ctx};
2321 0 ▶ _patch_map($P, 'speakToAs', $o->{pronouns}, $n->{pronouns}, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2322 0 my ($id, $entry) = @_;
2323 0 my @out;
2324 0 _unconvert_speak_to_as(\@out, { speakToAs => { pronouns => { $id => $entry } } }, $ctx);
2325 0 return @out;
2326 0 });
2327 }
2328  
2329 95 ▶sub _scalar_patcher {
Subroutine _scalar_patcher: called CC=1 SCAR=0.6
2330 95 my ($vname) = @_;
2331 1 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=17.1
2332 1 my ($P, $key, $old_card, $new_card) = @_;
2333 1 my $v = $new_card->{$key};
2334 1 ▶ _patch_single($P, [$vname],
Branch: 1 branch
BranchTrueFalseTotal
defined $v && !ref($v) && $v ne '' ? : 1 0 1
Condition33%2/6
defined $v and not ref $v and $v ne ""
!ll&&!rl&&r
001
defined $v and not ref $v
!ll&&!rl&&r
001
MC/DC0%0/3
defined $v and not ref $v and $v ne ""
defined $v not ref $v $v ne ""
Truth table25%1/4
A and not(B) and C
A= defined $vB= not ref $vC= $v ne ""
ABCresult
0XX0
10X0
1100
1111
2335 defined $v && !ref $v && $v ne '' ? { name => $vname, value => $v, params => {} } : undef);
2336 95 };
2337 }
2338  
2339 0 ▶sub _patch_kind {
Subroutine _patch_kind: not called CC=11 SCAR=48.8
2340 0 my ($P, $key, $old_card, $new_card) = @_;
2341 0 my $v = $new_card->{kind};
2342 0 my @names = qw(kind x-addressbookserver-kind);
2343 0 my ($line) = _lines_named($P, @names);
2344 0 ▶ $v = undef if defined $v && (ref $v || $v eq '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and ref $v || $v eq "" 0 0 0
Condition0%0/6
defined $v and ref $v || $v eq ""
!ll&&!rl&&r
000
ref $v or $v eq ""
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $v and ref $v || $v eq ""
defined $v ref $v $v eq ""
Truth table0%0/4
A and B or C
A= defined $vB= ref $vC= $v eq ""
ABCresult
0XX0
1000
11X1
1011
2345 # Apple writes no kind for an individual
2346 0 ▶ $v = undef if defined $v && $v eq 'individual' && ($P->{v3} || ($line && $line->{name} ne 'kind'));
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and $v eq "individual" and $P->{"v3"} || $line && $line->{"name"} ne "kind" 0 0 0
Condition0%0/12
defined $v and $v eq "individual" and $P->{"v3"} || $line && $line->{"name"} ne "kind"
!ll&&!rl&&r
000
defined $v and $v eq "individual"
!ll&&!rl&&r
000
$P->{"v3"} or $line and $line->{"name"} ne "kind"
l!l&&r!l&&!r
000
$line and $line->{"name"} ne "kind"
!ll&&!rl&&r
000
MC/DC0%0/5
defined $v and $v eq "individual" and $P->{"v3"} || $line && $line->{"name"} ne "kind"
defined $v $v eq "individual" $P->{"v3"} $line $line->{"name"} ne "kind"
Truth table0%0/6
A and B and C or D and E
A= defined $vB= $v eq "individual"C= $P->{"v3"}D= $lineE= $line->{"name"} ne "kind"
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
2347 0 ▶ my $name = $line ? $line->{name} : 'kind';
Branch: 1 branch
BranchTrueFalseTotal
$line ? : 0 0 0
2348 0 ▶ _patch_single($P, \@names, defined $v ? { name => $name, value => $v, params => {} } : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
2349 }
2350  
2351 %PATCHERS = (
2352 name => \&_patch_name,
2353 keywords => \&_patch_keywords,
2354 members => \&_patch_members,
2355 relatedTo => \&_patch_related,
2356 speakToAs => \&_patch_speak_to_as,
2357 kind => \&_patch_kind,
2358 uid => _scalar_patcher('uid'),
2359 prodId => _scalar_patcher('prodid'),
2360 updated => _scalar_patcher('rev'),
2361 created => _scalar_patcher('created'),
2362 language => _scalar_patcher('language'),
2363 (map {
2364 19 my $key = $_;
2365 22 ▶ ($key => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2366 22 my ($P, $k, $old_card, $new_card) = @_;
2367 22 ▶ _patch_map($P, $key, $old_card->{$key}, $new_card->{$key}, _map_writer($P, $key),
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'addresses' ? : 2 20 22
2368 $key eq 'addresses' ? \&_address_siblings : undef);
2369 323 });
2370 } keys %MAP_WRITERS),
2371 );
2372  
2373 ###############################################################################
2374 # WRITING: content lines, byte for byte the same for the same input
2375 ###############################################################################
2376  
2377 # Text::VCardFast's hash2vcard writes the parameters of a property in
2378 # Perl's hash order, so the same Card gave different bytes on every run
2379 # (and a CardDAV server a new ETag for every unchanged save). The writer
2380 # here keeps its property order but orders everything it writes.
2381  
2382 # properties in this order first (Text::VCardFast's), then by name
2383 19 my @PROP_ORDER = qw(version fn n nickname lang gender org title role bday
2384 anniversary email tel adr url impp);
2385 19 my %PROP_ORDER = map { $PROP_ORDER[$_] => $_ + 1 } 0..$#PROP_ORDER;
2386  
2387 # parameters in this order first, then by name
2388 19 my @PARAM_ORDER = qw(prop-id value type pref);
2389 19 my %PARAM_ORDER = map { $PARAM_ORDER[$_] => $_ + 1 } 0..$#PARAM_ORDER;
2390  
2391 # A whole vCard from a list of properties; items of one group are written
2392 # together, after the first of them
2393 360 ▶sub _write_vcard {
Subroutine _write_vcard: called CC=14 SCAR=26.5
2394 360 my ($props, $eol) = @_;
2395 360 ▶ $eol //= "\n";
Condition50%1/2
$eol //= "\n"
l!l
0360
MC/DC0%0/1
$eol //= "\n"
$eol
Truth table50%1/2
A
A= $eol
Aresult
11
00
2396  
2397 360 my %by_name;
2398 360 push @{$by_name{lc $_->{name}}}, $_ for @$props;
2399 my @names = sort {
2400 360 ▶ ($PROP_ORDER{$a} // 1000) <=> ($PROP_ORDER{$b} // 1000) || $a cmp $b
Condition85%6/7
($PROP_ORDER{$a} // 1000) <=> ($PROP_ORDER{$b} // 1000) || $a cmp $b
l!l&&r!l&&!r
1073857220
$PROP_ORDER{$a} // 1000
l!l
91027358
$PROP_ORDER{$b} // 1000
l!l
83328128
MC/DC0%0/2
($PROP_ORDER{$a} // 1000) <=> ($PROP_ORDER{$b} // 1000) || $a cmp $b
$PROP_ORDER{$a} $a cmp $b
MC/DC100%1/1
$PROP_ORDER{$b} // 1000
$PROP_ORDER{$b}
Truth table66%2/3
A || B
A= $PROP_ORDER{$a}B= $a cmp $b
ABresult
1X1
011
000
Truth table100%2/2
A
A= $PROP_ORDER{$b}
Aresult
11
00
2401 } keys %by_name;
2402  
2403 360 my %groups;
2404 360 for my $prop (@$props) {
2405 6763 ▶ push @{$groups{lc $prop->{group}}}, $prop if defined $prop->{group} && $prop->{group} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $prop->{"group"} and $prop->{"group"} ne "" 332 6431 6763
Condition66%2/3
defined $prop->{"group"} and $prop->{"group"} ne ""
!ll&&!rl&&r
64310332
MC/DC50%1/2
defined $prop->{"group"} and $prop->{"group"} ne ""
defined $prop->{"group"} $prop->{"group"} ne ""
Truth table66%2/3
A and B
A= defined $prop->{"group"}B= $prop->{"group"} ne ""
ABresult
0X0
100
111
2406 }
2407  
2408 360 my (%done, @out);
2409 360 for my $name (@names) {
2410 5264 for my $prop (@{$by_name{$name}}) {
2411 6763 ▶ next if $done{$prop}++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$prop}++ 166 6597 6763
2412 6597 push @out, $prop;
2413 6597 ▶ if (defined $prop->{group} && $prop->{group} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prop->{"group"} and $prop->{"group"} ne "") 166 6431 6597
Condition66%2/3
defined $prop->{"group"} and $prop->{"group"} ne ""
!ll&&!rl&&r
64310166
MC/DC50%1/2
defined $prop->{"group"} and $prop->{"group"} ne ""
defined $prop->{"group"} $prop->{"group"} ne ""
Truth table66%2/3
A and B
A= defined $prop->{"group"}B= $prop->{"group"} ne ""
ABresult
0X0
100
111
2414 166 push @out, grep { !$done{$_}++ } @{$groups{lc $prop->{group}}};
2415 }
2416 }
2417 }
2418  
2419 360 return join '', "BEGIN:VCARD$eol", (map { _fold(_prop_line($_), $eol) } @out), "END:VCARD$eol";
2420 }
2421  
2422 # One property as an unfolded content line, cleaned of control characters
2423 6769 ▶sub _prop_line {
Subroutine _prop_line: called CC=3 SCAR=11.0
2424 6769 my ($prop) = @_;
2425 6769 my $line = '';
2426 6769 ▶ $line .= "$prop->{group}." if defined $prop->{group} && $prop->{group} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $prop->{"group"} and $prop->{"group"} ne "" 333 6436 6769
Condition66%2/3
defined $prop->{"group"} and $prop->{"group"} ne ""
!ll&&!rl&&r
64360333
MC/DC50%1/2
defined $prop->{"group"} and $prop->{"group"} ne ""
defined $prop->{"group"} $prop->{"group"} ne ""
Truth table66%2/3
A and B
A= defined $prop->{"group"}B= $prop->{"group"} ne ""
ABresult
0X0
100
111
2427 6769 $line .= uc $prop->{name};
2428 6769 $line .= _params_text($prop->{params});
2429 6769 return $line . ':' . _value_text($prop);
2430 }
2431  
2432 6769 ▶sub _params_text {
Subroutine _params_text: called CC=7 SCAR=19.6
2433 6769 my ($params) = @_;
2434 6769 ▶ return '' unless $params && %$params;
Branch: 1 branch
BranchTrueFalseTotal
unless $params and %$params 2272 4497 6769
Condition100%3/3
$params and %$params
!ll&&!rl&&r
118110914497
MC/DC100%2/2
$params and %$params
$params %$params
Truth table100%3/3
A and B
A= $paramsB= %$params
ABresult
0X0
100
111
2435 4497 my $text = '';
2436 4497 for my $name (_param_order($params)) {
2437 8462 my $vals = $params->{$name};
2438 8462 ▶ for my $val (ref $vals eq 'ARRAY' ? @$vals : ($vals)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $vals eq 'ARRAY' ? : 8462 0 8462
2439 8831 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 8831 8831
2440 8831 $text .= ';' . uc($name) . '=' . _param_value($val);
2441 }
2442 }
2443 4497 return $text;
2444 }
2445  
2446 4520 ▶sub _param_order {
Subroutine _param_order: called CC=4 SCAR=13.9
2447 4520 my ($params) = @_;
2448 return sort {
2449 4520 ▶ ($PARAM_ORDER{$a} // 1000) <=> ($PARAM_ORDER{$b} // 1000) || $a cmp $b
Condition85%6/7
($PARAM_ORDER{$a} // 1000) <=> ($PARAM_ORDER{$b} // 1000) || $a cmp $b
l!l&&r!l&&!r
44965620
$PARAM_ORDER{$a} // 1000
l!l
38451213
$PARAM_ORDER{$b} // 1000
l!l
36071451
MC/DC0%0/2
($PARAM_ORDER{$a} // 1000) <=> ($PARAM_ORDER{$b} // 1000) || $a cmp $b
$PARAM_ORDER{$a} $a cmp $b
MC/DC100%1/1
$PARAM_ORDER{$b} // 1000
$PARAM_ORDER{$b}
Truth table66%2/3
A || B
A= $PARAM_ORDER{$a}B= $a cmp $b
ABresult
1X1
011
000
Truth table100%2/2
A
A= $PARAM_ORDER{$b}
Aresult
11
00
2450 } keys %$params;
2451 }
2452  
2453 # RFC 6868 caret encoding; quoted when it holds a separator or space
2454 8833 ▶sub _param_value {
Subroutine _param_value: called CC=4 SCAR=14.5
2455 8833 my ($val) = @_;
2456 8833 $val = _clean_vcard_text($val);
2457 8833 ▶ $val =~ s/([\n^"])/$1 eq "\n" ? '^n' : $1 eq '^' ? '^^' : "^'"/ge;
Branch: 2 branches
BranchTrueFalseTotal
$1 eq "\n" ? : 41 0 41
$1 eq '^' ? : 0 0 0
2458 8833 ▶ $val = qq{"$val"} if $val =~ /[;:,\s]/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /[;:,\s]/ 212 8621 8833
2459 8833 return $val;
2460 }
2461  
2462 # RFC 6350 S3.4: TEXT escapes BACKSLASH, COMMA, SEMICOLON and NEWLINE
2463 7669 ▶sub _escape_text {
Subroutine _escape_text: called CC=3 SCAR=11.0
2464 7669 my ($val) = @_;
2465 7669 ▶ die "Text::JSContact: a vCard value must be a string, not $val\n" if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 1 7668 7669
2466 7668 ▶ $val = _clean_vcard_text($val // '');
Condition50%1/2
$val // ""
l!l
76680
MC/DC0%0/1
$val // ""
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
2467 7668 $val =~ s/([\\,;])/\\$1/g;
2468 7668 $val =~ s/\n/\\n/g;
2469 7668 return $val;
2470 }
2471  
2472 # A structured value is a list of components, each a string or a list
2473 # of strings (written comma-separated)
2474 6792 ▶sub _value_text {
Subroutine _value_text: called CC=4 SCAR=13.9
2475 6792 my ($prop) = @_;
2476 6792 ▶ if (my $values = $prop->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $prop->{"values"}) 834 5958 6792
2477 return join ';', map {
2478 834 ▶ ref $_ eq 'ARRAY' ? join(',', map { _escape_text($_) } @$_) : _escape_text($_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 2266 950 3216
2479 } @$values;
2480 }
2481 5958 ▶ return _uri_text($prop->{value}) if _is_uri_value($prop);
Branch: 1 branch
BranchTrueFalseTotal
if _is_uri_value($prop) 1904 4054 5958
2482 4054 return _escape_text($prop->{value});
2483 }
2484  
2485 # Fold a content line at 75 octets (RFC 6350 S3.2), never inside a
2486 # character or between a backslash and the character it escapes
2487 6794 ▶sub _fold {
Subroutine _fold: called CC=13 SCAR=25.7
2488 6794 my ($line, $eol) = @_;
2489 6794 my @out;
2490 6794 ▶ if ($line !~ /[^\x00-\x7f]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ /[^\x00-\x7f]/) { } 6518 276 6794
2491 # one character, one octet
2492 6518 my $pos = 0;
2493 6518 ▶ while ($pos < length $line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $line) 7163 6518 13681
2494 7163 ▶ my $max = @out ? 74 : 75;
Branch: 1 branch
BranchTrueFalseTotal
@out ? : 645 6518 7163
2495 7163 my $chunk = substr($line, $pos, $max);
2496 # an odd run of backslashes at the end splits an escape
2497 7163 ▶ chop $chunk if length $chunk == $max && $pos + $max < length $line
Branch: 1 branch
BranchTrueFalseTotal
if length $chunk == $max and $pos + $max < length $line and $chunk =~ /(?<!\\)(?:\\\\)*\\\z/ 0 7163 7163
Condition83%5/6
length $chunk == $max and $pos + $max < length $line and $chunk =~ /(?<!\\)(?:\\\\)*\\\z/
!ll&&!rl&&r
65186450
length $chunk == $max and $pos + $max < length $line
!ll&&!rl&&r
642197645
MC/DC0%0/3
length $chunk == $max and $pos + $max < length $line and $chunk =~ /(?<!\\)(?:\\\\)*\\\z/
length $chunk == $max $pos + $max < length $line $chunk =~ /(?<!\\)(?:\\\\)*\\\z/
Truth table75%3/4
A and B and C
A= length $chunk == $maxB= $pos + $max < length $lineC= $chunk =~ /(?<!\\)(?:\\\\)*\\\z/
ABCresult
0XX0
10X0
1100
1111
2498 && $chunk =~ /(?<!\\)(?:\\\\)*\\\z/;
2499 7163 push @out, $chunk;
2500 7163 $pos += length $chunk;
2501 }
2502 }
2503 else {
2504 276 my $cur = '';
2505 276 my $len = 0;
2506 276 for my $ch ($line =~ /(\\.|.)/sg) {
2507 10280 ▶ my $l = $ch =~ /[^\x00-\x7f]/ ? length(encode_utf8($ch)) : length $ch;
Branch: 1 branch
BranchTrueFalseTotal
$ch =~ /[^\x00-\x7f]/ ? : 719 9561 10280
2508 10280 ▶ my $max = @out ? 74 : 75;
Branch: 1 branch
BranchTrueFalseTotal
@out ? : 364 9916 10280
2509 10280 ▶ if ($len + $l > $max && $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len + $l > $max and $len) 33 10247 10280
Condition66%2/3
$len + $l > $max and $len
!ll&&!rl&&r
10247033
MC/DC50%1/2
$len + $l > $max and $len
$len + $l > $max $len
Truth table66%2/3
A and B
A= $len + $l > $maxB= $len
ABresult
0X0
100
111
2510 33 push @out, $cur;
2511 33 $cur = '';
2512 33 $len = 0;
2513 }
2514 10280 $cur .= $ch;
2515 10280 $len += $l;
2516 }
2517 276 push @out, $cur;
2518 }
2519 6794 ▶ push @out, '' unless @out;
Branch: 1 branch
BranchTrueFalseTotal
unless @out 0 6794 6794
2520 6794 return join("$eol ", @out) . $eol;
2521 }
2522  
2523 # RFC 6350 S3.4 escapes COMMA, SEMICOLON, BACKSLASH and NEWLINE in TEXT
2524 # values only; a URI value is written as it is. Escaping every value
2525 # turns "data:image/png;base64,..." into "data:image/png\;base64\,...",
2526 # no longer a URI (Cyrus then stores it as TEXT with a nonsense media
2527 # type). These are the properties whose default value type is URI
2528 # (RFC 6350 S6, RFC 6715, RFC 8605, RFC 9554), plus Apple's member
2529 # property.
2530 19 my %URI_DEFAULT = map { $_ => 1 } qw(
2531 source photo impp geo logo member sound uid url key fburl caladruri
2532 caluri related org-directory contact-uri socialprofile
2533 x-addressbookserver-member
2534 );
2535  
2536 5958 ▶sub _is_uri_value {
Subroutine _is_uri_value: called CC=11 SCAR=24.5
2537 5958 my ($prop) = @_;
2538 5958 ▶ return 0 if exists $prop->{values};
Branch: 1 branch
BranchTrueFalseTotal
if exists $prop->{"values"} 0 5958 5958
2539 5958 my $val = $prop->{value};
2540 5958 ▶ return 0 if !defined $val || ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $val or ref $val 1 5957 5958
Condition66%2/3
not defined $val or ref $val
l!l&&r!l&&!r
015957
MC/DC50%1/2
not defined $val or ref $val
not defined $val ref $val
Truth table66%2/3
not(A) or B
A= not defined $valB= ref $val
ABresult
1X1
011
000
2541 # only something that is a URI: a bare IMPP username, a vCard 3 GEO
2542 # or a non-URI UID stays TEXT
2543 5957 ▶ return 0 unless $val =~ /^[a-z][a-z0-9+.-]*:/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^[a-z][a-z0-9+.-]*:/i 4053 1904 5957
2544 1904 ▶ my $params = $prop->{params} // {};
Condition100%2/2
$prop->{"params"} // {}
l!l
1722182
MC/DC100%1/1
$prop->{"params"} // {}
$prop->{"params"}
Truth table100%2/2
A
A= $prop->{"params"}
Aresult
11
00
2545 1904 ▶ return 0 if $params->{encoding};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"encoding"} 0 1904 1904
2546 1904 ▶ my @vt = map { lc } grep { defined } map { ref $_ ? @$_ : $_ } $params->{value} // ();
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 197 0 197
Condition66%2/3
$params->{"value"} // ()
l!l&&r!l&&!r
19717070
MC/DC0%0/2
$params->{"value"} // ()
$params->{"value"} ()
Truth table66%2/3
A // B
A= $params->{"value"}B= ()
ABresult
1X1
011
000
2547 1904 ▶ return $vt[0] eq 'uri' if @vt;
Branch: 1 branch
BranchTrueFalseTotal
if @vt 197 1707 1904
2548 1707 ▶ return $URI_DEFAULT{lc $prop->{name}} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$URI_DEFAULT{lc $prop->{'name'}} ? : 1707 0 1707
2549 }
2550  
2551 # A value that is a URI: a scheme, a colon, and no white space
2552 738 ▶sub _looks_like_uri {
Subroutine _looks_like_uri: called CC=3 SCAR=12.5
2553 738 my ($val) = @_;
2554 738 ▶ return defined $val && !ref $val && $val =~ /^[a-z][a-z0-9+.-]*:\S+\z/i;
Condition50%3/6
defined $val && !ref($val) && $val =~ /^[a-z][a-z0-9+.-]*:\S+\z/i
!ll&&!rl&&r
0436302
defined $val and not ref $val
!ll&&!rl&&r
00738
MC/DC33%1/3
defined $val && !ref($val) && $val =~ /^[a-z][a-z0-9+.-]*:\S+\z/i
defined $val not ref $val $val =~ /^[a-z][a-z0-9+.-]*:\S+\z/i
Truth table50%2/4
A and not(B) && C
A= defined $valB= not ref $valC= $val =~ /^[a-z][a-z0-9+.-]*:\S+\z/i
ABCresult
0XX0
10X0
1100
1111
2555 }
2556  
2557 1904 ▶sub _uri_text {
Subroutine _uri_text: called CC=1 SCAR=0.0
2558 1904 my ($val) = @_;
2559 1904 $val = _clean_vcard_text($val);
2560 # A URI has neither, and written raw a LF would end the content line,
2561 # so percent-encode them; it also means a reader's TEXT unescaping
2562 # (Text::VCardFast's applies to every value) leaves the URI alone.
2563 1904 $val =~ s/\\/%5C/g;
2564 1904 $val =~ s/\n/%0A/g;
2565 1904 return $val;
2566 }
2567  
2568 # A CR from the card data could end the content line early and forge
2569 # extra properties. vCard allows no control characters but HTAB, so turn
2570 # CR into LF (escaped as \n, or ^n in a parameter) and drop the rest.
2571 18422 ▶sub _clean_vcard_text {
Subroutine _clean_vcard_text: called CC=4 SCAR=15.5
2572 18422 my ($val) = @_;
2573 0 ▶ return [ map { _clean_vcard_text($_) } @$val ] if ref $val eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "ARRAY" 0 18422 18422
2574 18422 ▶ return $val if !defined $val || ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $val or ref $val 0 18422 18422
Condition33%1/3
not defined $val or ref $val
l!l&&r!l&&!r
0018422
MC/DC0%0/2
not defined $val or ref $val
not defined $val ref $val
Truth table33%1/3
not(A) or B
A= not defined $valB= ref $val
ABresult
1X1
011
000
2575 18422 $val =~ s/\r\n?/\n/g;
2576 18422 $val =~ s/[\x00-\x08\x0b-\x1f\x7f]//g;
2577 18422 return $val;
2578 }
2579  
2580 # Compare two JSContact cards and return hash of changed property names
2581 72 ▶sub _diff_cards {
Subroutine _diff_cards: called CC=9 SCAR=22.0
2582 72 my ($old, $new) = @_;
2583 72 my $json = JSON->new->canonical->utf8;
2584  
2585 72 my %changed;
2586  
2587 # Check all keys in both old and new
2588 my %all_keys;
2589 72 $all_keys{$_} = 1 for keys %$old, keys %$new;
2590  
2591 # Skip internal/metadata keys
2592 72 delete $all_keys{$_} for qw(@type version CPath href _raw meta);
2593  
2594 72 for my $key (keys %all_keys) {
2595 858 my $old_val = $old->{$key};
2596 858 my $new_val = $new->{$key};
2597  
2598 # Both undef/missing -> no change
2599 858 ▶ next if !defined $old_val && !defined $new_val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old_val or defined $new_val 0 858 858
Condition66%2/3
defined $old_val or defined $new_val
l!l&&r!l&&!r
85530
MC/DC0%0/2
defined $old_val or defined $new_val
defined $old_val defined $new_val
Truth table66%2/3
A or B
A= defined $old_valB= defined $new_val
ABresult
1X1
011
000
2600  
2601 # One exists, other doesn't -> changed
2602 858 ▶ if (!defined $old_val || !defined $new_val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $old_val and defined $new_val) 4 854 858
Condition100%3/3
defined $old_val and defined $new_val
!ll&&!rl&&r
31854
MC/DC100%2/2
defined $old_val and defined $new_val
defined $old_val defined $new_val
Truth table100%3/3
A and B
A= defined $old_valB= defined $new_val
ABresult
0X0
100
111
2603 4 $changed{$key} = 1;
2604 4 next;
2605 }
2606  
2607 # Compare via canonical JSON encoding
2608 854 my $old_json = $json->encode([$old_val]);
2609 854 my $new_json = $json->encode([$new_val]);
2610 854 ▶ $changed{$key} = 1 if $old_json ne $new_json;
Branch: 1 branch
BranchTrueFalseTotal
if $old_json ne $new_json 32 822 854
2611 }
2612  
2613 72 return %changed;
2614 }
2615  
2616 19 1;
2617  
2618 __END__
↑ Summary