blib/lib/Role/MimeInfo.pm

filter: Statementstmt 80.0% 52 / 65 Branchbran 39.2% 11 / 28 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 60.3% 79 / 131 CC 21 SCAR 36.6
CC
21
Cov
60%
mimetype
110.0
mimetype_isa
14.1
BEGIN
1.0
CRAP 38.8
↑ Summary
1 package Role::MimeInfo;
2  
3 2 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use File::MimeInfo ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::MimeInfo::Magic ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use IO::Scalar ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IO::ScalarArray ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 Role::MimeInfo - Bolt-on type checking against GNOME shared-mime-info
20  
21 =head1 VERSION
22  
23 Version 0.02
24  
25 =cut
26  
27 2 our $VERSION = '0.02';
28  
29 =head1 SYNOPSIS
30  
31 use Moo; # or Moose or Mouse or whatever
32  
33 with 'Role::MimeInfo';
34  
35 # are you ready to check some mime types???
36  
37 =head1 METHODS
38  
39 =head2 mimetype
40  
41 Proxy for L<File::MimeInfo::Magic/mimetype>.
42  
43 =cut
44  
45 0 ▶sub mimetype {
Subroutine mimetype: not called CC=10 SCAR=47.0
Pod documented
46 0 my (undef, $obj) = @_;
47 0 ▶ return unless defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 0 0
48  
49 # treat as a filename if not a ref
50 0 ▶ my $ref = ref $obj or return File::MimeInfo::mimetype($obj);
Branch: 1 branch
BranchTrueFalseTotal
my $ref = ref $obj or return File::MimeInfo::mimetype($obj) 0 0 0
51 0 my $bl = Scalar::Util::blessed($obj);
52  
53 0 ▶ if ($ref eq 'GLOB' or ($bl and $ref->can('seek') and $ref->can('read'))) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref eq 'GLOB' or $bl and $ref->can('seek') and $ref->can('read')) { } 0 0 0
elsif ($ref eq 'SCALAR') { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
elsif (my $ov = overload::Method($obj, '""')) { } 0 0 0
Condition0%0/9
$ref eq "GLOB" or $bl and $ref->can("seek") and $ref->can("read")
l!l&&r!l&&!r
000
$bl and $ref->can("seek") and $ref->can("read")
!ll&&!rl&&r
000
$bl and $ref->can("seek")
!ll&&!rl&&r
000
MC/DC0%0/4
$ref eq "GLOB" or $bl and $ref->can("seek") and $ref->can("read")
$ref eq "GLOB" $bl $ref->can("seek") $ref->can("read")
Truth table0%0/5
A or B and C and D
A= $ref eq "GLOB"B= $blC= $ref->can("seek")D= $ref->can("read")
ABCDresult
1XXX1
01111
00XX0
010X0
01100
54 0 return File::MimeInfo::Magic::mimetype($obj);
55 }
56 elsif ($ref eq 'SCALAR') {
57 0 $obj = IO::Scalar->new($obj);
58 }
59 elsif ($ref eq 'ARRAY') {
60 0 $obj = IO::ScalarArray->new($obj);
61 }
62 elsif (my $ov = overload::Method($obj, '""')) {
63 0 my $tmp = $ov->($obj);
64 0 $obj = IO::Scalar->new(\$tmp);
65 }
66 else {
67 0 Carp::croak("mimetype: don't know how to dispatch $ref");
68 }
69  
70 0 File::MimeInfo::Magic::mimetype($obj);
71 }
72  
73 =head2 mimetype_isa
74  
75 Proxy for L<File::MimeInfo/mimetype_isa> with additional
76 behaviour for self-identity and recursive type checking.
77  
78 =cut
79  
80 2 ▶sub mimetype_isa {
Subroutine mimetype_isa: called CC=12 SCAR=26.5
Pod documented
81 2 my (undef, $child, $ancestor) = @_;
82 2 ▶ return unless defined $child;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child 0 2 2
83  
84 # strip and lowercase the parameters
85 2 $child =~ s/^\s*([^;[:space:]]+).*?/\L$1/;
86  
87 # start queueing it up
88 2 my %t = ($child => 1);
89  
90 2 my $canon = File::MimeInfo::mimetype_canon($child);
91 2 ▶ $t{$canon}++ if $canon and $canon ne $child;
Branch: 1 branch
BranchTrueFalseTotal
if $canon and $canon ne $child 0 2 2
Condition33%1/3
$canon and $canon ne $child
!ll&&!rl&&r
020
MC/DC0%0/2
$canon and $canon ne $child
$canon $canon ne $child
Truth table33%1/3
A and B
A= $canonB= $canon ne $child
ABresult
0X0
100
111
92  
93 2 ▶ if (defined $ancestor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ancestor) 2 0 2
94 2 $ancestor =~ s/^\s*([^;[:space:]]+).*?/\L$1/;
95 2 ▶ return 1 if $t{$ancestor};
Branch: 1 branch
BranchTrueFalseTotal
if $t{$ancestor} 1 1 2
96  
97 # canonicalize the ancestor and try again
98 1 ▶ $ancestor = File::MimeInfo::mimetype_canon($ancestor) || $ancestor;
Condition33%1/3
File::MimeInfo::mimetype_canon($ancestor) || $ancestor
l!l&&r!l&&!r
100
MC/DC0%0/2
File::MimeInfo::mimetype_canon($ancestor) || $ancestor
File::MimeInfo::mimetype_canon($ancestor) $ancestor
Truth table33%1/3
A || B
A= File::MimeInfo::mimetype_canon($ancestor)B= $ancestor
ABresult
1X1
011
000
99 1 ▶ return 1 if $t{$ancestor};
Branch: 1 branch
BranchTrueFalseTotal
if $t{$ancestor} 0 1 1
100 }
101  
102 # now we recursively (okay, iteratively) check
103 1 my @q = ($child);
104 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@q and do { do { foreach my $t (File::MimeInfo::mimetype_isa(shift @q)) { $t = lc $t; push @q, $t unless defined $t{$t}; ++$t{$t}; } } } 2 1 3
105 # this second loop is necessary because we get a list here
106 3 for my $t (File::MimeInfo::mimetype_isa(shift @q)) {
107 5 $t = lc $t; # JIC
108 5 ▶ push @q, $t unless defined $t{$t};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t{$t} 2 3 5
109 5 $t{$t}++;
110 }
111 } while @q;
112  
113 # just give true or false if an ancestor was supplied
114 1 ▶ return !!$t{lc $ancestor} if defined $ancestor;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ancestor 1 0 1
115  
116 # otherwise just cough up the whole pile
117 0 return sort keys %t;
118 }
119  
120 =head1 SEE ALSO
121  
122 =over 4
123  
124 =item
125  
126 L<File::MimeInfo>
127  
128 =item
129  
130 L<Moo>
131  
132 =back
133  
134 =head1 AUTHOR
135  
136 Dorian Taylor, C<< <dorian at cpan.org> >>
137  
138 =head1 TODO
139  
140 =over 4
141  
142 =item
143  
144 Expose the rest of the interface of L<File::MimeInfo> in a reasonable
145 way.
146  
147 =back
148  
149 =head1 BUGS
150  
151 Please report any bugs or feature requests to
152 L<https://github.com/doriantaylor/p5-role-mimeinfo/issues> .
153  
154 =head1 LICENSE AND COPYRIGHT
155  
156 Copyright 2017 Dorian Taylor.
157  
158 Licensed under the Apache License, Version 2.0 (the "License"); you
159 may not use this file except in compliance with the License. You may
160 obtain a copy of the License at
161 L<http://www.apache.org/licenses/LICENSE-2.0>.
162  
163 Unless required by applicable law or agreed to in writing, software
164 distributed under the License is distributed on an "AS IS" BASIS,
165 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
166 implied. See the License for the specific language governing
167 permissions and limitations under the License.
168  
169 =cut
170  
171 2 1; # End of Role::MimeInfo
↑ Summary