DBIx/Class/Relationship/ManyToMany.pm

filter: Statementstmt 65.4% 140 / 214 Branchbran 35.7% 45 / 126 Conditioncond 27.2% 12 / 44 MC/DCmcdc 7.6% 2 / 26 Subroutinesub 71.4% 20 / 28 Podpod 0.0% 0 / 1 totaltotal 49.8% 219 / 439 CC 330 SCAR 27.1
CC
330
Cov
51%
__ANON__
21.2
set_artists
21.2
set_artists_test_m2m
21.2
CRAP 15.0
« DBIx/Class/Relationship/BelongsTo.pm ↑ Summary DBIx/Class/InflateColumn/DateTime.pm »
1 package # hide from PAUSE
2 DBIx::Class::Relationship::ManyToMany;
3  
4 308 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 308 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 308 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 308 ▶use Sub::Name 'subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 308 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 308 ▶use DBIx::Class::_Util 'fail_on_internal_wantarray';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 308 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 35 our %_pod_inherit_config =
14 (
15 class_map => { 'DBIx::Class::Relationship::ManyToMany' => 'DBIx::Class::Relationship' }
16 );
17  
18 sub many_to_many {
19 4316 ▶ my ($class, $meth, $rel, $f_rel, $rel_attrs) = @_;
Subroutine many_to_many: called CC=7 SCAR=26.3
Pod undocumented
20  
21 4316 ▶ $class->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless $rel 0 4316 4316
22 "missing relation in many-to-many"
23 ) unless $rel;
24  
25 4316 ▶ $class->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless $f_rel 0 4316 4316
26 "missing foreign relation in many-to-many"
27 ) unless $f_rel;
28  
29 {
30 308 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 308 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 4316 my $add_meth = "add_to_${meth}";
34 4316 my $remove_meth = "remove_from_${meth}";
35 4316 my $set_meth = "set_${meth}";
36 4316 my $rs_meth = "${meth}_rs";
37  
38 4316 for ($add_meth, $remove_meth, $set_meth, $rs_meth) {
39 17264 ▶ if ( $class->can ($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can($_)) 2 17262 17264
40 2 ▶ carp (<<"EOW") unless $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"DBIC_OVERWRITE_HELPER_METHODS_OK"} 1 1 2
41  
42 ***************************************************************************
43 The many-to-many relationship '$meth' is trying to create a utility method
44 called $_.
45 This will completely overwrite one such already existing method on class
46 $class.
47  
48 You almost certainly want to rename your method or the many-to-many
49 relationship, as the functionality of the original method will not be
50 accessible anymore.
51  
52 To disable this warning set to a true value the environment variable
53 DBIC_OVERWRITE_HELPER_METHODS_OK
54  
55 ***************************************************************************
56 EOW
57 }
58 }
59  
60 4316 ▶ $rel_attrs->{alias} ||= $f_rel;
Condition33%1/3
$rel_attrs->{"alias"} ||= $f_rel
l!l&&r!l&&!r
043160
MC/DC0%0/2
$rel_attrs->{"alias"} ||= $f_rel
$rel_attrs->{"alias"} $f_rel
Truth table33%1/3
A ||= B
A= $rel_attrs->{"alias"}B= $f_rel
ABresult
1X1
011
000
61  
62 4316 my $rs_meth_name = join '::', $class, $rs_meth;
63 *$rs_meth_name = subname $rs_meth_name, sub {
64 0 ▶ my $self = shift;
Subroutine artists_test_m2m_noopt_rs: not called CC=7 SCAR=25.3
Subroutine producers_rs: called CC=7 SCAR=25.3
Subroutine artists_rs: called CC=7 SCAR=25.3
65 0 ▶ my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
Branch: 3 branches
BranchTrueFalseTotal
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 0 0
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 17 17
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 17 17
Condition33%3/9
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
000
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
2700
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
250
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
Truth table0%0/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
66 0 ▶ my @args = ($f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs });
Branch: 6 branches
BranchTrueFalseTotal
@_ > 0 ? : 0 0 0
$rel_attrs or {} 0 0 0
@_ > 0 ? : 1 27 28
$rel_attrs or {} 25 2 27
@_ > 0 ? : 0 14 14
$rel_attrs or {} 0 16 16
67 my $rs = $self->search_related($rel)->search_related(
68 0 ▶ $f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs }
Branch: 6 branches
BranchTrueFalseTotal
@_ > 0 ? : 0 0 0
$rel_attrs or {} 0 0 0
@_ > 0 ? : 4 23 27
$rel_attrs or {} 14 13 27
@_ > 0 ? : 0 14 14
$rel_attrs or {} 0 4 4
69 );
70 0 return $rs;
71 4316 };
72  
73 4316 my $meth_name = join '::', $class, $meth;
74 *$meth_name = subname $meth_name, sub {
75 5 ▶ DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and wantarray and my $sog = fail_on_internal_wantarray($_[0]);
Subroutine objects: called CC=2 SCAR=8.3
Subroutine cds: called CC=2 SCAR=8.3
Subroutine producers_sorted: called CC=2 SCAR=8.3
Subroutine artists: not called CC=2 SCAR=8.3
76 5 my $self = shift;
77 5 my $rs = $self->$rs_meth( @_ );
78 5 ▶ return (wantarray ? $rs->all : $rs);
Branch: 4 branches
BranchTrueFalseTotal
wantarray ? : 0 5 5
wantarray ? : 0 1 1
wantarray ? : 0 0 0
wantarray ? : 0 0 0
79 4316 };
80  
81 4316 my $add_meth_name = join '::', $class, $add_meth;
82 *$add_meth_name = subname $add_meth_name, sub {
83 0 ▶ my $self = shift;
Subroutine add_to_artists_test_m2m2: not called CC=7 SCAR=30.5
Subroutine add_to_fourkeys: called CC=7 SCAR=30.5
Subroutine __ANON__: not called CC=7 SCAR=30.5
Subroutine add_to_objects: not called CC=7 SCAR=30.5
84 0 ▶ @_ > 0 or $self->throw_exception(
Branch: 4 branches
BranchTrueFalseTotal
@_ > 0 or $self->throw_exception("$add_meth needs an object or hashref") 0 0 0
@_ > 0 or $self->throw_exception("$add_meth needs an object or hashref") 0 2 2
@_ > 0 or $self->throw_exception("$add_meth needs an object or hashref") 0 0 0
@_ > 0 or $self->throw_exception("$add_meth needs an object or hashref") 0 0 0
85 "${add_meth} needs an object or hashref"
86 );
87 0 my $source = $self->result_source;
88 0 my $schema = $source->schema;
89 0 my $rel_source_name = $source->relationship_info($rel)->{source};
90 0 my $rel_source = $schema->resultset($rel_source_name)->result_source;
91 0 my $f_rel_source_name = $rel_source->relationship_info($f_rel)->{source};
92 0 ▶ my $f_rel_rs = $schema->resultset($f_rel_source_name)->search({}, $rel_attrs||{});
Condition36%4/11
$rel_attrs || {}
l!l
00
$rel_attrs || {}
l!l
8514
$rel_attrs || {}
l!l
1200
$rel_attrs || {}
l!l
000
MC/DC0%0/1
$rel_attrs || {}
$rel_attrs
MC/DC100%1/1
$rel_attrs || {}
$rel_attrs
MC/DC0%0/1
$rel_attrs || {}
$rel_attrs
MC/DC0%0/1
$rel_attrs || {}
$rel_attrs
Truth table0%0/2
A
A= $rel_attrs
Aresult
11
00
Truth table100%2/2
A
A= $rel_attrs
Aresult
11
00
Truth table50%1/2
A
A= $rel_attrs
Aresult
11
00
Truth table0%0/2
A
A= $rel_attrs
Aresult
11
00
93  
94 0 my $obj;
95 0 ▶ if (ref $_[0]) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
if (ref $_[0]) { } 2 0 2
if (ref $_[0]) { } 0 0 0
if (ref $_[0]) { } 0 0 0
96 0 ▶ if (ref $_[0] eq 'HASH') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
if (ref $_[0] eq 'HASH') { } 2 0 2
if (ref $_[0] eq 'HASH') { } 0 0 0
if (ref $_[0] eq 'HASH') { } 0 0 0
97 0 $obj = $f_rel_rs->find_or_create($_[0]);
98 } else {
99 0 $obj = $_[0];
100 }
101 } else {
102 0 $obj = $f_rel_rs->find_or_create({@_});
103 }
104  
105 0 ▶ my $link_vals = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
Branch: 4 branches
BranchTrueFalseTotal
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 0 0
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 2 2
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 0 0
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 0 0
Condition8%1/12
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
000
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
200
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
000
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
Truth table0%0/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
106 0 my $link = $self->search_related($rel)->new_result($link_vals);
107 0 $link->set_from_related($f_rel, $obj);
108 0 $link->insert();
109 0 return $obj;
110 4316 };
111  
112 4316 my $set_meth_name = join '::', $class, $set_meth;
113 *$set_meth_name = subname $set_meth_name, sub {
114 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=7 SCAR=30.5
Subroutine set_artists_test_m2m_noopt: not called CC=7 SCAR=30.5
Subroutine set_collections: called CC=7 SCAR=30.5
115 0 ▶ @_ > 0 or $self->throw_exception(
Branch: 4 branches
BranchTrueFalseTotal
@_ > 0 or $self->throw_exception("{$set_meth} needs a list of objects or hashrefs") 0 0 0
@_ > 0 or $self->throw_exception("{$set_meth} needs a list of objects or hashrefs") 0 0 0
@_ > 0 or $self->throw_exception("{$set_meth} needs a list of objects or hashrefs") 4 15 19
@_ > 0 or $self->throw_exception("{$set_meth} needs a list of objects or hashrefs") 0 0 0
116 "{$set_meth} needs a list of objects or hashrefs"
117 );
118 0 ▶ my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_);
Branch: 4 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
ref $_[0] eq 'ARRAY' ? : 0 11 11
ref $_[0] eq 'ARRAY' ? : 4 15 19
ref $_[0] eq 'ARRAY' ? : 0 0 0
119 # if there is a where clause in the attributes, ensure we only delete
120 # rows that are within the where restriction
121 0 ▶ if ($rel_attrs && $rel_attrs->{where}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($rel_attrs and $rel_attrs->{'where'}) { } 0 0 0
if ($rel_attrs and $rel_attrs->{'where'}) { } 0 11 11
if ($rel_attrs and $rel_attrs->{'where'}) { } 7 20 27
if ($rel_attrs and $rel_attrs->{'where'}) { } 0 0 0
Condition33%3/9
$rel_attrs and $rel_attrs->{"where"}
!ll&&!rl&&r
000
$rel_attrs and $rel_attrs->{"where"}
!ll&&!rl&&r
1100
$rel_attrs and $rel_attrs->{"where"}
!ll&&!rl&&r
1202
MC/DC0%0/2
$rel_attrs and $rel_attrs->{"where"}
$rel_attrs $rel_attrs->{"where"}
MC/DC0%0/2
$rel_attrs and $rel_attrs->{"where"}
$rel_attrs $rel_attrs->{"where"}
MC/DC50%1/2
$rel_attrs and $rel_attrs->{"where"}
$rel_attrs $rel_attrs->{"where"}
Truth table0%0/3
A and B
A= $rel_attrsB= $rel_attrs->{"where"}
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $rel_attrsB= $rel_attrs->{"where"}
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $rel_attrsB= $rel_attrs->{"where"}
ABresult
0X0
100
111
122 0 $self->search_related( $rel, $rel_attrs->{where},{join => $f_rel})->delete;
123 } else {
124 0 $self->search_related( $rel, {} )->delete;
125 }
126 # add in the set rel objects
127 0 ▶ $self->$add_meth($_, ref($_[1]) ? $_[1] : {}) for (@to_set);
Branch: 4 branches
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
ref $_[1] ? : 0 11 11
ref $_[1] ? : 3 14 17
ref $_[1] ? : 0 0 0
128 4316 };
129  
130 4316 my $remove_meth_name = join '::', $class, $remove_meth;
131 *$remove_meth_name = subname $remove_meth_name, sub {
132 5 ▶ my ($self, $obj) = @_;
Subroutine remove_from_twokeys: called CC=3 SCAR=12.3
Subroutine remove_from_cds: called CC=3 SCAR=12.3
Subroutine remove_from_artists: called CC=3 SCAR=12.3
Subroutine remove_from_objects: not called CC=3 SCAR=12.3
133 5 ▶ $self->throw_exception("${remove_meth} needs an object")
Branch: 4 branches
BranchTrueFalseTotal
unless builtin::blessed($obj) 1 4 5
unless builtin::blessed($obj) 0 11 11
unless builtin::blessed($obj) 0 13 13
unless builtin::blessed($obj) 0 0 0
134 unless blessed ($obj);
135 4 my $rel_source = $self->search_related($rel)->result_source;
136 4 my $cond = $rel_source->relationship_info($f_rel)->{cond};
137 4 my ($link_cond, $crosstable) = $rel_source->_resolve_condition(
138 $cond, $obj, $f_rel, $f_rel
139 );
140  
141 4 ▶ $self->throw_exception(
Branch: 4 branches
BranchTrueFalseTotal
if $crosstable 0 4 4
if $crosstable 0 16 16
if $crosstable 1 23 24
if $crosstable 0 0 0
142 "Custom relationship '$rel' does not resolve to a join-free condition, "
143 ."unable to use with the ManyToMany helper '$f_rel'"
144 ) if $crosstable;
145  
146 4 $self->search_related($rel, $link_cond)->delete;
147 4316 };
148  
149 }
150 }
151  
152 35 1;
« DBIx/Class/Relationship/BelongsTo.pm ↑ Summary DBIx/Class/InflateColumn/DateTime.pm »