Generator/From/Pod.pm

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