Simple/TempFile.pm

filter: Statementstmt 100% 70 / 70 Branchbran 80.0% 24 / 30 Conditioncond 92.8% 13 / 14 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 93.2% 124 / 133 CC 25 SCAR 29.4
CC
25
Cov
94%
create_temp_file
25.3
BEGIN
1.0
BEGIN
1.0
CRAP 18.8
« Simple/Utils.pm ↑ Summary Simple/Helpers.pm »
1 package CLI::Simple::TempFile;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 18 my %GENERATED_ACCESSORS;
12  
13 18 our $VERSION = '2.6.0';
14  
15 ########################################################################
16 16 ▶sub create_temp_file {
Subroutine create_temp_file: called CC=25 SCAR=32.3
Pod documented
17 ########################################################################
18 16 my ( $self, %args ) = @_;
19  
20 16 ▶ croak 'create_temp_file requires an object instance'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 16 16
21 if !blessed($self);
22  
23 16 ▶ croak 'create_temp_file requires a hash-based object'
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($self) ne "HASH" 0 16 16
24 if reftype($self) ne 'HASH';
25  
26 16 ▶ my $no_accessors = delete $args{no_accessors} // 0;
Condition100%2/2
delete $args{"no_accessors"} // 0
l!l
313
MC/DC100%1/1
delete $args{"no_accessors"} // 0
delete $args{"no_accessors"}
Truth table100%2/2
A
A= delete $args{"no_accessors"}
Aresult
11
00
27 16 ▶ my $cleanup = delete $args{cleanup} // 1;
Condition100%2/2
delete $args{"cleanup"} // 1
l!l
313
MC/DC100%1/1
delete $args{"cleanup"} // 1
delete $args{"cleanup"}
Truth table100%2/2
A
A= delete $args{"cleanup"}
Aresult
11
00
28 16 my $has_accessor = exists $args{accessor};
29 16 my $accessor = delete $args{accessor};
30  
31 16 ▶ croak 'accessor and no_accessors cannot be combined'
Branch: 1 branch
BranchTrueFalseTotal
if $no_accessors and $has_accessor 1 15 16
Condition100%3/3
$no_accessors and $has_accessor
!ll&&!rl&&r
1321
MC/DC100%2/2
$no_accessors and $has_accessor
$no_accessors $has_accessor
Truth table100%3/3
A and B
A= $no_accessorsB= $has_accessor
ABresult
0X0
100
111
32 if $no_accessors && $has_accessor;
33  
34 15 ▶ $accessor //= 'temp_file';
Condition100%2/2
$accessor //= "temp_file"
l!l
114
MC/DC100%1/1
$accessor //= "temp_file"
$accessor
Truth table100%2/2
A
A= $accessor
Aresult
11
00
35  
36 15 ▶ croak 'cleanup must be 0 or 1'
Branch: 1 branch
BranchTrueFalseTotal
unless $cleanup =~ /\A[01]\z/ 1 14 15
37 if $cleanup !~ /\A[01]\z/;
38  
39 14 ▶ croak 'no_accessors must be 0 or 1'
Branch: 1 branch
BranchTrueFalseTotal
unless $no_accessors =~ /\A[01]\z/ 0 14 14
40 if $no_accessors !~ /\A[01]\z/;
41  
42 14 my %supported = map { $_ => 1 } qw(dir suffix template);
43 14 foreach my $key ( keys %args ) {
44 croak sprintf q{Unknown temporary file attribute: %s}, $key
45 3 ▶ if !exists $supported{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $supported{$key} 1 2 3
46 }
47  
48 13 my $class = ref $self;
49 13 ▶ if ( !$no_accessors ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_accessors) 11 2 13
50 11 ▶ croak sprintf q{Invalid accessor name: %s}, $accessor
Branch: 1 branch
BranchTrueFalseTotal
unless $accessor =~ /\A[a-zA-Z_]\w*\z/ 1 10 11
51 if $accessor !~ /\A[a-zA-Z_]\w*\z/;
52  
53 10 foreach my $prefix (qw(get set)) {
54 20 my $method = $prefix . '_' . $accessor;
55 croak sprintf q{Accessor already exists: %s}, $method
56 20 ▶ if $self->can($method) && !$GENERATED_ACCESSORS{$class}{$method};
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) and not $GENERATED_ACCESSORS{$class}{$method} 0 20 20
Condition66%2/3
$self->can($method) and not $GENERATED_ACCESSORS{$class}{$method}
!ll&&!rl&&r
1460
MC/DC0%0/2
$self->can($method) and not $GENERATED_ACCESSORS{$class}{$method}
$self->can($method) not $GENERATED_ACCESSORS{$class}{$method}
Truth table66%2/3
A and not(B)
A= $self->can($method)B= not $GENERATED_ACCESSORS{$class}{$method}
ABresult
0X0
100
111
57 }
58 }
59  
60 12 my %file_args = ( UNLINK => $cleanup );
61 12 ▶ $file_args{DIR} = $args{dir} if exists $args{dir};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"dir"} 0 12 12
62 12 ▶ $file_args{SUFFIX} = $args{suffix} if exists $args{suffix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"suffix"} 2 10 12
63 12 ▶ $file_args{TEMPLATE} = $args{template} if exists $args{template};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"template"} 0 12 12
64  
65 12 my $file = File::Temp->new(%file_args);
66 12 my $path = $file->filename;
67  
68 # Retain the File::Temp object, not merely its path. The accessor can
69 # later change without affecting which file is cleaned up.
70 12 ▶ $self->{_cli_simple_temp_files} //= [];
Condition100%2/2
$self->{"_cli_simple_temp_files"} //= []
l!l
39
MC/DC100%1/1
$self->{"_cli_simple_temp_files"} //= []
$self->{"_cli_simple_temp_files"}
Truth table100%2/2
A
A= $self->{"_cli_simple_temp_files"}
Aresult
11
00
71  
72 12 push @{ $self->{_cli_simple_temp_files} }, $file;
73  
74 12 ▶ if ( !$no_accessors ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_accessors) 10 2 12
75 10 foreach my $prefix (qw(get set)) {
76 20 my $method = $prefix . '_' . $accessor;
77 20 ▶ if ( !$GENERATED_ACCESSORS{$class}{$method} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($GENERATED_ACCESSORS{$class}{$method}) 14 6 20
78 18 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
79 14 ▶ if ( $prefix eq 'get' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix eq 'get') { } 7 7 14
80 7 ▶ *{"${class}::${method}"} = sub { return $_[0]->{$accessor}; };
Subroutine __ANON__: called CC=1 SCAR=0.0
81 }
82 else {
83 7 ▶ *{"${class}::${method}"} = sub { $_[0]->{$accessor} = $_[1]; return $_[0]->{$accessor}; };
Subroutine __ANON__: called CC=1 SCAR=0.0
84 }
85 14 $GENERATED_ACCESSORS{$class}{$method} = 1;
86 }
87 }
88 10 my $setter = 'set_' . $accessor;
89 10 $self->$setter($path);
90 }
91  
92 12 return $path;
93 }
94  
95 18 1;
96  
97 __END__
« Simple/Utils.pm ↑ Summary Simple/Helpers.pm »