File: | lib/Code/Statistics/File.pm |
Coverage: | 95.6% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | 2 2 2 | 0 0 0 | use strict; | ||||
2 | 2 2 2 | 0 0 0 | use warnings; | ||||
3 | |||||||
4 | package Code::Statistics::File; | ||||||
5 | |||||||
6 | # ABSTRACT: loads a file, searches for targets in it and measures their metrics | ||||||
7 | |||||||
8 | 2 2 2 | 0 0 0 | use 5.004; | ||||
9 | |||||||
10 | 2 2 2 | 0 0 0 | use Moose; | ||||
11 | 2 2 2 | 0 0 0 | use MooseX::HasDefaults::RO; | ||||
12 | 2 2 2 | 0 0 0 | use Code::Statistics::MooseTypes; | ||||
13 | |||||||
14 | 2 2 2 | 0 0 0 | use PPI::Document; | ||||
15 | 2 2 2 | 0 0 0 | use Path::Class qw(file); | ||||
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 - 52 | =head2 analyze Finds targets in the given file and collects the metrics on those. =cut | ||||||
53 | |||||||
54 | sub analyze { | ||||||
55 | 16 | 0 | my ( $self ) = @_; | ||||
56 | |||||||
57 | 16 16 16 | 0 0 0 | $self->_process_target_class( $_ ) for @{ $self->targets }; | ||||
58 | 16 | 0 | $self->_format_file_path; | ||||
59 | 16 | 0 | $self->progress->(); | ||||
60 | |||||||
61 | 16 | 15625 | return $self; | ||||
62 | } | ||||||
63 | |||||||
64 | sub _format_file_path { | ||||||
65 | 18 | 0 | my ( $self ) = @_; | ||||
66 | 18 | 0 | my $path = file( $self->path ); | ||||
67 | |||||||
68 | 18 | 0 | $path = $path->relative if $self->relative_paths; | ||||
69 | 18 | 0 | $path = $path->absolute if !$self->relative_paths; | ||||
70 | |||||||
71 | 18 | 0 | $path = $path->as_foreign( $self->foreign_paths ) if $self->foreign_paths; | ||||
72 | |||||||
73 | 18 | 0 | $self->{path} = $path->stringify; | ||||
74 | 18 | 0 | return $self; | ||||
75 | } | ||||||
76 | |||||||
77 | sub _process_target_class { | ||||||
78 | 48 | 0 | my ( $self, $target_type ) = @_; | ||||
79 | |||||||
80 | 48 48 | 0 0 | my @supported_metrics = grep $self->_are_compatible( $target_type, $_ ), @{ $self->metrics }; | ||||
81 | 48 | 0 | return if !@supported_metrics; | ||||
82 | |||||||
83 | 48 | 0 | my $targets = "Code::Statistics::Target::$target_type"->find_targets( $self ); | ||||
84 | 48 | 0 | return if !$targets; | ||||
85 | |||||||
86 | 32 32 | 0 0 | my @measurements = map _measure_target( $_, @supported_metrics ), @{$targets}; | ||||
87 | 32 | 0 | $self->{measurements}{$target_type} = \@measurements; | ||||
88 | |||||||
89 | 32 | 0 | return $self; | ||||
90 | } | ||||||
91 | |||||||
92 | sub _are_compatible { | ||||||
93 | 288 | 15625 | my ( $self, $target, $metric ) = @_; | ||||
94 | 288 | 0 | return 1 if "Code::Statistics::Target::$target"->supports( $metric ); | ||||
95 | 288 | 0 | return 1 if "Code::Statistics::Metric::$metric"->supports( $target ); | ||||
96 | 288 | 0 | return 0 if "Code::Statistics::Target::$target"->incompatible_with( $metric ); | ||||
97 | 288 | 0 | return 0 if "Code::Statistics::Metric::$metric"->incompatible_with( $target ); | ||||
98 | 240 | 0 | return 1; | ||||
99 | } | ||||||
100 | |||||||
101 | sub _measure_target { | ||||||
102 | 152 | 0 | my ( $target, @metrics ) = @_; | ||||
103 | |||||||
104 | 152 | 0 | my %measurement; | ||||
105 | 152 152 | 0 0 | $measurement{$_} = "Code::Statistics::Metric::$_"->measure( $target ) for @metrics; | ||||
106 | |||||||
107 | 152 | 0 | return \%measurement; | ||||
108 | } | ||||||
109 | |||||||
110 | 1; |