lib/HTML/FormFu/Role/Constraint/Others.pm

filter: Statementstmt 96.5% 111 / 115 Branchbran 71.4% 50 / 70 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 87.6% 185 / 211 CC 43 SCAR 30.3
CC
43
Cov
88%
mk_errors
32.0
__ANON__
9.4
pre_process
6.0
CRAP 20.7
« lib/HTML/FormFu/Deploy.pm ↑ Summary lib/HTML/FormFu/QueryType/CGI/Simple.pm »
1 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Role::Constraint::Others;
4 32 $HTML::FormFu::Role::Constraint::Others::VERSION = '2.09';
5 # ABSTRACT: Base class for constraints needing others() method
6  
7 32 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 32 ▶use HTML::FormFu::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 DEBUG_CONSTRAINTS_OTHERS
11 debug
12 32 );
13 32 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use List::Util 1.33 qw( any none );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 32 has others => ( is => 'rw', traits => ['Chained'] );
17 32 has other_siblings => ( is => 'rw', traits => ['Chained'] );
18 32 has attach_errors_to => ( is => 'rw', traits => ['Chained'] );
19 32 has attach_errors_to_base => ( is => 'rw', traits => ['Chained'] );
20 32 has attach_errors_to_others => ( is => 'rw', traits => ['Chained'] );
21  
22 87 ▶sub pre_process {
Subroutine pre_process: called CC=6 SCAR=18.0
Pod undocumented
23 87 my ($self) = @_;
24  
25 87 ▶ if ( $self->other_siblings ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->other_siblings) 11 76 87
26  
27 11 my $field = $self->field;
28 11 my $block = $field;
29  
30 # find the nearest parent that contains any field other than
31 # the one this constraint is attached to
32 11 ▶ while ( defined( my $parent = $block->parent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $parent = $block->parent)) 11 0 11
33 11 $block = $parent;
34  
35 11 ▶ last if grep { $_ ne $field } @{ $block->get_fields };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ ne $field;} @{$block->get_fields;} 11 0 11
36 }
37  
38 11 my @names;
39  
40 11 for my $sibling ( @{ $block->get_fields } ) {
41 36 ▶ next if $sibling == $field;
Branch: 1 branch
BranchTrueFalseTotal
if $sibling == $field 11 25 36
42  
43 25 push @names, $sibling->nested_name;
44 }
45  
46 11 $self->others( [@names] );
47 }
48 }
49  
50 4 ▶after repeatable_repeat => sub {
Subroutine __ANON__: called CC=9 SCAR=22.4
51 4 my ( $self, $repeatable, $new_block ) = @_;
52  
53 4 my $block_fields = $new_block->get_fields;
54  
55 # rename any 'others' fields
56 {
57 4 my $others = $self->others;
58 4 ▶ if ( !ref $others ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $others) 2 2 4
59 2 $others = [$others];
60 }
61 4 my @new_others;
62  
63 4 for my $name (@$others) {
64 6 my $field = $repeatable->get_field_with_original_name( $name,
65 $block_fields );
66  
67 6 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) { } 6 0 6
68 6 ▶ DEBUG_CONSTRAINTS_OTHERS && debug(
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug(sprintf("Repeatable renaming constraint 'other' '%s' to '%s'", $name, $field->nested_name)) 0 6 6
69 sprintf
70 "Repeatable renaming constraint 'other' '%s' to '%s'",
71 $name, $field->nested_name,
72 );
73  
74 6 push @new_others, $field->nested_name;
75 }
76 else {
77 0 push @new_others, $name;
78 }
79 }
80  
81 4 $self->others( \@new_others );
82 }
83  
84 # rename any 'attach_errors_to' fields
85 4 ▶ if ( my $others = $self->attach_errors_to ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $others = $self->attach_errors_to) 2 2 4
86 2 my @new_others;
87  
88 2 for my $name (@$others) {
89 4 my $field = $repeatable->get_field_with_original_name( $name,
90 $block_fields );
91  
92 4 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) { } 4 0 4
93 4 ▶ DEBUG_CONSTRAINTS_OTHERS && debug(
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug(sprintf("Repeatable renaming constraint 'attach_errors_to' '%s' to '%s'", $name, $field->nested_name)) 0 4 4
94 sprintf
95 "Repeatable renaming constraint 'attach_errors_to' '%s' to '%s'",
96 $name, $field->nested_name,
97 );
98  
99 4 push @new_others, $field->nested_name;
100 }
101 else {
102 0 push @new_others, $name;
103 }
104 }
105  
106 2 $self->attach_errors_to( \@new_others );
107 }
108 32 };
109  
110 101 ▶sub mk_errors {
Subroutine mk_errors: called CC=29 SCAR=34.6
Pod undocumented
111 101 my ( $self, $args ) = @_;
112  
113 101 my $pass = $args->{pass};
114 101 ▶ my @failed = $args->{failed} ? @{ $args->{failed} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$args->{'failed'} ? : 84 17 101
115 101 ▶ my @names = $args->{names} ? @{ $args->{names} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$args->{'names'} ? : 84 17 101
116  
117 101 ▶ my $force = $self->force_errors || $self->parent->force_errors;
Condition100%3/3
$self->force_errors || $self->parent->force_errors
l!l&&r!l&&!r
12584
MC/DC100%2/2
$self->force_errors || $self->parent->force_errors
$self->force_errors $self->parent->force_errors
Truth table100%3/3
A || B
A= $self->force_errorsB= $self->parent->force_errors
ABresult
1X1
011
000
118  
119 101 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( PASS => $pass );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("PASS", $pass) 0 101 101
120 101 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( NAMES => \@names );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("NAMES", \@names) 0 101 101
121 101 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( 'FAILED NAMES' => \@failed );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("FAILED NAMES", \@failed) 0 101 101
122 101 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( FORCE => $force );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("FORCE", $force) 0 101 101
123  
124 101 ▶ if ( $pass && !$force ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass and not $force) 56 45 101
Condition100%3/3
$pass and not $force
!ll&&!rl&&r
331256
MC/DC100%2/2
$pass and not $force
$pass not $force
Truth table100%3/3
A and not(B)
A= $passB= not $force
ABresult
0X0
100
111
125 56 ▶ DEBUG_CONSTRAINTS_OTHERS
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("constraint passed, or force_errors is false - returning no errors") 0 56 56
126 && debug(
127 'constraint passed, or force_errors is false - returning no errors'
128 );
129 56 return;
130 }
131  
132 45 my @can_error;
133 my @has_error;
134  
135 45 ▶ if ( $self->attach_errors_to ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->attach_errors_to) { } 1 44 45
elsif ($self->attach_errors_to_base) { } 11 33 44
elsif ($self->attach_errors_to_others) { } 2 31 33
136 1 push @can_error, @{ $self->attach_errors_to };
137  
138 1 ▶ if ( !$pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 1 0 1
139 1 push @has_error, @{ $self->attach_errors_to };
140 }
141 }
142 elsif ( $self->attach_errors_to_base ) {
143 11 push @can_error, $self->nested_name;
144  
145 11 ▶ if ( !$pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 9 2 11
146 9 push @has_error, $self->nested_name;
147 }
148 }
149 elsif ( $self->attach_errors_to_others ) {
150 push @can_error, ref $self->others
151 0 ▶ ? @{ $self->others }
Branch: 1 branch
BranchTrueFalseTotal
ref $self->others ? : 0 2 2
152 : $self->others;
153  
154 2 ▶ if ( !$pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 2 0 2
155 push @has_error, ref $self->others
156 0 ▶ ? @{ $self->others }
Branch: 1 branch
BranchTrueFalseTotal
ref $self->others ? : 0 2 2
157 : $self->others;
158 }
159 }
160 else {
161 31 push @can_error, @names;
162  
163 31 ▶ if ( !$pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 21 10 31
164 21 push @has_error, @failed;
165 }
166 }
167  
168 45 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( 'CAN ERROR' => \@can_error );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("CAN ERROR", \@can_error) 0 45 45
169 45 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( 'HAS ERROR' => \@has_error );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("HAS ERROR", \@has_error) 0 45 45
170  
171 45 my @errors;
172  
173 45 for my $name (@can_error) {
174  
175 98 ▶ next unless $force || grep { $name eq $_ } @has_error;
Branch: 1 branch
BranchTrueFalseTotal
unless $force or grep {$name eq $_;} @has_error 30 68 98
Condition100%3/3
$force or grep {$name eq $_;} @has_error
l!l&&r!l&&!r
363230
MC/DC100%2/2
$force or grep {$name eq $_;} @has_error
$force grep {$name eq $_;} @has_error
Truth table100%3/3
A or B
A= $forceB= grep {$name eq $_;} @has_error
ABresult
1X1
011
000
176  
177 68 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( 'CREATING ERROR' => $name );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("CREATING ERROR", $name) 0 68 68
178  
179 68 ▶ my $field = $self->form->get_field( { nested_name => $name } )
Branch: 1 branch
BranchTrueFalseTotal
my $field = $self->form->get_field({"nested_name", $name}) or die "others() field not found: '${name}'" 68 0 68
180 or die "others() field not found: '$name'";
181  
182 68 my $error = $self->mk_error;
183  
184 68 $error->parent($field);
185  
186 68 ▶ if ( !grep { $name eq $_ } @has_error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$name eq $_;} @has_error) 31 37 68
187 31 ▶ DEBUG_CONSTRAINTS_OTHERS
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("setting '${name}' error forced(1)") 0 31 31
188 && debug("setting '$name' error forced(1)");
189  
190 31 $error->forced(1);
191 }
192  
193 68 push @errors, $error;
194 }
195  
196 45 return @errors;
197 }
198  
199 4 ▶around clone => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
200 4 my ( $orig, $self, $args ) = @_;
201  
202 4 my $clone = $self->$orig($args);
203  
204 4 ▶ if ( ref $self->others ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->others) 2 2 4
205 2 $clone->others( Clone::clone( $self->others ) );
206 }
207  
208 4 return $clone;
209 32 };
210  
211 32 1;
212  
213 __END__
« lib/HTML/FormFu/Deploy.pm ↑ Summary lib/HTML/FormFu/QueryType/CGI/Simple.pm »