MuForm/Params.pm

filter: Statementstmt 59.0% 36 / 61 Branchbran 40.0% 12 / 30 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 57.1% 4 / 7 Podpod 0.0% 0 / 4 totaltotal 48.2% 54 / 112 SCAR 43.4
CC
22
Cov
52%
_collapse_hash
72.0
expand_hash
16.0
split_name
3.3
CRAP 77.1
« MuForm.pm ↑ Summary MuForm/Role/RequestToken.pm »
1 package Data::MuForm::Params;
2 # ABSTRACT: params handling
3  
4 82 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 82 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 82 has 'separator' => ( is => 'rw', default => '.' );
8  
9 sub split_name {
10 293 ▶ my ( $self, $name, $sep ) = @_;
Subroutine split_name: called CC=3 SCAR=11.9
Pod undocumented
11  
12 293 ▶ $sep ||= $self->separator;
Condition33%1/3
$sep ||= $self->separator
l!l&&r!l&&!r
29300
MC/DC0%0/2
$sep ||= $self->separator
$sep $self->separator
Truth table33%1/3
A ||= B
A= $sepB= $self->separator
ABresult
1X1
011
000
13 293 $sep = "\Q$sep";
14  
15 293 ▶ if ( $sep eq '[]' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sep eq "[]") 0 293 293
16 0 return grep { defined } (
17 $name =~ /
18 ^ (\w+) # root param
19 | \[ (\w+) \] # nested
20 /gx
21 );
22 }
23  
24 # These next two regexes are the escaping aware equivalent
25 # to the following:
26 # my ($first, @segments) = split(/\./, $name, -1);
27  
28 # m// splits on unescaped '.' chars. Can't fail b/c \G on next
29 # non ./ * -> escaped anything -> non ./ *
30 293 $name =~ m/^ ( [^\\$sep]* (?: \\(?:.|$) [^\\$sep]* )* ) /gx;
31 293 my $first = $1;
32 293 $first =~ s/\\(.)/$1/g; # remove escaping
33  
34 293 my (@segments) = $name =~
35 # . -> ( non ./ * -> escaped anything -> non ./ * )
36 m/\G (?:[$sep]) ( [^\\$sep]* (?: \\(?:.|$) [^\\$sep]* )* ) /gx;
37 # Escapes removed later, can be used to avoid using as array index
38  
39 293 return ( $first, @segments );
40 }
41  
42 sub expand_hash {
43 146 ▶ my ( $self, $flat, $sep ) = @_;
Subroutine expand_hash: called CC=13 SCAR=27.7
Pod undocumented
44  
45 146 my $deep = {};
46 146 ▶ $sep ||= $self->separator;
Condition33%1/3
$sep ||= $self->separator
l!l&&r!l&&!r
01460
MC/DC0%0/2
$sep ||= $self->separator
$sep $self->separator
Truth table33%1/3
A ||= B
A= $sepB= $self->separator
ABresult
1X1
011
000
47  
48 146 for my $name ( keys %$flat ) {
49  
50 293 my ( $first, @segments ) = $self->split_name( $name, $sep );
51  
52 293 my $box_ref = \$deep->{$first};
53 293 for (@segments) {
54 148 ▶ if ( /^(0|[1-9]\d*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(0|[1-9]\d*)$/) { } 59 89 148
55 59 ▶ $$box_ref = [] unless defined $$box_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$box_ref 16 43 59
56 59 ▶ croak "HFH: param clash for $name=$_"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$box_ref eq "ARRAY" 0 59 59
57 unless ref $$box_ref eq 'ARRAY';
58 59 $box_ref = \( $$box_ref->[$1] );
59 }
60 else {
61 89 ▶ s/\\(.)/$1/g if $sep; # remove escaping
Branch: 1 branch
BranchTrueFalseTotal
if $sep 89 0 89
62 89 ▶ $$box_ref = {} unless defined $$box_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$box_ref 40 49 89
63 89 ▶ $$box_ref = { '' => $$box_ref } if ( !ref $$box_ref );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$box_ref 0 89 89
64 89 ▶ croak "HFH: param clash for $name=$_"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$box_ref eq "HASH" 0 89 89
65 unless ref $$box_ref eq 'HASH';
66 89 $box_ref = \( $$box_ref->{$_} );
67 }
68 }
69 293 ▶ if ( defined $$box_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$box_ref) 0 293 293
70 0 ▶ croak "HFH: param clash for $name value $flat->{$name}"
Branch: 1 branch
BranchTrueFalseTotal
if ref $$box_ref ne "HASH" 0 0 0
71 if ref $$box_ref ne 'HASH';
72 0 $box_ref = \( $$box_ref->{''} );
73 }
74 293 $$box_ref = $flat->{$name};
75 }
76 146 return $deep;
77 }
78  
79 sub collapse_hash {
80 0 ▶ my $self = shift;
Subroutine collapse_hash: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my $deep = shift;
82 0 my $flat = {};
83  
84 0 $self->_collapse_hash( $deep, $flat, () );
85 0 return $flat;
86 }
87  
88 sub join_name {
89 0 ▶ my ( $self, @array ) = @_;
Subroutine join_name: not called CC=1 SCAR=6.9
Pod undocumented
90 0 my $sep = substr( $self->separator, 0, 1 );
91 0 return join $sep, @array;
92 }
93  
94 sub _collapse_hash {
95 0 ▶ my ( $self, $deep, $flat, @segments ) = @_;
Subroutine _collapse_hash: not called CC=8 SCAR=42.8
96  
97 0 ▶ if ( !ref $deep ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $deep) { } 0 0 0
elsif (ref $deep eq 'HASH') { } 0 0 0
elsif (ref $deep eq 'ARRAY') { } 0 0 0
98 0 my $name = $self->join_name(@segments);
99 0 $flat->{$name} = $deep;
100 }
101 elsif ( ref $deep eq 'HASH' ) {
102 0 for ( keys %$deep ) {
103 # escape \ and separator chars (once only, at this level)
104 0 my $name = $_;
105 0 ▶ if ( defined( my $sep = $self->separator ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $sep = $self->separator)) 0 0 0
106 0 $sep = "\Q$sep";
107 0 $name =~ s/([\\$sep])/\\$1/g;
108 }
109 0 $self->_collapse_hash( $deep->{$_}, $flat, @segments, $name );
110 }
111 }
112 elsif ( ref $deep eq 'ARRAY' ) {
113 0 for ( 0 .. $#$deep ) {
114 0 ▶ $self->_collapse_hash( $deep->[$_], $flat, @segments, $_ )
Branch: 1 branch
BranchTrueFalseTotal
if defined $deep->[$_] 0 0 0
115 if defined $deep->[$_];
116 }
117 }
118 else {
119 0 croak "Unknown reference type for ", $self->join_name(@segments), ":", ref $deep;
120 }
121 }
122  
123 82 1;
124  
125 __END__
126  
127 =pod
128  
129 =encoding UTF-8
130  
131 =head1 NAME
132  
133 Data::MuForm::Params - params handling
134  
135 =head1 VERSION
136  
137 version 0.04
138  
139 =head1 AUTHOR
140  
141 Gerda Shank
142  
143 =head1 COPYRIGHT AND LICENSE
144  
145 This software is copyright (c) 2017 by Gerda Shank.
146  
147 This is free software; you can redistribute it and/or modify it under
148 the same terms as the Perl 5 programming language system itself.
149  
150 =cut
« MuForm.pm ↑ Summary MuForm/Role/RequestToken.pm »