blib/lib/Devel/StackTrace/AsHTMLExtended.pm

filter: Statementstmt 62.8% 93 / 148 Branchbran 44.2% 23 / 52 Conditioncond 37.5% 6 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 78.5% 11 / 14 Podpod 0.0% 0 / 3 totaltotal 55.1% 134 / 243 CC 35 SCAR 28.7
CC
35
Cov
56%
_build_lexicals
42.0
render
24.5
_build_arguments
17.0
CRAP 17.6
↑ Summary
1 package Devel::StackTrace::AsHTMLExtended;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.15';
6  
7 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Devel::StackTrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶no warnings 'qw';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 my %enc = qw( & &amp; > &gt; < &lt; " &quot; ' &#39; );
13  
14 # NOTE: because we don't know which encoding $str is in, or even if
15 # $str is a wide character (decoded strings), we just leave the low
16 # bits, including latin-1 range and encode everything higher as HTML
17 # entities. I know this is NOT always correct, but should mostly work
18 # in case $str is encoded in utf-8 bytes or wide chars. This is a
19 # necessary workaround since we're rendering someone else's code which
20 # we can't enforce string encodings.
21  
22 10 ▶sub encode_html {
Subroutine encode_html: called CC=2 SCAR=6.9
Pod undocumented
23 10 my $str = shift;
24 10 ▶ $str =~ s/([^\x00-\x21\x23-\x25\x28-\x3b\x3d\x3f-\xff])/$enc{$1} || '&#' . ord($1) . ';' /ge;
Condition100%2/2
$enc{$1} || '&#' . ord($1) . ';'
l!l
61
MC/DC100%1/1
$enc{$1} || '&#' . ord($1) . ';'
$enc{$1}
Truth table100%2/2
A
A= $enc{$1}
Aresult
11
00
25 10 utf8::downgrade($str);
26 10 $str;
27 }
28  
29 1 ▶sub Devel::StackTrace::as_html_extended {
Subroutine as_html_extended: called CC=1 SCAR=0.0
Pod undocumented
30 1 __PACKAGE__->render(@_);
31 }
32  
33 1 ▶sub render {
Subroutine render: called CC=16 SCAR=32.0
Pod undocumented
34 1 my $class = shift;
35 1 my $trace = shift;
36 1 my %opt = @_;
37  
38 1 ▶ $opt{max_dump_size} ||= 256*1024;
Condition50%1/2
$opt{"max_dump_size"} ||= 262144
l!l
01
MC/DC0%0/1
$opt{"max_dump_size"} ||= 262144
$opt{"max_dump_size"}
Truth table50%1/2
A
A= $opt{"max_dump_size"}
Aresult
11
00
39  
40 1 ▶ my $msg = $opt{msg} // encode_html($trace->frame(0)->as_string(1));
Condition33%1/3
$opt{"msg"} // encode_html($trace->frame(0)->as_string(1))
l!l&&r!l&&!r
010
MC/DC0%0/2
$opt{"msg"} // encode_html($trace->frame(0)->as_string(1))
$opt{"msg"} encode_html($trace->frame(0)->as_string(1))
Truth table33%1/3
A // B
A= $opt{"msg"}B= encode_html($trace->frame(0)->as_string(1))
ABresult
1X1
011
000
41  
42 1 my $out;
43  
44 1 ▶ if (!$opt{inline}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"inline"}) 1 0 1
45 1 $out = qq{<!doctype html>
46 <html><head><title>Error: ${msg}</title>};
47 1 $out .= '
48 <!--[if lt IE 9]>
49 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
50 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
51 <![endif]-->
52 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
53 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
54 <link href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/default.min.css" rel="stylesheet">
55 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
56 <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/highlight.min.js"></script>';
57 }
58  
59 1 ▶ if (ref $opt{style}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{'style'}) { } 0 1 1
60 0 $out .= qq(<style type="text/css">${$opt{style}}</style>);
61 } else {
62 1 $out .= qq(<link rel="stylesheet" type="text/css" href=") . encode_html($opt{style}) . q(" />);
63 }
64  
65 1 ▶ if (!$opt{inline}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"inline"}) 1 0 1
66 1 $out .= qq(</head><body style="padding:1em; border-top: 0px;"><h1>Error trace</h1><pre class="message">$msg</pre>);
67 }
68  
69 1 my $i = 0;
70 1 ▶ while (my $frame = $trace->next_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $frame = $trace->next_frame) 1 1 2
71 1 $i++;
72 1 my $next_frame = $trace->frame($i); # peek next
73  
74 1 my $file_link;
75 1 ▶ if ($frame->filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->filename) 1 0 1
76  
77 1 ▶ if ($opt{source_link}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'source_link'}) { } 0 1 1
78 0 my $link_url = $opt{source_link}->($frame->filename, $frame->line);
79 0 $file_link = q(<a href=") . encode_html($link_url) . q(">) . encode_html($frame->filename) . q(</a>);
80 } else {
81 1 $file_link = encode_html($frame->filename);
82 }
83 }
84
85 1 $out .= '<div>';
86 1 ▶ if ($next_frame && $next_frame->subroutine) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_frame and $next_frame->subroutine) { } 0 1 1
Condition33%1/3
$next_frame and $next_frame->subroutine
!ll&&!rl&&r
100
MC/DC0%0/2
$next_frame and $next_frame->subroutine
$next_frame $next_frame->subroutine
Truth table33%1/3
A and B
A= $next_frameB= $next_frame->subroutine
ABresult
0X0
100
111
87 0 $out .= "<h3><tt>" . encode_html($next_frame->subroutine) . "</tt></h3>";
88 0 $out .= '<h5> at ';
89 0 ▶ $out .= $frame->filename ? $file_link : '';
Branch: 1 branch
BranchTrueFalseTotal
$frame->filename ? : 0 0 0
90 0 $out .= ' line ';
91 0 $out .= $frame->line;
92 0 $out .= '</h5>';
93 } else {
94 1 $out .= '<h3>';
95 1 ▶ $out .= $frame->filename ? $file_link : '';
Branch: 1 branch
BranchTrueFalseTotal
$frame->filename ? : 1 0 1
96 1 $out .= ' line ';
97 1 $out .= $frame->line;
98 1 $out .= '</h3>';
99 }
100 1 $out .= '</div>';
101  
102 1 $out .= '<div style="margin-left:2em">';
103 1 $out .= '<pre><code class="perl">';
104 1 $out .= _build_context($frame);
105 1 $out .= '</code></pre>';
106  
107 1 ▶ if (!$opt{inline}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"inline"}) 1 0 1
108 1 $out .= qq{<div class="panel-group" id="accordion-$i">};
109 1 $out .= _build_arguments($i, $next_frame, \%opt);
110 1 ▶ if ($frame->can('lexicals')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->can("lexicals")) 0 1 1
111 0 $out .= _build_lexicals($i, $frame->lexicals, \%opt)
112 }
113 1 $out .= qq{</div>};
114 }
115  
116 1 $out .= '</div>';
117 }
118  
119 1 ▶ if (!$opt{inline}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"inline"}) 1 0 1
120 1 $out .= '<script>hljs.initHighlightingOnLoad();
121 $(".collapse").collapse("hide");
122 $(".collapse_toggle").click(
123 function () {
124 var c = $(this).html();
125 if (c.indexOf("View") != -1) {
126 c = c.replace("View", "Hide");
127 $(this).html(c);
128 } else if(c.indexOf("Hide") != -1) {
129 c = c.replace("Hide", "View");
130 $(this).html(c);
131 }
132 })
133 </script>';
134  
135 1 $out .= "<footer>Generated by Devel::StackTrace::AsHTMLExtended $VERSION</footer>";
136 1 $out .= '</body></html>';
137 }
138  
139 1 $out;
140 }
141  
142 0 ▶my $dumper = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
143 0 my $value = shift;
144 0 ▶ $value = $$value if ref $value eq 'SCALAR' or ref $value eq 'REF';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "SCALAR" or ref $value eq "REF" 0 0 0
Condition0%0/3
ref $value eq "SCALAR" or ref $value eq "REF"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $value eq "SCALAR" or ref $value eq "REF"
ref $value eq "SCALAR" ref $value eq "REF"
Truth table0%0/3
A or B
A= ref $value eq "SCALAR"B= ref $value eq "REF"
ABresult
1X1
011
000
145 0 my $d = Data::Dumper->new([ $value ]);
146 0 $d->Indent(1)->Terse(1)->Deparse(1);
147 0 chomp(my $dump = $d->Dump);
148 0 $dump;
149 2 };
150  
151 0 ▶sub _build_accordian_panel {
Subroutine _build_accordian_panel: not called CC=1 SCAR=6.9
152 0 my ($id, $sub_id, $title, $contents) = @_;
153 0 my $ref = "p-$id-$sub_id";
154  
155 0 my $html = '<div class="panel panel-default"><div class="panel-heading"><h5 class="panel-title">';
156 0 $html .= qq{<a data-toggle="collapse" data-parent="#accordion-$id" href="#$ref" class="collapse_toggle">};
157 0 $html .= $title;
158 0 $html .= '</a></h5></div>';
159 0 $html .= qq{<div id="$ref" class="panel-collapse collapse in"><div class="panel-body">$contents</div></div>};
160 0 $html .= '</div>';
161 0 return $html;
162 }
163  
164 1 ▶sub _build_arguments {
Subroutine _build_arguments: called CC=5 SCAR=28.3
165 1 my($id, $frame, $opt) = @_;
166 1 my $ref = "arg-$id";
167  
168 1 ▶ return '' unless $frame && $frame->args;
Branch: 1 branch
BranchTrueFalseTotal
unless $frame and $frame->args 1 0 1
Condition33%1/3
$frame and $frame->args
!ll&&!rl&&r
100
MC/DC0%0/2
$frame and $frame->args
$frame $frame->args
Truth table33%1/3
A and B
A= $frameB= $frame->args
ABresult
0X0
100
111
169  
170 0 my @args = $frame->args;
171  
172 0 my $html = qq(<table class="table table-hover table-condensed table-bordered"><tr><th>Argument</th><th>Value</th></tr>);
173
174 # Don't use while each since Dumper confuses that
175 0 for my $idx (0 .. @args - 1) {
176 0 my $value = $args[$idx];
177 0 my $dump = $dumper->($value);
178 0 $html .= qq{<tr>};
179 0 $html .= qq{<td class="variable"><tt>\$_[$idx]</tt></td>};
180  
181 0 ▶ if (length($dump) > $opt->{max_dump_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dump > $opt->{"max_dump_size"}) 0 0 0
182 0 $dump = "[Warning: Truncated to dump limit]\n" . substr($dump, 0, $opt->{max_dump_size});
183 }
184  
185 0 $html .= qq{<td class="value"><pre class="pre-scrollable"><code>} . encode_html($dump) . qq{</code></pre></td>};
186 0 $html .= qq{</tr>};
187 }
188 0 $html .= qq(</table>);
189  
190 0 return _build_accordian_panel($id, "args", "View Arguments", $html);
191 }
192  
193 0 ▶sub _build_lexicals {
Subroutine _build_lexicals: not called CC=6 SCAR=37.4
194 0 my($id, $lexicals, $opt) = @_;
195  
196 0 ▶ return '' unless keys %$lexicals;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$lexicals 0 0 0
197  
198 0 my $html = qq(<table class="table table-hover table-condensed table-bordered"><tr><th>Variable</th><th>Value</th></tr>);
199 # Don't use while each since Dumper confuses that
200 0 for my $var (sort keys %$lexicals) {
201 0 my $value = $lexicals->{$var};
202 0 my $dump = $dumper->($value);
203 0 ▶ $dump =~ s/^\{(.*)\}$/($1)/s if $var =~ /^\%/;
Branch: 1 branch
BranchTrueFalseTotal
if $var =~ /^\%/ 0 0 0
204 0 ▶ $dump =~ s/^\[(.*)\]$/($1)/s if $var =~ /^\@/;
Branch: 1 branch
BranchTrueFalseTotal
if $var =~ /^\@/ 0 0 0
205 0 $html .= q{<tr>};
206 0 $html .= q{<td class="variable"><tt>} . encode_html($var) . q{</tt></td>};
207  
208 0 ▶ if (length($dump) > $opt->{max_dump_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dump > $opt->{"max_dump_size"}) 0 0 0
209 0 $dump = "[Warning: Truncated to dump limit]\n" . substr($dump, 0, $opt->{max_dump_size});
210 }
211  
212 0 $html .= q{<td class="value"><pre class="pre-scrollable"><code>} . encode_html($dump) . q{</code></pre></td>};
213 0 $html .= q{</tr>};
214 }
215 0 $html .= qq(</table>);
216  
217 0 return _build_accordian_panel($id, "lex", "View Lexicals", $html);
218 }
219  
220 1 ▶sub _build_context {
Subroutine _build_context: called CC=8 SCAR=20.9
221 1 my $frame = shift;
222 1 my $file = $frame->filename;
223 1 my $linenum = $frame->line;
224 1 my $code;
225 1 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 1 0 1
226 1 my $start = $linenum - 3;
227 1 my $end = $linenum + 3;
228 1 ▶ $start = $start < 1 ? 1 : $start;
Branch: 1 branch
BranchTrueFalseTotal
$start < 1 ? : 0 1 1
229 1 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "cannot open ${file}:$!" 1 0 1
230 or die "cannot open $file:$!";
231 1 my $cur_line = 0;
232 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 9 0 9
233 9 ++$cur_line;
234 9 ▶ last if $cur_line > $end;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_line > $end 1 8 9
235 8 ▶ next if $cur_line < $start;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_line < $start 1 7 8
236 7 $line =~ s|\t| |g;
237 7 ▶ my @tag = $cur_line == $linenum
Branch: 1 branch
BranchTrueFalseTotal
$cur_line == $linenum ? : 1 6 7
238 ? (q{<strong style="background: #faa">}, '</strong>')
239 : ('', '');
240 7 $code .= sprintf(
241 '%s%5d: %s%s', $tag[0], $cur_line, encode_html($line),
242 $tag[1],
243 );
244 }
245 1 close $file;
246 }
247 1 return $code;
248 }
249  
250 2 1;
251 __END__
↑ Summary