MIME/Type.pm

filter: Statementstmt 91.8% 56 / 61 Branchbran 66.6% 16 / 24 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 88.0% 22 / 25 Podpod 90.4% 19 / 21 totaltotal 84.5% 120 / 142 CC 16 SCAR 11.7
CC
16
Cov
84%
init
8.3
simplified
4.4
defaultCharset
3.2
CRAP 3.2
« MojoX/MIME/Types.pm ↑ Summary
1 # This code is part of Perl distribution MIME-Types version 2.30.
2 # The POD got stripped from this file by OODoc version 3.05.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 1999-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package MIME::Type;{
13 6 our $VERSION = '2.30';
14 }
15  
16  
17 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #--------------------
23  
24 #--------------------
25  
26 use overload
27 6 ▶ '""' => 'type',
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 cmp => 'cmp';
29  
30 #--------------------
31  
32 2207 ▶sub new(@) { (bless {}, shift)->init( {@_} ) }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
33  
34 sub init($)
35 2207 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=8 SCAR=21.2
Pod undocumented
36  
37 my $type = $self->{MT_type} = $args->{type}
38 2207 ▶ or croak "ERROR: Type parameter is obligatory.";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->{"MT_type"} = $args->{"type"} or croak("ERROR: Type parameter is obligatory.") 2207 0 2207
39  
40 2207 ▶ $self->{MT_simplified} = $args->{simplified} || $self->simplified($type);
Condition33%1/3
$args->{'simplified'} || $self->simplified($type)
l!l&&r!l&&!r
022070
MC/DC0%0/2
$args->{'simplified'} || $self->simplified($type)
$args->{'simplified'} $self->simplified($type)
Truth table33%1/3
A || B
A= $args->{'simplified'}B= $self->simplified($type)
ABresult
1X1
011
000
41 2207 ▶ $self->{MT_extensions} = $args->{extensions} || [];
Condition100%2/2
$args->{'extensions'} || []
l!l
219413
MC/DC100%1/1
$args->{'extensions'} || []
$args->{'extensions'}
Truth table100%2/2
A
A= $args->{'extensions'}
Aresult
11
00
42  
43 $self->{MT_encoding}
44 = $args->{encoding} ? $args->{encoding}
45 2207 ▶ : $self->mediaType eq 'text' ? 'quoted-printable'
Branch: 2 branches
BranchTrueFalseTotal
$args->{'encoding'} ? : 102 2105 2207
$self->mediaType eq 'text' ? : 226 1879 2105
46 : 'base64';
47  
48 2207 ▶ $self->{MT_system} = $args->{system} if defined $args->{system};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"system"} 0 2207 2207
49 2207 ▶ $self->{MT_charset} = $args->{charset} if defined $args->{charset};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"charset"} 2193 14 2207
50 2207 $self;
51 }
52  
53 #--------------------
54  
55 15134 ▶sub type() { $_[0]->{MT_type} }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
56  
57  
58 sub simplified(;$)
59 4417 ▶{ my $thing = shift;
Subroutine simplified: called CC=4 SCAR=14.9
Pod documented
60 4417 ▶ @_ or return $thing->{MT_simplified};
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $thing->{"MT_simplified"} 2239 2178 4417
61  
62 2239 my $mime = shift;
63  
64 2239 ▶ $mime =~ m!^\s*(?:x\-)?([\w.+-]+)/(?:x\-)?([\w.+-]+)\s*$!i ? lc "$1/$2"
Branch: 2 branches
BranchTrueFalseTotal
$mime =~ m[^\s*(?:x\-)?([\w.+-]+)/(?:x\-)?([\w.+-]+)\s*$]i ? : 2239 0 2239
$mime eq 'text' ? : 0 0 0
65 : $mime eq 'text' ? 'text/plain' # some silly mailers...
66 : $mime; # doesn't follow rules, f.i. one word
67 }
68  
69  
70 4356 ▶sub extensions() { @{$_[0]->{MT_extensions}} }
Subroutine extensions: called CC=1 SCAR=0.0
Pod documented
71 4207 ▶sub encoding() { $_[0]->{MT_encoding} }
Subroutine encoding: called CC=1 SCAR=0.0
Pod documented
72 0 ▶sub system() { $_[0]->{MT_system} }
Subroutine system: not called CC=1 SCAR=6.9
Pod documented
73  
74  
75 0 ▶sub charset() { $_[0]->{MT_charset} }
Subroutine charset: not called CC=1 SCAR=6.9
Pod documented
76  
77 #--------------------
78  
79 2107 ▶sub mediaType() { $_[0]->simplified =~ m!^([\w.-]+)/! ? $1 : undef }
Subroutine mediaType: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->simplified =~ m[^([\w.-]+)/] ? : 2107 0 2107
80 2 ▶sub mainType() { $_[0]->mediaType } # Backwards compatibility
Subroutine mainType: called CC=1 SCAR=0.0
Pod undocumented
81  
82  
83 4 ▶sub subType() { $_[0]->simplified =~ m!/([\w+.-]+)$! ? $1 : undef }
Subroutine subType: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->simplified =~ m[/([\w+.-]+)$] ? : 4 0 4
84  
85  
86 8 ▶sub isRegistered() { lc($_[0]->type) !~ m{^x\-|/x\-} }
Subroutine isRegistered: called CC=1 SCAR=0.0
Pod documented
87  
88  
89 # http://tools.ietf.org/html/rfc4288#section-3
90 4 ▶sub isVendor() { $_[0]->simplified =~ m!/vnd\.! }
Subroutine isVendor: called CC=1 SCAR=0.0
Pod documented
91 4 ▶sub isPersonal() { $_[0]->simplified =~ m!/prs\.! }
Subroutine isPersonal: called CC=1 SCAR=0.0
Pod documented
92 4 ▶sub isExperimental() { $_[0]->simplified =~ m!/x\.! }
Subroutine isExperimental: called CC=1 SCAR=0.0
Pod documented
93  
94  
95 4 ▶sub isBinary() { $_[0]->encoding eq 'base64' }
Subroutine isBinary: called CC=1 SCAR=0.0
Pod documented
96 4 ▶sub isText() { $_[0]->encoding ne 'base64' }
Subroutine isText: called CC=1 SCAR=0.0
Pod documented
97 6 *isAscii = \&isText;
98  
99  
100 # simplified names only!
101 6 my %sigs = map +($_ => 1),
102 qw(application/pgp-keys application/pgp application/pgp-signature
103 application/pkcs10 application/pkcs7-mime application/pkcs7-signature
104 text/vCard);
105  
106 0 ▶sub isSignature() { $sigs{$_[0]->simplified} }
Subroutine isSignature: not called CC=1 SCAR=6.9
Pod documented
107  
108  
109 sub cmp($)
110 37 ▶{ my ($self, $other) = @_;
Subroutine cmp: called CC=2 SCAR=6.9
Pod documented
111 37 ▶ my $type = ref $other ? $other->simplified : (ref $self)->simplified($other);
Branch: 1 branch
BranchTrueFalseTotal
ref $other ? : 7 30 37
112 37 $self->simplified cmp $type;
113 }
114  
115 19 ▶sub equals($) { $_[0]->cmp($_[1])==0 }
Subroutine equals: called CC=1 SCAR=0.0
Pod documented
116  
117  
118 6 my %ctext;
119 6 $ctext{$_} = 'US-ASCII' for qw/plain cql cql-expression cql-identifier css directory dns encaprtp enriched/;
120 6 $ctext{$_} = 'UTF-8' for qw/cache-manifest calendar csv csv-schema ecmascript/;
121 6 $ctext{$_} = '_REQUIRED' for qw//;
122  
123 sub defaultCharset()
124 2 ▶{ my $self = shift;
Subroutine defaultCharset: called CC=3 SCAR=11.6
Pod documented
125 2 my $st = (lc $self->subType) =~ s/^x-//r;
126 2 ▶ my $default = $ctext{$st} // return undef;
Condition100%2/2
$ctext{$st} // (return undef)
l!l
11
MC/DC100%1/1
$ctext{$st} // (return undef)
$ctext{$st}
Truth table100%2/2
A
A= $ctext{$st}
Aresult
11
00
127  
128 1 ▶ if($default eq '_REQUIRED')
Branch: 1 branch
BranchTrueFalseTotal
if ($default eq "_REQUIRED") 0 1 1
129 0 { warn "MediaType ".($self->subType)." requires an explicit character-set.";
130 0 return undef;
131 }
132  
133 1 $default;
134 }
135  
136 6 1;
« MojoX/MIME/Types.pm ↑ Summary