Generator/Util.pm

filter: Statementstmt 91.1% 41 / 45 Branchbran 77.2% 17 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 87.8% 65 / 74 SCAR 26.0
CC
13
Cov
87%
merge_definitions
13.9
BEGIN
1.0
BEGIN
1.0
CRAP 13.4
« Generator/From/Pod.pm ↑ Summary Generator/From/Definitions.pm »
1 package OpenAPI::Generator::Util;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our @EXPORT_OK = qw(
11 merge_definitions
12 );
13  
14 sub merge_definitions {
15  
16 3 ▶ my(@definitions) = @_;
Subroutine merge_definitions: called CC=13 SCAR=26.3
Pod documented
17  
18 3 ▶ croak 'no definitions to merge' unless @definitions;
Branch: 1 branch
BranchTrueFalseTotal
unless @definitions 0 3 3
19  
20 3 ▶ if (scalar @definitions == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @definitions == 1) 1 2 3
21 1 return $definitions[0];
22 }
23  
24 2 my %common_def = (
25 paths => {},
26 components => {
27 schemas => {},
28 securitySchemes => {},
29 parameters => {}
30 },
31 );
32  
33 2 for my $def (@definitions) {
34  
35 # paths
36 6 ▶ while (my($path, $path_schema) = each %{$def->{paths}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$def{"paths"};}) 8 6 14
37 8 ▶ while (my($method, $method_schema) = each %{$path_schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$path_schema;}) 8 8 16
38 8 ▶ if (exists $common_def{paths}{$path}{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $common_def{"paths"}{$path}{$method}) 0 8 8
39 0 croak "$method $path duplicates";
40 }
41 8 $common_def{paths}{$path}{$method} = $method_schema;
42 }
43 }
44  
45 # comp param
46 6 ▶ while (my($param, $param_schema) = each %{$def->{components}{parameters}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$def{"components"}{"parameters"};}) 5 6 11
47 5 ▶ if (exists $common_def{components}{parameters}{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $common_def{"components"}{"parameters"}{$param}) 0 5 5
48 0 croak "param $param duplicates"
49 }
50 5 $common_def{components}{parameters}{$param} = $param_schema;
51 }
52  
53 # comp schemas
54 6 ▶ while (my($comp, $comp_schema) = each %{$def->{components}{schemas}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$def{"components"}{"schemas"};}) 5 6 11
55 5 ▶ if (exists $common_def{components}{schemas}{$comp}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $common_def{"components"}{"schemas"}{$comp}) 0 5 5
56 0 croak "schema $comp duplicates"
57 }
58 5 $common_def{components}{schemas}{$comp} = $comp_schema;
59 }
60  
61 # comp security
62 6 ▶ while (my($security, $security_schema) = each %{$def->{components}{securitySchemes}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$def{"components"}{"securitySchemes"};}) 5 6 11
63 5 ▶ if (exists $common_def{components}{securitySchemes}{$security}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $common_def{"components"}{"securitySchemes"}{$security}) 0 5 5
64 0 croak "security $security duplicates"
65 }
66 5 $common_def{components}{securitySchemes}{$security} = $security_schema;
67 }
68 }
69  
70 2 return \%common_def;
71 }
72  
73 3 1
74  
75 __END__
« Generator/From/Pod.pm ↑ Summary Generator/From/Definitions.pm »