Dumpfile/Node/Properties.pm

filter: Statementstmt 92.7% 179 / 193 Branchbran 72.5% 45 / 62 Conditioncond 56.6% 17 / 30 MC/DCmcdc 33.3% 6 / 18 Subroutinesub 100% 26 / 26 Podpod 100% 16 / 16 totaltotal 83.7% 289 / 345 CC 49 SCAR 16.9
CC
49
Cov
85%
new
11.0
from_string
10.4
as_string
6.1
CRAP 5.4
« Dumpfile.pm ↑ Summary Dumpfile/Node/Content.pm »
1 ################################################################################
2 # Copyright (c) 2008 Martin Scharrer <martin@scharrer-online.de>
3 # This is open source software under the GPL v3 or later.
4 #
5 # $Id: Properties.pm 103 2008-10-14 21:11:21Z martin $
6 ################################################################################
7 package SVN::Dumpfile::Node::Properties;
8 12 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 Readonly my $NL => chr(10);
14  
15 12 our $VERSION = do { '$Rev: 103 $' =~ /\$Rev: (\d+) \$/; '0.13' . ".$1" };
16  
17 52 ▶sub new {
Subroutine new: called CC=11 SCAR=24.0
Pod documented
18 52 my $class = shift;
19 52 my $self = bless {
20 order => [],
21 property => {},
22 deleted => [],
23 unknown => [],
24 }, $class;
25  
26 52 ▶ if ( @_ == 1 && ref $_[0] eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 3 49 52
elsif (@_ == 1 and ref $_[0] eq 'ARRAY' and @{$_[0];} % 2 == 0) { } 2 47 49
elsif (@_ % 2 == 0) { } 5 42 47
elsif (@_ == 1 and not defined $_[0]) { } 41 1 42
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
6433
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
27 3 $self->{'property'} = { %{ $_[0] } };
28 3 @{ $self->{'order'} } = keys %{ $_[0] };
29 }
30 2 ▶ elsif ( @_ == 1 && ref $_[0] eq 'ARRAY' && @{ $_[0] } % 2 == 0 ) {
Condition83%5/6
@_ == 1 and ref $_[0] eq "ARRAY" and @{$_[0];} % 2 == 0
!ll&&!rl&&r
4702
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
6412
MC/DC66%2/3
@_ == 1 and ref $_[0] eq "ARRAY" and @{$_[0];} % 2 == 0
@_ == 1 ref $_[0] eq "ARRAY" @{$_[0];} % 2 == 0
Truth table75%3/4
A and B and C
A= @_ == 1B= ref $_[0] eq "ARRAY"C= @{$_[0];} % 2 == 0
ABCresult
0XX0
10X0
1100
1111
31 2 my $i = 0;
32 2 $self->{'property'} = { @{ $_[0] } };
33 2 @{ $self->{'order'} }
34 2 ▶ = map { $i++ % 2 ? () : $_ } @{ $_[0] }; # even entries only
Branch: 1 branch
BranchTrueFalseTotal
$i++ % 2 ? : 6 6 12
35 }
36 elsif ( @_ % 2 == 0 ) {
37 5 my $i = 0;
38 5 $self->{'property'} = {@_};
39 5 @{ $self->{'order'} }
40 5 ▶ = map { $i++ % 2 ? () : $_ } @_; # even entries only
Branch: 1 branch
BranchTrueFalseTotal
$i++ % 2 ? : 3 3 6
41 }
42 ▶ elsif ( @_ == 1 && !defined $_[0] ) {
Condition66%2/3
@_ == 1 and not defined $_[0]
!ll&&!rl&&r
1041
MC/DC50%1/2
@_ == 1 and not defined $_[0]
@_ == 1 not defined $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not defined $_[0]
ABresult
0X0
100
111
43  
44 # Ignore single undef value
45 }
46 else {
47 1 croak ${class}
48 . '::new() awaits hashref or key/value pairs as arguments.';
49 }
50  
51 51 return $self;
52 }
53  
54 12 ▶sub number {
Subroutine number: called CC=1 SCAR=0.0
Pod documented
55 12 my $self = shift;
56 12 return scalar keys %{ $self->{property} };
57 }
58  
59 41 ▶sub add {
Subroutine add: called CC=4 SCAR=14.0
Pod documented
60 41 my ( $self, $prop, $value, $position ) = @_;
61 41 my $order = $self->{order};
62  
63 41 ▶ if ( !defined $position || $position > @$order ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $position or $position > @$order) 40 1 41
Condition66%2/3
not defined $position or $position > @$order
l!l&&r!l&&!r
4001
MC/DC50%1/2
not defined $position or $position > @$order
not defined $position $position > @$order
Truth table66%2/3
not(A) or B
A= not defined $positionB= $position > @$order
ABresult
1X1
011
000
64 40 $position = @$order;
65 }
66  
67 41 my $existed = exists $self->{property}{$prop};
68 41 $self->{property}{$prop} = $value;
69 41 ▶ splice @$order, $position, 0, $prop
Branch: 1 branch
BranchTrueFalseTotal
unless $existed 41 0 41
70 if not $existed;
71 41 return $self;
72 }
73  
74 3 ▶sub del {
Subroutine del: called CC=4 SCAR=13.9
Pod documented
75 3 my $self = shift;
76 3 my $prop = shift;
77  
78 3 ▶ return unless exists $self->{property}{$prop};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"property"}{$prop} 0 3 3
79 3 delete $self->{property}{$prop};
80  
81 3 my $order = $self->{order};
82 3 for my $i ( 0 .. $#$order ) {
83 11 ▶ if ( $order->[$i] eq $prop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order->[$i] eq $prop) 2 9 11
84 2 splice @$order, $i, 1;
85 2 last;
86 }
87 }
88 3 return $self;
89 }
90  
91 2 ▶sub mark_deleted {
Subroutine mark_deleted: called CC=1 SCAR=0.0
Pod documented
92 2 my $self = shift;
93 2 my $prop = shift;
94  
95 2 $self->del($prop);
96 2 push @{ $self->{deleted} }, $prop;
97 2 return $self;
98 }
99  
100 1 ▶sub unmark_deleted {
Subroutine unmark_deleted: called CC=3 SCAR=11.0
Pod documented
101 1 my $self = shift;
102 1 my $prop = shift;
103  
104 1 my $deleted = $self->{deleted};
105 1 for my $i ( 0 .. $#$deleted ) {
106 2 ▶ if ( $deleted->[$i] eq $prop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deleted->[$i] eq $prop) 1 1 2
107 1 splice @$deleted, $i, 1;
108 1 last;
109 }
110 }
111 1 return $self;
112 }
113  
114 3 ▶sub is_deleted {
Subroutine is_deleted: called CC=4 SCAR=13.9
Pod documented
115 3 my $self = shift;
116 3 my $prop = shift;
117 3 ▶ return unless defined $prop;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prop 0 3 3
118  
119 3 foreach my $deleted ( @{ $self->{deleted} } ) {
120 4 ▶ return 1 if $deleted eq $prop;
Branch: 1 branch
BranchTrueFalseTotal
if $deleted eq $prop 2 2 4
121 }
122 1 return;
123 }
124  
125 4 ▶sub list_deleted {
Subroutine list_deleted: called CC=1 SCAR=0.0
Pod documented
126 4 my $self = shift;
127  
128 4 return @{ $self->{deleted} };
129 }
130  
131 107 ▶sub parse {
Subroutine parse: called CC=4 SCAR=14.2
Pod documented
132 107 my $self = shift;
133 107 my $propstrref = shift; # String in SVN property format to parse
134  
135 107 ▶ return unless defined $propstrref and defined $$propstrref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $propstrref and defined $$propstrref 0 107 107
Condition33%1/3
defined $propstrref and defined $$propstrref
!ll&&!rl&&r
00107
MC/DC0%0/2
defined $propstrref and defined $$propstrref
defined $propstrref defined $$propstrref
Truth table33%1/3
A and B
A= defined $propstrrefB= defined $$propstrref
ABresult
0X0
100
111
136 107 ▶ return unless ( $$propstrref =~ s/^([A-Z]) (\d+)$NL//o );
Branch: 1 branch
BranchTrueFalseTotal
unless $$propstrref =~ s/^([A-Z]) (\d+)$NL//o 29 78 107
137 78 my ( $ident, $length ) = ( $1, $2 );
138  
139 78 my $entry
140 = substr( $$propstrref, 0, $length, '' ); # get key with length given by
141 # above line and replace it with an null-string
142 78 $$propstrref =~ s/^$NL//o; # delete trailing new-line
143  
144 78 return ( $ident, $entry );
145 }
146  
147 29 ▶sub from_string {
Subroutine from_string: called CC=8 SCAR=23.4
Pod documented
148 29 my $self = shift;
149 29 my $propstr = shift; # String in SVN property format to parse
150 29 my $prophash = $self->{property}; # Hash reference to store properties
151 29 my $proporder = $self->{order}; # Array ref. to store order of properties
152  
153 29 ▶ return if not defined $propstr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $propstr 0 29 29
154 29 my @props;
155  
156 # Parse string and save all property entries in array
157 29 ▶ while ( my ( $ident, $entry ) = $self->parse( \$propstr ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->parse(\$propstr)) 78 29 107
158 78 push @props, [ $ident, $entry ];
159 }
160  
161 29 ▶ for ( my $i = 0; $i < $#props; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#props) 39 29 68
162 39 my ( $ident, $entry ) = @{ $props[$i] };
163 39 ▶ if ( $ident eq 'K' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ident eq 'K') { } 39 0 39
elsif ($ident eq 'D') { } 0 0 0
164 39 my ( $ident2, $value ) = @{ $props[ ++$i ] };
165 39 ▶ if ( $ident2 eq 'V' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ident2 eq "V") 39 0 39
166 39 $self->add( $entry, $value );
167 }
168 }
169 elsif ( $ident eq 'D' ) {
170 0 push @{ $self->{deleted} }, $entry;
171 }
172 else {
173 0 push @{ $self->{unknown} }, [ $ident, $entry ];
174 0 print STDERR "Error: Found unknown entry in property field:\n",
175 "------\n", "${ident}: $entry", "\n";
176 }
177 }
178  
179 29 ▶ if ( not $propstr =~ s/(?:PROPS-)?END$NL\Z//o ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($propstr =~ s/(?:PROPS-)?END$NL\Z//o) 0 29 29
180 0 print STDERR "Error at parsing properties at input line $.:",
181 "Couldn't understand '$propstr'.\n";
182 0 return 0;
183 }
184  
185 29 return scalar @props;
186 }
187  
188 26 ▶sub read {
Subroutine read: called CC=4 SCAR=14.0
Pod documented
189 12 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
190 26 my $self = shift;
191 26 my $fh = shift;
192 26 my $length = shift;
193 26 my $str;
194  
195 26 my $ret = eval { $fh->read( $str, $length ) };
196 26 ▶ return $ret unless defined $ret and $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret and $ret 0 26 26
Condition33%1/3
defined $ret and $ret
!ll&&!rl&&r
0026
MC/DC0%0/2
defined $ret and $ret
defined $ret $ret
Truth table33%1/3
A and B
A= defined $retB= $ret
ABresult
0X0
100
111
197  
198 26 ▶ return ( $self->from_string($str) ) ? $ret : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->from_string($str) ? : 17 9 26
199 }
200  
201 18 ▶sub write {
Subroutine write: called CC=4 SCAR=16.6
Pod documented
202 18 my $self = shift;
203 18 my $fh = shift;
204  
205 18 ▶ unless ( eval { $fh->isa('IO::Handle') }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $fh->isa("IO::Handle") } } or ref $fh eq "GLOB" or ref \$fh eq "GLOB") 0 18 18
Condition33%2/6
eval { do { $fh->isa("IO::Handle") } } or ref $fh eq "GLOB" or ref \$fh eq "GLOB"
l!l&&r!l&&!r
1800
eval { do { $fh->isa("IO::Handle") } } or ref $fh eq "GLOB"
l!l&&r!l&&!r
1800
MC/DC0%0/3
eval { do { $fh->isa("IO::Handle") } } or ref $fh eq "GLOB" or ref \$fh eq "GLOB"
eval { do { $fh->isa("IO::Handle") } } ref $fh eq "GLOB" ref \$fh eq "GLOB"
Truth table25%1/4
A or B or C
A= eval { do { $fh->isa("IO::Handle") } }B= ref $fh eq "GLOB"C= ref \$fh eq "GLOB"
ABCresult
1XX1
01X1
0011
0000
206 || ref $fh eq 'GLOB'
207 || ref \$fh eq 'GLOB' )
208 {
209 0 croak "Given argument is no valid file handle.";
210 }
211  
212 18 return $fh->print( $self->as_string );
213 }
214  
215 # Load properties from a file
216 2 ▶sub load {
Subroutine load: called CC=5 SCAR=16.9
Pod documented
217 12 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
218 2 my $self = shift;
219 2 my $fr = shift; # File handle or name
220 2 my $fh;
221 my $str;
222  
223 2 ▶ if ( eval { $fr->isa('IO::Handle') } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $fr->isa('IO::Handle') } }) { } 0 2 2
224 0 $fh = $fr;
225 }
226 else {
227 2 $fh = IO::File->new( $fr, '<' );
228 2 ▶ return unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 2 2
229 }
230  
231 2 $str = join '', $fh->getlines;
232 2 ▶ return unless defined $str and $str ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str ne "" 0 2 2
Condition33%1/3
defined $str and $str ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
defined $str and $str ne ""
defined $str $str ne ""
Truth table33%1/3
A and B
A= defined $strB= $str ne ""
ABresult
0X0
100
111
233  
234 2 return $self->from_string($str);
235 }
236  
237 # Save properties to a file
238 1 ▶sub save {
Subroutine save: called CC=3 SCAR=11.2
Pod documented
239 12 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
240 1 my $self = shift;
241 1 my $fr = shift; # File handle or name
242 1 my $fh;
243 my $str;
244  
245 1 ▶ if ( eval { $fr->isa('IO::Handle') } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $fr->isa('IO::Handle') } }) { } 0 1 1
246 0 $fh = $fr;
247 }
248 else {
249 1 $fh = IO::File->new( $fr, '>' );
250 1 ▶ return unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 1 1
251 }
252  
253 1 return $fh->print( $self->as_string(1) );
254 }
255  
256 17 ▶sub length {
Subroutine length: called CC=1 SCAR=0.0
Pod documented
257 12 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
258 17 return bytes::length shift->as_string;
259 }
260  
261 # Returns formatted string in SVN property format
262 65 ▶sub as_string {
Subroutine as_string: called CC=6 SCAR=18.1
Pod documented
263 12 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
264 65 my $self = shift;
265 65 my $forfile = shift; # bool
266 65 my $prophash = $self->{property}; # Hash reference to store properties
267 65 my $proporder = $self->{order}; # Array ref. to store order of properties
268 65 my $propstr = ''; # Return string
269  
270 # Create check-hash
271 65 my %prop_notprinted = map { $_ => 0 } ( keys %$prophash );
272  
273 # Print properties by given order
274 65 foreach my $key (@$proporder) {
275 $propstr
276 .= 'K '
277 . bytes::length($key)
278 . $NL
279 . $key
280 . $NL . 'V '
281 . bytes::length( $prophash->{$key} )
282 . $NL
283 84 . $prophash->{$key}
284 . $NL;
285 84 delete $prop_notprinted{$key}; # printed so delete from check-hash
286 }
287  
288 # Print now all remaining properties (if any)
289 65 foreach my $key ( sort keys %prop_notprinted ) {
290 $propstr
291 .= 'K '
292 . bytes::length($key)
293 . $NL
294 . $key
295 . $NL . 'V '
296 . bytes::length( $prophash->{$key} )
297 . $NL
298 0 . $prophash->{$key}
299 . $NL;
300 }
301  
302 # Print list of deleted properties
303 65 foreach my $entry ( @{ $self->{deleted} } ) {
304 0 $propstr .= 'D ' . bytes::length($entry) . $NL . $entry . $NL;
305 }
306 65 foreach my $ref ( @{ $self->{unknown} } ) {
307 0 my ( $ident, $entry ) = @$ref;
308 0 $propstr .= "$ident " . bytes::length($entry) . $NL . $entry . $NL;
309 }
310  
311 65 ▶ $propstr .= ($forfile) ? "END$NL" : "PROPS-END$NL";
Branch: 1 branch
BranchTrueFalseTotal
$forfile ? : 4 61 65
312 65 return $propstr;
313 }
314  
315 # Alias:
316 12 *to_string = \&as_string;
317  
318 12 1;
319 __END__
« Dumpfile.pm ↑ Summary Dumpfile/Node/Content.pm »