Strict.pm

filter: Statementstmt 98.8% 174 / 176 Branchbran 76.1% 67 / 88 Conditioncond 55.1% 27 / 49 MC/DCmcdc 28.5% 8 / 28 Subroutinesub 100% 34 / 34 Podpod 100% 14 / 14 totaltotal 83.2% 324 / 389 CC 68 SCAR 18.0
CC
68
Cov
86%
register_types
14.6
new
10.5
_profile_check
9.6
CRAP 6.0
↑ Summary Strict/UserConstraints.pm »
1 package Config::Strict;
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 $Data::Dumper::Indent = 0;
7 6 ▶use Carp qw(confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $VERSION = '0.07';
10  
11 6 ▶use Declare::Constraints::Simple -All;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Config::Strict::UserConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 my %type_registry = (
15 Bool => IsOneOf( 0, 1 ),
16 Num => IsNumber,
17 Int => IsInt,
18 Str => HasLength,
19 ArrayRef => IsArrayRef,
20 HashRef => IsHashRef,
21 CodeRef => IsCodeRef,
22 Regexp => IsRegex,
23 # Enum => undef,
24 # Anon => undef,
25 );
26  
27 3 ▶sub register_types {
Subroutine register_types: called CC=12 SCAR=26.8
Pod documented
28 # Allow user type registration
29 3 my $class = shift;
30 3 ▶ croak "No class" unless $class and not ref $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class and not ref $class 0 3 3
Condition33%1/3
$class and not ref $class
!ll&&!rl&&r
003
MC/DC0%0/2
$class and not ref $class
$class not ref $class
Truth table33%1/3
A and not(B)
A= $classB= not ref $class
ABresult
0X0
100
111
31 3 ▶ croak "Invalid name-constraint pairs args (@_)" unless @_ and @_ % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and @_ % 2 == 0 0 3 3
Condition33%1/3
@_ and @_ % 2 == 0
!ll&&!rl&&r
003
MC/DC0%0/2
@_ and @_ % 2 == 0
@_ @_ % 2 == 0
Truth table33%1/3
A and B
A= @_B= @_ % 2 == 0
ABresult
0X0
100
111
32 3 my %nc = @_;
33 3 ▶ while ( my ( $name, $constraint ) = each %nc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %nc) 7 1 8
34 7 ▶ croak "No name" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 7 7
35 7 ▶ croak "No constraint" unless $constraint;
Branch: 1 branch
BranchTrueFalseTotal
unless $constraint 0 7 7
36 croak "$name is already a registered type"
37 7 ▶ if exists $type_registry{ $name }
Branch: 1 branch
BranchTrueFalseTotal
if exists $type_registry{$name} or $name eq "Enum" or $name eq "Anon" 1 6 7
Condition66%4/6
exists $type_registry{$name} or $name eq "Enum" or $name eq "Anon"
l!l&&r!l&&!r
106
exists $type_registry{$name} or $name eq "Enum"
l!l&&r!l&&!r
016
MC/DC33%1/3
exists $type_registry{$name} or $name eq "Enum" or $name eq "Anon"
exists $type_registry{$name} $name eq "Enum" $name eq "Anon"
Truth table50%2/4
A or B or C
A= exists $type_registry{$name}B= $name eq "Enum"C= $name eq "Anon"
ABCresult
1XX1
01X1
0011
0000
38 or $name eq 'Enum'
39 or $name eq 'Anon';
40 # print "Creating user type $name...\n";
41 6 ▶ if ( _check_is_profile( $constraint ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_check_is_profile($constraint)) { } 4 1 5
42 # Already a profile
43 4 $type_registry{ $name } = $constraint;
44 }
45 else {
46 # Make a profile from a bare sub
47 1 my $made = _make_constraint( $name => $constraint );
48 1 $type_registry{ $name } = $made;
49 }
50 }
51 }
52  
53 16 ▶sub _create_profile {
Subroutine _create_profile: called CC=2 SCAR=6.9
54 16 my $param = shift;
55  
56 # Check parameter hash structure
57 16 _validate_params( $param );
58  
59 my %profile = (
60 # Built-in types
61 (
62 map {
63 124 my $type = $_;
64 36 map { $_ => $type_registry{ $type } }
65 124 _flatten( $param->{ $_ } )
66 } keys %type_registry
67 ),
68 (
69 2 map { $_ => IsOneOf( @{ $param->{ Enum }{ $_ } } ) }
70 14 keys %{ $param->{ Enum } }
71 ),
72 # Anon types
73 (
74 map {
75 6 my $sub = $param->{ Anon }{ $_ };
76 6 ▶ $sub = _make_constraint( undef => $sub )
Branch: 1 branch
BranchTrueFalseTotal
unless _check_is_profile($sub) 3 3 6
77 # confess
78 #"Anon code blocks must be a Declare::Constraints::Simple profile."
79 # . " Use register_types to implement bare coderefs."
80 unless _check_is_profile( $sub );
81 6 $_ => $sub
82 15 } keys %{ $param->{ Anon } }
83 ),
84 );
85 14 \%profile;
86 } ## end sub _create_profile
87  
88 # Constructor
89 18 ▶sub new {
Subroutine new: called CC=10 SCAR=23.5
Pod documented
90 18 my $class = shift;
91 18 my $opts = shift;
92 18 ▶ confess "Invalid construction arguments: @_" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 18 18
93  
94 # Get the parameter hash
95 croak "No 'params' key in constructor"
96 unless exists $opts->{ params }
97 18 ▶ and ( my $param = delete $opts->{ params } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"params"} and my $param = delete $opts->{"params"} 2 16 18
Condition66%2/3
exists $opts->{"params"} and my $param = delete $opts->{"params"}
!ll&&!rl&&r
2016
MC/DC50%1/2
exists $opts->{"params"} and my $param = delete $opts->{"params"}
exists $opts->{"params"} my $param = delete $opts->{"params"}
Truth table66%2/3
A and B
A= exists $opts->{"params"}B= my $param = delete $opts->{"params"}
ABresult
0X0
100
111
98  
99 # Get required, default values
100 16 ▶ my $required = delete $opts->{ required } || [];
Condition100%2/2
delete $opts->{'required'} || []
l!l
97
MC/DC100%1/1
delete $opts->{'required'} || []
delete $opts->{'required'}
Truth table100%2/2
A
A= delete $opts->{'required'}
Aresult
11
00
101 16 ▶ my $default = delete $opts->{ defaults } || {};
Condition100%2/2
delete $opts->{'defaults'} || {}
l!l
97
MC/DC100%1/1
delete $opts->{'defaults'} || {}
delete $opts->{'defaults'}
Truth table100%2/2
A
A= delete $opts->{'defaults'}
Aresult
11
00
102  
103 # Check that options hash now empty
104 16 ▶ confess sprintf( "Invalid option(s): %s", Dumper( $opts ) )
Branch: 1 branch
BranchTrueFalseTotal
if %$opts 0 16 16
105 if %$opts;
106  
107 # Create the configuration profile
108 16 my $profile = _create_profile( $param );
109  
110 # Set required to all parameters if *
111 14 ▶ $required = [ keys %$profile ] if $required eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $required eq "*" 2 12 14
112 14 ▶ croak "Required parameters not an arrayref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $required and ref $required eq "ARRAY" 0 14 14
Condition33%1/3
ref $required and ref $required eq "ARRAY"
!ll&&!rl&&r
0014
MC/DC0%0/2
ref $required and ref $required eq "ARRAY"
ref $required ref $required eq "ARRAY"
Truth table33%1/3
A and B
A= ref $requiredB= ref $required eq "ARRAY"
ABresult
0X0
100
111
113 unless ref $required and ref $required eq 'ARRAY';
114 # @$required == 1 and $required->[ 0 ] eq '_all';
115  
116 # Validate required and defaults
117 14 _validate_required( $required, $profile );
118 13 _validate_defaults( $default, $required );
119  
120 # Construct
121 my $self = bless( {
122 11 _required => { map { $_ => 1 } @$required }
123 , # Convert to hash lookup
124 _profile => $profile,
125 },
126 $class
127 );
128 11 $self->set( %$default );
129 10 $self;
130 } ## end sub new
131  
132 35 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
133 35 my $self = shift;
134 35 $self->_get_check( @_ );
135 34 my $params = $self->{ _params };
136 return (
137 34 ▶ wantarray ? ( map { $params->{ $_ } } @_ ) : $params->{ $_[ 0 ] } );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 21 34
138 }
139  
140 46 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod documented
141 46 my $self = shift;
142 46 $self->_set_check( @_ );
143 25 my %pv = @_;
144 25 ▶ while ( my ( $p, $v ) = each %pv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %pv) 36 25 61
145 36 $self->{ _params }{ $p } = $v;
146 }
147 25 1;
148 }
149  
150 2 ▶sub unset {
Subroutine unset: called CC=2 SCAR=6.9
Pod documented
151 2 my $self = shift;
152 2 $self->_unset_check( @_ );
153 1 delete $self->{ _params }{ $_ } for @_;
154 }
155  
156 25 ▶sub param_is_set {
Subroutine param_is_set: called CC=2 SCAR=7.1
Pod documented
157 25 my $self = shift;
158 25 ▶ croak "No parameter passed" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 25 25
159 25 return exists $self->{ _params }{ $_[ 0 ] };
160 }
161  
162 1 ▶sub all_set_params {
Subroutine all_set_params: called CC=1 SCAR=0.0
Pod documented
163 1 keys %{ shift->{ _params } };
164 }
165  
166 1 ▶sub param_hash {
Subroutine param_hash: called CC=1 SCAR=0.0
Pod documented
167 1 %{ shift->{ _params } };
168 }
169  
170 1 ▶sub param_array {
Subroutine param_array: called CC=1 SCAR=0.0
Pod documented
171 1 my $self = shift;
172 1 my $params = $self->{ _params };
173 1 map { [ $_ => $params->{ $_ } ] } keys %$params;
174 }
175  
176 48 ▶sub param_exists {
Subroutine param_exists: called CC=2 SCAR=7.1
Pod documented
177 48 my $self = shift;
178 48 ▶ croak "No parameter passed" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 48 48
179 48 return exists $self->{ _profile }{ $_[ 0 ] };
180 }
181  
182 1 ▶sub all_params {
Subroutine all_params: called CC=1 SCAR=0.0
Pod documented
183 1 keys %{ shift->{ _profile } };
184 }
185  
186 24 ▶sub get_profile {
Subroutine get_profile: called CC=2 SCAR=7.1
Pod documented
187 24 my $self = shift;
188 24 ▶ croak "No parameter passed" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 24 24
189 24 $self->{ _profile }{ $_[ 0 ] };
190 }
191  
192 35 ▶sub _get_check {
Subroutine _get_check: called CC=2 SCAR=6.9
193 35 my ( $self, @params ) = @_;
194 35 my $profile = $self->{ _profile };
195 35 _profile_check( $profile, $_ ) for @params;
196 }
197  
198 46 ▶sub _set_check {
Subroutine _set_check: called CC=2 SCAR=6.9
199 46 my ( $self, %value ) = @_;
200 46 my $profile = $self->{ _profile };
201 46 ▶ while ( my ( $k, $v ) = each %value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %value) 57 25 82
202 57 _profile_check( $profile, $k => $v );
203 }
204 }
205  
206 2 ▶sub _unset_check {
Subroutine _unset_check: called CC=4 SCAR=13.9
207 2 my ( $self, @params ) = @_;
208 # Check against required parameters
209 2 for ( @params ) {
210 2 ▶ confess "$_ is a required parameter" if $self->param_is_required( $_ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->param_is_required($_) 1 1 2
211 }
212 # Check against profile
213 1 my $profile = $self->{ _profile };
214 1 _profile_check( $profile, $_ ) for @params;
215 }
216  
217 24 ▶sub validate {
Subroutine validate: called CC=6 SCAR=18.0
Pod documented
218 24 my $self = shift;
219 24 ▶ confess "No parameter-values pairs passed" unless @_ >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 2 0 24 24
220 24 ▶ confess "Uneven number of parameter-values pairs passed" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 24 24
221 24 my %pair = @_;
222 24 ▶ while ( my ( $param, $value ) = each %pair ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %pair) 25 10 35
223 25 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->param_exists($param) and $self->get_profile($param)->($value) 14 11 25
Condition100%3/3
$self->param_exists($param) and $self->get_profile($param)->($value)
!ll&&!rl&&r
11311
MC/DC100%2/2
$self->param_exists($param) and $self->get_profile($param)->($value)
$self->param_exists($param) $self->get_profile($param)->($value)
Truth table100%3/3
A and B
A= $self->param_exists($param)B= $self->get_profile($param)->($value)
ABresult
0X0
100
111
224 unless $self->param_exists( $param )
225 and $self->get_profile( $param )->( $value );
226 }
227 10 1;
228 }
229  
230 4 ▶sub param_is_required {
Subroutine param_is_required: called CC=3 SCAR=11.0
Pod documented
231 4 my ( $self, $param ) = @_;
232 4 ▶ return unless $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $param 0 4 4
233 4 ▶ return 1 if $self->{ _required }{ $param };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_required"}{$param} 2 2 4
234 2 0;
235 }
236  
237 # Static validator from profile
238 99 ▶sub _profile_check {
Subroutine _profile_check: called CC=8 SCAR=22.6
239 99 my ( $profile, $param ) = ( shift, shift );
240 99 ▶ confess "No parameter passed" unless defined $param;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param 0 99 99
241 confess "Invalid parameter: $param doesn't exist"
242 99 ▶ unless exists $profile->{ $param };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $profile->{$param} 2 97 99
243 97 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 56 41 97
244 56 my $value = shift;
245 56 my $result = $profile->{ $param }->( $value );
246 56 ▶ unless ( $result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 20 36 56
247 # Failed validation
248 20 ▶ confess $result->message
Branch: 1 branch
BranchTrueFalseTotal
if ref $result and $result->isa("Declare::Constraints::Simple::Result") 20 0 20
Condition33%1/3
ref $result and $result->isa("Declare::Constraints::Simple::Result")
!ll&&!rl&&r
0020
MC/DC0%0/2
ref $result and $result->isa("Declare::Constraints::Simple::Result")
ref $result $result->isa("Declare::Constraints::Simple::Result")
Truth table33%1/3
A and B
A= ref $resultB= $result->isa("Declare::Constraints::Simple::Result")
ABresult
0X0
100
111
249 if ref $result
250 and $result->isa( 'Declare::Constraints::Simple::Result' );
251 0 ▶ confess sprintf( "Invalid value (%s) for config parameter $param",
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
252 defined $value ? $value : 'undef' );
253 }
254 }
255 }
256  
257 16 ▶sub _validate_params {
Subroutine _validate_params: called CC=6 SCAR=20.2
258 16 my $param = shift;
259 16 ▶ confess "No parameters passed"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param and ref $param and ref $param eq "HASH" and %$param 0 16 16
Condition33%3/9
defined $param and ref $param and ref $param eq "HASH" and %$param
!ll&&!rl&&r
0016
defined $param and ref $param and ref $param eq "HASH"
!ll&&!rl&&r
0016
defined $param and ref $param
!ll&&!rl&&r
0016
MC/DC0%0/4
defined $param and ref $param and ref $param eq "HASH" and %$param
defined $param ref $param ref $param eq "HASH" %$param
Truth table20%1/5
A and B and C and D
A= defined $paramB= ref $paramC= ref $param eq "HASH"D= %$param
ABCDresult
0XXX0
10XX0
110X0
11100
11111
260 unless defined $param
261 and ref $param
262 and ref $param eq 'HASH'
263 and %$param;
264 my $param_profile = OnHashKeys( (
265 16 map { $_ => Or( HasLength, IsArrayRef ) }
266 qw( Bool Int Num Str Regexp ArrayRef HashRef )
267 ),
268 Enum => IsHashRef(
269 -keys => HasLength,
270 -values => IsArrayRef
271 ),
272 Anon => IsHashRef(
273 -keys => HasLength,
274 -values => IsCodeRef
275 ),
276 );
277 16 my $result = $param_profile->( $param );
278 16 ▶ confess $result->message unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 1 15 16
279 15 $result;
280 }
281  
282 13 ▶sub _validate_defaults {
Subroutine _validate_defaults: called CC=3 SCAR=11.0
283 13 my ( $default, $required ) = @_;
284 13 for ( @$required ) {
285 confess "$_ is a required parameter but isn't in the defaults"
286 16 ▶ unless exists $default->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $default->{$_} 2 14 16
287 }
288 11 1;
289 }
290  
291 14 ▶sub _validate_required {
Subroutine _validate_required: called CC=3 SCAR=11.0
292 14 my ( $required, $profile ) = @_;
293 14 for ( @$required ) {
294 confess "Required parameter '$_' not in the configuration profile"
295 17 ▶ unless exists $profile->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $profile->{$_} 1 16 17
296 }
297 13 1;
298 }
299  
300 124 ▶sub _flatten {
Subroutine _flatten: called CC=4 SCAR=13.9
301 124 my $val = shift;
302 124 ▶ return unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 93 31 124
303 31 ▶ return ( $val ) unless ref $val;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 23 8 31
304 8 ▶ return @{ $val } if ref $val eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "ARRAY" 7 1 8
305 1 confess "Not a valid parameter ref: " . ref $val;
306 }
307  
308 16 ▶sub _check_is_profile {
Subroutine _check_is_profile: called CC=6 SCAR=18.2
309 16 my ( $sub ) = @_;
310 16 ▶ confess "Given constraint not a coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $sub and ref $sub and ref $sub eq "CODE" 1 15 16
Condition66%4/6
$sub and ref $sub and ref $sub eq "CODE"
!ll&&!rl&&r
1015
$sub and ref $sub
!ll&&!rl&&r
0115
MC/DC33%1/3
$sub and ref $sub and ref $sub eq "CODE"
$sub ref $sub ref $sub eq "CODE"
Truth table50%2/4
A and B and C
A= $subB= ref $subC= ref $sub eq "CODE"
ABCresult
0XX0
10X0
1100
1111
311 unless $sub
312 and ref $sub
313 and ref $sub eq 'CODE';
314 15 my $class = blessed( $sub->( 1 ) );
315 # print $class;
316 15 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $class and $class eq "Declare::Constraints::Simple::Result" 4 11 15
Condition66%2/3
$class and $class eq "Declare::Constraints::Simple::Result"
!ll&&!rl&&r
4011
MC/DC50%1/2
$class and $class eq "Declare::Constraints::Simple::Result"
$class $class eq "Declare::Constraints::Simple::Result"
Truth table66%2/3
A and B
A= $classB= $class eq "Declare::Constraints::Simple::Result"
ABresult
0X0
100
111
317 unless $class and $class eq "Declare::Constraints::Simple::Result";
318 11 1;
319 }
320  
321 {
322 6 my $anon_count = 0;
323  
324 4 ▶ sub _make_constraint {
Subroutine _make_constraint: called CC=5 SCAR=16.8
325 4 my ( $name, $sub ) = @_;
326 # confess "No name provided" unless $name;
327 4 ▶ unless ( defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 4 4
328 # Anonymous constraint
329 0 $name = sprintf( "__ANON%d__", $anon_count++ );
330 }
331 4 ▶ confess "Not a coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $sub and ref $sub eq "CODE" 0 4 4
Condition33%1/3
$sub and ref $sub eq "CODE"
!ll&&!rl&&r
004
MC/DC0%0/2
$sub and ref $sub eq "CODE"
$sub ref $sub eq "CODE"
Truth table33%1/3
A and B
A= $subB= ref $sub eq "CODE"
ABresult
0X0
100
111
332 unless $sub and ref $sub eq 'CODE';
333 # Make the constraint
334 4 Config::Strict::UserConstraints->make_constraint( $name => $sub );
335 # Return the new constraint
336 4 my $class = 'Config::Strict::UserConstraints';
337 # print "Declarations: ",Dumper( [ $class->fetch_constraint_declarations ]),"\n";
338 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
339 4 my $made = ${ $class . '::CONSTRAINT_GENERATORS' }{ $name };
340 6 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
341 # Sanity check
342 4 ▶ croak "(assert) Generated constraint doesn't return a Result object"
Branch: 1 branch
BranchTrueFalseTotal
unless _check_is_profile($made) 0 4 4
343 unless _check_is_profile( $made );
344 4 return $made;
345 }
346 }
347  
348 6 1;
↑ Summary Strict/UserConstraints.pm »