User/Identity.pm

filter: Statementstmt 91.3% 95 / 104 Branchbran 66.1% 41 / 62 Conditioncond 46.6% 7 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 95.8% 23 / 24 Podpod 94.7% 18 / 19 totaltotal 79.7% 185 / 232 CC 39 SCAR 15.0
CC
39
Cov
79%
birth
7.5
fullName
7.3
courtesy
6.1
CRAP 4.5
« User/Identity/Item.pm ↑ Summary User/Identity/Archive.pm »
1 # This code is part of Perl distribution User-Identity version 4.00.
2 # The POD got stripped from this file by OODoc version 3.05.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2003-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package User::Identity;{
13 3 our $VERSION = '4.00';
14 }
15  
16 3 ▶use parent 'User::Identity::Item';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use Log::Report 'user-identity';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 #--------------------
24  
25 3 ▶use overload '""' => 'fullName';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #--------------------
28  
29 3 my @attributes = qw/charset courtesy birth full_name formal_name firstname
30 gender initials language nickname prefix surname titles/;
31  
32 sub init($)
33 7 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=3 SCAR=11.0
Pod undocumented
34  
35 exists $args->{$_} && ($self->{'UI_'.$_} = delete $args->{$_})
36 7 ▶ for @attributes;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{$_} and $$self{'UI_' . $_} = delete $args->{$_} 29 62 91
37  
38 7 $self->SUPER::init($args);
39 }
40  
41 sub type() { 'user' }
42 4 ▶sub user() { $_[0] }
Subroutine user: called CC=1 SCAR=0.0
Pod documented
43  
44 #--------------------
45  
46 1 ▶sub charset() { $_[0]->{UI_charset} || $ENV{LC_CTYPE} }
Subroutine charset: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"UI_charset"} or $ENV{"LC_CTYPE"} 1 0 1
47  
48  
49 sub nickname()
50 5 ▶{ my $self = shift;
Subroutine nickname: called CC=2 SCAR=7.2
Pod documented
51 5 ▶ $self->{UI_nickname} || $self->name;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"UI_nickname"} or $self->name 0 5 5
52 # TBI: If OS-specific info exists, then username
53 }
54  
55  
56 sub firstname()
57 9 ▶{ my $self = shift;
Subroutine firstname: called CC=2 SCAR=6.9
Pod documented
58 9 ▶ $self->{UI_firstname} || ucfirst $self->nickname;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"UI_firstname"} or ucfirst $self->nickname 7 2 9
59 }
60  
61  
62 sub initials()
63 6 ▶{ my $self = shift;
Subroutine initials: called CC=5 SCAR=16.1
Pod documented
64 6 ▶ return $self->{UI_initials} if defined $self->{UI_initials};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"UI_initials"} 2 4 6
65  
66 4 ▶ if(my $firstname = $self->firstname)
Branch: 1 branch
BranchTrueFalseTotal
if (my $firstname = $self->firstname) 4 0 4
67 4 { my $i = '';
68 4 ▶ while( $firstname =~ m/(\w+)(\-)?/g )
Branch: 1 branch
BranchTrueFalseTotal
if ($firstname =~ /(\w+)(\-)?/g) 12 4 16
69 12 { my ($part, $connect) = ($1,$2);
70 12 ▶ $connect ||= '.';
Condition100%2/2
$connect ||= "."
l!l
210
MC/DC100%1/1
$connect ||= "."
$connect
Truth table100%2/2
A
A= $connect
Aresult
11
00
71 12 $part =~ m/^(chr|th|\w)/i;
72 12 $i .= ucfirst(lc $1).$connect;
73 }
74 4 return $i;
75 }
76 }
77  
78  
79 1 ▶sub prefix() { $_[0]->{UI_prefix} }
Subroutine prefix: called CC=1 SCAR=0.0
Pod documented
80  
81  
82 1 ▶sub surname() { $_[0]->{UI_surname} }
Subroutine surname: called CC=1 SCAR=0.0
Pod documented
83  
84  
85 sub fullName()
86 10 ▶{ my $self = shift;
Subroutine fullName: called CC=7 SCAR=19.9
Pod documented
87 10 ▶ return $self->{UI_full_name} if defined $self->{UI_full_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"UI_full_name"} 0 10 10
88  
89 10 my ($first, $prefix, $surname) = @$self{ qw/UI_firstname UI_prefix UI_surname/};
90  
91 10 ▶ $surname //= ucfirst $self->nickname if defined $first;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first 8 2 10
Condition50%1/2
$surname //= ucfirst $self->nickname
l!l
80
MC/DC0%0/1
$surname //= ucfirst $self->nickname
$surname
Truth table50%1/2
A
A= $surname
Aresult
11
00
92 10 ▶ $first //= $self->firstname if defined $surname;
Branch: 1 branch
BranchTrueFalseTotal
if defined $surname 8 2 10
Condition33%1/3
$first //= $self->firstname
l!l&&r!l&&!r
800
MC/DC0%0/2
$first //= $self->firstname
$first $self->firstname
Truth table33%1/3
A //= B
A= $firstB= $self->firstname
ABresult
1X1
011
000
93  
94 10 my $full = join ' ', grep defined, ($first, $prefix, $surname);
95 10 ▶ $full = $self->firstname unless length $full;
Branch: 1 branch
BranchTrueFalseTotal
unless length $full 2 8 10
96  
97 # TBI: if OS-specific knowledge, then unix GCOS?
98  
99 10 $full;
100 }
101  
102  
103 sub formalName()
104 3 ▶{ my $self = shift;
Subroutine formalName: called CC=3 SCAR=11.2
Pod documented
105 3 ▶ return $self->{UI_formal_name} if defined $self->{UI_formal_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"UI_formal_name"} 0 3 3
106  
107 3 my $initials = $self->initials;
108  
109 3 my $firstname = $self->{UI_firstname};
110 3 ▶ $firstname = "($firstname)" if defined $firstname;
Branch: 1 branch
BranchTrueFalseTotal
if defined $firstname 3 0 3
111  
112 join ' ', grep defined,
113 3 $self->courtesy, $initials, @$self{ qw/UI_prefix UI_surname UI_titles/ };
114 }
115  
116  
117 3 my %male_courtesy= (
118 mister => 'en',
119 mr => 'en',
120 sir => 'en',
121 'de heer' => 'nl',
122 mijnheer => 'nl',
123 dhr => 'nl',
124 herr => 'de',
125 );
126  
127 3 my %male_courtesy_default = (
128 en => 'Mr.',
129 nl => 'De heer',
130 de => 'Herr',
131 );
132  
133 3 my %female_courtesy = (
134 miss => 'en',
135 ms => 'en',
136 mrs => 'en',
137 madam => 'en',
138 mevr => 'nl',
139 mevrouw => 'nl',
140 frau => 'de',
141 );
142  
143 3 my %female_courtesy_default = (
144 en => 'Madam',
145 nl => 'Mevrouw',
146 de => 'Frau',
147 );
148  
149 sub courtesy()
150 3 ▶{ my $self = shift;
Subroutine courtesy: called CC=6 SCAR=18.2
Pod documented
151 3 ▶ return $self->{UI_courtesy} if defined $self->{UI_courtesy};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"UI_courtesy"} 0 3 3
152  
153 3 ▶ my $table
Branch: 2 branches
BranchTrueFalseTotal
$self->isMale ? : 2 1 3
$self->isFemale ? : 1 0 1
154 = $self->isMale ? \%male_courtesy_default
155 : $self->isFemale ? \%female_courtesy_default
156 : return undef;
157  
158 3 my $lang = lc $self->language;
159 3 ▶ return $table->{$lang} if exists $table->{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $table->{$lang} 2 1 3
160  
161 1 $lang =~ s/\..*//; # "en_GB.utf8" --> "en-GB" and retry
162 1 ▶ return $table->{$lang} if exists $table->{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $table->{$lang} 0 1 1
163  
164 1 $lang =~ s/[-_].*//; # "en_GB.utf8" --> "en" and retry
165 1 $table->{$lang};
166 }
167  
168  
169 # TBI: if we have a courtesy, we may detect the language.
170 # TBI: when we have a postal address, we may derive the language from
171 # the country.
172 # TBI: if we have an e-mail addres, we may derive the language from
173 # that.
174  
175 3 ▶sub language() { $_[0]->{UI_language} || 'en' }
Subroutine language: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"UI_language"} or "en" 1 2 3
176  
177  
178 3 ▶sub gender() { $_[0]->{UI_gender} }
Subroutine gender: called CC=1 SCAR=0.0
Pod documented
179  
180  
181 sub isMale()
182 6 ▶{ my $self = shift;
Subroutine isMale: called CC=4 SCAR=16.6
Pod documented
183  
184 6 ▶ if(my $gender = $self->{UI_gender})
Branch: 1 branch
BranchTrueFalseTotal
if (my $gender = $self->{"UI_gender"}) 5 1 6
185 5 { return $gender =~ m/^[mh]/i;
186 }
187  
188 1 ▶ if(my $courtesy = $self->{UI_courtesy})
Branch: 1 branch
BranchTrueFalseTotal
if (my $courtesy = $self->{"UI_courtesy"}) 0 1 1
189 0 { $courtesy = lc $courtesy;
190 0 $courtesy =~ s/[^\s\w]//g;
191 0 ▶ return 1 if exists $male_courtesy{$courtesy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $male_courtesy{$courtesy} 0 0 0
192 }
193  
194 1 undef;
195 }
196  
197  
198 sub isFemale()
199 4 ▶{ my $self = shift;
Subroutine isFemale: called CC=4 SCAR=16.6
Pod documented
200  
201 4 ▶ if(my $gender = $self->{UI_gender})
Branch: 1 branch
BranchTrueFalseTotal
if (my $gender = $self->{"UI_gender"}) 3 1 4
202 3 { return $gender =~ m/^[vf]/i;
203 }
204  
205 1 ▶ if(my $courtesy = $self->{UI_courtesy})
Branch: 1 branch
BranchTrueFalseTotal
if (my $courtesy = $self->{"UI_courtesy"}) 0 1 1
206 0 { $courtesy = lc $courtesy;
207 0 $courtesy =~ s/[^\s\w]//g;
208 0 ▶ return 1 if exists $female_courtesy{$courtesy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $female_courtesy{$courtesy} 0 0 0
209 }
210  
211 1 undef;
212 }
213  
214  
215 3 ▶sub dateOfBirth() { $_[0]->{UI_birth} }
Subroutine dateOfBirth: called CC=1 SCAR=0.0
Pod documented
216  
217  
218 sub birth()
219 2 ▶{ my $birth = shift->dateOfBirth;
Subroutine birth: called CC=6 SCAR=20.2
Pod documented
220 2 my $time;
221  
222 2 ▶ if($birth =~ m/^\s*(\d{4})[-\s]*(\d{2})[-\s]*(\d{2})\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($birth =~ /^\s*(\d{4})[-\s]*(\d{2})[-\s]*(\d{2})\s*$/) 0 2 2
223 { # Pre-formatted.
224 0 return sprintf "%04d%02d%02d", $1, $2, $3;
225 }
226  
227 2 eval "require Date::Parse";
228 2 ▶ unless($@)
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 2 0 2
229 2 { my ($day,$month,$year) = (Date::Parse::strptime($birth))[3,4,5];
230 2 ▶ if(defined $year)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $year) 2 0 2
231 2 ▶ { return sprintf "%04d%02d%02d",
Branch: 1 branch
BranchTrueFalseTotal
defined $month ? : 2 0 2
Condition50%1/2
$day || 0
l!l
20
MC/DC0%0/1
$day || 0
$day
Truth table50%1/2
A
A= $day
Aresult
11
00
232 ($year + 1900),
233 (defined $month ? $month+1 : 0),
234 ($day || 0);
235 }
236 }
237  
238 # TBI: Other date parsers
239  
240 0 undef;
241 }
242  
243  
244 sub age()
245 1 ▶{ my $birth = shift->birth or return;
Subroutine age: called CC=5 SCAR=18.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $birth = (shift())->birth or return 1 0 1
246  
247 1 my ($year, $month, $day) = $birth =~ m/^(\d{4})(\d\d)(\d\d)$/;
248 1 my ($today, $tomonth, $toyear) = (localtime)[3,4,5];
249 1 $tomonth++;
250  
251 1 my $age = $toyear+1900 - $year;
252 1 ▶ $age-- if $month > $tomonth || ($month == $tomonth && $day >= $today);
Branch: 1 branch
BranchTrueFalseTotal
if $month > $tomonth or $month == $tomonth and $day >= $today 0 1 1
Condition33%2/6
$month > $tomonth or $month == $tomonth and $day >= $today
l!l&&r!l&&!r
001
$month == $tomonth and $day >= $today
!ll&&!rl&&r
100
MC/DC0%0/3
$month > $tomonth or $month == $tomonth and $day >= $today
$month > $tomonth $month == $tomonth $day >= $today
Truth table25%1/4
A or B and C
A= $month > $tomonthB= $month == $tomonthC= $day >= $today
ABCresult
1XX1
0111
00X0
0100
253 1 $age;
254 }
255  
256  
257 0 ▶sub titles() { $_[0]->{UI_titles} }
Subroutine titles: not called CC=1 SCAR=6.9
Pod documented
258  
259 3 1;
« User/Identity/Item.pm ↑ Summary User/Identity/Archive.pm »