Generator/From/Pod.pm

filter: Statementstmt 95.1% 79 / 83 Branchbran 84.2% 32 / 38 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 88.6% 133 / 150 SCAR 31.6
CC
23
Cov
90%
_parse_file
10.1
_extract_openapi_node
5.0
_check_src
4.6
CRAP 23.6
↑ Summary Generator/From/Definitions.pm »
1 package OpenAPI::Generator::From::Pod;
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;
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 2 ▶use Pod::Simple::SimpleTree;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use constant OPENAPI_HEAD_NAME => 'OPENAPI';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 BEGIN {
14  
15 2 ▶ if (eval { require YAML::XS }) {
Subroutine BEGIN: called CC=3 SCAR=12.7
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { require YAML::XS } }) { } 0 2 2
elsif (eval { do { require YAML } }) { } 0 2 2
16 0 YAML::XS->import('Load');
17 }
18 2 elsif (eval { require YAML }) {
19 0 YAML->import('Load');
20 }
21 else {
22 2 CPAN::Meta::YAML->import('Load');
23 }
24  
25 }
26  
27 sub new {
28  
29 3 ▶ bless {}, shift
Subroutine new: called CC=1 SCAR=0.0
Pod documented
30 }
31  
32 sub generate {
33  
34 3 ▶ my($self, $conf) = @_;
Subroutine generate: called CC=3 SCAR=11.0
Pod documented
35  
36 3 my $src = $conf->{src};
37 3 $self->_check_src($src);
38 3 my @files = $self->_src_as_files($src);
39  
40 3 my @defs;
41 3 push @defs, $self->_parse_file($_) for @files;
42  
43 3 ▶ return unless @defs;
Branch: 1 branch
BranchTrueFalseTotal
unless @defs 1 2 3
44  
45 2 merge_definitions(@defs);
46 }
47  
48 sub _parse_file {
49 5 ▶ my($self, $file) = @_;
Subroutine _parse_file: called CC=10 SCAR=23.1
50 5 my $parser = Pod::Simple::SimpleTree->new;
51  
52 5 my $struct = $parser->parse_file($file)->root;
53 5 my $openapi_node = $self->_extract_openapi_node($struct);
54  
55 5 ▶ unless ($openapi_node) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($openapi_node) 2 3 5
56 2 return;
57 }
58  
59 3 my %common_definition = (
60 paths => {},
61 components => {
62 schemas => {},
63 parameters => {},
64 securitySchemes => {},
65 },
66 );
67  
68 3 ▶ while (my($index, $node) = each @{$openapi_node}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @{$openapi_node;}) 36 3 39
69 36 ▶ next unless ref $node eq uc'array';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "ARRAY" 6 30 36
70 30 ▶ next unless $node->[0] eq 'item-text';
Branch: 1 branch
BranchTrueFalseTotal
unless $node->[0] eq "item-text" 15 15 30
71  
72 15 my $item_name = $node->[2];
73  
74 15 my $definition_node = $openapi_node->[$index + 1];
75 15 ▶ if (!$definition_node || $definition_node->[0] ne 'Verbatim') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $definition_node or $definition_node->[0] ne "Verbatim") 0 15 15
Condition33%1/3
not $definition_node or $definition_node->[0] ne "Verbatim"
l!l&&r!l&&!r
0015
MC/DC0%0/2
not $definition_node or $definition_node->[0] ne "Verbatim"
not $definition_node $definition_node->[0] ne "Verbatim"
Truth table33%1/3
not(A) or B
A= not $definition_nodeB= $definition_node->[0] ne "Verbatim"
ABresult
1X1
011
000
76 0 croak("can not find definition for $node->[2]")
77 }
78 15 my $definition = Load $definition_node->[2];
79  
80 15 ▶ if ($item_name =~ /^\s*SCHEMA/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($item_name =~ /^\s*SCHEMA/) { } 3 12 15
elsif ($item_name =~ /^\s*PARAM/) { } 3 9 12
elsif ($item_name =~ /^\s*SECURITY/) { } 3 6 9
81 3 my $schema_name = $self->_extract_component_name($item_name);
82 3 $common_definition{components}{schemas}{$schema_name} = $definition;
83 }
84 elsif ($item_name =~ /^\s*PARAM/) {
85 3 my $param_name = $self->_extract_component_name($item_name);
86 3 $common_definition{components}{parameters}{$param_name} = $definition;
87 }
88 elsif ($item_name =~ /^\s*SECURITY/) {
89 3 my $security_schema_name = $self->_extract_component_name($item_name);
90 3 $common_definition{components}{securitySchemes}{$security_schema_name} = $definition;
91 }
92 else {
93 6 my($method, $route) = $self->_extract_method_and_route($item_name);
94 6 $common_definition{paths}{$route}{$method} = $definition;
95 }
96 }
97  
98 3 return \%common_definition;
99 }
100  
101 sub _extract_component_name {
102  
103 9 ▶ my($type, $name) = split /\s/, $_[1];
Subroutine _extract_component_name: called CC=1 SCAR=0.0
104 9 return $name;
105 }
106  
107 sub _extract_method_and_route {
108  
109 6 ▶ my ($method, $route) = split /\s/, $_[1];
Subroutine _extract_method_and_route: called CC=1 SCAR=0.0
110 6 return lc($method), $route
111 }
112  
113 sub _extract_openapi_node {
114  
115 5 ▶ my($self, $struct) = @_;
Subroutine _extract_openapi_node: called CC=5 SCAR=16.2
116 5 ▶ while (my($index, $node) = each @{$struct}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @{$struct;}) 19 2 21
117 19 ▶ next unless ref $node eq uc'array';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "ARRAY" 10 9 19
118 9 ▶ if ($node->[2] eq OPENAPI_HEAD_NAME) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->[2] eq "OPENAPI") 3 6 9
119 3 my $openapi_node = $struct->[$index + 1];
120 3 ▶ if (!$openapi_node) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($openapi_node) 0 3 3
121 0 croak 'can not find openapi node: no nodes found below openapi annotation'
122 }
123  
124 3 return $openapi_node;
125 }
126 }
127 }
128  
129 sub _check_src {
130  
131 3 ▶ croak "$_[1] is not file or directory" unless(-f $_[1] or -d $_[1]);
Subroutine _check_src: called CC=4 SCAR=15.2
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_[1] or -d $_[1] 0 3 3
Condition66%2/3
-f $_[1] or -d $_[1]
l!l&&r!l&&!r
210
MC/DC0%0/2
-f $_[1] or -d $_[1]
-f $_[1] -d $_[1]
Truth table66%2/3
A or B
A= -f $_[1]B= -d $_[1]
ABresult
1X1
011
000
132 3 ▶ croak "$_[1] is not readable" unless(-r $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $_[1] 0 3 3
133 }
134  
135 sub _src_as_files {
136  
137 3 ▶ return $_[1] if -f $_[1];
Subroutine _src_as_files: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if -f $_[1] 2 1 3
138  
139 1 my @files;
140 1 ▶ find sub { push @files, $File::Find::name if /\.(pm|pl|t|pod)$/ }, $_[1];
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if /\.(pm|pl|t|pod)$/ 3 1 4
141 @files
142 1 }
143  
144 2 1
145  
146 __END__
↑ Summary Generator/From/Definitions.pm »