Upload/File.pm

filter: Statementstmt 87.2% 48 / 55 Branchbran 56.2% 9 / 16 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 72.2% 13 / 18 Podpod 100% 11 / 11 totaltotal 81.5% 93 / 114 CC 13 SCAR 12.8
CC
13
Cov
81%
move_to
12.0
_validate
5.0
new
4.0
CRAP 3.6
↑ Summary Upload.pm »
1 package Uniform::Upload::File;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use File::Copy qw(copy move);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Uniform::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.02';
11  
12 6 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
13 6 my ($class, %args) = @_;
14  
15 my $self = {
16 name => $args{name},
17 filename => $args{filename},
18 tmp_path => $args{tmp_path},
19 size => $args{size} || 0,
20 type => $args{type} || 'application/octet-stream',
21 max_size => $args{max_size},
22 6 ▶ allowed_types => $args{allowed_types} || [],
Condition83%5/6
$args{'size'} || 0
l!l
60
$args{'type'} || 'application/octet-stream'
l!l
42
$args{'allowed_types'} || []
l!l
33
MC/DC0%0/1
$args{'size'} || 0
$args{'size'}
MC/DC100%1/1
$args{'type'} || 'application/octet-stream'
$args{'type'}
MC/DC100%1/1
$args{'allowed_types'} || []
$args{'allowed_types'}
Truth table50%1/2
A
A= $args{'size'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'type'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'allowed_types'}
Aresult
11
00
23 error => undef,
24 };
25  
26 6 bless $self, $class;
27 6 $self->_validate;
28  
29 6 return $self;
30 }
31  
32 0 ▶sub name { $_[0]->{name} }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
33 1 ▶sub filename { $_[0]->{filename} }
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
34 0 ▶sub tmp_path { $_[0]->{tmp_path} }
Subroutine tmp_path: not called CC=1 SCAR=6.9
Pod documented
35 0 ▶sub size { $_[0]->{size} }
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
36 0 ▶sub type { $_[0]->{type} }
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
37 2 ▶sub error { $_[0]->{error} }
Subroutine error: called CC=1 SCAR=0.0
Pod documented
38 4 ▶sub is_valid { !defined $_[0]->{error} }
Subroutine is_valid: called CC=1 SCAR=0.0
Pod documented
39  
40 2 ▶sub sanitized_filename {
Subroutine sanitized_filename: called CC=2 SCAR=7.0
Pod documented
41 2 my ($self) = @_;
42 2 ▶ return '' unless defined $self->{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"filename"} 0 2 2
43  
44 2 my $clean = basename($self->{filename});
45 2 $clean =~ s/[^\w\.\-]/_/g;
46 2 return $clean;
47 }
48  
49 6 ▶sub _validate {
Subroutine _validate: called CC=5 SCAR=16.1
50 6 my ($self) = @_;
51  
52 6 ▶ if (defined $self->{max_size} && $self->{size} > $self->{max_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"max_size"} and $self->{"size"} > $self->{"max_size"}) 1 5 6
Condition100%3/3
defined $self->{"max_size"} and $self->{"size"} > $self->{"max_size"}
!ll&&!rl&&r
321
MC/DC100%2/2
defined $self->{"max_size"} and $self->{"size"} > $self->{"max_size"}
defined $self->{"max_size"} $self->{"size"} > $self->{"max_size"}
Truth table100%3/3
A and B
A= defined $self->{"max_size"}B= $self->{"size"} > $self->{"max_size"}
ABresult
0X0
100
111
53 1 $self->{error} = sprintf("File size (%d bytes) exceeds maximum limit (%d bytes)", $self->{size}, $self->{max_size});
54 1 return;
55 }
56  
57 5 ▶ if (@{ $self->{allowed_types} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"allowed_types"};}) 2 3 5
58 2 my %allowed = map { $_ => 1 } @{ $self->{allowed_types} };
59 2 ▶ unless ($allowed{ $self->{type} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allowed{$self->{"type"}}) 1 1 2
60 1 $self->{error} = sprintf("MIME type '%s' is not allowed", $self->{type});
61 1 return;
62 }
63 }
64 }
65  
66 1 ▶sub copy_to {
Subroutine copy_to: called CC=3 SCAR=11.7
Pod documented
67 1 my ($self, $dest) = @_;
68  
69 croak "Cannot copy invalid file: " . $self->{error}
70 1 ▶ unless $self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid 0 1 1
71  
72 1 ▶ copy($self->{tmp_path}, $dest)
Branch: 1 branch
BranchTrueFalseTotal
copy($self->{"tmp_path"}, $dest) or "Uniform::Exceptions"->throw("Failed to copy upload file to ${dest}: $!") 1 0 1
73 or Uniform::Exceptions->throw("Failed to copy upload file to $dest: $!");
74 }
75  
76 0 ▶sub move_to {
Subroutine move_to: not called CC=3 SCAR=24.8
Pod documented
77 0 my ($self, $dest) = @_;
78  
79 croak "Cannot move invalid file: " . $self->{error}
80 0 ▶ unless $self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid 0 0 0
81  
82 0 ▶ move($self->{tmp_path}, $dest)
Branch: 1 branch
BranchTrueFalseTotal
move($self->{"tmp_path"}, $dest) or "Uniform::Exceptions"->throw("Failed to move upload file to ${dest}: $!") 0 0 0
83 or Uniform::Exceptions->throw("Failed to move upload file to $dest: $!");
84 }
85  
86 4 1;
87  
88 __END__
↑ Summary Upload.pm »