File Coverage

blib/lib/Perl/Metrics/Lite/Analysis/DocumentFactory.pm
Criterion Covered Total %
statement 31 40 77.5
branch 4 8 50.0
condition n/a
subroutine 8 9 88.8
pod 0 1 0.0
total 43 58 74.1


line stmt bran cond sub pod time code
1             package Perl::Metrics::Lite::Analysis::DocumentFactory;
2 5     5   3676 use strict;
  5         21  
  5         140  
3 5     5   28 use warnings;
  5         11  
  5         115  
4 5     5   2727 use PPI;
  5         505116  
  5         200  
5 5     5   47 use PPI::Document;
  5         13  
  5         151  
6 5     5   517 use Perl::Metrics::Lite::Analysis::Util;
  5         13  
  5         195  
7              
8 5     5   32 use Carp qw(confess);
  5         13  
  5         1222  
9              
10             sub create_normalized_document {
11 19     19 0 96 my ( $class, $path ) = @_;
12              
13 19         40 my $document;
14 19 50       66 if ( ref $path ) {
15 0 0       0 if ( ref $path eq 'SCALAR' ) {
16 0         0 $document = PPI::Document->new($path);
17             }
18             else {
19 0         0 $document = $path;
20             }
21             }
22             else {
23 19 50       494 if ( !-r $path ) {
24 0         0 Carp::confess "Path '$path' is missing or not readable!";
25             }
26 19         90 $document = _create_ppi_document($path);
27             }
28              
29 19         73 $document;
30             }
31              
32             sub _create_ppi_document {
33 19     19   55 my $path = shift;
34 19         40 my $document;
35 19 100       252 if ( -s $path ) {
36 17         177 $document = PPI::Document->new($path);
37             }
38             else {
39              
40             # The file is empty. Create a PPI document with a single whitespace
41             # chararacter. This makes sure that the PPI tokens() method
42             # returns something, so we avoid a warning from
43             # PPI::Document::index_locations() which expects tokens() to return
44             # something other than undef.
45 2         9 my $one_whitespace_character = q{ };
46 2         23 $document = PPI::Document->new( \$one_whitespace_character );
47             }
48 19         245818 return $document;
49             }
50              
51             sub _make_pruned_document {
52 0     0     my $document = shift;
53 0           $document = Perl::Metrics::Lite::Analysis::Util::prune_non_code_lines($document);
54 0           $document->index_locations();
55 0           $document->readonly(1);
56 0           return $document;
57             }
58              
59             1;