blib/lib/DBIx/Class/Schema/Loader/Utils.pm

filter: Statementstmt 79.4% 124 / 156 Branchbran 60.7% 17 / 28 Conditioncond 55.5% 10 / 18 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 77.2% 34 / 44 Podpod 0.0% 0 / 15 totaltotal 69.6% 190 / 273 CC 24 SCAR 9.8
CC
24
Cov
75%
eval_package_without_redefine_warnings
6.0
no_warnings
6.0
warnings_exist
6.0
CRAP 2.7
« blib/lib/DBIx/Class/Schema/Loader/DBObject.pm ↑ Summary blib/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm »
1 package # hide from PAUSE
2 DBIx::Class::Schema::Loader::Utils;
3  
4 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 57 ▶use String::CamelCase 'wordsplit';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 57 ▶use Carp::Clan qw/^DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 57 ▶use List::Util 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 57 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 57 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 57 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 57 our @EXPORT_OK = qw/split_name dumper dumper_squashed eval_package_without_redefine_warnings class_path no_warnings warnings_exist warnings_exist_silent slurp_file write_file array_eq sigwarn_silencer apply firstidx uniq/;
14  
15 57 ▶use constant BY_CASE_TRANSITION_V7 =>
Subroutine BEGIN: called CC=1 SCAR=0.0
16 57 qr/(?<=[[:lower:]\d])[\W_]*(?=[[:upper:]])|[\W_]+/;
17  
18 57 ▶use constant BY_NON_ALPHANUM =>
Subroutine BEGIN: called CC=1 SCAR=0.0
19 57 qr/[\W_]+/;
20  
21 57 my $LF = "\x0a";
22 57 my $CRLF = "\x0d\x0a";
23  
24 5645 ▶sub split_name($;$) {
Subroutine split_name: called CC=5 SCAR=16.1
Pod undocumented
25 5645 my ($name, $v) = @_;
26  
27 5645 ▶ my $is_camel_case = $name =~ /[[:upper:]]/ && $name =~ /[[:lower:]]/;
Condition100%3/3
$name =~ /[[:upper:]]/ && $name =~ /[[:lower:]]/
!ll&&!rl&&r
534224279
MC/DC100%2/2
$name =~ /[[:upper:]]/ && $name =~ /[[:lower:]]/
$name =~ /[[:upper:]]/ $name =~ /[[:lower:]]/
Truth table100%3/3
A && B
A= $name =~ /[[:upper:]]/B= $name =~ /[[:lower:]]/
ABresult
0X0
100
111
28  
29 5645 ▶ if ((not $v) || $v >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $v or $v >= 8) 33 5612 5645
Condition66%2/3
not $v or $v >= 8
l!l&&r!l&&!r
3305612
MC/DC50%1/2
not $v or $v >= 8
not $v $v >= 8
Truth table66%2/3
not(A) or B
A= not $vB= $v >= 8
ABresult
1X1
011
000
30 33 return map split(BY_NON_ALPHANUM, $_), wordsplit($name);
31 }
32  
33 5612 ▶ return split $is_camel_case ? BY_CASE_TRANSITION_V7 : BY_NON_ALPHANUM, $name;
Branch: 1 branch
BranchTrueFalseTotal
$is_camel_case ? : 273 5339 5612
34 }
35  
36 0 ▶sub dumper($) {
Subroutine dumper: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my $val = shift;
38  
39 0 my $dd = Data::Dumper->new([]);
40 0 $dd->Terse(1)->Indent(1)->Useqq(1)->Deparse(1)->Quotekeys(0)->Sortkeys(1);
41 0 return $dd->Values([ $val ])->Dump;
42 }
43  
44 144 ▶sub dumper_squashed($) {
Subroutine dumper_squashed: called CC=1 SCAR=0.0
Pod undocumented
45 144 my $val = shift;
46  
47 144 my $dd = Data::Dumper->new([]);
48 144 $dd->Terse(1)->Indent(1)->Useqq(1)->Deparse(1)->Quotekeys(0)->Sortkeys(1)->Indent(0);
49 144 return $dd->Values([ $val ])->Dump;
50 }
51  
52 # copied from DBIx::Class::_Util, import from there once it's released
53 10286 ▶sub sigwarn_silencer {
Subroutine sigwarn_silencer: called CC=3 SCAR=11.3
Pod undocumented
54 10286 my $pattern = shift;
55  
56 10286 ▶ croak "Expecting a regexp" if ref $pattern ne 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $pattern ne "Regexp" 0 10286 10286
57  
58 0 ▶ my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) };
Subroutine __ANON__: not called CC=1 SCAR=0.6
Condition66%2/3
$SIG{'__WARN__'} || sub { warn @_; }
l!l&&r!l&&!r
801622700
MC/DC0%0/2
$SIG{'__WARN__'} || sub { warn @_; }
$SIG{'__WARN__'} sub { warn @_; }
Truth table66%2/3
A || B
A= $SIG{'__WARN__'}B= sub { warn @_; }
ABresult
1X1
011
000
59  
60 10286 ▶ return sub { &$orig_sig_warn unless $_[0] =~ $pattern };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /$pattern/ 5 87 92
61 }
62  
63 # Copied with stylistic adjustments from List::MoreUtils::PP
64 1335 ▶sub firstidx (&@) {
Subroutine firstidx: called CC=3 SCAR=11.1
Pod undocumented
65 1335 my $f = shift;
66 1335 foreach my $i (0..$#_) {
67 1349 local *_ = \$_[$i];
68 1349 ▶ return $i if $f->();
Branch: 1 branch
BranchTrueFalseTotal
if &$f() 1335 14 1349
69 }
70 0 return -1;
71 }
72  
73 129 ▶sub uniq (@) {
Subroutine uniq: called CC=1 SCAR=0.0
Pod undocumented
74 129 my %seen = ();
75 129 grep { not $seen{$_}++ } @_;
76 }
77  
78 1242 ▶sub apply (&@) {
Subroutine apply: called CC=3 SCAR=11.2
Pod undocumented
79 1242 my $action = shift;
80 1242 $action->() foreach my @values = @_;
81 1242 ▶ wantarray ? @values : $values[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1242 0 1242
82 }
83  
84 1644 ▶sub eval_package_without_redefine_warnings {
Subroutine eval_package_without_redefine_warnings: called CC=6 SCAR=17.9
Pod undocumented
85 1644 my ($pkg, $code) = @_;
86  
87 1644 local $SIG{__WARN__} = sigwarn_silencer(qr/^Subroutine \S+ redefined/);
88  
89 # This hairiness is to handle people using "use warnings FATAL => 'all';"
90 # in their custom or external content.
91 1644 my @delete_syms;
92 1644 my $try_again = 1;
93  
94 1644 ▶ while ($try_again) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try_again) 1652 1644 3296
95 1652 ▶ eval $code;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
96  
97 1652 ▶ if (my ($sym) = $@ =~ /^Subroutine (\S+) redefined/) {
Branch: 2 branches
BranchTrueFalseTotal
if (my($sym) = $@ =~ /^Subroutine (\S+) redefined/) { } 8 1644 1652
elsif ($@) { } 0 1644 1644
98 8 delete $INC{ +class_path($pkg) };
99 8 push @delete_syms, $sym;
100  
101 8 foreach my $sym (@delete_syms) {
102 57 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
103 11 undef *{"${pkg}::${sym}"};
104 }
105 }
106 elsif ($@) {
107 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
108 }
109 else {
110 1644 $try_again = 0;
111 }
112 }
113 }
114  
115 2591 ▶sub class_path {
Subroutine class_path: called CC=1 SCAR=0.0
Pod undocumented
116 2591 my $class = shift;
117  
118 2591 my $class_path = $class;
119 2591 $class_path =~ s{::}{/}g;
120 2591 $class_path .= '.pm';
121  
122 2591 return $class_path;
123 }
124  
125 0 ▶sub no_warnings(&;$) {
Subroutine no_warnings: not called CC=2 SCAR=17.9
Pod undocumented
126 0 my ($code, $test_name) = @_;
127  
128 0 my $failed = 0;
129  
130 0 ▶ my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$SIG{'__WARN__'} || sub { warn @_; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$SIG{'__WARN__'} || sub { warn @_; }
$SIG{'__WARN__'} sub { warn @_; }
Truth table0%0/3
A || B
A= $SIG{'__WARN__'}B= sub { warn @_; }
ABresult
1X1
011
000
131 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 $failed = 1;
133 0 $warn_handler->(@_);
134 0 };
135  
136 0 $code->();
137  
138 0 Test::More::ok ((not $failed), $test_name);
139 }
140  
141 0 ▶sub warnings_exist(&$$) {
Subroutine warnings_exist: not called CC=2 SCAR=17.9
Pod undocumented
142 0 my ($code, $re, $test_name) = @_;
143  
144 0 my $matched = 0;
145  
146 0 ▶ my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$SIG{'__WARN__'} || sub { warn @_; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$SIG{'__WARN__'} || sub { warn @_; }
$SIG{'__WARN__'} sub { warn @_; }
Truth table0%0/3
A || B
A= $SIG{'__WARN__'}B= sub { warn @_; }
ABresult
1X1
011
000
147 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
148 0 ▶ if ($_[0] =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /$re/) { } 0 0 0
149 0 $matched = 1;
150 }
151 else {
152 0 $warn_handler->(@_)
153 }
154 0 };
155  
156 0 $code->();
157  
158 0 Test::More::ok $matched, $test_name;
159 }
160  
161 0 ▶sub warnings_exist_silent(&$$) {
Subroutine warnings_exist_silent: not called CC=1 SCAR=6.9
Pod undocumented
162 0 my ($code, $re, $test_name) = @_;
163  
164 0 my $matched = 0;
165  
166 0 ▶ local $SIG{__WARN__} = sub { $matched = 1 if $_[0] =~ $re; };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /$re/ 0 0 0
167  
168 0 $code->();
169  
170 0 Test::More::ok $matched, $test_name;
171 }
172  
173 110 ▶sub slurp_file($) {
Subroutine slurp_file: called CC=2 SCAR=7.0
Pod undocumented
174 110 my $file_name = shift;
175  
176 110 ▶ open my $fh, '<:encoding(UTF-8)', $file_name,
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $file_name or croak("Can't open '${file_name}' for reading: $!") 110 0 110
177 or croak "Can't open '$file_name' for reading: $!";
178  
179 110 my $data = do { local $/; <$fh> };
180  
181 110 close $fh;
182  
183 110 $data =~ s/$CRLF|$LF/\n/g;
184  
185 110 return $data;
186 }
187  
188 1 ▶sub write_file($$) {
Subroutine write_file: called CC=2 SCAR=7.0
Pod undocumented
189 1 my $file_name = shift;
190  
191 1 ▶ open my $fh, '>:encoding(UTF-8)', $file_name,
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $file_name or croak("Can't open '${file_name}' for writing: $!") 1 0 1
192 or croak "Can't open '$file_name' for writing: $!";
193  
194 1 print $fh shift;
195 1 close $fh;
196 }
197  
198 1036 ▶sub array_eq($$) {
Subroutine array_eq: called CC=2 SCAR=6.9
Pod undocumented
199 57 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
200 1036 my ($l, $r) = @_;
201  
202 1036 ▶ return @$l == @$r && all { $l->[$_] eq $r->[$_] } 0..$#$l;
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
@$l == @$r && &all(sub { $l->[$_] eq $r->[$_]; } , 0 .. $l->$#*)
!ll&&!rl&&r
284510242
MC/DC100%2/2
@$l == @$r && &all(sub { $l->[$_] eq $r->[$_]; } , 0 .. $l->$#*)
@$l == @$r &all(sub { $l->[$_] eq $r->[$_]; } , 0 .. $l->$#*)
Truth table100%3/3
A && B
A= @$l == @$rB= &all(sub { $l->[$_] eq $r->[$_]; } , 0 .. $l->$#*)
ABresult
0X0
100
111
203 }
204  
205 57 1;
206 # vim:et sts=4 sw=4 tw=0:
« blib/lib/DBIx/Class/Schema/Loader/DBObject.pm ↑ Summary blib/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm »