File Coverage

lib/Code/Statistics/File.pm
Criterion Covered Total %
statement 57 57 100.0
branch 14 18 77.7
condition n/a
subroutine 13 13 100.0
pod 1 1 100.0
total 85 89 95.5


line stmt bran cond sub pod time code
1 1     1   4 use strict;
  1         3  
  1         30  
2 1     1   6 use warnings;
  1         2  
  1         96  
3              
4             package Code::Statistics::File;
5             {
6             $Code::Statistics::File::VERSION = '1.112980';
7             }
8              
9             # ABSTRACT: loads a file, searches for targets in it and measures their metrics
10              
11 1     1   22 use 5.004;
  1         4  
  1         47  
12              
13 1     1   7 use Moose;
  1         3  
  1         13  
14 1     1   7891 use MooseX::HasDefaults::RO;
  1         4  
  1         11  
15 1     1   11406 use Code::Statistics::MooseTypes;
  1         2  
  1         41  
16              
17 1     1   1323 use PPI::Document;
  1         82604  
  1         61  
18 1     1   15 use Path::Class qw(file);
  1         3  
  1         631  
19              
20             has relative_paths => ( isa => 'Bool' );
21             has foreign_paths => ( isa => 'Str' );
22              
23             has path => (
24             isa => 'Str',
25             required => 1,
26             );
27              
28             has original_path => (
29             isa => 'Str',
30             required => 1,
31             );
32              
33             has targets => (
34             isa => 'CS::InputList',
35             coerce => 1,
36             );
37              
38             has metrics => (
39             isa => 'CS::InputList',
40             coerce => 1,
41             );
42              
43             has ppi => (
44             isa => 'PPI::Document',
45             lazy => 1,
46             default => sub {
47             PPI::Document->new( $_[0]->path );
48             },
49             );
50              
51             has progress => ( isa => 'CodeRef' );
52              
53              
54             sub analyze {
55 8     8 1 17 my ( $self ) = @_;
56              
57 8         15 $self->_process_target_class( $_ ) for @{ $self->targets };
  8         265  
58 8         33 $self->_format_file_path;
59 8         257 $self->progress->();
60              
61 8         966 return $self;
62             }
63              
64             sub _format_file_path {
65 9     9   8612 my ( $self ) = @_;
66 9         323 my $path = file( $self->path );
67              
68 9 100       1978 $path = $path->relative if $self->relative_paths;
69 9 100       2247 $path = $path->absolute if !$self->relative_paths;
70              
71 9 100       525 $path = $path->as_foreign( $self->foreign_paths ) if $self->foreign_paths;
72              
73 9         1694 $self->{path} = $path->stringify;
74 9         308 return $self;
75             }
76              
77             sub _process_target_class {
78 32     32   78 my ( $self, $target_type ) = @_;
79              
80 32         56 my @supported_metrics = grep $self->_are_compatible( $target_type, $_ ), @{ $self->metrics };
  32         1046  
81 32 50       91 return if !@supported_metrics;
82              
83 32         212 my $targets = "Code::Statistics::Target::$target_type"->find_targets( $self );
84 32 100       61535 return if !$targets;
85              
86 16         33 my @measurements = map _measure_target( $_, @supported_metrics ), @{$targets};
  16         77  
87 16         82 $self->{measurements}{$target_type} = \@measurements;
88              
89 16         132 return $self;
90             }
91              
92             sub _are_compatible {
93 256     256   411 my ( $self, $target, $metric ) = @_;
94 256 50       1067 return 1 if "Code::Statistics::Target::$target"->force_support( $metric );
95 256 50       1182 return 1 if "Code::Statistics::Metric::$metric"->force_support( $target );
96 256 50       864 return 0 if "Code::Statistics::Target::$target"->incompatible_with( $metric );
97 256 100       993 return 0 if "Code::Statistics::Metric::$metric"->incompatible_with( $target );
98 192         597 return 1;
99             }
100              
101             sub _measure_target {
102 76     76   256 my ( $target, @metrics ) = @_;
103              
104 76         113 my %measurement;
105 76         456 $measurement{$_} = "Code::Statistics::Metric::$_"->measure( $target ) for @metrics;
106              
107 76         378 return \%measurement;
108             }
109              
110             1;
111              
112             __END__
113             =pod
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.112980
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             This software is Copyright (c) 2010 by Christian Walde.
133              
134             This is free software, licensed under:
135              
136             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE, Version 2, December 2004
137              
138             =cut
139