blib/lib/App/DBCritic.pm

filter: Statementstmt 87.9% 73 / 83 Branchbran 33.3% 4 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 90.0% 18 / 20 Podpod 100% 1 / 1 totaltotal 81.8% 99 / 121 CC 10 SCAR 10.0
CC
10
Cov
81%
_build_dsn
6.0
__ANON__
6.0
_coerce_schema
5.4
CRAP 2.7
↑ Summary blib/lib/App/DBCritic/Policy/NoPrimaryKey.pm »
1 package App::DBCritic;
2  
3 # ABSTRACT: Critique a database schema for best practices
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod use App::DBCritic;
8 #pod
9 #pod my $critic = App::DBCritic->new(
10 #pod dsn => 'dbi:Oracle:HR', username => 'scott', password => 'tiger');
11 #pod $critic->critique();
12 #pod
13 #pod =head1 DESCRIPTION
14 #pod
15 #pod This package is used to scan a database schema and catalog any violations
16 #pod of best practices as defined by a set of policy plugins. It takes conceptual
17 #pod and API inspiration from L<Perl::Critic|Perl::Critic>.
18 #pod
19 #pod B<dbcritic> is the command line interface.
20 #pod
21 #pod This is a work in progress - please see the L</SUPPORT> section below for
22 #pod information on how to contribute. It especially needs ideas (and
23 #pod implementations!) of new policies!
24 #pod
25 #pod =cut
26  
27 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 8 ▶use Modern::Perl '2011'; ## no critic (Modules::ProhibitUseQuotedVersion)
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 8 our $VERSION = '0.023'; # VERSION
32 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 8 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 8 ▶use List::Util 1.33 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 use Module::Pluggable
36 8 ▶ search_path => [ __PACKAGE__ . '::Policy' ],
Subroutine BEGIN: called CC=1 SCAR=0.0
37 sub_name => 'policies',
38 8 instantiate => 'new';
39  
40 #pod =method policies
41 #pod
42 #pod Returns an array of loaded policy names that will be applied during
43 #pod L</critique>. By default all modules under the
44 #pod C<App::DBCritic::Policy> namespace are loaded.
45 #pod
46 #pod =cut
47  
48 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 8 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 8 ▶use App::DBCritic::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 8 for (qw(username password class_name)) { has $_ => ( is => 'ro' ) }
53  
54 #pod =attr username
55 #pod
56 #pod The optional username used to connect to the database.
57 #pod
58 #pod =attr password
59 #pod
60 #pod The optional password used to connect to the database.
61 #pod
62 #pod =attr class_name
63 #pod
64 #pod The name of a L<DBIx::Class::Schema|DBIx::Class::Schema> class you wish to
65 #pod L</critique>.
66 #pod Only settable at construction time.
67 #pod
68 #pod =cut
69  
70 8 has dsn => ( is => 'ro', lazy => 1, default => \&_build_dsn );
71  
72 0 ▶sub _build_dsn {
Subroutine _build_dsn: not called CC=2 SCAR=17.9
73 0 my $self = shift;
74  
75 ## no critic (ErrorHandling::RequireUseOfExceptions)
76 0 ▶ croak 'No schema defined' if not $self->has_schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_schema 0 0 0
77 0 my $dbh = $self->schema->storage->dbh;
78  
79 ## no critic (ValuesAndExpressions::ProhibitAccessOfPrivateData)
80 0 return join q{:} => 'dbi', $dbh->{Driver}{Name}, $dbh->{Name};
81 }
82  
83 #pod =attr dsn
84 #pod
85 #pod The L<DBI|DBI> data source name (required) used to connect to the database.
86 #pod If no L</class_name> or L</schema> is provided, L<DBIx::Class::Schema::Loader|DBIx::Class::Schema::Loader> will then
87 #pod construct schema classes dynamically to be critiqued.
88 #pod
89 #pod =cut
90  
91 8 has schema => (
92 is => 'ro',
93 coerce => 1,
94 lazy => 1,
95 default => \&_build_schema,
96 coerce => \&_coerce_schema,
97 predicate => 1,
98 );
99  
100 1 ▶sub _build_schema {
Subroutine _build_schema: called CC=3 SCAR=12.7
101 1 my $self = shift;
102  
103 1 my @connect_info = map { $self->$_ } qw(dsn username password);
104  
105 1 ▶ if ( my $class_name = $self->class_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class_name = $self->class_name) 0 1 1
106 0 ▶ return $class_name->connect(@connect_info)
Branch: 1 branch
BranchTrueFalseTotal
if eval "require $class_name" 0 0 0
107 if eval "require $class_name";
108 }
109  
110 1 return _coerce_schema( \@connect_info );
111 }
112  
113 7 ▶sub _coerce_schema {
Subroutine _coerce_schema: called CC=4 SCAR=16.9
114 7 my $schema = shift;
115  
116 7 ▶ return $schema if blessed $schema and $schema->isa('DBIx::Class::Schema');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($schema) and $schema->isa("DBIx::Class::Schema") 6 1 7
Condition66%2/3
builtin::blessed($schema) and $schema->isa("DBIx::Class::Schema")
!ll&&!rl&&r
106
MC/DC50%1/2
builtin::blessed($schema) and $schema->isa("DBIx::Class::Schema")
builtin::blessed($schema) $schema->isa("DBIx::Class::Schema")
Truth table66%2/3
A and B
A= builtin::blessed($schema)B= $schema->isa("DBIx::Class::Schema")
ABresult
0X0
100
111
117  
118 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
119 0 ▶ if ( $_[0] !~ / has no primary key at /ms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0] =~ / has no primary key at /msu) 0 0 0
120 0 print {*STDERR} $_[0];
121 }
122 1 };
123 1 ▶ return App::DBCritic::Loader->connect( @{$schema} )
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq ref $schema 1 0 1
124 if 'ARRAY' eq ref $schema;
125 ## no critic (ErrorHandling::RequireUseOfExceptions)
126 0 croak q{don't know how to make a schema from a } . ref $schema;
127 }
128  
129 #pod =attr schema
130 #pod
131 #pod A L<DBIx::Class::Schema|DBIx::Class::Schema> object you wish to L</critique>.
132 #pod Only settable at construction time.
133 #pod
134 #pod =attr has_schema
135 #pod
136 #pod An attribute predicates that is true or false, depending on whether L</schema>
137 #pod has been defined.
138 #pod
139 #pod =cut
140  
141 8 has _elements => ( is => 'ro', lazy => 1, default => \&_build__elements );
142  
143 5 ▶sub _build__elements {
Subroutine _build__elements: called CC=1 SCAR=0.0
144 5 my $self = shift;
145 5 my $schema = $self->schema;
146 return {
147 Schema => [$schema],
148 7 ResultSource => [ map { $schema->source($_) } $schema->sources ],
149 5 ResultSet => [ map { $schema->resultset($_) } $schema->sources ],
150 };
151 }
152  
153 1 ▶sub critique {
Subroutine critique: called CC=2 SCAR=7.2
Pod documented
154 0 for ( @{ shift->violations } ) {say}
155 1 return;
156 }
157  
158 #pod =method critique
159 #pod
160 #pod Runs the L</schema> through the C<App::DBCritic> engine using all
161 #pod the policies that have been loaded and dumps a string representation of
162 #pod L</violations> to C<STDOUT>.
163 #pod
164 #pod =cut
165  
166 has violations => (
167 is => 'ro',
168 lazy => 1,
169 5 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
170 5 my $self = shift;
171 15 [ map { $self->_policy_loop( $_, $self->_elements->{$_} ) }
172 5 keys %{ $self->_elements },
173 ];
174 },
175 8 );
176  
177 #pod =method violations
178 #pod
179 #pod Returns an array reference of all
180 #pod L<App::DBCritic::Violation|App::DBCritic::Violation>s
181 #pod picked up by the various policies.
182 #pod
183 #pod =cut
184  
185 15 ▶sub _policy_loop {
Subroutine _policy_loop: called CC=2 SCAR=6.9
186 15 my ( $self, $policy_type, $elements_ref ) = @_;
187 15 my @violations;
188 15 for my $policy ( grep { _policy_applies_to( $_, $policy_type ) }
189 $self->policies )
190 {
191 4 push @violations, grep {$_}
192 20 map { $policy->violates( $_, $self->schema ) } @{$elements_ref};
193 }
194 15 return @violations;
195 }
196  
197 60 ▶sub _policy_applies_to {
Subroutine _policy_applies_to: called CC=1 SCAR=0.0
198 60 my ( $policy, $type ) = @_;
199 60 ▶ return any { $_ eq $type } @{ $policy->applies_to };
Subroutine __ANON__: called CC=1 SCAR=0.0
200 }
201  
202 8 1;
203  
204 __END__
↑ Summary blib/lib/App/DBCritic/Policy/NoPrimaryKey.pm »