lib/Perl/Critic/Utils/POD.pm

filter: Statementstmt 86.6% 117 / 135 Branchbran 66.6% 36 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.1% 27 / 35 Podpod 100% 19 / 19 totaltotal 81.8% 199 / 243 CC 28 SCAR 12.3
CC
28
Cov
81%
_get_module_abstract_from_filehandle
9.0
get_raw_pod_section_for_module
6.0
get_raw_module_abstract_for_module
6.0
CRAP 3.4
« lib/Perl/Critic/Policy/ControlStructures/ProhibitYadaOperator.pm ↑ Summary lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringySplit.pm »
1 package Perl::Critic::Utils::POD;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use English qw< -no_match_vars >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Pod::PlainText ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Pod::Select ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # TODO: non-fatal generic?
13 40 ▶use Perl::Critic::Exception::Fatal::Generic qw< throw_generic >;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use Perl::Critic::Exception::IO qw< throw_io >;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use Perl::Critic::Utils qw< :characters >;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 40 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 40 our $VERSION = '1.156';
20  
21 #-----------------------------------------------------------------------------
22  
23 40 our @EXPORT_OK = qw(
24 get_pod_file_for_module
25 get_raw_pod_section_from_file
26 get_raw_pod_section_from_filehandle
27 get_raw_pod_section_from_string
28 get_raw_pod_section_for_module
29 get_pod_section_from_file
30 get_pod_section_from_filehandle
31 get_pod_section_from_string
32 get_pod_section_for_module
33 trim_raw_pod_section
34 trim_pod_section
35 get_raw_module_abstract_from_file
36 get_raw_module_abstract_from_filehandle
37 get_raw_module_abstract_from_string
38 get_raw_module_abstract_for_module
39 get_module_abstract_from_file
40 get_module_abstract_from_filehandle
41 get_module_abstract_from_string
42 get_module_abstract_for_module
43 );
44  
45 40 our %EXPORT_TAGS = (
46 all => \@EXPORT_OK,
47 );
48  
49 #-----------------------------------------------------------------------------
50  
51 726 ▶sub get_pod_file_for_module {
Subroutine get_pod_file_for_module: called CC=3 SCAR=11.0
Pod documented
52 726 my ($module_name) = @_;
53  
54 # No File::Spec: %INC always uses forward slashes.
55 726 (my $relative_path = $module_name) =~ s< :: ></>xmsg;
56 726 $relative_path .= '.pm';
57  
58 726 ▶ my $absolute_path = $INC{$relative_path} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $absolute_path = $INC{$relative_path} or return 725 1 726
59  
60 725 (my $pod_path = $absolute_path) =~ s< [.] [^.]+ \z><.pod>xms;
61 725 ▶ return $pod_path if -f $pod_path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $pod_path 0 725 725
62  
63 725 return $absolute_path;
64 }
65  
66 #-----------------------------------------------------------------------------
67  
68 0 ▶sub get_raw_pod_section_from_file {
Subroutine get_raw_pod_section_from_file: not called CC=1 SCAR=6.9
Pod documented
69 0 my ($file_name, $section_name) = @_;
70  
71 0 return _get_pod_section_from_file(
72 $file_name,
73 $section_name,
74 Pod::Select->new(),
75 );
76 }
77  
78 #-----------------------------------------------------------------------------
79  
80 0 ▶sub get_raw_pod_section_from_filehandle {
Subroutine get_raw_pod_section_from_filehandle: not called CC=1 SCAR=6.9
Pod documented
81 0 my ($file_handle, $section_name) = @_;
82  
83 0 return _get_pod_section_from_filehandle(
84 $file_handle,
85 $section_name,
86 Pod::Select->new(),
87 );
88 }
89  
90 #-----------------------------------------------------------------------------
91  
92 7 ▶sub get_raw_pod_section_from_string {
Subroutine get_raw_pod_section_from_string: called CC=1 SCAR=0.0
Pod documented
93 7 my ($source, $section_name) = @_;
94  
95 7 return _get_pod_section_from_string(
96 $source,
97 $section_name,
98 Pod::Select->new(),
99 );
100 }
101  
102 #-----------------------------------------------------------------------------
103  
104 0 ▶sub get_raw_pod_section_for_module {
Subroutine get_raw_pod_section_for_module: not called CC=2 SCAR=17.9
Pod documented
105 0 my ($module_name, $section_name) = @_;
106  
107 0 ▶ my $file_name = get_pod_file_for_module($module_name)
Branch: 1 branch
BranchTrueFalseTotal
my $file_name = get_pod_file_for_module($module_name) or throw_generic(qq[Could not find POD for "$module_name".]) 0 0 0
108 or throw_generic qq<Could not find POD for "$module_name".>;
109  
110 0 return get_raw_pod_section_from_file($file_name, $section_name);
111 }
112  
113 #-----------------------------------------------------------------------------
114  
115 2 ▶sub get_pod_section_from_file {
Subroutine get_pod_section_from_file: called CC=1 SCAR=0.0
Pod documented
116 2 my ($file_name, $section_name) = @_;
117  
118 2 return _get_pod_section_from_file(
119 $file_name,
120 $section_name,
121 Pod::PlainText->new(),
122 );
123 }
124  
125 #-----------------------------------------------------------------------------
126  
127 0 ▶sub get_pod_section_from_filehandle {
Subroutine get_pod_section_from_filehandle: not called CC=1 SCAR=6.9
Pod documented
128 0 my ($file_handle, $section_name) = @_;
129  
130 0 return _get_pod_section_from_filehandle(
131 $file_handle,
132 $section_name,
133 Pod::PlainText->new(),
134 );
135 }
136  
137 #-----------------------------------------------------------------------------
138  
139 7 ▶sub get_pod_section_from_string {
Subroutine get_pod_section_from_string: called CC=1 SCAR=0.0
Pod documented
140 7 my ($source, $section_name) = @_;
141  
142 7 return _get_pod_section_from_string(
143 $source,
144 $section_name,
145 Pod::PlainText->new(),
146 );
147 }
148  
149 #-----------------------------------------------------------------------------
150  
151 3 ▶sub get_pod_section_for_module {
Subroutine get_pod_section_for_module: called CC=2 SCAR=6.9
Pod documented
152 3 my ($module_name, $section_name) = @_;
153  
154 3 ▶ my $file_name = get_pod_file_for_module($module_name)
Branch: 1 branch
BranchTrueFalseTotal
my $file_name = get_pod_file_for_module($module_name) or throw_generic(qq[Could not find POD for "$module_name".]) 2 1 3
155 or throw_generic qq<Could not find POD for "$module_name".>;
156  
157 2 return get_pod_section_from_file($file_name, $section_name);
158 }
159  
160 #-----------------------------------------------------------------------------
161  
162 2 ▶sub _get_pod_section_from_file {
Subroutine _get_pod_section_from_file: called CC=3 SCAR=11.3
163 2 my ($file_name, $section_name, $parser) = @_;
164  
165 2 ▶ open my $file_handle, '<', $file_name
Branch: 1 branch
BranchTrueFalseTotal
open my $file_handle, "<", $file_name or throw_io("message", qq[Could not open "$file_name": $Perl::Critic::Utils::POD::ERRNO], "file_name", $file_name, "errno", $Perl::Critic::Utils::POD::ERRNO) 2 0 2
166 or throw_io
167 message => qq<Could not open "$file_name": $ERRNO>,
168 file_name => $file_name,
169 errno => $ERRNO;
170  
171 2 my $content =
172 _get_pod_section_from_filehandle(
173 $file_handle, $section_name, $parser,
174 );
175  
176 2 ▶ close $file_handle
Branch: 1 branch
BranchTrueFalseTotal
close $file_handle or throw_io("message", qq[Could not close "$file_name": $Perl::Critic::Utils::POD::ERRNO], "file_name", $file_name, "errno", $Perl::Critic::Utils::POD::ERRNO) 2 0 2
177 or throw_io
178 message => qq<Could not close "$file_name": $ERRNO>,
179 file_name => $file_name,
180 errno => $ERRNO;
181  
182 2 return $content;
183 }
184  
185 #-----------------------------------------------------------------------------
186  
187 756 ▶sub _get_pod_section_from_filehandle {
Subroutine _get_pod_section_from_filehandle: called CC=4 SCAR=14.0
188 756 my ($file_handle, $section_name, $parser) = @_;
189  
190 756 $parser->select($section_name);
191  
192 756 my $content = $EMPTY;
193 756 ▶ open my $content_handle, '>', \$content
Branch: 1 branch
BranchTrueFalseTotal
open my $content_handle, ">", \$content or throw_generic("error opening scalar: $Perl::Critic::Utils::POD::OS_ERROR") 756 0 756
194 or throw_generic "error opening scalar: $OS_ERROR";
195  
196 756 $parser->parse_from_filehandle( $file_handle, $content_handle );
197  
198 756 ▶ close $content_handle or throw_generic "Failed to close in memory file: $OS_ERROR";
Branch: 1 branch
BranchTrueFalseTotal
close $content_handle or throw_generic("Failed to close in memory file: $Perl::Critic::Utils::POD::OS_ERROR") 756 0 756
199  
200 756 ▶ return if $content eq $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $content eq $Perl::Critic::Utils::POD::EMPTY 8 748 756
201 748 return $content;
202 }
203  
204 #-----------------------------------------------------------------------------
205  
206 14 ▶sub _get_pod_section_from_string {
Subroutine _get_pod_section_from_string: called CC=3 SCAR=11.3
207 14 my ($source, $section_name, $parser) = @_;
208  
209 14 ▶ open my $source_handle, '<', \$source
Branch: 1 branch
BranchTrueFalseTotal
open my $source_handle, "<", \$source or throw_generic("error opening scalar: $Perl::Critic::Utils::POD::OS_ERROR") 14 0 14
210 or throw_generic "error opening scalar: $OS_ERROR";
211  
212 14 my $content = _get_pod_section_from_filehandle(
213 $source_handle, $section_name, $parser,
214 );
215  
216 14 ▶ close $source_handle or throw_generic "Failed to close in memory file: $OS_ERROR";
Branch: 1 branch
BranchTrueFalseTotal
close $source_handle or throw_generic("Failed to close in memory file: $Perl::Critic::Utils::POD::OS_ERROR") 14 0 14
217  
218 14 return $content;
219 }
220  
221 #-----------------------------------------------------------------------------
222  
223 11 ▶sub trim_raw_pod_section {
Subroutine trim_raw_pod_section: called CC=2 SCAR=7.0
Pod documented
224 11 my ($pod) = @_;
225  
226 11 ▶ return if not defined $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod 0 11 11
227  
228 11 $pod =~ s< \A =head1 \b [^\n]* \n $ ><>xms;
229 11 $pod =~ s< \A \s+ ><>xms;
230 11 $pod =~ s< \s+ \z ><>xms;
231  
232 11 return $pod;
233 }
234  
235 #-----------------------------------------------------------------------------
236  
237 735 ▶sub trim_pod_section {
Subroutine trim_pod_section: called CC=2 SCAR=7.0
Pod documented
238 735 my ($pod) = @_;
239  
240 735 ▶ return if not defined $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod 0 735 735
241  
242 735 $pod =~ s< \A [^\n]* \n ><>xms;
243 735 $pod =~ s< \A \s* \n ><>xms;
244 735 $pod =~ s< \s+ \z ><>xms;
245  
246 735 return $pod;
247 }
248  
249 #-----------------------------------------------------------------------------
250  
251 0 ▶sub get_raw_module_abstract_from_file {
Subroutine get_raw_module_abstract_from_file: not called CC=1 SCAR=6.9
Pod documented
252 0 my ($file_name) = @_;
253  
254 return
255 0 _get_module_abstract_from_file(
256 $file_name,
257 Pod::Select->new(),
258 \&trim_raw_pod_section,
259 );
260 }
261  
262 #-----------------------------------------------------------------------------
263  
264 0 ▶sub get_raw_module_abstract_from_filehandle {
Subroutine get_raw_module_abstract_from_filehandle: not called CC=1 SCAR=6.9
Pod documented
265 0 my ($file_handle) = @_;
266  
267 return
268 0 _get_module_abstract_from_filehandle(
269 $file_handle,
270 Pod::Select->new(),
271 \&trim_raw_pod_section,
272 );
273 }
274  
275 #-----------------------------------------------------------------------------
276  
277 9 ▶sub get_raw_module_abstract_from_string {
Subroutine get_raw_module_abstract_from_string: called CC=1 SCAR=0.0
Pod documented
278 9 my ($source) = @_;
279  
280 return
281 9 _get_module_abstract_from_string(
282 $source,
283 Pod::Select->new(),
284 \&trim_raw_pod_section,
285 );
286 }
287  
288 #-----------------------------------------------------------------------------
289  
290 0 ▶sub get_raw_module_abstract_for_module {
Subroutine get_raw_module_abstract_for_module: not called CC=2 SCAR=17.9
Pod documented
291 0 my ($module_name) = @_;
292  
293 0 ▶ my $file_name = get_pod_file_for_module($module_name)
Branch: 1 branch
BranchTrueFalseTotal
my $file_name = get_pod_file_for_module($module_name) or throw_generic(qq[Could not find POD for "$module_name".]) 0 0 0
294 or throw_generic qq<Could not find POD for "$module_name".>;
295  
296 0 return get_raw_module_abstract_from_file($file_name);
297 }
298  
299 #-----------------------------------------------------------------------------
300  
301 723 ▶sub get_module_abstract_from_file {
Subroutine get_module_abstract_from_file: called CC=1 SCAR=0.0
Pod documented
302 723 my ($file_name) = @_;
303  
304 return
305 723 _get_module_abstract_from_file(
306 $file_name,
307 Pod::PlainText->new(),
308 \&trim_pod_section,
309 );
310 }
311  
312 #-----------------------------------------------------------------------------
313  
314 0 ▶sub get_module_abstract_from_filehandle {
Subroutine get_module_abstract_from_filehandle: not called CC=1 SCAR=6.9
Pod documented
315 0 my ($file_handle) = @_;
316  
317 return
318 0 _get_module_abstract_from_filehandle(
319 $file_handle,
320 Pod::PlainText->new(),
321 \&trim_pod_section,
322 );
323 }
324  
325 #-----------------------------------------------------------------------------
326  
327 8 ▶sub get_module_abstract_from_string {
Subroutine get_module_abstract_from_string: called CC=1 SCAR=0.0
Pod documented
328 8 my ($source) = @_;
329  
330 return
331 8 _get_module_abstract_from_string(
332 $source,
333 Pod::PlainText->new(),
334 \&trim_pod_section,
335 );
336 }
337  
338 #-----------------------------------------------------------------------------
339  
340 723 ▶sub get_module_abstract_for_module {
Subroutine get_module_abstract_for_module: called CC=2 SCAR=7.1
Pod documented
341 723 my ($module_name) = @_;
342  
343 723 ▶ my $file_name = get_pod_file_for_module($module_name)
Branch: 1 branch
BranchTrueFalseTotal
my $file_name = get_pod_file_for_module($module_name) or throw_generic(qq[Could not find POD for "$module_name".]) 723 0 723
344 or throw_generic qq<Could not find POD for "$module_name".>;
345  
346 723 return get_module_abstract_from_file($file_name);
347 }
348  
349 #-----------------------------------------------------------------------------
350  
351 723 ▶sub _get_module_abstract_from_file {
Subroutine _get_module_abstract_from_file: called CC=3 SCAR=11.3
352 723 my ($file_name, $parser, $trimmer) = @_;
353  
354 723 ▶ open my $file_handle, '<', $file_name
Branch: 1 branch
BranchTrueFalseTotal
open my $file_handle, "<", $file_name or throw_io("message", qq[Could not open "$file_name": $Perl::Critic::Utils::POD::ERRNO], "file_name", $file_name, "errno", $Perl::Critic::Utils::POD::ERRNO) 723 0 723
355 or throw_io
356 message => qq<Could not open "$file_name": $ERRNO>,
357 file_name => $file_name,
358 errno => $ERRNO;
359  
360 723 my $module_abstract =
361 _get_module_abstract_from_filehandle(
362 $file_handle, $parser, $trimmer,
363 );
364  
365 723 ▶ close $file_handle
Branch: 1 branch
BranchTrueFalseTotal
close $file_handle or throw_io("message", qq[Could not close "$file_name": $Perl::Critic::Utils::POD::ERRNO], "file_name", $file_name, "errno", $Perl::Critic::Utils::POD::ERRNO) 723 0 723
366 or throw_io
367 message => qq<Could not close "$file_name": $ERRNO>,
368 file_name => $file_name,
369 errno => $ERRNO;
370  
371 723 return $module_abstract;
372 }
373  
374 #-----------------------------------------------------------------------------
375  
376 740 ▶sub _get_module_abstract_from_filehandle { ## no critic (RequireFinalReturn)
Subroutine _get_module_abstract_from_filehandle: called CC=9 SCAR=22.0
377 740 my ($file_handle, $parser, $trimmer) = @_;
378  
379 740 my $name_section =
380 _get_pod_section_from_filehandle( $file_handle, 'NAME', $parser );
381 740 ▶ return if not $name_section;
Branch: 1 branch
BranchTrueFalseTotal
unless $name_section 2 738 740
382  
383 738 $name_section = $trimmer->($name_section);
384 738 ▶ return if not $name_section;
Branch: 1 branch
BranchTrueFalseTotal
unless $name_section 2 736 738
385  
386 # Testing for parser class, blech. But it's a lot simpler and it's all
387 # hidden in the implementation.
388 736 ▶ if ('Pod::Select' eq ref $parser) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Pod::Select' eq ref $parser) { } 7 729 736
389 7 ▶ if ( $name_section =~ m< \n >xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name_section =~ / \n /msx) 1 6 7
390 1 throw_generic
391 qq<Malformed NAME section in "$name_section". >
392 . q<It must be on a single line>;
393 }
394 }
395 else {
396 729 $name_section =~ s< \s+ >< >xmsg;
397  
398 # Ugh. Pod::PlainText splits up module names.
399 729 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($name_section =~ / \A \s* ( \w [ \w:]+ \w ) ( \s* - .* )? \z /msx) 728 1 729
400 $name_section =~ m<
401 \A
402 \s*
403 (
404 \w [ \w:]+ \w
405 )
406 (
407 \s*
408 -
409 .*
410 )?
411 \z
412 >xms
413 ) {
414 728 my ($module_name, $rest) = ($1, $2);
415  
416 728 $module_name =~ s/ [ ] //xms;
417  
418 728 ▶ $name_section = $module_name . ( $rest ? $rest : $EMPTY );
Branch: 1 branch
BranchTrueFalseTotal
$rest ? : 727 1 728
419 }
420 }
421  
422 735 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($name_section =~ / \A \s* [\w:]+ # Module name. \s+ - # The required single hyphen. \s+ ( \S # At least one non-whitespace. (?: .* \S)? # Everything up to the last non-whitespace. ) \s* \z /msx) 727 8 735
423 $name_section =~ m<
424 \A
425 \s*
426 [\w:]+ # Module name.
427 \s+
428 - # The required single hyphen.
429 \s+
430 (
431 \S # At least one non-whitespace.
432 (?: .* \S)? # Everything up to the last non-whitespace.
433 )
434 \s*
435 \z
436 >xms
437 ) {
438 727 my $module_abstract = $1;
439 727 return $module_abstract;
440 }
441  
442 8 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($name_section =~ / \A \s* [\w:]+ # Module name. (?: \s* - )? # The single hyphen is now optional. \s* \z /msx) 4 4 8
443 $name_section =~ m<
444 \A
445 \s*
446 [\w:]+ # Module name.
447 (?: \s* - )? # The single hyphen is now optional.
448 \s*
449 \z
450 >xms
451 ) {
452 4 return;
453 }
454  
455 4 throw_generic qq<Malformed NAME section in "$name_section".>;
456 }
457  
458 #-----------------------------------------------------------------------------
459  
460 17 ▶sub _get_module_abstract_from_string {
Subroutine _get_module_abstract_from_string: called CC=3 SCAR=11.3
461 17 my ($source, $parser, $trimmer) = @_;
462  
463 17 ▶ open my $source_handle, '<', \$source
Branch: 1 branch
BranchTrueFalseTotal
open my $source_handle, "<", \$source or throw_generic("error opening scalar: $Perl::Critic::Utils::POD::OS_ERROR") 17 0 17
464 or throw_generic "error opening scalar: $OS_ERROR";
465  
466 17 my $module_abstract = _get_module_abstract_from_filehandle(
467 $source_handle, $parser, $trimmer,
468 );
469  
470 12 ▶ close $source_handle or throw_generic "Failed to close in memory file: $OS_ERROR";
Branch: 1 branch
BranchTrueFalseTotal
close $source_handle or throw_generic("Failed to close in memory file: $Perl::Critic::Utils::POD::OS_ERROR") 12 0 12
471  
472 12 return $module_abstract;
473 }
474  
475 #-----------------------------------------------------------------------------
476  
477 40 1;
478  
479 __END__
« lib/Perl/Critic/Policy/ControlStructures/ProhibitYadaOperator.pm ↑ Summary lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringySplit.pm »