File Coverage

blib/lib/Perl/Metrics/Lite/Analysis/Util.pm
Criterion Covered Total %
statement 57 58 98.2
branch 14 18 77.7
condition n/a
subroutine 10 10 100.0
pod 3 4 75.0
total 84 90 93.3


line stmt bran cond sub pod time code
1             package Perl::Metrics::Lite::Analysis::Util;
2 6     6   524 use strict;
  6         26  
  6         172  
3 6     6   32 use warnings;
  6         13  
  6         178  
4 6     6   527 use English qw(-no_match_vars);
  6         3958  
  6         37  
5 6     6   2895 use Readonly;
  6         4103  
  6         3785  
6              
7             Readonly::Scalar my $ALL_NEWLINES_REGEX => ## no critic
8             qr/ ( \Q$INPUT_RECORD_SEPARATOR\E ) /sxm;
9              
10             Readonly::Scalar my $LAST_CHARACTER => -1; ## no critic
11              
12             sub get_node_length {
13 76     76 1 7778 my $node = shift;
14 76         155 my $eval_result = eval { $node = prune_non_code_lines($node); };
  76         211  
15 76 50       402 return 0 if not $eval_result;
16 76 50       227 return 0 if ( !defined $node );
17 76         301 my $string = $node->content;
18 76 50       23084 return 0 if ( !length $string );
19              
20 76         221 $string = _normailze_string($string);
21 76         245 my $line_count = _count_lines($string);
22              
23 76         362 return $line_count;
24             }
25              
26             sub _normailze_string {
27 76     76   147 my $string = shift;
28            
29             # Replace whitespace-newline with newline
30 76         1308 $string
31             =~ s/ \s+ \Q$INPUT_RECORD_SEPARATOR\E /$INPUT_RECORD_SEPARATOR/smxg;
32 76         689 $string =~ s/\Q$INPUT_RECORD_SEPARATOR\E /$INPUT_RECORD_SEPARATOR/smxg;
33 76         300 $string =~ s/ \A \s+ //msx; # Remove leading whitespace
34 76         285 $string;
35             }
36              
37             sub _count_lines {
38 76     76   171 my $string = shift;
39 76         704 my @newlines = ( $string =~ /$ALL_NEWLINES_REGEX/smxg );
40 76         171 my $line_count = scalar @newlines;
41              
42             # if the string is not empty and the last character is not a newline then add 1
43 76 100       226 if ( length $string ) {
44 72         203 my $last_char = substr $string, $LAST_CHARACTER, 1;
45 72 100       256 if ( $last_char ne "$INPUT_RECORD_SEPARATOR" ) {
46 61         140 $line_count++;
47             }
48             }
49 76         212 return $line_count;
50             }
51              
52             sub get_packages {
53 18     18 1 46 my $document = shift;
54              
55 18         51 my @unique_packages = ();
56 18         85 my $found_packages = $document->find('PPI::Statement::Package');
57              
58             return \@unique_packages
59 18 100       53067 if (
60             !Perl::Metrics::Lite::Analysis::Util::is_ref( $found_packages, 'ARRAY' ) );
61              
62 9         44 my %seen_packages = ();
63              
64 9         22 foreach my $package ( @{$found_packages} ) {
  9         29  
65 15         216 $seen_packages{ $package->namespace() }++;
66             }
67              
68 9         304 @unique_packages = sort keys %seen_packages;
69              
70 9         47 return \@unique_packages;
71             }
72              
73             sub prune_non_code_lines {
74 76     76 0 143 my $document = shift;
75 76 50       242 if ( !defined $document ) {
76 0         0 Carp::confess('Did not supply a document!');
77             }
78 76         441 $document->prune('PPI::Token::Comment');
79 76         153090 $document->prune('PPI::Token::Pod');
80 76         142533 $document->prune('PPI::Token::End');
81              
82 76         143497 return $document;
83             }
84              
85             sub is_ref {
86 65     65 1 165 my $thing = shift;
87 65         137 my $type = shift;
88 65         152 my $ref = ref $thing;
89 65 100       300 return if !$ref;
90 35 100       131 return if ( $ref ne $type );
91 34         151 return $ref;
92             }
93              
94             1;
95              
96             __END__