Lazy/Role/Check/IP.pm

filter: Statementstmt 95.7% 45 / 47 Branchbran 75.0% 21 / 28 Conditioncond 50.0% 6 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 79.2% 80 / 101 CC 21 SCAR 30.2
CC
21
Cov
83%
check
25.2
BEGIN
1.0
BEGIN
1.0
CRAP 20.5
« Lazy/Role/Check/RegExp.pm ↑ Summary Lazy/Role/Composer.pm »
1 package Validator::Lazy::Role::Check::IP;
2  
3 2 our $VERSION = '0.03';
4  
5 =head1 NAME
6  
7 Validator::Lazy::Role::Check::IP
8  
9  
10 =head1 VERSION
11  
12 Version 0.03
13  
14  
15 =head1 SYNOPSIS
16  
17 use Validator::Lazy;
18 my $v = Validator::Lazy->new( { ip => { IP => { v => 4, type => 'Public' } } } ); # empty, or list, or scalar with 2letters countrycode
19  
20 my $ok = $v->check( ip => '192.1.1.1' ); # ok is false
21 say Dumper $v->errors; # [ { code => 'IP_PUBLIC_TYPE_REQUIRED', field => 'ip', data => { } } ]
22  
23  
24 =head1 DESCRIPTION
25  
26 An internal Role for Validator::Lazy, part of Validator::Lazy package.
27  
28 Provides "IP" type for Validator::Lazy config.
29 Allows to check ip addresses.
30  
31 When called without param - performs simple check as any valid ip
32 When param is passed - performs additional check as value-in-list
33  
34 =head1 METHODS
35  
36 =head2 C<check>
37  
38 Called from inside if Validator::Lazy->check process
39  
40 Temporary overrides internal Validator::Lazy::check method like this:
41  
42 $validator->check( $value, $param );
43  
44 $param - hash, that can contain this optional keys:
45 'version' or 'ver' or 'v' = v4 v6 4 6
46 'type' = Public|Private|Reserved|Loopback|... as described in Net::IP package
47 'mask' = 32,24,16,...
48  
49 $value - your value to check
50  
51  
52  
53 =head1 SUPPORT AND DOCUMENTATION
54  
55 After installing, you can find documentation for this module with the perldoc command.
56  
57 perldoc Validator::Lazy
58  
59 You can also look for information at:
60  
61 RT, CPAN's request tracker (report bugs here)
62 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy
63  
64 AnnoCPAN, Annotated CPAN documentation
65 http://annocpan.org/dist/Validator-Lazy
66  
67 CPAN Ratings
68 http://cpanratings.perl.org/d/Validator-Lazy
69  
70 Search CPAN
71 http://search.cpan.org/dist/Validator-Lazy/
72  
73  
74 =head1 AUTHOR
75  
76 ANTONC <antonc@cpan.org>
77  
78 =head1 LICENSE
79  
80 This program is free software; you can redistribute it and/or modify it
81 under the terms of the the Artistic License (2.0). You may obtain a
82 copy of the full license at:
83  
84 L<http://www.perlfoundation.org/artistic_license_2_0>
85  
86 =cut
87  
88 2 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 2 ▶use Net::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94  
95 12 ▶sub check {
Subroutine check: called CC=21 SCAR=32.3
Pod documented
96 12 my ( $self, $value, $param ) = @_;
97  
98 12 ▶ return $value unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 2 10 12
99  
100 10 my $ip = Net::IP->new( $value );
101  
102 10 ▶ unless ( $ip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip) 2 8 10
103 2 $self->add_error();
104 2 return $value;
105 }
106  
107 8 ▶ if ( my $err = $ip->error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $ip->error) 0 8 8
108 0 $self->add_error( { error_code => $err } );
109 0 return $value;
110 }
111 # ver => [ 4, 6 ]
112 8 ▶ my $v = $param->{version} || $param->{ver} || $param->{v};
Condition66%4/6
$param->{'version'} || $param->{'ver'} || $param->{'v'}
l!l&&r!l&&!r
305
$param->{"version"} or $param->{"ver"}
l!l&&r!l&&!r
035
MC/DC33%1/3
$param->{'version'} || $param->{'ver'} || $param->{'v'}
$param->{"version"} $param->{"ver"} $param->{'v'}
Truth table50%2/4
A or B || C
A= $param->{"version"}B= $param->{"ver"}C= $param->{'v'}
ABCresult
1XX1
01X1
0011
0000
113  
114 8 ▶ if ( $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 3 5 8
115  
116 3 $v =~ s/^v//i;
117  
118 3 ▶ confess 'Bad version. ver = 4 or 6 allowed' unless $v =~ /^[46]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /^[46]$/u 0 3 3
119  
120 3 ▶ if ( $ip->version ne $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip->version ne $v) 1 2 3
121 1 $self->add_error( 'IP_V' . $v . '_REQUIRED' );
122 1 return $value;
123 };
124 };
125  
126 7 ▶ if ( my $tt = $param->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tt = $param->{"type"}) 2 5 7
127  
128 2 ▶ my @t =
Branch: 2 branches
BranchTrueFalseTotal
ref $tt && ref $tt eq 'ARRAY' ? : 2 0 2
!ref($tt) ? : 0 0 0
Condition33%1/3
ref $tt and ref $tt eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $tt and ref $tt eq "ARRAY"
ref $tt ref $tt eq "ARRAY"
Truth table33%1/3
A and B
A= ref $ttB= ref $tt eq "ARRAY"
ABresult
0X0
100
111
129 ref $tt && ref $tt eq 'ARRAY' ? @$tt :
130 ! ref $tt ? [ $tt ] :
131 ();
132  
133 2 my $t_ok = scalar @t;
134 2 ▶ $t_ok &&= /^[A-Z]+$/i for @t;
Condition33%1/3
$t_ok &&= /^[A-Z]+$/iu
!ll&&!rl&&r
002
MC/DC0%0/2
$t_ok &&= /^[A-Z]+$/iu
$t_ok /^[A-Z]+$/iu
Truth table33%1/3
A &&= B
A= $t_okB= /^[A-Z]+$/iu
ABresult
0X0
100
111
135 2 ▶ confess 'Bad type. type = [Public|Private|Reserved|Loopback|...] allowed' unless $t_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $t_ok 0 2 2
136  
137 2 for my $t ( @t ) {
138 # type => [ Public, Private, Reserved, Loopback,... ]
139 2 ▶ if ( $ip->iptype ne uc($t) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip->iptype ne uc $t) 1 1 2
140 1 $self->add_error( 'IP_' . uc($t) . '_TYPE_REQUIRED' );
141 1 return $value;
142 };
143 };
144 };
145  
146 # mask => 32/24/...
147 6 ▶ if ( my $m = $param->{mask} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $param->{"mask"}) 2 4 6
148 2 ▶ confess 'Bad mask. mask = dd allowed' unless $m =~ /^\d{1,2}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $m =~ /^\d{1,2}$/u 0 2 2
149  
150 2 ▶ if ( $ip->prefixlen ne $m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip->prefixlen ne $m) 1 1 2
151 1 $self->add_error( 'MASK_ERROR', { required => $m } );
152 1 return $value;
153 };
154 };
155  
156 5 return $value;
157 };
158  
159 2 1;
« Lazy/Role/Check/RegExp.pm ↑ Summary Lazy/Role/Composer.pm »