Guess.pm

filter: Statementstmt 92.8% 208 / 224 Branchbran 76.9% 60 / 78 Conditioncond 70.8% 17 / 24 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 100% 56 / 56 Podpod 75.0% 6 / 8 totaltotal 87.4% 354 / 405 CC 49 SCAR 18.3
CC
49
Cov
87%
__ANON__
30.3
__ANON__
6.0
to_string
5.0
CRAP 6.2
« Guess/Role/SQL/SQLite.pm ↑ Summary Guess/Role/DateTime/Naive.pm »
1 package Type::Guess;
2  
3 # ABSTRACT: Guess data types
4  
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use MooX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use Class::Method::Modifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 use overload
16 10 ▶ '""' => \&to_string,
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 '&{}' => \&to_sub;
18  
19 # ------------------------------------------------------------------------------
20  
21 # has type => "Str";
22  
23 # has length => 0;
24 # has precision => 0;
25  
26 # has max => 0;
27 # has format => "";
28 # has integer_chars => 0;
29  
30 # # these two are set initially based
31 # has length_ro => 0;
32 # has integer_chars_ro => 0;
33 # has precision_ro => 0;
34  
35 # has percentages => 0;
36 # has signed => 0;
37  
38 # ------------------------------------------------------------------------------
39  
40 1 ▶has type => ( is => 'rw', default => sub { "Str" });
Subroutine __ANON__: called CC=1 SCAR=0.0
41  
42 1 ▶has length => ( is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
43  
44 1 ▶has precision => ( is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
45  
46 64 ▶has max => ( is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
47  
48 64 ▶has format => ( is => 'rw', default => sub { "" });
Subroutine __ANON__: called CC=1 SCAR=0.0
49  
50 1 ▶has integer_chars => ( is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
51  
52 1 ▶has percentages => ( is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
53  
54 1 ▶has signed => ( is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
55  
56 # -------------------------------------------------------
57 # these three are set initially and never again
58 # -------------------------------------------------------
59 1 ▶has length_ro => ( is => 'ro', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
60 1 ▶has integer_chars_ro => ( is => 'ro', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
61 1 ▶has precision_ro => ( is => 'ro', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
62  
63 # -------------------------------------------------------
64 # class properties
65 # -------------------------------------------------------
66  
67 # class_has "tolerance" => ( is => "rw", default => sub { 0 });
68  
69 # class_has "skip_empty" => ( is => "rw", default => sub { 1 });
70  
71 # class_has "encoding" => ( is => "rw", default => sub { "" });
72  
73  
74 127 ▶around "new" => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
75 127 my $orig = shift;
76 127 my $ret;
77  
78 127 ▶ if ((ref $_[1] eq "HASH") || (scalar @_ == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq 'HASH' or scalar @_ == 1) { } 64 63 127
Condition100%3/3
ref $_[1] eq "HASH" or scalar @_ == 1
l!l&&r!l&&!r
63163
MC/DC100%2/2
ref $_[1] eq "HASH" or scalar @_ == 1
ref $_[1] eq "HASH" scalar @_ == 1
Truth table100%3/3
A or B
A= ref $_[1] eq "HASH"B= scalar @_ == 1
ABresult
1X1
011
000
79 64 $ret = $orig->(@_);
80 } else {
81 63 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 63 63
82 63 ▶ my @args = $class->skip_empty ? grep { defined $_ && /^.+$/ } @_ : @_;
Branch: 1 branch
BranchTrueFalseTotal
$class->skip_empty ? : 56 7 63
Condition66%2/3
defined $_ && /^.+$/
!ll&&!rl&&r
04205
MC/DC50%1/2
defined $_ && /^.+$/
defined $_ /^.+$/
Truth table66%2/3
A && B
A= defined $_B= /^.+$/
ABresult
0X0
100
111
83 63 return $orig->($class, $class->analyse(@args)->as_hash)
84 }
85 10 };
86  
87 66 ▶sub analyse {
Subroutine analyse: called CC=2 SCAR=6.9
Pod documented
88 66 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 63 66
89 66 my $ret;
90  
91 66 $ret->{type} = $class->_type(@_);
92  
93 63 $ret->{precision} = $class->_precision(@_);
94 63 $ret->{precision_ro} = $ret->{precision};
95  
96 63 $ret->{length} = $class->_length(@_);
97 63 $ret->{length_ro} = $ret->{length};
98  
99 63 $ret->{integer_chars} = $class->_integer_chars(@_);
100 63 $ret->{integer_chars_ro} = $ret->{integer_chars};
101  
102 63 $ret->{percentages} = $class->_percentages(@_);
103 63 $ret->{signed} = $class->_signed(@_);
104  
105 63 return $class->new($ret);
106 }
107  
108 60 ▶sub as_hash {
Subroutine as_hash: called CC=2 SCAR=6.9
Pod documented
109 60 my $self = shift;
110 60 my $ret = {};
111 60 $\ = "\n";
112 60 for (keys $self->%*) {
113 660 $ret->{$_} = $self->$_
114 }
115 60 return $ret;
116 }
117  
118 62 ▶around "length_ro" => sub { warn "length_ro is read-only" if defined $_[2]; return $_[0]->($_[1]) };
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[2] 0 62 62
119 60 ▶around "precision_ro" => sub { warn "precision_ro is read-only" if defined $_[2]; return $_[0]->($_[1]) };
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[2] 0 60 60
120 64 ▶around "integer_chars_ro" => sub { warn "integer_chars_ro is read-only" if defined $_[2]; return $_[0]->($_[1]) };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[2] 1 63 64
121  
122 191 ▶around "precision" => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
123 191 my $orig = shift;
124 191 my $self = shift;
125 191 ▶ return 0 unless $self->type =~ /^(Num)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type =~ /^(Num)$/ 107 84 191
126 84 return $orig->($self, @_);
127 10 };
128  
129 64 ▶around "signed" => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
130 64 my $orig = shift;
131 64 my $self = shift;
132 64 ▶ return 0 unless $self->type =~ /^(Int|Num)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type =~ /^(Int|Num)$/ 17 47 64
133 47 return $orig->($self, @_);
134 10 };
135  
136 773 ▶around "type" => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
137 773 my $orig = shift;
138 773 my $self = shift;
139 773 ▶ if (defined $_[0] && $_[0] eq "Str") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] eq "Str") 2 771 773
Condition66%2/3
defined $_[0] and $_[0] eq "Str"
!ll&&!rl&&r
77102
MC/DC50%1/2
defined $_[0] and $_[0] eq "Str"
defined $_[0] $_[0] eq "Str"
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq "Str"
ABresult
0X0
100
111
140 2 my $ret = $orig->($self, @_);
141 2 $self->length($self->length_ro);
142 2 return $ret;
143 }
144 771 return $orig->($self, @_);
145 10 };
146  
147 138 ▶around "length" => sub {
Subroutine __ANON__: called CC=14 SCAR=34.1
148 138 my $orig = shift;
149 138 my $self = shift;
150 138 $\ = "\n";
151 138 ▶ return $orig->($self, @_) unless $self->type =~ /^(Int|Num)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type =~ /^(Int|Num)$/ 59 79 138
152 79 ▶ if ($self->type eq "Num") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->type eq 'Num') { } 24 55 79
elsif ($self->type eq 'Int') { } 55 0 55
153 24 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 1 23 24
154 1 ▶ my $int_chars = $_[0] - ($self->precision + ($self->percentages ? 1 : 0) + 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->percentages ? : 0 1 1
155 0 ▶ if ($int_chars > $self->integer_chars_ro) { $self->integer_chars($int_chars) } else { warn "Length value is too low - cannot chop" }
Branch: 1 branch
BranchTrueFalseTotal
if ($int_chars > $self->integer_chars_ro) { } 1 0 1
156 }
157 24 ▶ return $self->integer_chars + $self->precision + ($self->percentages ? 1 : 0) + 1
Branch: 1 branch
BranchTrueFalseTotal
$self->percentages ? : 4 20 24
158 }
159 elsif ($self->type eq "Int") {
160 55 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 1 54 55
161 1 ▶ my $int_chars = $_[0] - ($self->precision + ($self->percentages ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$self->percentages ? : 0 1 1
162 0 ▶ if ($int_chars > $self->integer_chars_ro) { $self->integer_chars($int_chars) } else { warn "Length value is too low - cannot chop" }
Branch: 1 branch
BranchTrueFalseTotal
if ($int_chars > $self->integer_chars_ro) { } 0 1 1
163 }
164 55 ▶ return $self->integer_chars + $self->precision + ($self->percentages ? 1 : 0)
Branch: 1 branch
BranchTrueFalseTotal
$self->percentages ? : 0 55 55
165 }
166 else {
167 0 print $self->type;
168 0 print "here";
169 0 print scalar @_;
170 0 print join " ", map { qq("$_") } @_;
171 0 ▶ if (!@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @_) { } 0 0 0
elsif (defined $_[0]) { } 0 0 0
172 0 print "nothing";
173 0 return $orig->($self)
174 } elsif (defined $_[0]) {
175 0 print "defined";
176 0 return $orig->($self, @_);
177 } else {
178 0 print "undef";
179 0 $orig->($self, $self->length_ro)
180 }
181 }
182 10 };
183  
184  
185 10 our $opts = { tolerance => 0, skip_empty => 1, encoding => "" };
186 10 our $skip_empty = 1;
187  
188 170 ▶sub class_opts {
Subroutine class_opts: called CC=3 SCAR=11.0
Pod undocumented
189 170 my ($class, $opt, $val) = @_;
190 170 ▶ die sprintf "Invalid option %s\n" unless exists $opts->{$opt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Type::Guess::opts->{$opt} 0 170 170
191 170 ▶ $opts->{$opt} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 17 153 170
192 170 return $opts->{$opt}
193 }
194  
195 176 ▶sub tolerance { return shift()->class_opts("tolerance", shift()) }
Subroutine tolerance: called CC=1 SCAR=0.0
Pod documented
196 73 ▶sub skip_empty { return shift()->class_opts("skip_empty", shift()) }
Subroutine skip_empty: called CC=1 SCAR=0.0
Pod documented
197  
198 127 ▶sub _enough($&@) {
Subroutine _enough: called CC=1 SCAR=0.0
199 127 my $class = shift;
200 127 my $sub = shift;
201 127 my @input = @_;
202 127 my $tolerance = $class->tolerance;
203 127 my $enough = scalar @input * (1 - $tolerance);
204 127 return (scalar grep { $sub->($_) } @input) >= $enough
205 }
206  
207 43 ▶sub _type {
Subroutine _type: called CC=3 SCAR=11.0
208 10 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
209 43 my $class = shift();
210 43 my @vals = @_;
211 43 @vals = map { s/^\+//; s/^-//; s/%$//; $_ } @vals;
212 # return "Int" if $class->_enough(sub { looks_like_number($_) && $_ == int($_) }, @vals);
213 43 ▶ return "Int" if $class->_enough(sub { (looks_like_number($_) && $_ == int($_)) || !defined $_ || /^$/ }, @vals);
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
if $class->_enough(sub { &looks_like_number($_) and $_ == int $_ or not defined $_ or /^$/; } , @vals) 22 21 43
&looks_like_number($_) and $_ == int $_ or not defined $_ or /^$/ 91 63 154
Condition83%5/6
&looks_like_number($_) and $_ == int $_ or not defined $_
l!l&&r!l&&!r
91063
&looks_like_number($_) and $_ == int $_
!ll&&!rl&&r
432091
MC/DC66%2/3
&looks_like_number($_) and $_ == int $_ or not defined $_
&looks_like_number($_) $_ == int $_ not defined $_
Truth table60%3/5
A and B or not(C)
A= &looks_like_number($_)B= $_ == int $_C= not defined $_
ABCresult
11X1
0X11
1011
0X00
1000
214 # return "Num" if $class->_enough(sub{ looks_like_number($_) }, @vals);
215 21 ▶ return "Num" if $class->_enough(sub{ looks_like_number($_) || !defined $_ || /^$/ }, @vals);
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
if $class->_enough(sub { &looks_like_number($_) or not defined $_ or /^$/; } , @vals) 9 12 21
&looks_like_number($_) or not defined $_ or /^$/ 30 36 66
Condition66%2/3
&looks_like_number($_) or not defined $_
l!l&&r!l&&!r
30036
MC/DC50%1/2
&looks_like_number($_) or not defined $_
&looks_like_number($_) not defined $_
Truth table66%2/3
A or not(B)
A= &looks_like_number($_)B= not defined $_
ABresult
1X1
011
000
216 12 return "Str"
217 }
218  
219 63 ▶sub _precision {
Subroutine _precision: called CC=2 SCAR=6.9
220 10 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
221 63 my $class = shift();
222 63 ▶ return List::Util::max map { /^\d*\.\d*$/ ? length($_=~ s/\d*\.//r) : 0 } map { local $_ = $_ ; s/^\+//; s/^-//; s/%$//; $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
/^\d*\.\d*$/ ? : 37 199 236
223 }
224  
225 63 ▶sub _integer_chars {
Subroutine _integer_chars: called CC=2 SCAR=6.9
226 10 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 63 my $class = shift();
228 63 ▶ return List::Util::max map { /([\+\-]*\d+)\.*\d*/ ? length($1) : 0 } @_;
Branch: 1 branch
BranchTrueFalseTotal
/([\+\-]*\d+)\.*\d*/ ? : 179 57 236
229 }
230  
231 63 ▶sub _signed {
Subroutine _signed: called CC=4 SCAR=13.9
232 63 my $class = shift();
233 10 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
234 63 ▶ return "+-" if (List::Util::any { /^([\-])/ } @_) && (List::Util::any { /^([\+])/ } @_);
Subroutine __ANON__: called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &List::Util::any(sub { /^([\-])/; } , @_) and &List::Util::any(sub { /^([\+])/; } , @_) 0 63 63
Condition66%2/3
&List::Util::any(sub { /^([\-])/; } , @_) and &List::Util::any(sub { /^([\+])/; } , @_)
!ll&&!rl&&r
51120
MC/DC0%0/2
&List::Util::any(sub { /^([\-])/; } , @_) and &List::Util::any(sub { /^([\+])/; } , @_)
&List::Util::any(sub { /^([\-])/; } , @_) &List::Util::any(sub { /^([\+])/; } , @_)
Truth table66%2/3
A and B
A= &List::Util::any(sub { /^([\-])/; } , @_)B= &List::Util::any(sub { /^([\+])/; } , @_)
ABresult
0X0
100
111
235 63 ▶ return "-" if (List::Util::any { /^([\-])/ } @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &List::Util::any(sub { /^([\-])/; } , @_) 12 51 63
236 51 return undef;
237 }
238  
239  
240 60 ▶sub _length {
Subroutine _length: called CC=1 SCAR=0.0
241 60 my $class = shift();
242 10 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 60 return List::Util::max map { length($_) } @_;
244 }
245  
246 63 ▶sub _percentages {
Subroutine _percentages: called CC=1 SCAR=0.0
247 10 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
248 63 my $class = shift();
249 63 ▶ return $class->_enough(sub { /%$/ }, @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
250 }
251  
252 34 ▶sub to_sub {
Subroutine to_sub: called CC=1 SCAR=0.0
Pod undocumented
253 34 my $self = shift;
254 34 my $format = $self->to_string;
255 10 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
256 34 ▶ return sub { return sprintf $format, shift() }
Subroutine __ANON__: called CC=1 SCAR=0.0
257 34 }
258  
259 53 ▶sub to_string {
Subroutine to_string: called CC=5 SCAR=16.2
Pod documented
260 53 my $self = shift;
261 53 my $format = $self->format;
262  
263 53 ▶ if ($format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) { } 0 53 53
264 0 return $format;
265 } else {
266 53 ▶ if ($self->type eq "Int") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->type eq 'Int') { } 21 32 53
elsif ($self->type eq 'Num') { } 15 17 32
267 21 $format = "%" . $self->length . "i";
268 }
269 elsif ($self->type eq "Num") {
270 15 $format = '%' . (1 + $self->integer_chars + $self->precision) . "." . $self->precision . "f";
271 15 ▶ $format .= "%%" if $self->percentages;
Branch: 1 branch
BranchTrueFalseTotal
if $self->percentages 8 7 15
272 }
273 else {
274 17 $format = "%-" . $self->length . "s";
275 }
276 53 return $format;
277 }
278 }
279  
280 23 ▶sub with_roles {
Subroutine with_roles: called CC=4 SCAR=15.5
Pod documented
281 23 my ($self, @roles) = @_;
282 23 ▶ my $class = Scalar::Util::blessed($self) // $self;
Condition33%1/3
builtin::blessed($self) // $self
l!l&&r!l&&!r
0230
MC/DC0%0/2
builtin::blessed($self) // $self
builtin::blessed($self) $self
Truth table33%1/3
A // B
A= builtin::blessed($self)B= $self
ABresult
1X1
011
000
283 23 ▶ @roles = map { /^\+(.+)$/ ? "${class}::Role::$1" : $_ } @roles;
Branch: 1 branch
BranchTrueFalseTotal
/^\+(.+)$/ ? : 26 0 26
284 23 ▶ if (Scalar::Util::blessed($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) 0 23 23
285 0 return Role::Tiny->apply_roles_to_object($self, @roles);
286 }
287 23 return Role::Tiny->create_class_with_roles($class, @roles);
288 }
289  
290  
291 10 1
« Guess/Role/SQL/SQLite.pm ↑ Summary Guess/Role/DateTime/Naive.pm »