TypeInference.pm

filter: Statementstmt 100% 85 / 85 Branchbran 100% 8 / 8 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 23 / 23 Podpod 100% 1 / 1 totaltotal 97.6% 122 / 125 CC 7 SCAR 7.4
CC
7
Cov
99%
infer
6.0
_infer_type_for
2.0
BEGIN
1.0
CRAP 2.1
↑ Summary TypeInference/Type/Number.pm »
1 package JSON::TypeInference;
2 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 our $VERSION = "1.0.2";
7  
8 2 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use List::UtilsBy qw(partition_by sort_by);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use JSON::TypeInference::Type::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use JSON::TypeInference::Type::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use JSON::TypeInference::Type::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use JSON::TypeInference::Type::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use JSON::TypeInference::Type::Number;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use JSON::TypeInference::Type::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use JSON::TypeInference::Type::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use JSON::TypeInference::Type::Union;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use JSON::TypeInference::Type::Unknown;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use constant ENTITY_TYPE_CLASSES => [
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 map { join '::', 'JSON::TypeInference::Type', $_ } qw( Array Boolean Null Number Object String )
23 2 ];
24  
25 # [Any] => Type
26 22 ▶sub infer {
Subroutine infer: called CC=6 SCAR=17.9
Pod documented
27 22 my ($class, $dataset) = @_;
28 22 ▶ my $dataset_by_type = { partition_by { _infer_type_for($_) } @$dataset };
Subroutine __ANON__: called CC=1 SCAR=0.0
29 22 my $possible_type_classes = [ keys %$dataset_by_type ];
30 my $candidate_types = [ map {
31 22 my $type_class = $_;
32 24 ▶ if ($type_class eq 'JSON::TypeInference::Type::Array') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type_class eq 'JSON::TypeInference::Type::Array') { } 3 21 24
elsif ($type_class eq 'JSON::TypeInference::Type::Object') { } 3 18 21
33 3 my $dataset = $dataset_by_type->{$type_class};
34 3 $class->_infer_array_element_types($dataset);
35 } elsif ($type_class eq 'JSON::TypeInference::Type::Object') {
36 3 my $dataset = $dataset_by_type->{$type_class}; # ArrayRef[HashRef[Str, Any]]
37 3 $class->_infer_object_property_types($dataset);
38 } else {
39 18 $type_class->new;
40 }
41 } @$possible_type_classes ];
42  
43 22 ▶ if (JSON::TypeInference::Type::Maybe->looks_like_maybe($candidate_types)) {
Branch: 2 branches
BranchTrueFalseTotal
if ('JSON::TypeInference::Type::Maybe'->looks_like_maybe($candidate_types)) { } 1 21 22
elsif (scalar @$candidate_types > 1) { } 2 19 21
44 1 ▶ my $entity_type = first { ! $_->isa('JSON::TypeInference::Type::Null') } @$candidate_types;
Subroutine __ANON__: called CC=1 SCAR=0.0
45 1 return JSON::TypeInference::Type::Maybe->new($entity_type);
46 } elsif (scalar(@$candidate_types) > 1) {
47 2 ▶ return JSON::TypeInference::Type::Union->new(sort_by { $_->name } @$candidate_types);
Subroutine __ANON__: called CC=1 SCAR=0.0
48 } else {
49 19 ▶ return $candidate_types->[0] // JSON::TypeInference::Type::Unknown->new;
Condition66%2/3
$candidate_types->[0] // "JSON::TypeInference::Type::Unknown"->new
l!l&&r!l&&!r
1810
MC/DC0%0/2
$candidate_types->[0] // "JSON::TypeInference::Type::Unknown"->new
$candidate_types->[0] "JSON::TypeInference::Type::Unknown"->new
Truth table66%2/3
A // B
A= $candidate_types->[0]B= "JSON::TypeInference::Type::Unknown"->new
ABresult
1X1
011
000
50 }
51 }
52  
53 # ArrayRef[ArrayRef[Any]] => JSON::TypeInference::Type::Array
54 3 ▶sub _infer_array_element_types {
Subroutine _infer_array_element_types: called CC=1 SCAR=0.0
55 3 my ($class, $dataset) = @_;
56 3 my $elements = [ map { @$_ } @$dataset ];
57 3 my $element_type = $class->infer($elements);
58 3 return JSON::TypeInference::Type::Array->new($element_type);
59 }
60  
61 # ArrayRef[HashRef[Str, Any]] => JSON::TypeInference::Type::Object
62 3 ▶sub _infer_object_property_types {
Subroutine _infer_object_property_types: called CC=1 SCAR=0.0
63 3 my ($class, $dataset) = @_;
64 3 my $keys = [ map { keys %$_ } @$dataset ]; # ArrayRef[Str]
65 my $dataset_by_prop = { map {
66 3 my $prop = $_;
67 7 ($prop => [ map { $_->{$prop} } @$dataset ])
68 } @$keys }; # HashRef[Str, ArrayRef[Str]]
69 3 my $prop_types = { map { ($_ => $class->infer($dataset_by_prop->{$_})) } @$keys };
70 3 return JSON::TypeInference::Type::Object->new($prop_types);
71 }
72  
73 # Any => Type
74 40 ▶sub _infer_type_for {
Subroutine _infer_type_for: called CC=2 SCAR=6.9
75 40 my ($data) = @_;
76 40 ▶ return (first { $_->accepts($data) } @{ENTITY_TYPE_CLASSES()}) // 'JSON::TypeInference::Type::Unknown';
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%2/2
&first(sub { $_->accepts($data); } , @{["JSON::TypeInference::Type::Array","JSON::TypeInference::Type::Boolean","JSON::TypeInference::Type::Null","JSON::TypeInference::Type::Number","JSON::TypeInference::Type::Object","JSON::TypeInference::Type::String"];}) // "JSON::TypeInference::Type::Unknown"
l!l
391
MC/DC100%1/1
&first(sub { $_->accepts($data); } , @{["JSON::TypeInference::Type::Array","JSON::TypeInference::Type::Boolean","JSON::TypeInference::Type::Null","JSON::TypeInference::Type::Number","JSON::TypeInference::Type::Object","JSON::TypeInference::Type::String"];}) // "JSON::TypeInference::Type::Unknown"
&first(sub { $_->accepts($data); } , @{["JSON::TypeInference::Type::Array","JSON::TypeInference::Type::Boolean","JSON::TypeInference::Type::Null","JSON::TypeInference::Type::Number","JSON::TypeInference::Type::Object","JSON::TypeInference::Type::String"];})
Truth table100%2/2
A
A= &first(sub { $_->accepts($data); } , @{["JSON::TypeInference::Type::Array","JSON::TypeInference::Type::Boolean","JSON::TypeInference::Type::Null","JSON::TypeInference::Type::Number","JSON::TypeInference::Type::Object","JSON::TypeInference::Type::String"];})
Aresult
11
00
77 }
78  
79 2 1;
80 __END__
↑ Summary TypeInference/Type/Number.pm »