lib/byterazor/HiD/BrokenLink.pm

filter: Statementstmt 98.4% 184 / 187 Branchbran 81.0% 81 / 100 Conditioncond 62.5% 55 / 88 MC/DCmcdc 43.7% 21 / 48 Subroutinesub 100% 22 / 22 Podpod 100% 2 / 2 totaltotal 81.6% 365 / 447 CC 91 SCAR 29.7
CC
91
Cov
85%
scan
37.5
_extract_css_urls
37.1
_base_directories
10.6
CRAP 19.6
↑ Summary
1 # ABSTRACT: broken link checker for published sites
2  
3  
4 package byterazor::HiD::BrokenLink;
5  
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 extends 'HiD::Plugin';
8  
9 1 ▶use 5.014; # strict
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings qw/ FATAL /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our $VERSION = '0.1';
19  
20  
21 6 ▶sub after_publish {
Subroutine after_publish: called CC=9 SCAR=22.0
Pod documented
22 6 my( $self , $site ) = @_;
23  
24 6 ▶ my $cfg = $site->config->{brokenlink} || {};
Condition50%1/2
$site->config->{'brokenlink'} || {}
l!l
60
MC/DC0%0/1
$site->config->{'brokenlink'} || {}
$site->config->{'brokenlink'}
Truth table50%1/2
A
A= $site->config->{'brokenlink'}
Aresult
11
00
25 6 ▶ return 1 unless $cfg->{enabled} // 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->{"enabled"} // 1 1 5 6
Condition50%1/2
$cfg->{"enabled"} // 1
l!l
60
MC/DC0%0/1
$cfg->{"enabled"} // 1
$cfg->{"enabled"}
Truth table50%1/2
A
A= $cfg->{"enabled"}
Aresult
11
00
26  
27 5 my $result = $self->scan( $site );
28 5 my $broken = $result->{broken};
29  
30 5 for my $b ( @$broken ) {
31 $site->WARN(
32 "brokenlink: $b->{src}: $b->{link} -> missing $b->{resolved}"
33 4 ▶ . ( $b->{reason} ? " ($b->{reason})" : '' )
Branch: 1 branch
BranchTrueFalseTotal
$b->{'reason'} ? : 1 3 4
34 );
35 }
36  
37 $site->INFO(
38 sprintf(
39 "brokenlink: checked %d reference(s) in %d file(s); %d broken" ,
40 5 $result->{checked} , $result->{files_checked} , scalar @$broken
41 )
42 );
43  
44 5 ▶ $self->_write_report( $cfg->{report} , $result ) if $cfg->{report};
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->{"report"} 1 4 5
45  
46 5 ▶ if ( $cfg->{fail_on_broken} && @$broken ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->{"fail_on_broken"} and @$broken) 1 4 5
Condition100%3/3
$cfg->{"fail_on_broken"} and @$broken
!ll&&!rl&&r
311
MC/DC100%2/2
$cfg->{"fail_on_broken"} and @$broken
$cfg->{"fail_on_broken"} @$broken
Truth table100%3/3
A and B
A= $cfg->{"fail_on_broken"}B= @$broken
ABresult
0X0
100
111
47 1 die "brokenlink: " . scalar( @$broken ) . " broken reference(s) found";
48 }
49  
50 4 return 1;
51 }
52  
53  
54 33 ▶sub scan {
Subroutine scan: called CC=37 SCAR=36.2
Pod documented
55 33 my( $self , $site ) = @_;
56  
57 33 my $dest = $site->destination;
58 33 ▶ my $cfg = $site->config->{brokenlink} || {};
Condition50%1/2
$site->config->{'brokenlink'} || {}
l!l
330
MC/DC0%0/1
$site->config->{'brokenlink'} || {}
$site->config->{'brokenlink'}
Truth table50%1/2
A
A= $site->config->{'brokenlink'}
Aresult
11
00
59  
60 33 ▶ my $check_html = $cfg->{check_html} // 1;
Condition50%1/2
$cfg->{"check_html"} // 1
l!l
033
MC/DC0%0/1
$cfg->{"check_html"} // 1
$cfg->{"check_html"}
Truth table50%1/2
A
A= $cfg->{"check_html"}
Aresult
11
00
61 33 ▶ my $check_css = $cfg->{check_css} // 1;
Condition100%2/2
$cfg->{"check_css"} // 1
l!l
132
MC/DC100%1/1
$cfg->{"check_css"} // 1
$cfg->{"check_css"}
Truth table100%2/2
A
A= $cfg->{"check_css"}
Aresult
11
00
62 33 ▶ my $check_js = $cfg->{check_js} // 0;
Condition100%2/2
$cfg->{"check_js"} // 0
l!l
132
MC/DC100%1/1
$cfg->{"check_js"} // 0
$cfg->{"check_js"}
Truth table100%2/2
A
A= $cfg->{"check_js"}
Aresult
11
00
63 33 ▶ my $strip_baseurl = $cfg->{strip_baseurl} // 1;
Condition100%2/2
$cfg->{"strip_baseurl"} // 1
l!l
132
MC/DC100%1/1
$cfg->{"strip_baseurl"} // 1
$cfg->{"strip_baseurl"}
Truth table100%2/2
A
A= $cfg->{"strip_baseurl"}
Aresult
11
00
64 33 ▶ my $exclude = $cfg->{exclude} || [];
Condition100%2/2
$cfg->{'exclude'} || []
l!l
132
MC/DC100%1/1
$cfg->{'exclude'} || []
$cfg->{'exclude'}
Truth table100%2/2
A
A= $cfg->{'exclude'}
Aresult
11
00
65  
66 my $prefix = $strip_baseurl
67 ? $self->_baseurl_prefix( $site->config->{baseurl} )
68 33 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
$strip_baseurl ? : 32 1 33
69  
70 33 my ( %files , @file_rels ) = ( () , () );
71 33 my ( %dirs , @dir_rels ) = ( () , () );
72  
73 33 ▶ if ( -d $dest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dest) 33 0 33
74 33 for my $f ( File::Find::Rule->file()->in( $dest ) ) {
75 52 my $rel = _rel( $f , $dest );
76 52 ▶ next if $rel eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $rel eq "." 0 52 52
77 52 $files{$rel} = 1;
78 52 push @file_rels , $rel;
79 }
80  
81 33 for my $d ( File::Find::Rule->directory()->in( $dest ) ) {
82 47 my $rel = _rel( $d , $dest );
83 47 ▶ next if $rel eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $rel eq "." 33 14 47
84 14 $dirs{$rel} = 1;
85 14 push @dir_rels , $rel;
86 }
87 }
88  
89 33 my $files_checked = 0;
90 33 my $checked = 0;
91 33 my @broken;
92  
93 33 for my $rel ( sort @file_rels ) {
94 52 my $is_html = $rel =~ m{\A(?:[^/]+/)*[^/]*\.html?\z}i;
95 52 my $is_css = $rel =~ m{\A(?:[^/]+/)*[^/]*\.css\z}i;
96 52 my $is_js = $rel =~ m{\A(?:[^/]+/)*[^/]*\.js\z}i;
97  
98 52 ▶ next unless ( $is_html and $check_html )
Branch: 1 branch
BranchTrueFalseTotal
unless $is_html and $check_html or $is_css and $check_css or $is_js and $check_js 9 43 52
Condition93%14/15
$is_html and $check_html or $is_css and $check_css or $is_js and $check_js
l!l&&r!l&&!r
4219
$is_html and $check_html or $is_css and $check_css
l!l&&r!l&&!r
39310
$is_html and $check_html
!ll&&!rl&&r
13039
$is_css and $check_css
!ll&&!rl&&r
913
$is_js and $check_js
!ll&&!rl&&r
721
MC/DC83%5/6
$is_html and $check_html or $is_css and $check_css or $is_js and $check_js
$is_html $check_html $is_css $check_css $is_js $check_js
Truth table40%6/15
A and B or C and D or E and F
A= $is_htmlB= $check_htmlC= $is_cssD= $check_cssE= $is_jsF= $check_js
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
99 or ( $is_css and $check_css )
100 or ( $is_js and $check_js );
101  
102 43 $files_checked++;
103  
104 43 my $content = path( $dest , $rel )->slurp_raw;
105  
106 43 my $source_dir = $rel;
107 43 $source_dir =~ s{[^/]+\z}{};
108 43 $source_dir =~ s{/+\z}{};
109  
110 43 my ( $refs , $base_href ) = ( [] , undef );
111  
112 43 ▶ if ( $is_html ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_html) { } 39 4 43
elsif ($is_css) { } 3 1 4
113 39 ( $refs , $base_href ) = $self->_extract_html( $content );
114 }
115 elsif ( $is_css ) {
116 3 $refs = $self->_extract_css_urls( $content );
117 }
118 else {
119 1 $refs = $self->_extract_js_refs( $content );
120 }
121  
122 43 my ( $resolve_dir , $skip_all ) = $self->_base_directories(
123 $base_href , $source_dir , $prefix
124 );
125  
126 43 for my $ref ( @$refs ) {
127 41 ▶ next if @$exclude and grep { $ref =~ /$_/ } @$exclude;
Branch: 1 branch
BranchTrueFalseTotal
if @$exclude and grep {$ref =~ /$_/u;} @$exclude 1 40 41
Condition100%3/3
@$exclude and grep {$ref =~ /$_/u;} @$exclude
!ll&&!rl&&r
3911
MC/DC100%2/2
@$exclude and grep {$ref =~ /$_/u;} @$exclude
@$exclude grep {$ref =~ /$_/u;} @$exclude
Truth table100%3/3
A and B
A= @$excludeB= grep {$ref =~ /$_/u;} @$exclude
ABresult
0X0
100
111
128  
129 40 my $link = $ref;
130 40 ▶ $link =~ s{\A\Q$prefix\E(?=[/?#]|\z)}{} if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 3 37 40
131  
132 40 ▶ my $target = $skip_all ? undef : $self->_classify( $link , $resolve_dir );
Branch: 1 branch
BranchTrueFalseTotal
$skip_all ? : 1 39 40
133 40 ▶ next unless defined $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 10 30 40
134  
135 30 $checked++;
136  
137 30 ▶ if ( $target->{escape} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->{"escape"}) 2 28 30
138 2 push @broken , {
139 src => $rel ,
140 link => $ref ,
141 resolved => $ref ,
142 reason => 'escapes destination' ,
143 };
144 2 next;
145 }
146  
147 28 my $r = $target->{rel};
148 my $ok = $r eq ''
149 ? 1
150 : ( $target->{trailing}
151 ? ( exists $dirs{$r} or exists $files{"$r/index.html"} )
152 : ( exists $files{$r}
153 or exists $dirs{$r}
154 28 ▶ or exists $files{"$r/index.html"} ) );
Branch: 2 branches
BranchTrueFalseTotal
$r eq '' ? : 0 28 28
$target->{'trailing'} ? : 2 26 28
Condition77%7/9
exists $dirs{$r} or exists $files{"$r/index.html"}
l!l&&r!l&&!r
101
exists $files{$r} or exists $dirs{$r} or exists $files{"$r/index.html"}
l!l&&r!l&&!r
1709
exists $files{$r} or exists $dirs{$r}
l!l&&r!l&&!r
1619
MC/DC50%1/2
exists $dirs{$r} or exists $files{"$r/index.html"}
exists $dirs{$r} exists $files{"$r/index.html"}
MC/DC66%2/3
exists $files{$r} or exists $dirs{$r} or exists $files{"$r/index.html"}
exists $files{$r} exists $dirs{$r} exists $files{"$r/index.html"}
Truth table66%2/3
A or B
A= exists $dirs{$r}B= exists $files{"$r/index.html"}
ABresult
1X1
011
000
Truth table75%3/4
A or B or C
A= exists $files{$r}B= exists $dirs{$r}C= exists $files{"$r/index.html"}
ABCresult
1XX1
01X1
0011
0000
155  
156 push @broken , {
157 src => $rel ,
158 link => $ref ,
159 28 ▶ resolved => $r eq '' ? '/' : $r . ( $target->{trailing} ? '/' : '' ) ,
Branch: 3 branches
BranchTrueFalseTotal
unless $ok 10 18 28
$r eq '' ? : 0 10 10
$target->{'trailing'} ? : 1 9 10
160 } unless $ok;
161 }
162 }
163  
164 return {
165 33 files => [ sort keys %files ] ,
166 dirs => [ sort keys %dirs ] ,
167 files_checked => $files_checked ,
168 checked => $checked ,
169 broken => \@broken ,
170 };
171 }
172  
173  
174 39 ▶sub _extract_html {
Subroutine _extract_html: called CC=1 SCAR=0.1
175 39 my( $self , $content ) = @_;
176  
177 39 my @refs;
178 my $base_href;
179 39 my $style_depth = 0;
180 39 my $style_buf = '';
181  
182 my $parser = HTML::Parser->new(
183 api_version => 3 ,
184 handlers => {
185 start => [
186 50 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=22.3
187 50 my( $p , $tag , $attr ) = @_;
188  
189 50 ▶ if ( $tag eq 'base' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "base") 3 47 50
190 $base_href = $attr->{href}
191 if !defined $base_href
192 3 ▶ and defined $attr->{href} and length $attr->{href};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $base_href and defined $attr->{"href"} and length $attr->{"href"} 3 0 3
Condition33%2/6
not defined $base_href and defined $attr->{"href"} and length $attr->{"href"}
!ll&&!rl&&r
003
not defined $base_href and defined $attr->{"href"}
!ll&&!rl&&r
003
MC/DC0%0/3
not defined $base_href and defined $attr->{"href"} and length $attr->{"href"}
not defined $base_href defined $attr->{"href"} length $attr->{"href"}
Truth table25%1/4
not(A) and B and C
A= not defined $base_hrefB= defined $attr->{"href"}C= length $attr->{"href"}
ABCresult
0XX0
10X0
1100
1111
193 3 return; # the base href itself is not a reference
194 }
195  
196 push @refs , $attr->{$_}
197 47 ▶ for grep { defined $attr->{$_} and length $attr->{$_} }
Condition66%2/3
defined $attr->{$_} && length $attr->{$_}
!ll&&!rl&&r
153035
MC/DC50%1/2
defined $attr->{$_} && length $attr->{$_}
defined $attr->{$_} length $attr->{$_}
Truth table66%2/3
A && B
A= defined $attr->{$_}B= length $attr->{$_}
ABresult
0X0
100
111
198 qw/ href src data poster /;
199  
200 47 ▶ $style_depth++ if $tag eq 'style';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "style" 1 46 47
201 } ,
202 'self,tagname,attr' ,
203 ],
204 end => [
205 40 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
206 40 ▶ if ( $_[1] eq 'style' and $style_depth ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] eq "style" and $style_depth) 1 39 40
Condition66%2/3
$_[1] eq "style" and $style_depth
!ll&&!rl&&r
3901
MC/DC50%1/2
$_[1] eq "style" and $style_depth
$_[1] eq "style" $style_depth
Truth table66%2/3
A and B
A= $_[1] eq "style"B= $style_depth
ABresult
0X0
100
111
207 1 $style_depth--;
208 1 ▶ if ( !$style_depth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $style_depth) 1 0 1
209 1 push @refs , @{ $self->_extract_css_urls( $style_buf ) };
210 1 $style_buf = '';
211 }
212 }
213 } ,
214 'self,tagname' ,
215 ],
216 text => [
217 38 ▶ sub { $style_buf .= $_[1] if $style_depth; } ,
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $style_depth 1 37 38
218 39 'self,text,is_cdata' ,
219 ],
220 },
221 );
222  
223 39 $parser->parse( $content );
224  
225 39 return \@refs , $base_href;
226 }
227  
228  
229 4 ▶sub _extract_css_urls {
Subroutine _extract_css_urls: called CC=11 SCAR=36.1
230 4 my( $self , $content ) = @_;
231  
232 4 my @refs;
233  
234 4 ▶ while ( $content =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ / url\s*\(\s* (?: "([^"]*)" | '([^']*)' | ([^\s)"']+) ) \s*\) /gux) 3 4 7
235 url\s*\(\s*
236 (?: "([^"]*)"
237 | '([^']*)'
238 | ([^\s)"']+) )
239 \s*\)
240 }gx ) {
241 3 ▶ my $ref = $1 // $2 // $3;
Condition33%2/6
($1 // $2) // $3
l!l&&r!l&&!r
030
$1 // $2
l!l&&r!l&&!r
003
MC/DC0%0/3
($1 // $2) // $3
$1 $2 $3
Truth table25%1/4
A // B // C
A= $1B= $2C= $3
ABCresult
1XX1
01X1
0011
0000
242 3 ▶ push @refs , $ref if defined $ref and length $ref;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref and length $ref 3 0 3
Condition33%1/3
defined $ref and length $ref
!ll&&!rl&&r
003
MC/DC0%0/2
defined $ref and length $ref
defined $ref length $ref
Truth table33%1/3
A and B
A= defined $refB= length $ref
ABresult
0X0
100
111
243 }
244  
245 4 ▶ while ( $content =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ / \@\s*import\s+ (?: "([^"]*)" | '([^']*)' | (?!(?:url\s*)?\()([^\s;)"']+) ) /gux) 0 4 4
246 @\s*import\s+
247 (?: "([^"]*)"
248 | '([^']*)'
249 | (?!(?:url\s*)?\()([^\s;)"']+) )
250 }gx ) {
251 0 ▶ my $ref = $1 // $2 // $3;
Condition0%0/6
($1 // $2) // $3
l!l&&r!l&&!r
000
$1 // $2
l!l&&r!l&&!r
000
MC/DC0%0/3
($1 // $2) // $3
$1 $2 $3
Truth table0%0/4
A // B // C
A= $1B= $2C= $3
ABCresult
1XX1
01X1
0011
0000
252 0 ▶ push @refs , $ref if defined $ref and length $ref;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref and length $ref 0 0 0
Condition0%0/3
defined $ref and length $ref
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ref and length $ref
defined $ref length $ref
Truth table0%0/3
A and B
A= defined $refB= length $ref
ABresult
0X0
100
111
253 }
254  
255 4 return \@refs;
256 }
257  
258  
259 1 ▶sub _extract_js_refs {
Subroutine _extract_js_refs: called CC=2 SCAR=6.9
260 1 my( $self , $content ) = @_;
261  
262 1 my @refs;
263  
264 1 ▶ while ( $content =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[ ["'] ( /(?:[^/"'\n]+/)*[^/"'\n]* \.(?:css|js|mjs|json|png|jpe?g|gif|svg|webp|avif|ico |woff2?|ttf|otf|eot|mp4|webm|mp3|ogg) ) ["'] ]gux) 3 1 4
265 ["']
266 ( /(?:[^/"'\n]+/)*[^/"'\n]*
267 \.(?:css|js|mjs|json|png|jpe?g|gif|svg|webp|avif|ico
268 |woff2?|ttf|otf|eot|mp4|webm|mp3|ogg) )
269 ["']
270 }gx ) {
271 3 push @refs , $1;
272 }
273  
274 1 return \@refs;
275 }
276  
277  
278 43 ▶sub _base_directories {
Subroutine _base_directories: called CC=9 SCAR=23.6
279 43 my( $self , $base_href , $source_dir , $prefix ) = @_;
280  
281 43 ▶ return ( $source_dir , 0 )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base_href and length $base_href 40 3 43
Condition66%2/3
defined $base_href and length $base_href
!ll&&!rl&&r
4003
MC/DC50%1/2
defined $base_href and length $base_href
defined $base_href length $base_href
Truth table66%2/3
A and B
A= defined $base_hrefB= length $base_href
ABresult
0X0
100
111
282 unless defined $base_href and length $base_href;
283  
284 3 my $href = $base_href;
285 3 ▶ $href =~ s{\A\Q$prefix\E(?=[/?#]|\z)}{} if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 3 3
286 3 $href =~ s{\#.*}{};
287 3 $href =~ s{\?.*}{};
288  
289 3 ▶ if ( $href =~ m{\A//} or $href =~ m{\A[a-zA-Z][a-zA-Z0-9+.\-]*:} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href =~ m[\A//]u or $href =~ /\A[a-zA-Z][a-zA-Z0-9+.\-]*:/u) 1 2 3
Condition66%2/3
$href =~ m[\A//]u or $href =~ /\A[a-zA-Z][a-zA-Z0-9+.\-]*:/u
l!l&&r!l&&!r
012
MC/DC50%1/2
$href =~ m[\A//]u or $href =~ /\A[a-zA-Z][a-zA-Z0-9+.\-]*:/u
$href =~ m[\A//]u $href =~ /\A[a-zA-Z][a-zA-Z0-9+.\-]*:/u
Truth table66%2/3
A or B
A= $href =~ m[\A//]uB= $href =~ /\A[a-zA-Z][a-zA-Z0-9+.\-]*:/u
ABresult
1X1
011
000
290 1 return ( undef , 1 );
291 }
292  
293 2 $href =~ s{/+\z}{};
294  
295 2 my $resolve_dir = $source_dir;
296 2 ▶ if ( $href =~ m{\A/} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($href =~ m[\A/]u) { } 2 0 2
elsif (length $href) { } 0 0 0
297 2 $href =~ s{\A/}{};
298 2 $resolve_dir = $href;
299 }
300 elsif ( length $href ) {
301 0 ▶ $resolve_dir = $source_dir ne '' ? $source_dir . '/' . $href : $href;
Branch: 1 branch
BranchTrueFalseTotal
$source_dir ne '' ? : 0 0 0
302 }
303  
304 2 return ( $resolve_dir , 0 );
305 }
306  
307  
308 39 ▶sub _classify {
Subroutine _classify: called CC=6 SCAR=17.9
309 39 my( $self , $link , $resolve_dir ) = @_;
310  
311 39 $link =~ s{\#.*}{};
312 39 $link =~ s{\?.*}{};
313 39 ▶ return unless length $link;
Branch: 1 branch
BranchTrueFalseTotal
unless length $link 1 38 39
314  
315 38 ▶ return if $link =~ m{\A//};
Branch: 1 branch
BranchTrueFalseTotal
if $link =~ m[\A//]u 1 37 38
316 37 ▶ return if $link =~ m{\A[a-zA-Z][a-zA-Z0-9+.\-]*:};
Branch: 1 branch
BranchTrueFalseTotal
if $link =~ /\A[a-zA-Z][a-zA-Z0-9+.\-]*:/u 7 30 37
317  
318 30 ▶ my $trailing = $link =~ m{/+\z} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$link =~ m[/+\z]u ? : 2 28 30
319 30 $link =~ s{/+\z}{};
320  
321 30 ▶ if ( $link =~ m{\A/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ m[\A/]u) 7 23 30
322 7 $link =~ s{\A/}{};
323 7 return { rel => $link , trailing => $trailing };
324 }
325  
326 23 return $self->_clean_rel( $resolve_dir , $link , $trailing );
327 }
328  
329 23 ▶sub _clean_rel {
Subroutine _clean_rel: called CC=7 SCAR=19.7
330 23 my( $self , $dir , $link , $trailing ) = @_;
331  
332 23 ▶ my @parts = $dir ne '' ? split( m{/} , $dir ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$dir ne '' ? : 6 17 23
333  
334 23 for my $seg ( split( m{/} , $link ) ) {
335 34 ▶ next if $seg eq '' or $seg eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $seg eq "" or $seg eq "." 0 34 34
Condition33%1/3
$seg eq "" or $seg eq "."
l!l&&r!l&&!r
0034
MC/DC0%0/2
$seg eq "" or $seg eq "."
$seg eq "" $seg eq "."
Truth table33%1/3
A or B
A= $seg eq ""B= $seg eq "."
ABresult
1X1
011
000
336 34 ▶ if ( $seg eq '..' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg eq '..') { } 7 27 34
337 7 ▶ if ( @parts ) { pop @parts; }
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) { } 5 2 7
338 2 else { return { escape => 1 }; }
339 }
340 else {
341 27 push @parts , $seg;
342 }
343 }
344  
345 21 return { rel => join( '/' , @parts ) , trailing => $trailing };
346 }
347  
348 32 ▶sub _baseurl_prefix {
Subroutine _baseurl_prefix: called CC=4 SCAR=14.0
349 32 my( $self , $baseurl ) = @_;
350  
351 32 ▶ return unless defined $baseurl and $baseurl =~ m{\Ahttps?://}i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $baseurl and $baseurl =~ m[\Ahttps?://]iu 30 2 32
Condition66%2/3
defined $baseurl and $baseurl =~ m[\Ahttps?://]iu
!ll&&!rl&&r
3002
MC/DC50%1/2
defined $baseurl and $baseurl =~ m[\Ahttps?://]iu
defined $baseurl $baseurl =~ m[\Ahttps?://]iu
Truth table66%2/3
A and B
A= defined $baseurlB= $baseurl =~ m[\Ahttps?://]iu
ABresult
0X0
100
111
352  
353 2 $baseurl =~ s{[?#].*$}{};
354 2 $baseurl =~ s{/+\z}{};
355  
356 2 ▶ return length $baseurl ? $baseurl : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $baseurl ? : 2 0 2
357 }
358  
359  
360 1 ▶sub _write_report {
Subroutine _write_report: called CC=3 SCAR=11.0
361 1 my( $self , $path , $result ) = @_;
362  
363 my $text = "brokenlink: checked $result->{checked} reference(s) "
364 . "in $result->{files_checked} file(s); "
365 1 . scalar( @{ $result->{broken} } ) . " broken\n\n";
366  
367 1 for my $b ( @{ $result->{broken} } ) {
368 $text .= "- $b->{src}: $b->{link} -> missing $b->{resolved}"
369 1 ▶ . ( $b->{reason} ? " ($b->{reason})" : '' )
Branch: 1 branch
BranchTrueFalseTotal
$b->{'reason'} ? : 0 1 1
370 . "\n";
371 }
372  
373 1 my $file = path( $path );
374 1 $file->parent->mkpath;
375 1 $file->spew( $text );
376  
377 1 return $file->stringify;
378 }
379  
380 99 ▶sub _rel {
Subroutine _rel: called CC=1 SCAR=0.0
381 99 my( $path , $base ) = @_;
382  
383 99 my $rel = File::Spec->abs2rel( $path , $base );
384 99 $rel =~ s{^/+}{};
385 99 $rel =~ s{/+\z}{};
386  
387 99 return $rel;
388 }
389  
390 1 __PACKAGE__->meta->make_immutable;
391 1 1;
392  
393 __END__
↑ Summary