1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Statistics.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Statistics;
14 use English qw(-no_match_vars);
16 use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_sub };
18 #-----------------------------------------------------------------------------
20 our $VERSION = '1.088';
22 #-----------------------------------------------------------------------------
27 my $self = bless {}, $class;
29 $self->{_modules} = 0;
31 $self->{_statements} = 0;
33 $self->{_violations_by_policy} = {};
34 $self->{_violations_by_severity} = {};
35 $self->{_total_violations} = 0;
40 #-----------------------------------------------------------------------------
43 my ($self, $doc, $violations) = @_;
47 my $subs = $doc->find('PPI::Statement::Sub');
49 foreach my $sub ( @{$subs} ) {
51 $self->{_subs_total_mccabe} += calculate_mccabe_of_sub( $sub );
55 my $statements = $doc->find('PPI::Statement');
56 $self->{_statements} += $statements ? scalar @{$statements} : 0;
58 ## no critic (RequireExtendedFormatting, RequireLineBoundaryMatching)
59 my @lines = split /$INPUT_RECORD_SEPARATOR/, $doc->serialize();
61 $self->{_lines} += scalar @lines;
63 foreach my $violation ( @{ $violations } ) {
64 $self->{_violations_by_severity}->{ $violation->severity() }++;
65 $self->{_violations_by_policy}->{ $violation->policy() }++;
66 $self->{_total_violations}++;
72 #-----------------------------------------------------------------------------
77 return $self->{_modules};
80 #-----------------------------------------------------------------------------
85 return $self->{_subs};
88 #-----------------------------------------------------------------------------
93 return $self->{_statements};
96 #-----------------------------------------------------------------------------
101 return $self->{_lines};
104 #-----------------------------------------------------------------------------
106 sub _subs_total_mccabe {
109 return $self->{_subs_total_mccabe};
112 #-----------------------------------------------------------------------------
114 sub violations_by_severity {
117 return $self->{_violations_by_severity};
120 #-----------------------------------------------------------------------------
122 sub violations_by_policy {
125 return $self->{_violations_by_policy};
128 #-----------------------------------------------------------------------------
130 sub total_violations {
133 return $self->{_total_violations};
136 #-----------------------------------------------------------------------------
138 sub statements_other_than_subs {
141 return $self->statements() - $self->subs();
144 #-----------------------------------------------------------------------------
146 sub average_sub_mccabe {
149 return if $self->subs() == 0;
151 return $self->_subs_total_mccabe() / $self->subs();
154 #-----------------------------------------------------------------------------
156 sub violations_per_file {
159 return if $self->modules() == 0;
161 return $self->total_violations() / $self->modules();
164 #-----------------------------------------------------------------------------
166 sub violations_per_statement {
169 my $statements = $self->statements_other_than_subs();
171 return if $statements == 0;
173 return $self->total_violations() / $statements;
176 #-----------------------------------------------------------------------------
178 sub violations_per_line_of_code {
181 return if $self->lines() == 0;
183 return $self->total_violations() / $self->lines();
186 #-----------------------------------------------------------------------------
192 #-----------------------------------------------------------------------------
196 =for stopwords McCabe
200 Perl::Critic::Statistics - Compile stats on Perl::Critic violations.
205 This class accumulates statistics on Perl::Critic violations across one or
206 more files. NOTE: This class is experimental and subject to change.
215 Create a new instance of Perl::Critic::Statistics. No arguments are supported
219 =item C< accumulate( $doc, \@violations ) >
221 Accumulates statistics about the C<$doc> and the C<@violations> that were
227 The number of chunks of code (usually files) that have been analyzed.
232 The total number of subroutines analyzed by this Critic.
235 =item C<statements()>
237 The total number of statements analyzed by this Critic.
242 The total number of lines of code analyzed by this Critic.
245 =item C<violations_by_severity()>
247 The number of violations of each severity found by this Critic as a
248 reference to a hash keyed by severity.
251 =item C<violations_by_policy()>
253 The number of violations of each policy found by this Critic as a
254 reference to a hash keyed by full policy name.
257 =item C<total_violations()>
259 The the total number of violations found by this Critic.
262 =item C<statements_other_than_subs()>
264 The total number of statements minus the number of subroutines.
265 Useful because a subroutine is considered a statement by PPI.
268 =item C<average_sub_mccabe()>
270 The average McCabe score of all scanned subroutines.
273 =item C<violations_per_file()>
275 The total violations divided by the number of modules.
278 =item C<violations_per_statement()>
280 The total violations divided by the number statements minus
284 =item C<violations_per_line_of_code()>
286 The total violations divided by the lines of code.
294 Elliot Shank C<< <perl@galumph.com> >>
299 Copyright (c) 2007-2008 Elliot Shank
301 This program is free software; you can redistribute it and/or modify
302 it under the same terms as Perl itself. The full text of this license
303 can be found in the LICENSE file included with this module.
307 ##############################################################################
310 # cperl-indent-level: 4
312 # indent-tabs-mode: nil
313 # c-indentation-style: bsd
315 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :