Form/ListInput.pm

filter: Statementstmt 98.4% 128 / 130 Branchbran 92.8% 65 / 70 Conditioncond 100% 44 / 44 MC/DCmcdc 100% 26 / 26 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 9 totaltotal 94.5% 276 / 292 CC 56 SCAR 26.8
CC
56
Cov
97%
value
27.1
add_to_form
10.0
new
10.0
CRAP 14.6
« Form.pm ↑ Summary Form/TextInput.pm »
1 package HTML::Form::ListInput;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use parent 'HTML::Form::Input';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 our $VERSION = '6.13';
9  
10 # ABSTRACT: An HTML form list input element for use with HTML::Form
11  
12 #select/option (val1, val2, ....)
13 #input/radio (undef, val1, val2,...)
14 #input/checkbox (undef, value)
15 #select-multiple/option (undef, value)
16  
17 93 ▶sub new {
Subroutine new: called CC=10 SCAR=23.0
Pod undocumented
18 93 my $class = shift;
19 93 my $self = $class->SUPER::new(@_);
20  
21 93 my $value = delete $self->{value};
22 93 my $value_name = delete $self->{value_name};
23 93 my $type = $self->{type};
24  
25 93 ▶ if ( $type eq "checkbox" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'checkbox') { } 7 86 93
26 7 ▶ $value = "on" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 5 7
27 $self->{menu} = [
28 7 { value => undef, name => "off", },
29 { value => $value, name => $value_name, },
30 ];
31 7 ▶ $self->{current} = ( delete $self->{checked} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $self->{'checked'} ? : 5 2 7
32  
33 }
34 else {
35 $self->{option_disabled}++
36 86 ▶ if $type eq "radio" && delete $self->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "radio" and delete $self->{"disabled"} 4 82 86
Condition100%3/3
$type eq "radio" and delete $self->{"disabled"}
!ll&&!rl&&r
71114
MC/DC100%2/2
$type eq "radio" and delete $self->{"disabled"}
$type eq "radio" delete $self->{"disabled"}
Truth table100%3/3
A and B
A= $type eq "radio"B= delete $self->{"disabled"}
ABresult
0X0
100
111
37 $self->{menu} = [
38 86 { value => $value, name => $value_name },
39 ];
40 86 ▶ my $checked = $self->{checked} || $self->{option_selected};
Condition100%3/3
$self->{'checked'} || $self->{'option_selected'}
l!l&&r!l&&!r
21371
MC/DC100%2/2
$self->{'checked'} || $self->{'option_selected'}
$self->{'checked'} $self->{'option_selected'}
Truth table100%3/3
A || B
A= $self->{'checked'}B= $self->{'option_selected'}
ABresult
1X1
011
000
41 86 delete $self->{checked};
42 86 delete $self->{option_selected};
43 86 ▶ if ( exists $self->{multiple} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'multiple'}) { } 16 70 86
44 16 unshift( @{ $self->{menu} }, { value => undef, name => "off" } );
45 16 ▶ $self->{current} = $checked ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$checked ? : 2 14 16
46 }
47 else {
48 70 ▶ $self->{current} = 0 if $checked;
Branch: 1 branch
BranchTrueFalseTotal
if $checked 13 57 70
49 }
50 }
51 93 $self;
52 }
53  
54 93 ▶sub add_to_form {
Subroutine add_to_form: called CC=10 SCAR=23.0
Pod undocumented
55 93 my ( $self, $form ) = @_;
56 93 my $type = $self->type;
57  
58 93 ▶ return $self->SUPER::add_to_form($form)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "checkbox" 7 86 93
59 if $type eq "checkbox";
60  
61 86 ▶ if ( $type eq "option" && exists $self->{multiple} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "option" and exists $self->{"multiple"}) 16 70 86
Condition100%3/3
$type eq "option" and exists $self->{"multiple"}
!ll&&!rl&&r
155516
MC/DC100%2/2
$type eq "option" and exists $self->{"multiple"}
$type eq "option" exists $self->{"multiple"}
Truth table100%3/3
A and B
A= $type eq "option"B= exists $self->{"multiple"}
ABresult
0X0
100
111
62 16 ▶ $self->{disabled} ||= delete $self->{option_disabled};
Condition100%3/3
$self->{"disabled"} ||= delete $self->{"option_disabled"}
l!l&&r!l&&!r
3310
MC/DC100%2/2
$self->{"disabled"} ||= delete $self->{"option_disabled"}
$self->{"disabled"} delete $self->{"option_disabled"}
Truth table100%3/3
A ||= B
A= $self->{"disabled"}B= delete $self->{"option_disabled"}
ABresult
1X1
011
000
63 16 return $self->SUPER::add_to_form($form);
64 }
65  
66 70 ▶ Carp::croak "Assert" if @{ $self->{menu} } != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"menu"};} != 1 0 70 70
67 70 my $m = $self->{menu}[0];
68 70 ▶ $m->{disabled}++ if delete $self->{option_disabled};
Branch: 1 branch
BranchTrueFalseTotal
if delete $self->{"option_disabled"} 9 61 70
69  
70 # if there was no name we have to search for an input that explicitly has
71 # no name either, because otherwise the name attribute would be ignored
72 my $prev = $form->find_input(
73 $self->{name} || \undef, $self->{type},
74 $self->{idx}
75 70 ▶ );
Condition100%2/2
$self->{'name'} || \(undef)
l!l
619
MC/DC100%1/1
$self->{'name'} || \(undef)
$self->{'name'}
Truth table100%2/2
A
A= $self->{'name'}
Aresult
11
00
76 70 ▶ return $self->SUPER::add_to_form($form) unless $prev;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev 32 38 70
77  
78 # merge menus
79 38 ▶ $prev->{current} = @{ $prev->{menu} } if exists $self->{current};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"current"} 4 34 38
80 38 push( @{ $prev->{menu} }, $m );
81 }
82  
83 55 ▶sub fixup {
Subroutine fixup: called CC=4 SCAR=13.9
Pod undocumented
84 55 my $self = shift;
85 55 ▶ if ( $self->{type} eq "option" && !( exists $self->{current} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "option" and not exists $self->{"current"}) 11 44 55
Condition100%3/3
$self->{"type"} eq "option" and not exists $self->{"current"}
!ll&&!rl&&r
172711
MC/DC100%2/2
$self->{"type"} eq "option" and not exists $self->{"current"}
$self->{"type"} eq "option" not exists $self->{"current"}
Truth table100%3/3
A and not(B)
A= $self->{"type"} eq "option"B= not exists $self->{"current"}
ABresult
0X0
100
111
86 11 $self->{current} = 0;
87 }
88 55 ▶ $self->{menu}[ $self->{current} ]{seen}++ if exists $self->{current};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"current"} 47 8 55
89 }
90  
91 46 ▶sub disabled {
Subroutine disabled: called CC=4 SCAR=13.9
Pod undocumented
92 46 my $self = shift;
93 46 my $type = $self->type;
94  
95 46 ▶ my $old = $self->{disabled} || _menu_all_disabled( @{ $self->{menu} } );
Condition100%3/3
$self->{'disabled'} || _menu_all_disabled(@{$$self{'menu'};})
l!l&&r!l&&!r
10432
MC/DC100%2/2
$self->{'disabled'} || _menu_all_disabled(@{$$self{'menu'};})
$self->{'disabled'} _menu_all_disabled(@{$$self{'menu'};})
Truth table100%3/3
A || B
A= $self->{'disabled'}B= _menu_all_disabled(@{$$self{'menu'};})
ABresult
1X1
011
000
96 46 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 41 46
97 5 my $v = shift;
98 5 $self->{disabled} = $v;
99 5 for ( @{ $self->{menu} } ) {
100 11 $_->{disabled} = $v;
101 }
102 }
103 46 return $old;
104 }
105  
106 36 ▶sub _menu_all_disabled {
Subroutine _menu_all_disabled: called CC=3 SCAR=11.0
107 36 for (@_) {
108 38 ▶ return 0 unless $_->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"disabled"} 32 6 38
109 }
110 4 return 1;
111 }
112  
113 96 ▶sub value {
Subroutine value: called CC=27 SCAR=33.0
Pod undocumented
114 96 my $self = shift;
115 96 my $old;
116 $old = $self->{menu}[ $self->{current} ]{value}
117 96 ▶ if exists $self->{current};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"current"} 92 4 96
118 96 ▶ $old = $self->{value} if exists $self->{value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"value"} 0 96 96
119 96 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 41 55 96
120 41 my $i = 0;
121 41 my $val = shift;
122 41 my $cur;
123 my $disabled;
124 41 for ( @{ $self->{menu} } ) {
125 79 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and defined $_->{"value"} and $val eq $_->{"value"} or not defined $val || defined $_->{"value"}) 27 52 79
Condition100%12/12
defined $val and defined $_->{"value"} and $val eq $_->{"value"} or not defined $val || defined $_->{"value"}
l!l&&r!l&&!r
19852
defined $val and defined $_->{"value"} and $val eq $_->{"value"}
!ll&&!rl&&r
293119
defined $val and defined $_->{"value"}
!ll&&!rl&&r
92050
defined $val || defined $_->{'value'}
l!l&&r!l&&!r
5118
MC/DC100%5/5
defined $val and defined $_->{"value"} and $val eq $_->{"value"} or not defined $val || defined $_->{"value"}
defined $val defined $_->{"value"} $val eq $_->{"value"} defined $val defined $_->{'value'}
Truth table50%5/10
A and B and C or not(D || E)
A= defined $valB= defined $_->{"value"}C= $val eq $_->{"value"}D= defined $valE= defined $_->{'value'}
ABCDEresult
111XX1
0XX001
10X001
110001
0XX1X0
0XX010
10X1X0
10X010
1101X0
110010
126 (
127 defined($val)
128 && defined( $_->{value} )
129 && $val eq $_->{value}
130 )
131 || ( !defined($val) && !defined( $_->{value} ) )
132 ) {
133 27 $cur = $i;
134 27 $disabled = $_->{disabled};
135 27 ▶ last unless $disabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $disabled 20 7 27
136 }
137 59 $i++;
138 }
139 41 ▶ if ( !( defined $cur ) || $disabled ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cur or $disabled) 21 20 41
Condition100%3/3
not defined $cur or $disabled
l!l&&r!l&&!r
14720
MC/DC100%2/2
not defined $cur or $disabled
not defined $cur $disabled
Truth table100%3/3
not(A) or B
A= not defined $curB= $disabled
ABresult
1X1
011
000
140 21 ▶ if ( defined $val ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $val) { } 20 1 21
elsif ($self->{'strict'}) { } 1 0 1
141  
142 # try to search among the alternative names as well
143 20 my $i = 0;
144 20 my $cur_ignorecase;
145 20 my $lc_val = lc($val);
146 20 for ( @{ $self->{menu} } ) {
147 43 ▶ if ( defined $_->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_->{"name"}) 35 8 43
148 35 ▶ if ( $val eq $_->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq $_->{"name"}) 4 31 35
149 4 $disabled = $_->{disabled};
150 4 $cur = $i;
151 4 ▶ last unless $disabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $disabled 3 1 4
152 }
153 32 ▶ if ( !defined($cur_ignorecase)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cur_ignorecase and $lc_val eq lc $_->{"name"}) 3 29 32
Condition100%3/3
not defined $cur_ignorecase and $lc_val eq lc $_->{"name"}
!ll&&!rl&&r
1283
MC/DC100%2/2
not defined $cur_ignorecase and $lc_val eq lc $_->{"name"}
not defined $cur_ignorecase $lc_val eq lc $_->{"name"}
Truth table100%3/3
not(A) and B
A= not defined $cur_ignorecaseB= $lc_val eq lc $_->{"name"}
ABresult
0X0
100
111
154 && $lc_val eq lc( $_->{name} ) ) {
155 3 $cur_ignorecase = $i;
156 }
157 }
158 40 $i++;
159 }
160 20 ▶ unless ( defined $cur ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cur) 10 10 20
161 10 $cur = $cur_ignorecase;
162 10 ▶ if ( defined $cur ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $cur) { } 2 8 10
elsif ($self->{'strict'}) { } 8 0 8
163 2 $disabled = $self->{menu}[$cur]{disabled};
164 }
165 elsif ( $self->{strict} ) {
166 8 my $n = $self->name;
167 8 Carp::croak("Illegal value '$val' for field '$n'");
168 }
169 }
170 }
171 elsif ( $self->{strict} ) {
172 1 my $n = $self->name;
173 1 Carp::croak("The '$n' field can't be unchecked");
174 }
175 }
176 32 ▶ if ( $self->{strict} && $disabled ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict"} and $disabled) 7 25 32
Condition100%3/3
$self->{"strict"} and $disabled
!ll&&!rl&&r
6197
MC/DC100%2/2
$self->{"strict"} and $disabled
$self->{"strict"} $disabled
Truth table100%3/3
A and B
A= $self->{"strict"}B= $disabled
ABresult
0X0
100
111
177 7 my $n = $self->name;
178 7 Carp::croak("The value '$val' has been disabled for field '$n'");
179 }
180 25 ▶ if ( defined $cur ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cur) { } 25 0 25
181 25 $self->{current} = $cur;
182 25 $self->{menu}[$cur]{seen}++;
183 25 delete $self->{value};
184 }
185 else {
186 0 $self->{value} = $val;
187 0 delete $self->{current};
188 }
189 }
190 80 $old;
191 }
192  
193 1 ▶sub check {
Subroutine check: called CC=1 SCAR=0.0
Pod undocumented
194 1 my $self = shift;
195 1 $self->{current} = 1;
196 1 $self->{menu}[1]{seen}++;
197 }
198  
199 2 ▶sub possible_values {
Subroutine possible_values: called CC=1 SCAR=0.0
Pod undocumented
200 2 my $self = shift;
201 2 map $_->{value}, grep !$_->{disabled}, @{ $self->{menu} };
202 }
203  
204 3 ▶sub other_possible_values {
Subroutine other_possible_values: called CC=2 SCAR=6.9
Pod undocumented
205 3 my $self = shift;
206 3 ▶ map $_->{value}, grep !$_->{seen} && !$_->{disabled}, @{ $self->{menu} };
Condition100%3/3
$_->{'seen'} || $_->{'disabled'}
l!l&&r!l&&!r
412
MC/DC100%2/2
$_->{'seen'} || $_->{'disabled'}
$_->{'seen'} $_->{'disabled'}
Truth table100%3/3
A || B
A= $_->{'seen'}B= $_->{'disabled'}
ABresult
1X1
011
000
207 }
208  
209 7 ▶sub value_names {
Subroutine value_names: called CC=3 SCAR=11.0
Pod undocumented
210 7 my $self = shift;
211 7 my @names;
212 7 for ( @{ $self->{menu} } ) {
213 10 my $n = $_->{name};
214 10 ▶ $n = $_->{value} unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 1 9 10
215 10 push( @names, $n );
216 }
217 7 @names;
218 }
219  
220 11 1;
221  
222 __END__
« Form.pm ↑ Summary Form/TextInput.pm »