Generator/From/Definitions.pm

filter: Statementstmt 92.5% 50 / 54 Branchbran 59.0% 13 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 81.9% 77 / 94 SCAR 27.3
CC
14
Cov
81%
generate
6.1
_check_src
5.4
BEGIN
4.6
CRAP 15.3
« Generator/From/Pod.pm ↑ Summary Generator/Util.pm »
1 package OpenAPI::Generator::From::Definitions;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use OpenAPI::Generator::Util qw(merge_definitions);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 BEGIN {
11  
12 2 ▶ if (eval { require YAML::XS }) {
Subroutine BEGIN: called CC=4 SCAR=15.2
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { require YAML::XS } }) { } 0 2 2
elsif (eval { do { require YAML } }) { } 0 2 2
13 0 YAML::XS->import('Load');
14 }
15 2 elsif (eval { require YAML }) {
16 0 YAML->import('Load');
17 }
18 else {
19 2 require CPAN::Meta::YAML;
20 2 CPAN::Meta::YAML->import('Load');
21 }
22  
23 2 ▶ if (eval { require JSON::XS }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require JSON::XS } }) { } 0 2 2
24 0 JSON::XS->import('decode_json')
25 }
26 else {
27 2 require JSON::PP;
28 2 JSON::PP->import('decode_json');
29 }
30  
31 }
32  
33 sub new {
34  
35 1 ▶ bless {}, shift
Subroutine new: called CC=1 SCAR=0.0
Pod documented
36 }
37  
38 sub generate {
39  
40 1 ▶ my($self, $conf) = @_;
Subroutine generate: called CC=6 SCAR=18.1
Pod documented
41  
42 1 my $src = $conf->{src};
43 1 $self->_check_src($src);
44 1 my @files = $self->_src_as_files($src);
45  
46 1 ▶ if (!@files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 0 1 1
47 0 croak "no files found in $src";
48 }
49  
50 1 my @defs = @{ $conf->{definitions} };
51  
52 1 for my $file (@files) {
53 3 ▶ croak "$file is not readable" unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 3 3
54  
55 3 local $/ = '';
56 3 ▶ open my $fh, '<', $file or croak "can't open file $file";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("can't open file $file") 0 3 3
57 3 my $content = <$fh>;
58 3 close $fh;
59  
60  
61 3 ▶ if ($file =~ /\.json$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.json$/) { } 1 2 3
62 1 push @defs, decode_json($content);
63 }
64 else {
65 2 push @defs, Load($content);
66 }
67 }
68  
69 1 merge_definitions(@defs)
70 }
71  
72 sub _check_src {
73  
74 1 ▶ croak "$_[1] is not file or directory" unless(-f $_[1] or -d $_[1]);
Subroutine _check_src: called CC=4 SCAR=16.9
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_[1] or -d $_[1] 0 1 1
Condition33%1/3
-f $_[1] or -d $_[1]
l!l&&r!l&&!r
010
MC/DC0%0/2
-f $_[1] or -d $_[1]
-f $_[1] -d $_[1]
Truth table33%1/3
A or B
A= -f $_[1]B= -d $_[1]
ABresult
1X1
011
000
75 1 ▶ croak "$_[1] is not readable" unless(-r $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $_[1] 0 1 1
76 }
77  
78 sub _src_as_files {
79  
80 1 ▶ return $_[1] if -f $_[1];
Subroutine _src_as_files: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if -f $_[1] 0 1 1
81  
82 1 my @files;
83 1 ▶ find sub { push @files, $File::Find::name if /\.(yml|yaml|json)$/ }, $_[1];
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if /\.(yml|yaml|json)$/ 3 1 4
84 @files
85 1 }
86  
87 2 1
88  
89 __END__
« Generator/From/Pod.pm ↑ Summary Generator/Util.pm »