blib/lib/Parse/MIME.pm

filter: Statementstmt 100% 60 / 60 Branchbran 100% 26 / 26 Conditioncond 95.2% 20 / 21 MC/DCmcdc 90.9% 10 / 11 Subroutinesub 100% 15 / 15 Podpod 100% 7 / 7 totaltotal 98.5% 138 / 140 CC 23 SCAR 17.0
CC
23
Cov
99%
fitness_and_quality_parsed
11.0
best_match
6.0
parse_media_range
5.0
CRAP 5.5
↑ Summary
1 1 ▶use 5.006; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Parse::MIME;
4 1 our $VERSION = '1.006';
5  
6 1 ▶BEGIN { require Exporter; *import = \&Exporter::import }
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our @EXPORT_OK = qw(
8 &parse_mime_type &parse_media_range &parse_media_range_list
9 &fitness_and_quality_parsed &quality_parsed &quality
10 &best_match
11 );
12 1 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
13  
14 1 ▶sub _numify($) { no warnings 'numeric'; 0 + shift }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _numify: called CC=1 SCAR=0.0
15  
16 # takes any number of args and returns copies stripped of surrounding whitespace
17 237 ▶sub _strip { s/\A +//, s/ +\z// for my @s = @_; @s[ 0 .. $#s ] }
Subroutine _strip: called CC=2 SCAR=6.9
18  
19 # check whether first two args are equal or one of them is a wildcard
20 119 ▶sub _match { $_[0] eq $_[1] or grep { $_ eq '*' } @_[0,1] }
Subroutine _match: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq $_[1] or grep {$_ eq "*";} @_[0, 1] 57 62 119
21  
22 89 ▶sub parse_mime_type {
Subroutine parse_mime_type: called CC=2 SCAR=6.9
Pod documented
23 89 my ( $mime_type ) = @_;
24  
25 89 my @part = split /;/, $mime_type;
26 89 my $full_type = _strip shift @part;
27 89 my %param = map { _strip split /=/, $_, 2 } @part;
28  
29 # Java URLConnection class sends an Accept header that includes a single "*"
30 # Turn it into a legal wildcard.
31 89 ▶ $full_type = '*/*' if $full_type eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $full_type eq "*" 2 87 89
32  
33 89 my ( $type, $subtype ) = _strip split m!/!, $full_type;
34  
35 89 return ( $type, $subtype, \%param );
36 }
37  
38 87 ▶sub parse_media_range {
Subroutine parse_media_range: called CC=5 SCAR=16.1
Pod documented
39 87 my ( $range ) = @_;
40  
41 87 my ( $type, $subtype, $param ) = parse_mime_type $range;
42  
43 $param->{'q'} = 1
44 unless defined $param->{'q'}
45 and length $param->{'q'}
46 and _numify $param->{'q'} <= 1
47 87 ▶ and _numify $param->{'q'} >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param->{"q"} and length $param->{"q"} and &_numify($param->{"q"}) <= 1 and &_numify($param->{"q"}) >= 0 51 36 87
Condition88%8/9
defined $param->{"q"} and length $param->{"q"} and &_numify($param->{"q"}) <= 1 and &_numify($param->{"q"}) >= 0
!ll&&!rl&&r
51036
defined $param->{"q"} and length $param->{"q"} and &_numify($param->{"q"}) <= 1
!ll&&!rl&&r
50136
defined $param->{"q"} and length $param->{"q"}
!ll&&!rl&&r
48237
MC/DC75%3/4
defined $param->{"q"} and length $param->{"q"} and &_numify($param->{"q"}) <= 1 and &_numify($param->{"q"}) >= 0
defined $param->{"q"} length $param->{"q"} &_numify($param->{"q"}) <= 1 &_numify($param->{"q"}) >= 0
Truth table80%4/5
A and B and C and D
A= defined $param->{"q"}B= length $param->{"q"}C= &_numify($param->{"q"}) <= 1D= &_numify($param->{"q"}) >= 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
48  
49 87 return ( $type, $subtype, $param );
50 }
51  
52 18 ▶sub parse_media_range_list {
Subroutine parse_media_range_list: called CC=1 SCAR=0.0
Pod documented
53 18 my ( $media_range_list ) = @_;
54 18 return map { parse_media_range $_ } split /,/, $media_range_list;
55 }
56  
57 29 ▶sub fitness_and_quality_parsed {
Subroutine fitness_and_quality_parsed: called CC=11 SCAR=24.0
Pod documented
58 29 my ( $mime_type, @parsed_ranges ) = @_;
59  
60 29 my ( $best_fitness, $best_fit_q ) = ( -1, 0 );
61  
62 29 my ( $target_type, $target_subtype, $target_param )
63 = parse_media_range $mime_type;
64  
65 29 ▶ while ( my ( $type, $subtype, $param ) = splice @parsed_ranges, 0, 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@parsed_ranges, 0, 3)) 67 29 96
66  
67 67 ▶ if ( _match( $type, $target_type ) and _match( $subtype, $target_subtype ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_match($type, $target_type) and _match($subtype, $target_subtype)) 42 25 67
Condition100%3/3
_match($type, $target_type) and _match($subtype, $target_subtype)
!ll&&!rl&&r
151042
MC/DC100%2/2
_match($type, $target_type) and _match($subtype, $target_subtype)
_match($type, $target_type) _match($subtype, $target_subtype)
Truth table100%3/3
A and B
A= _match($type, $target_type)B= _match($subtype, $target_subtype)
ABresult
0X0
100
111
68  
69 42 ▶ my $fitness
Branch: 2 branches
BranchTrueFalseTotal
$type eq $target_type ? : 28 14 42
$subtype eq $target_subtype ? : 19 23 42
70 = ( $type eq $target_type ? 100 : 0 )
71 + ( $subtype eq $target_subtype ? 10 : 0 )
72 ;
73  
74 42 ▶ while ( my ( $k, $v ) = each %$param ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$param) 50 42 92
75 ++$fitness
76 if $k ne 'q'
77 and exists $target_param->{ $k }
78 50 ▶ and $target_param->{ $k } eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $k ne "q" and exists $target_param->{$k} and $target_param->{$k} eq $v 2 48 50
Condition100%6/6
$k ne "q" and exists $target_param->{$k} and $target_param->{$k} eq $v
!ll&&!rl&&r
4442
$k ne "q" and exists $target_param->{$k}
!ll&&!rl&&r
4226
MC/DC100%3/3
$k ne "q" and exists $target_param->{$k} and $target_param->{$k} eq $v
$k ne "q" exists $target_param->{$k} $target_param->{$k} eq $v
Truth table100%4/4
A and B and C
A= $k ne "q"B= exists $target_param->{$k}C= $target_param->{$k} eq $v
ABCresult
0XX0
10X0
1100
1111
79 }
80  
81 42 ▶ ( $best_fitness, $best_fit_q ) = ( $fitness, $param->{'q'} )
Branch: 1 branch
BranchTrueFalseTotal
if $fitness > $best_fitness 28 14 42
82 if $fitness > $best_fitness;
83 }
84 }
85  
86 29 return ( $best_fitness, _numify $best_fit_q );
87 }
88  
89 7 ▶sub quality_parsed {
Subroutine quality_parsed: called CC=1 SCAR=0.0
Pod documented
90 7 return +( fitness_and_quality_parsed @_ )[1];
91 }
92  
93 7 ▶sub quality {
Subroutine quality: called CC=1 SCAR=0.0
Pod documented
94 7 my ( $mime_type, $ranges ) = @_;
95 7 my @parsed_range = parse_media_range_list $ranges;
96 7 return quality_parsed $mime_type, @parsed_range;
97 }
98  
99 11 ▶sub best_match {
Subroutine best_match: called CC=6 SCAR=17.9
Pod documented
100 11 my ( $supported, $header ) = @_;
101 11 my @parsed_header = parse_media_range_list $header;
102  
103 # fitness_and_quality_parsed will return fitness -1 on failure,
104 # so we want to start with an invalid value greater than that
105 11 my ( $best_fitness, $best_fit_q, $match ) = ( -.5, 0 );
106  
107 11 for my $type ( @$supported ) {
108 22 my ( $fitness, $fit_q ) = fitness_and_quality_parsed $type, @parsed_header;
109 22 ▶ next if $fitness < $best_fitness;
Branch: 1 branch
BranchTrueFalseTotal
if $fitness < $best_fitness 8 14 22
110 14 ▶ next if $fitness == $best_fitness and $fit_q < $best_fit_q;
Branch: 1 branch
BranchTrueFalseTotal
if $fitness == $best_fitness and $fit_q < $best_fit_q 1 13 14
Condition100%3/3
$fitness == $best_fitness and $fit_q < $best_fit_q
!ll&&!rl&&r
1121
MC/DC100%2/2
$fitness == $best_fitness and $fit_q < $best_fit_q
$fitness == $best_fitness $fit_q < $best_fit_q
Truth table100%3/3
A and B
A= $fitness == $best_fitnessB= $fit_q < $best_fit_q
ABresult
0X0
100
111
111 13 ( $best_fitness, $best_fit_q, $match ) = ( $fitness, $fit_q, $type );
112 }
113  
114 11 ▶ return if not defined $match;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match 1 10 11
115 10 return $match;
116 }
117  
118 __END__
↑ Summary