User/Identity/Item.pm

filter: Statementstmt 91.4% 64 / 70 Branchbran 60.5% 23 / 38 Conditioncond 50.0% 13 / 26 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 81.2% 13 / 16 Podpod 91.6% 11 / 12 totaltotal 71.1% 126 / 177 CC 29 SCAR 17.3
CC
29
Cov
75%
removeCollection
12.0
addCollection
10.6
add
6.1
CRAP 5.6
« User/Identity/Collection.pm ↑ Summary User/Identity.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::Item;{
13 3 our $VERSION = '4.00';
14 }
15  
16  
17 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use Log::Report 'user-identity';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 #--------------------
25  
26 sub new(@)
27 15 ▶{ my $class = shift;
Subroutine new: called CC=4 SCAR=14.0
Pod documented
28 15 ▶ @_ or return undef; # no empty users.
Branch: 1 branch
BranchTrueFalseTotal
@_ or return undef 14 1 15
29  
30 14 ▶ unshift @_, 'name' if @_ %2; # odd-length list: starts with nick
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 12 2 14
31  
32 14 my %args = @_;
33 14 my $self = (bless {}, $class)->init(\%args);
34  
35 14 ▶ if(my @missing = keys %args)
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing) = keys %args) 0 14 14
36 0 { warning __xn"unknown option '{name}' for {class}.", "unknown options {names} for {class}",
37 scalar @missing, name => $missing[0], names => \@missing, class => $class;
38 }
39  
40 14 $self;
41 }
42  
43 sub init($)
44 14 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=2 SCAR=7.4
Pod undocumented
45  
46 $self->{UII_name} = delete $args->{name}
47 14 ▶ // error __x"each item requires a name.";
Condition33%1/3
delete $args->{"name"} // &error(&__x("each item requires a name."))
l!l&&r!l&&!r
1400
MC/DC0%0/2
delete $args->{"name"} // &error(&__x("each item requires a name."))
delete $args->{"name"} &error(&__x("each item requires a name."))
Truth table33%1/3
A // B
A= delete $args->{"name"}B= &error(&__x("each item requires a name."))
ABresult
1X1
011
000
48  
49 14 $self->{UII_description} = delete $args->{description};
50 14 $self;
51 }
52  
53 #--------------------
54  
55 sub name(;$)
56 38 ▶{ my $self = shift;
Subroutine name: called CC=2 SCAR=7.2
Pod documented
57 38 ▶ @_ ? ($self->{UII_name} = shift) : $self->{UII_name};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 38 38
58 }
59  
60  
61 0 ▶sub description() { $_[0]->{UII_description} }
Subroutine description: not called CC=1 SCAR=6.9
Pod documented
62  
63 #--------------------
64  
65 3 our %collectors = (
66 emails => 'User::Identity::Collection::Emails',
67 locations => 'User::Identity::Collection::Locations',
68 systems => 'User::Identity::Collection::Systems',
69 users => 'User::Identity::Collection::Users',
70 ); # *s is tried as well, so email, system, and location will work
71  
72 sub addCollection(@)
73 2 ▶{ my $self = shift;
Subroutine addCollection: called CC=9 SCAR=23.6
Pod documented
74 2 ▶ @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return 2 0 2
75  
76 2 my $object;
77 2 ▶ if(ref $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 1 1 2
78 1 { $object = shift;
79 1 ▶ $object->isa('User::Identity::Collection') or error __x"this {object} is not a collection.", object => $object;
Branch: 1 branch
BranchTrueFalseTotal
$object->isa("User::Identity::Collection") or &error(&__x("this {object} is not a collection.", "object", $object)) 1 0 1
80 }
81 else
82 1 ▶ { unshift @_, 'type' if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 0 1
83 1 my %args = @_;
84 1 ▶ my $type = delete $args{type} or panic "no collection type specified";
Branch: 1 branch
BranchTrueFalseTotal
my $type = delete $args{"type"} or &panic("no collection type specified") 1 0 1
85  
86 1 ▶ my $class = $collectors{$type} || $collectors{$type.'s'} || $type;
Condition33%2/6
$User::Identity::Item::collectors{$type} || $User::Identity::Item::collectors{$type . 's'} || $type
l!l&&r!l&&!r
100
$User::Identity::Item::collectors{$type} or $User::Identity::Item::collectors{$type . 's'}
l!l&&r!l&&!r
010
MC/DC0%0/3
$User::Identity::Item::collectors{$type} || $User::Identity::Item::collectors{$type . 's'} || $type
$User::Identity::Item::collectors{$type} $User::Identity::Item::collectors{$type . 's'} $type
Truth table25%1/4
A or B || C
A= $User::Identity::Item::collectors{$type}B= $User::Identity::Item::collectors{$type . 's'}C= $type
ABCresult
1XX1
01X1
0011
0000
87 1 eval "require $class";
88 1 ▶ $@ and error __x"cannot load collection module {type} ({class}): {err}", type => $type, class => $class, err => $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and &error(&__x("cannot load collection module {type} ({class}): {err}", "type", $type, "class", $class, "err", $@)) 0 1 1
89  
90 1 $object = $class->new(%args);
91 }
92  
93 2 $object->parent($self);
94 2 $self->{UI_col}{$object->name} = $object;
95 }
96  
97  
98  
99 sub removeCollection($)
100 0 ▶{ my $self = shift;
Subroutine removeCollection: not called CC=3 SCAR=24.8
Pod documented
101 0 ▶ my $name = ref $_[0] ? $_[0]->name : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
102  
103 delete $self->{UI_col}{$name}
104 0 ▶ || delete $self->{UI_col}{$name.'s'};
Branch: 1 branch
BranchTrueFalseTotal
delete $self->{"UI_col"}{$name} or delete $self->{"UI_col"}{$name . 's'} 0 0 0
105 }
106  
107  
108  
109 sub collection($;$)
110 6 ▶{ my $self = shift;
Subroutine collection: called CC=4 SCAR=14.1
Pod documented
111 6 my $collname = shift;
112 6 ▶ my $collection = $self->{UI_col}{$collname} || $self->{UI_col}{$collname.'s'} || return;
Condition80%4/5
$self->{'UI_col'}{$collname} || $self->{'UI_col'}{$collname . 's'} || (return)
l!l
42
$self->{"UI_col"}{$collname} or $self->{"UI_col"}{$collname . 's'}
l!l&&r!l&&!r
105
MC/DC50%1/2
$self->{'UI_col'}{$collname} || $self->{'UI_col'}{$collname . 's'} || (return)
$self->{"UI_col"}{$collname} $self->{"UI_col"}{$collname . 's'}
Truth table66%2/3
A or B
A= $self->{"UI_col"}{$collname}B= $self->{"UI_col"}{$collname . 's'}
ABresult
1X1
011
000
113  
114 4 ▶ wantarray ? $collection->roles : $collection;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
115 }
116  
117  
118 sub add($$)
119 2 ▶{ my ($self, $collname) = (shift, shift);
Subroutine add: called CC=5 SCAR=18.1
Pod documented
120 2 ▶ my $collection
Branch: 1 branch
BranchTrueFalseTotal
ref $collname && $collname->isa('User::Identity::Collection') ? : 0 2 2
Condition50%3/6
ref $collname and $collname->isa("User::Identity::Collection")
!ll&&!rl&&r
200
$self->collection($collname) or $self->addCollection($collname)
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $collname and $collname->isa("User::Identity::Collection")
ref $collname $collname->isa("User::Identity::Collection")
MC/DC0%0/2
$self->collection($collname) or $self->addCollection($collname)
$self->collection($collname) $self->addCollection($collname)
Truth table33%1/3
A and B
A= ref $collnameB= $collname->isa("User::Identity::Collection")
ABresult
0X0
100
111
Truth table66%2/3
A or B
A= $self->collection($collname)B= $self->addCollection($collname)
ABresult
1X1
011
000
121 = ref $collname && $collname->isa('User::Identity::Collection') ? $collname
122 : ($self->collection($collname) || $self->addCollection($collname));
123  
124 2 ▶ defined $collection
Branch: 1 branch
BranchTrueFalseTotal
defined $collection or &error(&__x("invalid collection {name}.", "name", $collname)) 2 0 2
125 or error __x"invalid collection {name}.", name => $collname;
126  
127 2 $collection->addRole(@_);
128 }
129  
130  
131 0 ▶sub type { "item" }
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
132  
133  
134 sub parent(;$)
135 18 ▶{ my $self = shift;
Subroutine parent: called CC=2 SCAR=6.9
Pod documented
136 18 ▶ @_ or return $self->{UII_parent};
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $self->{"UII_parent"} 6 12 18
137  
138 6 $self->{UII_parent} = shift;
139 6 weaken($self->{UII_parent});
140 6 $self->{UII_parent};
141 }
142  
143  
144 sub user()
145 9 ▶{ my $self = shift;
Subroutine user: called CC=2 SCAR=6.9
Pod documented
146 9 my $parent = $self->parent;
147 9 ▶ defined $parent ? $parent->user : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $parent ? : 7 2 9
148 }
149  
150 #--------------------
151  
152 sub find($$)
153 5 ▶{ my $all = shift->{UI_col};
Subroutine find: called CC=5 SCAR=17.2
Pod documented
154 5 my $collname = shift;
155 my $collection
156 = ref $collname && $collname->isa('User::Identity::Collection') ? $collname
157 5 ▶ : ($all->{$collname} || $all->{$collname.'s'});
Branch: 1 branch
BranchTrueFalseTotal
ref $collname && $collname->isa('User::Identity::Collection') ? : 0 5 5
Condition50%3/6
ref $collname and $collname->isa("User::Identity::Collection")
!ll&&!rl&&r
500
$all->{$collname} or $$all{$collname . 's'}
l!l&&r!l&&!r
032
MC/DC0%0/2
ref $collname and $collname->isa("User::Identity::Collection")
ref $collname $collname->isa("User::Identity::Collection")
MC/DC50%1/2
$all->{$collname} or $$all{$collname . 's'}
$all->{$collname} $$all{$collname . 's'}
Truth table33%1/3
A and B
A= ref $collnameB= $collname->isa("User::Identity::Collection")
ABresult
0X0
100
111
Truth table66%2/3
A or B
A= $all->{$collname}B= $$all{$collname . 's'}
ABresult
1X1
011
000
158  
159 5 ▶ defined $collection ? $collection->find(shift) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $collection ? : 3 2 5
160 }
161  
162 3 1;
« User/Identity/Collection.pm ↑ Summary User/Identity.pm »