Zonemaster/DNSName.pm

filter: Statementstmt 27.5% 19 / 69 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 35.7% 5 / 14 Podpod 100% 7 / 7 totaltotal 24.0% 31 / 129 CC 14 SCAR 32.6
CC
14
Cov
19%
__ANON__
72.0
common
20.0
string
6.0
CRAP 26.1
« Zonemaster/Test.pm ↑ Summary Zonemaster/Test/Address.pm »
1 package Zonemaster::DNSName v1.0.0;
2  
3 1 ▶use 5.14.2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 0 ▶coerce 'Zonemaster::DNSName', from 'Str', via { Zonemaster::DNSName->new( $_ ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
8  
9 use overload
10 1 '""' => \&string,
11 1 ▶ 'cmp' => \&str_cmp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 has 'labels' => ( is => 'ro', isa => 'ArrayRef[Str]', required => 1 );
14  
15 around BUILDARGS => sub {
16 0 ▶ my $orig = shift;
Subroutine __ANON__: not called CC=8 SCAR=42.8
17 0 my $class = shift;
18  
19 0 ▶ if ( @_ == 1 && !ref $_[0] ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 0 0 0
elsif (ref $_[0] and ref $_[0] eq __PACKAGE__) { } 0 0 0
elsif (ref $_[0] and ref $_[0] eq 'Zonemaster::Zone') { } 0 0 0
Condition0%0/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table0%0/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
20 0 my $name = shift;
21 0 ▶ $name = '' if not defined( $name );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
22 0 my @labels = split( /\./, $name );
23 0 return $class->$orig( labels => \@labels );
24 }
25 ▶ elsif ( ref( $_[0] ) and ref( $_[0] ) eq __PACKAGE__ ) {
Condition0%0/3
ref $_[0] and ref $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq __PACKAGE__
ref $_[0] ref $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq __PACKAGE__
ABresult
0X0
100
111
26 0 return $_[0];
27 }
28 ▶ elsif ( ref( $_[0] ) and ref( $_[0] ) eq 'Zonemaster::Zone' ) {
Condition0%0/3
ref $_[0] and ref $_[0] eq "Zonemaster::Zone"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "Zonemaster::Zone"
ref $_[0] ref $_[0] eq "Zonemaster::Zone"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "Zonemaster::Zone"
ABresult
0X0
100
111
29 0 return $_[0]->name;
30 }
31 else {
32 0 return $class->$orig( @_ );
33 }
34 1 };
35  
36 sub string {
37 0 ▶ my $self = shift;
Subroutine string: not called CC=2 SCAR=17.9
Pod documented
38  
39 0 my $name = join( '.', @{ $self->labels } );
40 0 ▶ $name = '.' if $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 0 0 0
41  
42 0 return $name;
43 }
44  
45 sub fqdn {
46 0 ▶ my ( $self ) = @_;
Subroutine fqdn: not called CC=1 SCAR=6.9
Pod documented
47  
48 0 return join( '.', @{ $self->labels } ) . '.';
49 }
50  
51 sub str_cmp {
52 0 ▶ my ( $self, $other ) = @_;
Subroutine str_cmp: not called CC=2 SCAR=17.9
Pod documented
53 0 ▶ $other //= ''; # Treat undefined value as root
Condition0%0/2
$other //= ""
l!l
00
MC/DC0%0/1
$other //= ""
$other
Truth table0%0/2
A
A= $other
Aresult
11
00
54  
55 0 $other =~ s/(.+)\.$/$1/;
56  
57 0 return ( uc( "$self" ) cmp uc( $other ) );
58 }
59  
60 sub next_higher {
61 0 ▶ my $self = shift;
Subroutine next_higher: not called CC=2 SCAR=17.9
Pod documented
62 0 my @l = @{ $self->labels };
63 0 ▶ if ( @l ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@l) { } 0 0 0
64 0 shift @l;
65 0 return Zonemaster::DNSName->new( labels => \@l );
66 }
67 else {
68 0 return;
69 }
70 }
71  
72 sub common {
73 0 ▶ my ( $self, $other ) = @_;
Subroutine common: not called CC=4 SCAR=30.0
Pod documented
74  
75 0 my @me = reverse @{ $self->labels };
76 0 my @them = reverse @{ $other->labels };
77  
78 0 my $count = 0;
79 0 ▶ while ( @me and @them ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@me and @them) 0 0 0
Condition0%0/3
@me and @them
!ll&&!rl&&r
000
MC/DC0%0/2
@me and @them
@me @them
Truth table0%0/3
A and B
A= @meB= @them
ABresult
0X0
100
111
80 0 my $m = shift @me;
81 0 my $t = shift @them;
82 0 ▶ if ( uc( $m ) eq uc( $t ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $m eq uc $t) { } 0 0 0
83 0 $count += 1;
84 0 next;
85 }
86 else {
87 0 last;
88 }
89 }
90  
91 0 return $count;
92 } ## end sub common
93  
94 sub prepend {
95 0 ▶ my ( $self, $label ) = @_;
Subroutine prepend: not called CC=1 SCAR=6.9
Pod documented
96 0 my @labels = ( $label, @{ $self->labels } );
97  
98 0 return $self->new( { labels => \@labels } );
99 }
100  
101 sub TO_JSON {
102 0 ▶ my ( $self ) = @_;
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod documented
103  
104 0 return $self->string;
105 }
106  
107 ## no critic (Modules::RequireExplicitInclusion)
108 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 1 __PACKAGE__->meta->make_immutable;
110  
111 1 1;
112  
113 =head1 NAME
114  
115 Zonemaster::DNSName - class representing DNS names
116  
117 =head1 SYNOPSIS
118  
119 my $name1 = Zonemaster::Name->new('www.example.org');
120 my $name2 = Zonemaster::Name->new('ns.example.org');
121 say "Yay!" if $name1->common($name2) == 2;
122  
123 =head1 ATTRIBUTES
124  
125 =over
126  
127 =item labels
128  
129 A reference to a list of strings, being the labels the DNS name is made up from.
130  
131 =back
132  
133 =head1 METHODS
134  
135 =over
136  
137 =item new($input) _or_ new({ labels => \@labellist})
138  
139 The constructor can be called with either a single argument or with a reference
140 to a hash as in the example above.
141  
142 If there is a single argument, it must be either a non-reference, a
143 L<Zonemaster::DNSName> object or a L<Zonemaster::Zone> object.
144  
145 If it's a non-reference, it will be split at period characters (possibly after
146 stringification) and the resulting list used as the name's labels.
147  
148 If it's a L<Zonemaster::DNSName> object it will simply be returned.
149  
150 If it's a L<Zonemaster::Zone> object, the value of its C<name> attribute will
151 be returned.
152  
153 =item string()
154  
155 Returns a string representation of the name. The string representation is created by joining the labels with dots. If there are no labels, a
156 single dot is returned. The names created this way do not have a trailing dot.
157  
158 The stringification operator is overloaded to this function, so it should rarely be necessary to call it directly.
159  
160 =item fqdn()
161  
162 Returns the name as a string complete with a trailing dot.
163  
164 =item str_cmp($other)
165  
166 Overloads string comparison. Comparison is made after converting the names to upper case, and ignores any trailing dot on the other name.
167  
168 =item next_higher()
169  
170 Returns a new L<Zonemaster::DNSName> object, representing the name of the called one with the leftmost label removed.
171  
172 =item common($other)
173  
174 Returns the number of labels from the rightmost going left that are the same in both names. Used by the recursor to check for redirections going
175 up the DNS tree.
176  
177 =item prepend($label)
178  
179 Returns a new L<Zonemaster::DNSName> object, representing the called one with the given label prepended.
180  
181 =item TO_JSON
182  
183 Helper method for JSON encoding.
184  
185 =back
186  
187 =cut
« Zonemaster/Test.pm ↑ Summary Zonemaster/Test/Address.pm »