User/Identity/Collection.pm

filter: Statementstmt 79.4% 58 / 73 Branchbran 31.2% 10 / 32 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 76.1% 16 / 21 Podpod 88.8% 8 / 9 totaltotal 67.8% 95 / 140 CC 19 SCAR 18.2
CC
19
Cov
65%
renameRole
20.0
removeRole
12.0
init
6.9
CRAP 6.2
« User/Identity/Location.pm ↑ Summary User/Identity/Item.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::Collection;{
13 1 our $VERSION = '4.00';
14 }
15  
16 1 ▶use parent 'User::Identity::Item';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Log::Report 'user-identity';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use User::Identity ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Hash::Ordered ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 #--------------------
29  
30 1 ▶use overload '""' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
31 13 my $self = shift;
32 13 $self->name . ": " . join(", ", sort map $_->name, $self->roles);
33 1 };
34  
35  
36 1 ▶use overload '@{}' => sub { [ $_[0]->roles ] };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 #--------------------
39  
40 0 ▶sub type { 'people' }
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
41  
42  
43 sub init($)
44 2 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=6 SCAR=19.3
Pod undocumented
45 2 ▶ defined($self->SUPER::init($args)) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->SUPER::init($args) or return 2 0 2
46  
47 2 ▶ $self->{UIC_itype} = delete $args->{item_type} or panic;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"UIC_itype"} = delete $args->{"item_type"} or &panic() 2 0 2
48 2 tie %{$self->{UIC_roles}}, 'Hash::Ordered';
49  
50 2 my $roles = $args->{roles};
51 2 ▶ my @roles = ! defined $roles ? () : ref $roles eq 'ARRAY' ? @$roles : $roles;
Branch: 2 branches
BranchTrueFalseTotal
!defined($roles) ? : 2 0 2
ref $roles eq 'ARRAY' ? : 0 0 0
52 2 $self->addRole($_) for @roles;
53 2 $self;
54 }
55  
56 #--------------------
57  
58 21 ▶sub roles() { values %{ $_[0]->{UIC_roles}} }
Subroutine roles: called CC=1 SCAR=0.0
Pod documented
59  
60  
61 3 ▶sub itemType { $_[0]->{UIC_itype} }
Subroutine itemType: called CC=1 SCAR=0.0
Pod documented
62  
63 #--------------------
64  
65 sub addRole(@)
66 3 ▶{ my $self = shift;
Subroutine addRole: called CC=6 SCAR=18.3
Pod documented
67 3 my $maintains = $self->itemType;
68  
69 3 my $role;
70 3 ▶ if(ref $_[0] && ref $_[0] ne 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] ne 'ARRAY') { } 1 2 3
Condition66%2/3
ref $_[0] and ref $_[0] ne "ARRAY"
!ll&&!rl&&r
021
MC/DC50%1/2
ref $_[0] and ref $_[0] ne "ARRAY"
ref $_[0] ref $_[0] ne "ARRAY"
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] ne "ARRAY"
ABresult
0X0
100
111
71 1 { $role = shift;
72 1 ▶ $role->isa($maintains)
Branch: 1 branch
BranchTrueFalseTotal
$role->isa($maintains) or &error(&__x("wrong type of role for {collection}: requires a {expect} but got a {type}.", "collection", ref $self, "expect", $maintains, "type", ref $role)) 1 0 1
73 or error __x"wrong type of role for {collection}: requires a {expect} but got a {type}.",
74 collection => ref $self, expect => $maintains, type => ref $role;
75 }
76 else
77 2 ▶ { $role = $maintains->new(ref $_[0] ? @{$_[0]} : @_)
Branch: 2 branches
BranchTrueFalseTotal
$role = $maintains->new(ref $_[0] ? @{$_[0];} : @_) or &error(&__x("cannot create a {type} to add this to my collection.", "type", $maintains)) 2 0 2
ref $_[0] ? : 2 0 2
78 or error __x"cannot create a {type} to add this to my collection.", type => $maintains;
79 }
80  
81 3 $role->parent($self);
82 3 $self->{UIC_roles}{$role->name} = $role;
83 3 $role;
84 }
85  
86  
87 sub removeRole($)
88 0 ▶{ my ($self, $which) = @_;
Subroutine removeRole: not called CC=3 SCAR=24.8
Pod documented
89 0 ▶ my $name = ref $which ? $which->name : $which;
Branch: 1 branch
BranchTrueFalseTotal
ref $which ? : 0 0 0
90 0 ▶ my $role = delete $self->{UIC_roles}{$name} or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $role = delete $self->{"UIC_roles"}{$name} or return () 0 0 0
91 0 $role->parent(undef);
92 0 $role;
93 }
94  
95  
96 sub renameRole($$$)
97 0 ▶{ my ($self, $which, $newname) = @_;
Subroutine renameRole: not called CC=4 SCAR=30.0
Pod documented
98 0 ▶ my $name = ref $which ? $which->name : $which;
Branch: 1 branch
BranchTrueFalseTotal
ref $which ? : 0 0 0
99  
100 0 ▶ ! exists $self->{UIC_roles}{$newname}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"UIC_roles"}{$newname} and &error(&__x("cannot rename {from} into {to}: already exists", "from", $name, "to", $newname)) 0 0 0
101 or error __x"cannot rename {from} into {to}: already exists", from => $name, to => $newname;
102  
103 0 ▶ my $role = delete $self->{UIC_roles}{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $role = delete $self->{"UIC_roles"}{$name} or &error(&__x("cannot rename {from} into {to}: doesn't exist", "from", $name, "to", $newname)) 0 0 0
104 or error __x"cannot rename {from} into {to}: doesn't exist", from => $name, to => $newname;
105  
106 0 $role->name($newname); # may imply change other attributes.
107 0 $self->{UIC_roles}{$newname} = $role;
108 }
109  
110  
111 0 ▶sub sorted() { sort {$a->name cmp $b->name} shift->roles}
Subroutine sorted: not called CC=1 SCAR=6.9
Pod documented
112  
113 #--------------------
114  
115 sub find($)
116 3 ▶{ my ($self, $select) = @_;
Subroutine find: called CC=4 SCAR=19.1
Pod documented
117  
118 !defined $select ? ($self->roles)[0]
119 : !ref $select ? $self->{UIC_roles}{$select}
120 : wantarray ? (grep $select->($_, $self), $self->roles)
121 0 ▶ : first { $select->($_, $self) } $self->roles;
Subroutine __ANON__: not called CC=1 SCAR=2.2
Branch: 3 branches
BranchTrueFalseTotal
!defined($select) ? : 0 3 3
!ref($select) ? : 3 0 3
wantarray ? : 0 0 0
122 }
123  
124 1 1;
« User/Identity/Location.pm ↑ Summary User/Identity/Item.pm »