File Coverage

lib/Code/Statistics/File.pm
Criterion Covered Total %
statement 56 56 100.0
branch 14 18 77.7
condition n/a
subroutine 13 13 100.0
pod 1 1 100.0
total 84 88 95.4


line stmt bran cond sub pod time code
1 1     1   6 use strict;
  1         2  
  1         31  
2 1     1   6 use warnings;
  1         1  
  1         62  
3              
4             package Code::Statistics::File;
5             $Code::Statistics::File::VERSION = '1.190680';
6             # ABSTRACT: loads a file, searches for targets in it and measures their metrics
7              
8 1     1   20 use 5.004;
  1         3  
9              
10 1     1   6 use Moose;
  1         1  
  1         9  
11 1     1   6205 use MooseX::HasDefaults::RO;
  1         3  
  1         7  
12 1     1   4754 use Code::Statistics::MooseTypes;
  1         2  
  1         24  
13              
14 1     1   560 use PPI::Document;
  1         34827  
  1         44  
15 1     1   10 use Path::Class qw(file);
  1         2  
  1         571  
16              
17             has relative_paths => ( isa => 'Bool' );
18             has foreign_paths => ( isa => 'Str' );
19              
20             has path => (
21             isa => 'Str',
22             required => 1,
23             );
24              
25             has original_path => (
26             isa => 'Str',
27             required => 1,
28             );
29              
30             has targets => (
31             isa => 'CS::InputList',
32             coerce => 1,
33             );
34              
35             has metrics => (
36             isa => 'CS::InputList',
37             coerce => 1,
38             );
39              
40             has ppi => (
41             isa => 'PPI::Document',
42             lazy => 1,
43             default => sub {
44             PPI::Document->new( $_[0]->path );
45             },
46             );
47              
48             has progress => ( isa => 'CodeRef' );
49              
50              
51             sub analyze {
52 8     8 1 18 my ( $self ) = @_;
53              
54 8         11 $self->_process_target_class( $_ ) for @{ $self->targets };
  8         165  
55 8         32 $self->_format_file_path;
56 8         178 $self->progress->();
57              
58 8         813 return $self;
59             }
60              
61             sub _format_file_path {
62 9     9   8122 my ( $self ) = @_;
63 9         203 my $path = file( $self->path );
64              
65 9 100       1350 $path = $path->relative if $self->relative_paths;
66 9 100       1748 $path = $path->absolute if !$self->relative_paths;
67              
68 9 100       332 $path = $path->as_foreign( $self->foreign_paths ) if $self->foreign_paths;
69              
70 9         1487 $self->{path} = $path->stringify;
71 9         225 return $self;
72             }
73              
74             sub _process_target_class {
75 32     32   64 my ( $self, $target_type ) = @_;
76              
77 32         45 my @supported_metrics = grep $self->_are_compatible( $target_type, $_ ), @{ $self->metrics };
  32         689  
78 32 50       62 return if !@supported_metrics;
79              
80 32         114 my $targets = "Code::Statistics::Target::$target_type"->find_targets( $self );
81 32 100       37734 return if !$targets;
82              
83 16         25 my @measurements = map _measure_target( $_, @supported_metrics ), @{$targets};
  16         54  
84 16         56 $self->{measurements}{$target_type} = \@measurements;
85              
86 16         93 return $self;
87             }
88              
89             sub _are_compatible {
90 256     256   341 my ( $self, $target, $metric ) = @_;
91 256 50       591 return 1 if "Code::Statistics::Target::$target"->force_support( $metric );
92 256 50       633 return 1 if "Code::Statistics::Metric::$metric"->force_support( $target );
93 256 50       412 return 0 if "Code::Statistics::Target::$target"->incompatible_with( $metric );
94 256 100       494 return 0 if "Code::Statistics::Metric::$metric"->incompatible_with( $target );
95 192         317 return 1;
96             }
97              
98             sub _measure_target {
99 76     76   213 my ( $target, @metrics ) = @_;
100              
101 76         97 my %measurement;
102 76         317 $measurement{$_} = "Code::Statistics::Metric::$_"->measure( $target ) for @metrics;
103              
104 76         240 return \%measurement;
105             }
106              
107             1;
108              
109             __END__
110              
111             =pod
112              
113             =encoding UTF-8
114              
115             =head1 NAME
116              
117             Code::Statistics::File - loads a file, searches for targets in it and measures their metrics
118              
119             =head1 VERSION
120              
121             version 1.190680
122              
123             =head2 analyze
124             Finds targets in the given file and collects the metrics on those.
125              
126             =head1 AUTHOR
127              
128             Christian Walde <mithaldu@yahoo.de>
129              
130             =head1 COPYRIGHT AND LICENSE
131              
132              
133             Christian Walde has dedicated the work to the Commons by waiving all of his
134             or her rights to the work worldwide under copyright law and all related or
135             neighboring legal rights he or she had in the work, to the extent allowable by
136             law.
137              
138             Works under CC0 do not require attribution. When citing the work, you should
139             not imply endorsement by the author.
140              
141             =cut