lib/HTML/FormFu/Constraint/Equal.pm

filter: Statementstmt 96.4% 55 / 57 Branchbran 78.9% 30 / 38 Conditioncond 72.2% 13 / 18 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 1 totaltotal 81.3% 109 / 134 SCAR 34.0
CC
28
Cov
87%
process
15.2
_values_eq
10.9
_arrays_eq
4.1
CRAP 29.8
« lib/HTML/FormFu/Constraint/DependOn.pm ↑ Summary lib/HTML/FormFu/Role/Render.pm »
1 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Constraint::Equal;
4 9 $HTML::FormFu::Constraint::Equal::VERSION = '2.06';
5 # ABSTRACT: Multi-field Equality Constraint
6  
7 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 extends 'HTML::FormFu::Constraint';
9  
10 9 with 'HTML::FormFu::Role::Constraint::Others';
11  
12 9 use HTML::FormFu::Util qw(
13 DEBUG_CONSTRAINTS
14 debug
15 9 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use List::Util 1.33 qw( all );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 our $EMPTY_STR = q{};
19  
20 sub process {
21 35 ▶ my ( $self, $params ) = @_;
Subroutine process: called CC=15 SCAR=27.2
Pod undocumented
22  
23 # check when condition
24 35 ▶ return if !$self->_process_when($params);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_process_when($params) 0 35 35
25  
26 35 my $others = $self->others;
27 35 ▶ return if !defined $others;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $others 0 35 35
28  
29 35 my $value = $self->get_nested_hash_value( $params, $self->nested_name );
30  
31 35 ▶ DEBUG_CONSTRAINTS && debug( VALUE => $value );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS() and debug("VALUE", $value) 0 35 35
32  
33 35 ▶ my @names = ref $others ? @{$others} : ($others);
Branch: 1 branch
BranchTrueFalseTotal
ref $others ? : 31 4 35
34 35 my @failed;
35 my %values;
36  
37 35 for my $name (@names) {
38  
39 66 my $other_value = $self->get_nested_hash_value( $params, $name );
40  
41 66 ▶ DEBUG_CONSTRAINTS && debug( NAME => $name, VALUE => $value );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS() and debug("NAME", $name, "VALUE", $value) 0 66 66
42  
43 66 my $ok = _values_eq( $value, $other_value );
44  
45 66 ▶ if ( $self->not ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->not) { } 28 38 66
elsif (not $ok) { } 11 27 38
46 28 ▶ if ( $value eq $EMPTY_STR ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value eq $HTML::FormFu::Constraint::Equal::EMPTY_STR) { } 12 16 28
elsif ($ok) { } 6 10 16
47  
48 # no error if both values are empty and not(1) is set
49 }
50 elsif ($ok) {
51 6 push @failed, $name;
52 }
53 }
54 elsif ( !$ok ) {
55 11 push @failed, $name;
56 }
57  
58 66 $values{$name} = $other_value;
59 }
60  
61 # special case for $self->not()
62 # no errors if all values are empty
63 35 ▶ if ( $self->not
Branch: 1 branch
BranchTrueFalseTotal
if ($self->not and $value eq $HTML::FormFu::Constraint::Equal::EMPTY_STR and &all(sub { not defined $_ or $_ eq $EMPTY_STR; } , values %values)) 2 33 35
Condition100%6/6
$self->not and $value eq $HTML::FormFu::Constraint::Equal::EMPTY_STR and &all(sub {
!ll&&!rl&&r
2942
$self->not and $value eq $HTML::FormFu::Constraint::Equal::EMPTY_STR
!ll&&!rl&&r
2096
MC/DC0%0/3
$self->not and $value eq $HTML::FormFu::Constraint::Equal::EMPTY_STR and &all(sub { not defined $_ or $_ eq $EMPTY_STR; } , values %values)
$self->not $value eq $HTML::FormFu::Constraint::Equal::EMPTY_STR &all(sub { not defined $_ or $_ eq $EMPTY_STR; } , values %values)
Truth table50%2/4
A and B and C
A= $self->notB= $value eq $HTML::FormFu::Constraint::Equal::EMPTY_STRC= &all(sub { not defined $_ or $_ eq $EMPTY_STR; } , values %values)
ABCresult
0XX0
10X0
1100
1111
64 && $value eq $EMPTY_STR
65 9 ▶ && all { !defined || $_ eq $EMPTY_STR } values %values )
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
not defined $_ or $_ eq $HTML::FormFu::Constraint::Equal::EMPTY_STR 9 0 9
66 {
67 2 return;
68 }
69  
70 33 ▶ return $self->mk_errors(
Branch: 1 branch
BranchTrueFalseTotal
@failed ? : 14 19 33
71 { pass => @failed ? 0 : 1,
72 failed => \@failed,
73 names => [ $self->nested_name, @names ],
74 } );
75 }
76  
77 sub _values_eq {
78 71 ▶ my ( $v1, $v2 ) = @_;
Subroutine _values_eq: called CC=10 SCAR=23.9
79  
80 # the params should be coming from a CGI.pm compatible query object,
81 # so the value is either a string or an arrayref of strings
82  
83 71 ▶ return 1 if !defined $v1 && !defined $v2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v1 or defined $v2 0 71 71
Condition33%1/3
defined $v1 or defined $v2
l!l&&r!l&&!r
7100
MC/DC0%0/2
defined $v1 or defined $v2
defined $v1 defined $v2
Truth table33%1/3
A or B
A= defined $v1B= defined $v2
ABresult
1X1
011
000
84  
85 71 ▶ return if !defined $v1 || !defined $v2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v1 and defined $v2 2 69 71
Condition66%2/3
defined $v1 and defined $v2
!ll&&!rl&&r
0269
MC/DC50%1/2
defined $v1 and defined $v2
defined $v1 defined $v2
Truth table66%2/3
A and B
A= defined $v1B= defined $v2
ABresult
0X0
100
111
86  
87 69 ▶ if ( !ref $v1 && !ref $v2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $v1 || ref $v2) { } 59 10 69
elsif (ref $v1 eq 'ARRAY' and ref $v2 eq 'ARRAY') { } 8 2 10
Condition66%2/3
ref $v1 || ref $v2
l!l&&r!l&&!r
10059
MC/DC50%1/2
ref $v1 || ref $v2
ref $v1 ref $v2
Truth table66%2/3
A || B
A= ref $v1B= ref $v2
ABresult
1X1
011
000
88 59 ▶ return 1 if $v1 eq $v2;
Branch: 1 branch
BranchTrueFalseTotal
if $v1 eq $v2 36 23 59
89 }
90 ▶ elsif ( ( ref $v1 eq 'ARRAY' ) && ( ref $v2 eq 'ARRAY' ) ) {
Condition66%2/3
ref $v1 eq "ARRAY" and ref $v2 eq "ARRAY"
!ll&&!rl&&r
028
MC/DC50%1/2
ref $v1 eq "ARRAY" and ref $v2 eq "ARRAY"
ref $v1 eq "ARRAY" ref $v2 eq "ARRAY"
Truth table66%2/3
A and B
A= ref $v1 eq "ARRAY"B= ref $v2 eq "ARRAY"
ABresult
0X0
100
111
91 8 return _arrays_eq( $v1, $v2 );
92 }
93  
94 25 return;
95 }
96  
97 sub _arrays_eq {
98 8 ▶ my @a1 = sort @{ $_[0] };
Subroutine _arrays_eq: called CC=4 SCAR=14.0
99 8 my @a2 = sort @{ $_[1] };
100  
101 8 ▶ return if scalar @a1 != scalar @a2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @a1 != scalar @a2 0 8 8
102  
103 8 for my $i ( 0 .. $#a1 ) {
104 15 ▶ return if $a1[$i] ne $a2[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $a1[$i] ne $a2[$i] 0 15 15
105 }
106  
107 8 return 1;
108 }
109  
110 sub _localize_args {
111 0 ▶ my ($self) = @_;
Subroutine _localize_args: not called CC=1 SCAR=6.9
112  
113 0 return $self->parent->label;
114 }
115  
116 9 __PACKAGE__->meta->make_immutable;
117  
118 9 1;
119  
120 __END__
121  
122 =pod
123  
124 =encoding UTF-8
125  
126 =head1 NAME
127  
128 HTML::FormFu::Constraint::Equal - Multi-field Equality Constraint
129  
130 =head1 VERSION
131  
132 version 2.06
133  
134 =head1 SYNOPSIS
135  
136 - type: Password
137 name: password
138 constraints:
139 - type: Equal
140 others: repeat_password
141 - type: Password
142 name: repeat_password
143  
144 =head1 DESCRIPTION
145  
146 All fields named in L<HTML::FormFu::Role::Constraint::Others/others> must have an equal value to the field this
147 constraint is attached to.
148  
149 =head1 SEE ALSO
150  
151 Is a sub-class of, and inherits methods from
152 L<HTML::FormFu::Role::Constraint::Others>, L<HTML::FormFu::Constraint>
153  
154 L<HTML::FormFu>
155  
156 =head1 AUTHOR
157  
158 Carl Franks C<cfranks@cpan.org>
159  
160 =head1 LICENSE
161  
162 This library is free software, you can redistribute it and/or modify it under
163 the same terms as Perl itself.
164  
165 =head1 AUTHOR
166  
167 Carl Franks <cpan@fireartist.com>
168  
169 =head1 COPYRIGHT AND LICENSE
170  
171 This software is copyright (c) 2018 by Carl Franks.
172  
173 This is free software; you can redistribute it and/or modify it under
174 the same terms as the Perl 5 programming language system itself.
175  
176 =cut
« lib/HTML/FormFu/Constraint/DependOn.pm ↑ Summary lib/HTML/FormFu/Role/Render.pm »