oCLI/Plugin/Validate.pm

filter: Statementstmt 22.8% 13 / 57 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 21.4% 3 / 14 Podpod 0.0% 0 / 3 totaltotal 14.8% 16 / 108 CC 17 SCAR 36.8
CC
17
Cov
15%
before_code
110.0
__ANON__
6.0
__ANON__
6.0
CRAP 39.7
↑ Summary oCLI/Context.pm »
1 package oCLI::Plugin::Validate;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Enable code references in dclone so that our validation code
7 # refs don't result in "Can't store CODE items at"
8 # https://metacpan.org/pod/release/AMS/Storable-2.21/Storable.pm#CODE-REFERENCES
9 1 $Storable::Deparse = 1;
10 1 $Storable::Eval = 1;
11  
12 has tests => (
13 is => 'ro',
14 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 return +{
16 0 ▶ def => sub { defined $_[2] ? $_[2] : $_[3] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
17 0 ▶ defined => sub { defined $_[2] or die "Error: --$_[1] was expected.\n"; $_[2] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] or die "Error: --$_[1] was expected.\n" 0 0 0
18 0 ▶ num => sub { looks_like_number($_[2] or die "Error: --$_[1] expects a number.\n"); $_[2] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[2] || die("Error: --$_[1] expects a number.\n")
l!l
00
MC/DC0%0/1
$_[2] || die("Error: --$_[1] expects a number.\n")
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
19 0 ▶ gte => sub { $_[2] >= $_[3] or die "Error: --$_[1] must be a number greater than or equal to $_[3].\n"; $_[2] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] >= $_[3] or die "Error: --$_[1] must be a number greater than or equal to $_[3].\n" 0 0 0
20 0 ▶ lte => sub { $_[2] <= $_[3] or die "Error: --$_[1] must be a number less than or equal to $_[3].\n"; $_[2] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] <= $_[3] or die "Error: --$_[1] must be a number less than or equal to $_[3].\n" 0 0 0
21 0 ▶ min => sub { length($_[2]) >= $_[3] or die "Error: --$_[1] must be a string longer than $_[3] characters.\n"; $_[2] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_[2] >= $_[3] or die "Error: --$_[1] must be a string longer than $_[3] characters.\n" 0 0 0
22 0 ▶ max => sub { length($_[2]) <= $_[3] or die "Error: --$_[1] must be a string shorter than $_[3] characters.\n"; $_[2] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_[2] <= $_[3] or die "Error: --$_[1] must be a string shorter than $_[3] characters.\n" 0 0 0
23 0 };
24 }
25 1 );
26  
27 0 ▶sub after_context { }
Subroutine after_context: not called CC=1 SCAR=0.0
Pod undocumented
28  
29 # This function is run before the code reference is called.
30 #
31 # Self is the object of this code.
32 # c is the context object, it can be modified before running the code
33 # d is the command structure that we're running
34 0 ▶sub before_code {
Subroutine before_code: not called CC=10 SCAR=47.0
Pod undocumented
35 0 my ( $self, $c, $d ) = @_;
36  
37 0 ▶ if ( ( not $d->{validate} ) or ( ref($d->{validate}) ne 'ARRAY' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $d->{"validate"} or ref $d->{"validate"} ne "ARRAY") 0 0 0
Condition0%0/3
not $d->{"validate"} or ref $d->{"validate"} ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $d->{"validate"} or ref $d->{"validate"} ne "ARRAY"
not $d->{"validate"} ref $d->{"validate"} ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not $d->{"validate"}B= ref $d->{"validate"} ne "ARRAY"
ABresult
1X1
011
000
38 0 $c->trace("No validation structure to check, skipping");
39 0 return;
40 }
41  
42 0 my $validate = dclone( $d->{validate} );
43  
44 0 ▶ while ( defined( my $token = shift @{$validate} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $token = shift @{$validate;})) 0 0 0
45 0 $c->trace("Checking $token");
46
47 0 my $meta = shift @{$validate};
48  
49 0 foreach my $test ( @{$meta->[0]} ) {
50 0 my ( $ref, $value );
51  
52  
53 0 ▶ if ( looks_like_number($token) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($token)) { } 0 0 0
54 0 $ref = \$c->req->args->[$token];
55 0 $value = $c->req->args->[$token];
56 } else {
57 0 $ref = \$c->req->settings->{$token};
58 0 $value = $c->req->settings->{$token};
59 }
60  
61 0 ▶ if ( ref($test) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $test eq "CODE") 0 0 0
62 0 $c->trace("Running validation code block.");
63 0 $$ref = $test->($c, $token, $value);
64 0 next;
65 }
66  
67 0 ▶ if ( index($test, '=') != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($test, "=") != -1) 0 0 0
68 0 my ( $function, $arg ) = split(/=/, $test, 2);
69 0 $c->trace("Running validation function $function with user-supplied value $arg.");
70  
71 die "Error: unknown function $function called in validation."
72 0 ▶ unless defined $self->tests->{$function};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->tests->{$function} 0 0 0
73 0 $$ref = $self->tests->{$function}->($c, $token, $value, $arg );
74 0 next;
75 }
76  
77 # Last case, a bare function name.
78 die "Error: unknown function $test called in validation."
79 0 ▶ unless defined $self->tests->{$test};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->tests->{$test} 0 0 0
80  
81 0 $$ref = $self->tests->{$test}->($c, $token, $value);
82 }
83  
84 }
85 }
86  
87 0 ▶sub after_code {
Subroutine after_code: not called CC=1 SCAR=0.0
Pod undocumented
88  
89 }
90  
91 1 1;
↑ Summary oCLI/Context.pm »