FormHandler/Field/Upload.pm

filter: Statementstmt 39.1% 18 / 46 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 3 / 6 Podpod 33.3% 1 / 3 totaltotal 22.2% 22 / 99 CC 17 SCAR 43.0
CC
17
Cov
22%
validate
90.0
is_real_fh
90.0
get_class_messages
2.0
CRAP 73.9
« FormHandler/Model/CDBI.pm ↑ Summary FormHandler/Widget/Form/Role/HTMLAttributes.pm »
1 package HTML::FormHandler::Field::Upload;
2 # ABSTRACT: file upload field
3 1 $HTML::FormHandler::Field::Upload::VERSION = '0.410002';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 extends 'HTML::FormHandler::Field';
8  
9  
10 1 has '+widget' => ( default => 'Upload', );
11 1 has min_size => ( is => 'rw', isa => 'Maybe[Int]', default => 1 );
12 1 has max_size => ( is => 'rw', isa => 'Maybe[Int]', default => 1048576 );
13 1 has '+type_attr' => ( default => 'file' );
14  
15 1 our $class_messages = {
16 'upload_file_not_found' => 'File not found for upload field',
17 'upload_file_empty' => 'File uploaded is empty',
18 'upload_file_too_small' => 'File is too small (< [_1] bytes)',
19 'upload_file_too_big' => 'File is too big (> [_1] bytes)',
20 };
21 0 ▶sub get_class_messages {
Subroutine get_class_messages: not called CC=1 SCAR=6.9
Pod undocumented
22 0 my $self = shift;
23 return {
24 0 %{ $self->next::method },
25 %$class_messages,
26 }
27 }
28  
29 0 ▶sub validate {
Subroutine validate: not called CC=9 SCAR=45.0
Pod documented
30 0 my $self = shift;
31  
32 0 my $upload = $self->value;
33 0 my $size = 0;
34 0 ▶ if( blessed $upload && $upload->can('size') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($upload) and $upload->can('size')) { } 0 0 0
elsif (is_real_fh($upload)) { } 0 0 0
Condition0%0/3
builtin::blessed($upload) and $upload->can("size")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($upload) and $upload->can("size")
builtin::blessed($upload) $upload->can("size")
Truth table0%0/3
A and B
A= builtin::blessed($upload)B= $upload->can("size")
ABresult
0X0
100
111
35 0 $size = $upload->size;
36 }
37 elsif( is_real_fh( $upload ) ) {
38 0 $size = -s $upload;
39 }
40 else {
41 0 return $self->add_error($self->get_message('upload_file_not_found'));
42 }
43 0 ▶ return $self->add_error($self->get_message('upload_file_empty'))
Branch: 1 branch
BranchTrueFalseTotal
unless $size > 0 0 0 0
44 unless $size > 0;
45  
46 0 ▶ if( defined $self->min_size && $size < $self->min_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->min_size and $size < $self->min_size) 0 0 0
Condition0%0/3
defined $self->min_size and $size < $self->min_size
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->min_size and $size < $self->min_size
defined $self->min_size $size < $self->min_size
Truth table0%0/3
A and B
A= defined $self->min_sizeB= $size < $self->min_size
ABresult
0X0
100
111
47 0 $self->add_error( $self->get_message('upload_file_too_small'), $self->min_size );
48 }
49  
50 0 ▶ if( defined $self->max_size && $size > $self->max_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->max_size and $size > $self->max_size) 0 0 0
Condition0%0/3
defined $self->max_size and $size > $self->max_size
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->max_size and $size > $self->max_size
defined $self->max_size $size > $self->max_size
Truth table0%0/3
A and B
A= defined $self->max_sizeB= $size > $self->max_size
ABresult
0X0
100
111
51 0 $self->add_error( $self->get_message('upload_file_too_big'), $self->max_size );
52 }
53 0 return;
54 }
55  
56 # stolen from Plack::Util::is_real_fh
57 0 ▶sub is_real_fh {
Subroutine is_real_fh: not called CC=9 SCAR=45.0
Pod undocumented
58 0 my $fh = shift;
59  
60 0 ▶ my $reftype = Scalar::Util::reftype($fh) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $reftype = builtin::reftype($fh) or return 0 0 0
61 0 ▶ if( $reftype eq 'IO'
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq 'IO' or $reftype eq 'GLOB' and *{$fh;}{'IO'}) { } 0 0 0
Condition0%0/5
$reftype eq "IO" or $reftype eq "GLOB" and *{$fh;}{"IO"}
l!l&&r!l&&!r
000
$reftype eq "GLOB" and *{$fh;}{"IO"}
!ll
00
MC/DC0%0/2
$reftype eq "IO" or $reftype eq "GLOB" and *{$fh;}{"IO"}
$reftype eq "IO" $reftype eq "GLOB"
Truth table0%0/3
A or B
A= $reftype eq "IO"B= $reftype eq "GLOB"
ABresult
1X1
011
000
62 0 or $reftype eq 'GLOB' && *{$fh}{IO} ){
63 0 my $m_fileno = $fh->fileno;
64 0 ▶ return unless defined $m_fileno;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m_fileno 0 0 0
65 0 ▶ return unless $m_fileno >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $m_fileno >= 0 0 0 0
66 0 my $f_fileno = fileno($fh);
67 0 ▶ return unless defined $f_fileno;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f_fileno 0 0 0
68 0 ▶ return unless $f_fileno >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $f_fileno >= 0 0 0 0
69 0 return 1;
70 }
71 else {
72 0 return;
73 }
74 }
75  
76 1 __PACKAGE__->meta->make_immutable;
77 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 1;
79  
80 __END__
« FormHandler/Model/CDBI.pm ↑ Summary FormHandler/Widget/Form/Role/HTMLAttributes.pm »