Expander.pm

filter: Statementstmt 100% 161 / 161 Branchbran 89.4% 68 / 76 Conditioncond 71.7% 33 / 46 MC/DCmcdc 51.8% 14 / 27 Subroutinesub 100% 25 / 25 Podpod 90.9% 10 / 11 totaltotal 89.8% 311 / 346 CC 60 SCAR 19.0
CC
60
Cov
93%
walk
13.1
guess_type_of
10.2
plugin_for
8.0
CRAP 6.7
↑ Summary Expander/Plugin/ApplicationZip.pm »
1 package MIME::Expander;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 $VERSION = '0.02';
7  
8 3 ▶use vars qw($PrefixGuess $PrefixPlugin @DefaultGuesser @EnabledPlugins);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 $PrefixGuess = 'MIME::Expander::Guess';
11 3 $PrefixPlugin = 'MIME::Expander::Plugin';
12 3 @DefaultGuesser = ('MMagic', 'FileName');
13 3 @EnabledPlugins = ();
14 }
15  
16 3 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Email::MIME::ContentType ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use MIME::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Module::Pluggable search_path => $PrefixPlugin, sub_name => 'expanders';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use MIME::Expander::Plugin::MessageRFC822;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 3 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
25 3 my $class = shift;
26 3 @EnabledPlugins = @_;
27 }
28  
29 349 ▶sub regulate_type {
Subroutine regulate_type: called CC=5 SCAR=16.3
Pod documented
30 349 ▶ return undef unless( defined $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 48 301 349
31 301 my $type = $_[1];
32  
33 # There is regexp from Email::MIME::ContentType 1.015
34 301 my $tspecials = quotemeta '()<>@,;:\\"/[]?=';
35 301 my $discrete = qr/[^$tspecials]+/;
36 301 my $composite = qr/[^$tspecials]+/;
37 301 my $params = qr/;.*/;
38 301 ▶ return undef unless( $type =~ m[ ^ ($discrete) / ($composite) \s* ($params)? $ ]x );
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ m[ ^ ($discrete) / ($composite) \s* ($params)? $ ]x 2 299 301
39  
40 299 my $ct = Email::MIME::ContentType::parse_content_type($type);
41 299 ▶ return undef if( ! $ct->{discrete} or ! $ct->{composite} );
Branch: 1 branch
BranchTrueFalseTotal
unless $ct->{"discrete"} and $ct->{"composite"} 0 299 299
Condition33%1/3
$ct->{"discrete"} and $ct->{"composite"}
!ll&&!rl&&r
00299
MC/DC0%0/2
$ct->{"discrete"} and $ct->{"composite"}
$ct->{"discrete"} $ct->{"composite"}
Truth table33%1/3
A and B
A= $ct->{"discrete"}B= $ct->{"composite"}
ABresult
0X0
100
111
42 299 return MIME::Type->simplified(join('/',$ct->{discrete}, $ct->{composite}));
43 }
44  
45 18 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
46 18 my $class = shift;
47 18 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0180
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
48 18 my $self = {
49 expects => [],
50 guesser => undef,
51 depth => undef,
52 };
53 18 bless $self, $class;
54 18 return $self->init(@_);
55 }
56  
57 22 ▶sub init {
Subroutine init: called CC=6 SCAR=18.0
Pod documented
58 22 my $self = shift;
59 22 my $args;
60 22 ▶ if( 0 == @_ % 2 ){
Branch: 1 branch
BranchTrueFalseTotal
if (0 == @_ % 2) { } 19 3 22
61 19 $args = { @_ }
62 }else{
63 3 ▶ $args = shift || {};
Condition50%1/2
shift() || {}
l!l
30
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
64 }
65  
66 $self->expects(
67 22 ▶ exists $args->{expects} ? $args->{expects} : [] );
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'expects'} ? : 3 19 22
68  
69 $self->guesser(
70 22 ▶ exists $args->{guesser} ? $args->{guesser} : undef );
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'guesser'} ? : 0 22 22
71  
72 $self->depth(
73 22 ▶ exists $args->{depth} ? $args->{depth} : undef );
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'depth'} ? : 3 19 22
74  
75 22 return $self;
76 }
77  
78 144 ▶sub expects {
Subroutine expects: called CC=4 SCAR=13.9
Pod documented
79 144 my $self = shift;
80 144 ▶ if( @_ ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 27 117 144
81 27 $self->{expects} = shift;
82 die "setting value is not acceptable, it requires an reference of ARRAY"
83 27 ▶ if( defined $self->{expects} and ref($self->{expects}) ne 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"expects"} and ref $self->{"expects"} ne "ARRAY" 1 26 27
Condition100%3/3
defined $self->{"expects"} and ref $self->{"expects"} ne "ARRAY"
!ll&&!rl&&r
2241
MC/DC100%2/2
defined $self->{"expects"} and ref $self->{"expects"} ne "ARRAY"
defined $self->{"expects"} ref $self->{"expects"} ne "ARRAY"
Truth table100%3/3
A and B
A= defined $self->{"expects"}B= ref $self->{"expects"} ne "ARRAY"
ABresult
0X0
100
111
84 }
85 143 return $self->{expects};
86 }
87  
88 61 ▶sub is_expected {
Subroutine is_expected: called CC=7 SCAR=19.5
Pod documented
89 61 my $self = shift;
90 61 ▶ my $type = shift or undef;
Branch: 1 branch
BranchTrueFalseTotal
my $type = shift() or undef 61 0 61
91 61 ▶ die "invalid type $type that has not looks as mime/type"
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ m[^.+/.+$] 1 60 61
92 if( $type !~ m,^.+/.+$, );
93 60 ▶ return () unless( $self->expects );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->expects 5 55 60
94 55 ▶ for my $regexp ( map { ref $_ ? $_ : qr/$_/ } @{$self->expects} ){
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 5 33 38
95 33 ▶ return 1 if( $type =~ $regexp );
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /$regexp/ 9 24 33
96 }
97 46 return ();
98 }
99  
100 57 ▶sub depth {
Subroutine depth: called CC=4 SCAR=13.9
Pod documented
101 57 my $self = shift;
102 57 ▶ if( @_ ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 26 31 57
103 26 $self->{depth} = shift;
104 die "setting value is not acceptable, it requires a native number"
105 26 ▶ if( defined $self->{depth} and $self->{depth} =~ /\D/ );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"depth"} and $self->{"depth"} =~ /\D/ 1 25 26
Condition100%3/3
defined $self->{"depth"} and $self->{"depth"} =~ /\D/
!ll&&!rl&&r
2141
MC/DC100%2/2
defined $self->{"depth"} and $self->{"depth"} =~ /\D/
defined $self->{"depth"} $self->{"depth"} =~ /\D/
Truth table100%3/3
A and B
A= defined $self->{"depth"}B= $self->{"depth"} =~ /\D/
ABresult
0X0
100
111
106 }
107 56 return $self->{depth};
108 }
109  
110 83 ▶sub guesser {
Subroutine guesser: called CC=5 SCAR=16.1
Pod documented
111 83 my $self = shift;
112 83 ▶ if( @_ ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 27 56 83
113 27 $self->{guesser} = shift;
114 die "setting value is not acceptable, it requires an reference of CODE or ARRAY"
115 if( defined $self->{guesser}
116 and ref($self->{guesser}) ne 'CODE'
117 27 ▶ and ref($self->{guesser}) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"guesser"} and ref $self->{"guesser"} ne "CODE" and ref $self->{"guesser"} ne "ARRAY" 1 26 27
Condition100%6/6
defined $self->{"guesser"} and ref $self->{"guesser"} ne "CODE" and ref $self->{"guesser"} ne "ARRAY"
!ll&&!rl&&r
2421
defined $self->{"guesser"} and ref $self->{"guesser"} ne "CODE"
!ll&&!rl&&r
2223
MC/DC100%3/3
defined $self->{"guesser"} and ref $self->{"guesser"} ne "CODE" and ref $self->{"guesser"} ne "ARRAY"
defined $self->{"guesser"} ref $self->{"guesser"} ne "CODE" ref $self->{"guesser"} ne "ARRAY"
Truth table100%4/4
A and B and C
A= defined $self->{"guesser"}B= ref $self->{"guesser"} ne "CODE"C= ref $self->{"guesser"} ne "ARRAY"
ABCresult
0XX0
10X0
1100
1111
118 }
119 82 return $self->{guesser};
120 }
121  
122 51 ▶sub guess_type_of {
Subroutine guess_type_of: called CC=10 SCAR=23.2
Pod documented
123 51 my $self = shift;
124 51 ▶ my $ref_data = shift or die "missing mandatory parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $ref_data = shift() or die "missing mandatory parameter" 51 0 51
125 51 ▶ my $info = shift || {};
Condition50%1/2
shift() || {}
l!l
510
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
126
127 51 my $type = undef;
128 51 my $routine = $self->guesser;
129  
130 51 ▶ if( ref $routine eq 'CODE' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $routine eq 'CODE') { } 2 49 51
131 2 $type = $self->guesser->($ref_data, $info);
132  
133 }else{
134 49 my @routines;
135 49 ▶ if( ref $routine eq 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $routine eq 'ARRAY') { } 2 47 49
136 2 @routines = @$routine;
137 }else{
138 47 @routines = @DefaultGuesser;
139 }
140 49 for my $klass ( @routines ){
141 50 ▶ $klass = join('::', $PrefixGuess, $klass) if( $klass !~ /:/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $klass =~ /:/ 50 0 50
142 50 Module::Load::load $klass;
143 50 $type = $self->regulate_type( $klass->type($ref_data, $info) );
144 50 ▶ last if( $type and $type ne 'application/octet-stream');
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type ne "application/octet-stream" 49 1 50
Condition66%2/3
$type and $type ne "application/octet-stream"
!ll&&!rl&&r
1049
MC/DC50%1/2
$type and $type ne "application/octet-stream"
$type $type ne "application/octet-stream"
Truth table66%2/3
A and B
A= $typeB= $type ne "application/octet-stream"
ABresult
0X0
100
111
145 }
146 }
147 51 ▶ return ($type || 'application/octet-stream');
Condition100%2/2
$type || 'application/octet-stream'
l!l
501
MC/DC100%1/1
$type || 'application/octet-stream'
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
148 }
149  
150 50 ▶sub plugin_for {
Subroutine plugin_for: called CC=8 SCAR=20.8
Pod documented
151 50 my $self = shift;
152 50 my $type = shift;
153  
154 50 my $plugin = undef;
155 50 for my $available ( $self->expanders ){
156  
157 203 my $klass = undef;
158 203 ▶ unless( @EnabledPlugins ){
Branch: 1 branch
BranchTrueFalseTotal
if (@MIME::Expander::EnabledPlugins) { } 8 195 203
159 195 $klass = $available;
160 }else{
161 8 for my $enable ( @EnabledPlugins ){
162 8 ▶ $enable = join('::', $PrefixPlugin, $enable)
Branch: 1 branch
BranchTrueFalseTotal
unless $enable =~ /:/ 1 7 8
163 if( $enable !~ /:/ );
164 8 ▶ if( $available eq $enable ){
Branch: 1 branch
BranchTrueFalseTotal
if ($available eq $enable) 2 6 8
165 2 $klass = $available;
166 2 last;
167 }
168 }
169 8 ▶ next unless( $klass );
Branch: 1 branch
BranchTrueFalseTotal
unless $klass 6 2 8
170 }
171
172 197 Module::Load::load $klass;
173 197 ▶ if( $klass->is_acceptable( $self->regulate_type($type) ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($klass->is_acceptable($self->regulate_type($type))) 24 173 197
174 24 $plugin = $klass->new;
175 24 last;
176 }
177 }
178 50 return $plugin;
179 }
180  
181 45 ▶sub create_media {
Subroutine create_media: called CC=6 SCAR=18.8
Pod documented
182 45 my $self = shift;
183 45 ▶ my $ref_data = shift or die "missing mandatory parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $ref_data = shift() or die "missing mandatory parameter" 45 0 45
184 45 ▶ my $info = shift || {};
Condition50%1/2
shift() || {}
l!l
450
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
185  
186 45 my $type = $self->regulate_type($info->{content_type});
187 45 ▶ if( ! $type or $type eq 'application/octet-stream' ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $type or $type eq "application/octet-stream") 45 0 45
Condition33%1/3
not $type or $type eq "application/octet-stream"
l!l&&r!l&&!r
4500
MC/DC0%0/2
not $type or $type eq "application/octet-stream"
not $type $type eq "application/octet-stream"
Truth table33%1/3
not(A) or B
A= not $typeB= $type eq "application/octet-stream"
ABresult
1X1
011
000
188 45 $type = $self->guess_type_of($ref_data, $info);
189 }
190
191 45 ▶ if( MIME::Expander::Plugin::MessageRFC822->is_acceptable(
Branch: 1 branch
BranchTrueFalseTotal
if ('MIME::Expander::Plugin::MessageRFC822'->is_acceptable($self->regulate_type($type))) { } 3 42 45
192 $self->regulate_type($type)
193 )){
194 3 return Email::MIME->new($ref_data);
195 }else{
196 return Email::MIME->create(
197 attributes => {
198 content_type => $type,
199 encoding => 'binary',
200 filename => $info->{filename},
201 },
202 42 body => $ref_data,
203 );
204 }
205 }
206  
207 9 ▶sub walk {
Subroutine walk: called CC=13 SCAR=25.7
Pod documented
208 9 my $self = shift;
209 9 my $data = shift;
210 9 my $callback = shift;
211 9 ▶ my $info = shift || {};
Condition50%1/2
shift() || {}
l!l
09
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
212 9 my $c = 0;
213  
214 9 my @medias = ();
215 9 ▶ if( blessed($data) and $data->isa('Email::Simple') ){
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data) and $data->isa('Email::Simple')) { } 1 8 9
Condition66%2/3
builtin::blessed($data) and $data->isa("Email::Simple")
!ll&&!rl&&r
801
MC/DC50%1/2
builtin::blessed($data) and $data->isa("Email::Simple")
builtin::blessed($data) $data->isa("Email::Simple")
Truth table66%2/3
A and B
A= builtin::blessed($data)B= $data->isa("Email::Simple")
ABresult
0X0
100
111
216 1 push @medias, $data;
217 }else{
218 8 ▶ @medias = ($self->create_media(
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'SCALAR' ? : 4 4 8
219 ref $data eq 'SCALAR' ? $data : \$data,
220 $info));
221 }
222  
223 # reset vars for depth option
224 9 my $ptr = 0;
225 9 my $limit = 0;
226 9 my $level = 1;
227 9 my $bound = scalar @medias;
228
229 # when expandable contents, then append it to @medias
230 9 ▶ while( my $media = shift @medias ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $media = shift @medias) 46 9 55
231 46 my $type = $media->content_type;
232 46 my $plugin = $self->plugin_for($type);
233 46 ▶ if( $limit or $self->is_expected( $type ) or ! $plugin ){
Branch: 1 branch
BranchTrueFalseTotal
if ($limit or $self->is_expected($type) or not $plugin) { } 28 18 46
Condition100%6/6
$limit or $self->is_expected($type) or not $plugin
l!l&&r!l&&!r
42418
$limit or $self->is_expected($type)
l!l&&r!l&&!r
1342
MC/DC100%3/3
$limit or $self->is_expected($type) or not $plugin
$limit $self->is_expected($type) not $plugin
Truth table100%4/4
A or B or not(C)
A= $limitB= $self->is_expected($type)C= not $plugin
ABCresult
1XX1
01X1
0011
0000
234 # expected or un-expandable data
235 28 ▶ $callback->($media) if( ref $callback eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback eq "CODE" 28 0 28
236 28 ++$c;
237 }else{
238 # expand more
239 37 ▶ $plugin->expand( $media , sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
240 37 push @medias, $self->create_media( @_ );
241 18 });
242 }
243  
244 46 ++$ptr;
245 46 ▶ if( $bound <= $ptr ){
Branch: 1 branch
BranchTrueFalseTotal
if ($bound <= $ptr) 22 24 46
246 22 ▶ if( $self->depth and $self->depth <= $level ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->depth and $self->depth <= $level) 2 20 22
Condition66%2/3
$self->depth and $self->depth <= $level
!ll&&!rl&&r
2002
MC/DC50%1/2
$self->depth and $self->depth <= $level
$self->depth $self->depth <= $level
Truth table66%2/3
A and B
A= $self->depthB= $self->depth <= $level
ABresult
0X0
100
111
247 2 $limit = 1;
248 }
249 22 $bound += scalar @medias;
250 22 ++$level;
251 }
252 }
253
254 9 return $c;
255 }
256  
257  
258 3 1;
259 __END__
↑ Summary Expander/Plugin/ApplicationZip.pm »