Expander.pm

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