blib/lib/DBIO/Generate/Util.pm

filter: Statementstmt 100% 52 / 52 Branchbran 77.7% 14 / 18 Conditioncond 55.5% 5 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 89.3% 92 / 103 CC 13 SCAR 10.1
CC
13
Cov
90%
assert_method_name
4.3
assert_pkg
4.1
pl_val
4.0
CRAP 2.8
« blib/lib/DBIO/Test/Storage.pm ↑ Summary blib/lib/DBIO/Test/Slow.pm »
1 package DBIO::Generate::Util;
2 # ABSTRACT: Safe Perl-literal emission helpers for DBIO::Generate styles
3  
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 our $VERSION = '0.900007';
8  
9 14 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Carp::Clan qw/^DBIO/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # SECURITY: every DB-derived identifier (column / table / constraint /
16 # relationship / moniker name, default values, view definitions, ...) reaches
17 # generated Perl source through this module. The escaping logic lives here, in
18 # ONE place, on purpose: a hostile DB identifier must never become executable
19 # Perl in a generated Result class. Do not inline / duplicate this — see
20 # t/generate/08-codegen-injection.t.
21  
22  
23 # Render an arbitrary string as a safe, single-token Perl string literal.
24 # B::perlstring escapes quotes, backslashes, newlines and all other special
25 # characters, so the result is a complete literal that can be spliced into
26 # emitted source wherever a string is wanted (hash key, call argument, ...).
27 # undef-safe: undef becomes the literal undef.
28 1143 ▶sub pl_str {
Subroutine pl_str: called CC=2 SCAR=7.1
Pod documented
29 1143 my ($s) = @_;
30 1143 ▶ return 'undef' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 1143 1143
31 1143 return B::perlstring($s);
32 }
33  
34  
35 25 ▶sub pl_val {
Subroutine pl_val: called CC=4 SCAR=13.9
Pod documented
36 25 my ($v) = @_;
37 25 ▶ return pl_str($v) unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 25 25
38 25 ▶ if (ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) 5 20 25
39 5 local $Data::Dumper::Indent = 0;
40 5 local $Data::Dumper::Terse = 1;
41 5 local $Data::Dumper::Sortkeys = 1;
42 5 return Data::Dumper::Dumper($v);
43 }
44 20 ▶ return $v =~ /\A-?[0-9]+\z/ ? $v : pl_str($v);
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /\A-?[0-9]+\z/ ? : 14 6 20
45 }
46  
47  
48 # Render a value safe for emission on a `# ABSTRACT: ...` comment line.
49 # A newline in the source string would otherwise break out of the comment and
50 # inject a fresh line of (executable) code, so all vertical whitespace is
51 # collapsed to single spaces and the result trimmed. undef -> empty string.
52 164 ▶sub abstract_comment {
Subroutine abstract_comment: called CC=2 SCAR=7.0
Pod documented
53 164 my ($s) = @_;
54 164 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 164 164
55 164 $s =~ s/\s+/ /g;
56 164 $s =~ s/\A\s+//;
57 164 $s =~ s/\s+\z//;
58 164 return $s;
59 }
60  
61  
62 # Validate a string that is about to be emitted as a BAREWORD package /
63 # class name (e.g. `package $name;`), where a string literal is not an
64 # option. A bareword can carry arbitrary code, so anything that is not a
65 # strict, well-formed Perl package name is rejected loudly.
66 219 ▶sub assert_pkg {
Subroutine assert_pkg: called CC=4 SCAR=14.2
Pod documented
67 219 my ($name) = @_;
68 219 ▶ croak "refusing to emit invalid/unsafe package name: " . (defined $name ? "'$name'" : 'undef')
Branch: 2 branches
BranchTrueFalseTotal
unless defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/ 6 213 219
defined $name ? : 6 0 6
Condition66%2/3
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/
!ll&&!rl&&r
06213
MC/DC50%1/2
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/
defined $name $name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/
Truth table66%2/3
A and B
A= defined $nameB= $name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/
ABresult
0X0
100
111
69 unless defined $name
70 && $name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/;
71 213 return $name;
72 }
73  
74  
75 129 ▶sub assert_method_name {
Subroutine assert_method_name: called CC=4 SCAR=14.6
Pod documented
76 129 my ($method, $what) = @_;
77 129 ▶ DBIO::Exception->throw("$what is not a Perl identifier")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method and not ref $method and $method =~ /\A[A-Za-z_]\w*\z/ 80 49 129
Condition50%3/6
defined $method and not ref $method and $method =~ /\A[A-Za-z_]\w*\z/
!ll&&!rl&&r
08049
defined $method and not ref $method
!ll&&!rl&&r
00129
MC/DC33%1/3
defined $method and not ref $method and $method =~ /\A[A-Za-z_]\w*\z/
defined $method not ref $method $method =~ /\A[A-Za-z_]\w*\z/
Truth table50%2/4
A and not(B) and C
A= defined $methodB= not ref $methodC= $method =~ /\A[A-Za-z_]\w*\z/
ABCresult
0XX0
10X0
1100
1111
78 unless defined $method && !ref $method && $method =~ /\A[A-Za-z_]\w*\z/;
79 49 return $method;
80 }
81  
82  
83 14 my %RELATIONSHIP_METHOD = map { $_ => 1 }
84 qw(belongs_to has_one has_many might_have many_to_many);
85  
86 79 ▶sub assert_relationship_method {
Subroutine assert_relationship_method: called CC=2 SCAR=6.9
Pod documented
87 79 my ($method) = @_;
88 79 assert_method_name($method, 'relationship method');
89 DBIO::Exception->throw('relationship method is not a known relationship method')
90 39 ▶ unless $RELATIONSHIP_METHOD{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless $RELATIONSHIP_METHOD{$method} 5 34 39
91 34 return $method;
92 }
93  
94 14 1;
95  
96 __END__
« blib/lib/DBIO/Test/Storage.pm ↑ Summary blib/lib/DBIO/Test/Slow.pm »