QuadPres/WriteContents.pm

filter: Statementstmt 26.2% 21 / 80 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 5 / 10 Podpod 100% 1 / 1 totaltotal 23.6% 27 / 114 CC 13 SCAR 44.6
CC
13
Cov
21%
_stringify_contents
156.0
_get_key_order
6.0
_sort_keys
2.0
CRAP 86.1
« QuadPres/Url.pm ↑ Summary QuadPres.pm »
1 package QuadPres::WriteContents;
2 1 $QuadPres::WriteContents::VERSION = '0.32.0';
3 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use MooX qw/ late /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 has '_contents' =>
11 ( isa => "HashRef", is => "ro", init_arg => "contents", required => 1, );
12  
13  
14 1 my @output_contents_keys_order = (qw(url title subs images));
15  
16 my %output_contents_keys_values =
17 1 ( map { $output_contents_keys_order[$_] => $_ }
18 ( 0 .. $#output_contents_keys_order ) );
19  
20 sub _get_key_order
21 0 ▶{
Subroutine _get_key_order: not called CC=2 SCAR=17.9
22 0 my ($key) = (@_);
23  
24 return
25 exists( $output_contents_keys_values{$key} )
26 0 ▶ ? $output_contents_keys_values{$key}
Branch: 1 branch
BranchTrueFalseTotal
exists $output_contents_keys_values{$key} ? : 0 0 0
27 : scalar(@output_contents_keys_order);
28 }
29  
30 sub _sort_keys
31 0 ▶{
Subroutine _sort_keys: not called CC=1 SCAR=6.9
32 0 my ($hash) = @_;
33 return [
34 0 sort { _get_key_order($a) <=> _get_key_order($b) }
35 keys(%$hash)
36 ];
37 }
38 1 my %special_chars = (
39 "\n" => "\\n",
40 "\t" => "\\t",
41 "\r" => "\\r",
42 "\f" => "\\f",
43 "\b" => "\\b",
44 "\a" => "\\a",
45 "\e" => "\\e",
46 );
47  
48 sub _string_to_perl
49 0 ▶{
Subroutine _string_to_perl: not called CC=1 SCAR=6.9
50 0 my $s = shift;
51 0 $s =~ s/([\\\"])/\\$1/g;
52  
53 0 $s =~ s/([\n\t\r\f\b\a\e])/$special_chars{$1}/ge;
54 0 $s =~ s/([\x00-\x1F\x80-\xFF])/sprintf("\\x%.2xd", ord($1))/ge;
55  
56 0 return $s;
57 }
58  
59  
60 sub update_contents
61 0 ▶{
Subroutine update_contents: not called CC=1 SCAR=6.9
Pod documented
62 0 my ($self) = @_;
63  
64 0 open my $contents_fh, ">", "Contents.pm";
65 0 print {$contents_fh}
66 "package Contents;\n\nuse strict;\n\nmy \$contents = \n";
67  
68 0 print {$contents_fh} $self->_stringify_contents();
69  
70 0 print {$contents_fh} <<"EOF";
71  
72 sub get_contents
73 {
74 return \$contents;
75 }
76  
77 1;
78 EOF
79  
80 0 close($contents_fh);
81 }
82  
83 sub _stringify_contents
84 0 ▶{
Subroutine _stringify_contents: not called CC=12 SCAR=50.5
85 0 my $self = shift;
86  
87 0 my $contents = $self->_contents();
88  
89 0 my $indent = " ";
90  
91 0 my @branches = ( { 'b' => $contents, 'i' => -1 } );
92  
93 0 my $ret = "";
94  
95 0 ▶MAIN_LOOP: while ( @branches > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (@branches > 0) 0 0 0
96 {
97 0 my $last_element = $branches[$#branches];
98 0 my $b = $last_element->{'b'};
99 0 my $i = $last_element->{'i'};
100 0 my $p1 = $indent x ( 2 * ( scalar(@branches) - 1 ) );
101 0 my $p2 = $p1 . $indent;
102 0 my $p3 = $p2 . $indent;
103 0 ▶ if ( $i < 0 )
Branch: 2 branches
BranchTrueFalseTotal
if ($i < 0) { } 0 0 0
elsif (not exists $b->{'subs'} or $i >= scalar @{$b->{'subs'};}) { } 0 0 0
104 {
105 0 $ret .= "${p1}\{\n";
106 0 foreach my $field (qw(url title))
107 {
108 0 ▶ if ( exists( $b->{$field} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $b->{$field}) 0 0 0
109 {
110 $ret .= "${p2}'$field' => \""
111 0 . _string_to_perl( $b->{$field} ) . "\",\n";
112 }
113 }
114  
115 0 ▶ if ( exists( $b->{'subs'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $b->{"subs"}) 0 0 0
116 {
117 0 $ret .= "${p2}'subs' =>\n";
118 0 $ret .= "${p2}\[\n";
119  
120 # push @branches { 'b' => $b->{'subs'} }
121 }
122 0 $last_element->{'i'} = 0;
123 0 next MAIN_LOOP;
124 }
125 ▶ elsif (( !exists( $b->{'subs'} ) )
Condition0%0/3
not exists $b->{"subs"} or $i >= scalar @{$b->{"subs"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $b->{"subs"} or $i >= scalar @{$b->{"subs"};}
not exists $b->{"subs"} $i >= scalar @{$b->{"subs"};}
Truth table0%0/3
not(A) or B
A= not exists $b->{"subs"}B= $i >= scalar @{$b->{"subs"};}
ABresult
1X1
011
000
126 0 || ( $i >= scalar( @{ $b->{'subs'} } ) ) )
127 {
128 0 ▶ $ret .= "${p2}],\n" if ( exists( $b->{'subs'} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $b->{"subs"} 0 0 0
129 0 ▶ if ( exists( $b->{'images'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $b->{"images"}) 0 0 0
130 {
131 0 $ret .= "${p2}'images' =>\n";
132 0 $ret .= "${p2}\[\n";
133 0 foreach my $img ( @{ $b->{'images'} } )
134 {
135 0 $ret .= "${p3}\"" . _string_to_perl($img) . "\",\n";
136 }
137 0 $ret .= "${p2}],\n";
138 }
139 0 pop(@branches);
140 0 ▶ $ret .= "${p1}}" . ( ( @branches > 0 ) ? "," : ";" ) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
@branches > 0 ? : 0 0 0
141 0 next MAIN_LOOP;
142 }
143 else
144 {
145 0 push @branches, { 'b' => $b->{'subs'}->[$i], 'i' => -1 };
146 0 ++( $last_element->{'i'} );
147 0 next MAIN_LOOP;
148 }
149 }
150  
151 0 return $ret;
152 }
153  
154 1 1;
155  
156 __END__
« QuadPres/Url.pm ↑ Summary QuadPres.pm »