File Coverage

blib/lib/Perl/Critic/Statistics.pm
Criterion Covered Total %
statement 88 102 86.2
branch 20 24 83.3
condition 3 3 100.0
subroutine 18 25 72.0
pod 19 19 100.0
total 148 173 85.5


line stmt bran cond sub pod time code
1             package Perl::Critic::Statistics;
2              
3 40     40   1296 use 5.010001;
  40         207  
4 40     40   280 use strict;
  40         114  
  40         902  
5 40     40   241 use warnings;
  40         111  
  40         1313  
6              
7 40     40   286 use English qw(-no_match_vars);
  40         140  
  40         284  
8              
9 40     40   15218 use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_sub };
  40         127  
  40         50485  
10              
11             #-----------------------------------------------------------------------------
12              
13             our $VERSION = '1.148';
14              
15             #-----------------------------------------------------------------------------
16              
17             sub new {
18 2730     2730 1 7599 my ( $class ) = @_;
19              
20 2730         7111 my $self = bless {}, $class;
21              
22 2730         6602 $self->{_modules} = 0;
23 2730         5319 $self->{_subs} = 0;
24 2730         5215 $self->{_statements} = 0;
25 2730         4885 $self->{_lines} = 0;
26 2730         6431 $self->{_lines_of_blank} = 0;
27 2730         6030 $self->{_lines_of_comment} = 0;
28 2730         5151 $self->{_lines_of_data} = 0;
29 2730         7564 $self->{_lines_of_perl} = 0;
30 2730         5443 $self->{_lines_of_pod} = 0;
31 2730         6123 $self->{_violations_by_policy} = {};
32 2730         5798 $self->{_violations_by_severity} = {};
33 2730         5577 $self->{_total_violations} = 0;
34              
35 2730         7436 return $self;
36             }
37              
38             #-----------------------------------------------------------------------------
39              
40             sub accumulate {
41 2724     2724 1 6435 my ($self, $doc, $violations) = @_;
42              
43 2724         6448 $self->{_modules}++;
44              
45 2724         7511 my $subs = $doc->find('PPI::Statement::Sub');
46 2724 100       8021 if ($subs) {
47 440         1709 foreach my $sub ( @{$subs} ) {
  440         1587  
48 636         1340 $self->{_subs}++;
49 636         2616 $self->{_subs_total_mccabe} += calculate_mccabe_of_sub( $sub );
50             }
51             }
52              
53 2724         7029 my $statements = $doc->find('PPI::Statement');
54 2724 100       8164 $self->{_statements} += $statements ? scalar @{$statements} : 0;
  2709         6185  
55              
56             ## no critic (RequireDotMatchAnything, RequireExtendedFormatting, RequireLineBoundaryMatching)
57 2724         20652 my @lines = split /$INPUT_RECORD_SEPARATOR/, $doc->serialize();
58             ## use critic
59 2724         1555486 $self->{_lines} += scalar @lines;
60             {
61 2724         5703 my ( $in_data, $in_pod );
  2724         5498  
62 2724         5805 foreach ( @lines ) {
63 23306 100 100     127573 if ( q{=} eq substr $_, 0, 1 ) { ## no critic (ProhibitCascadingIfElse)
    100          
    100          
    100          
    100          
    100          
64 179         508 $in_pod = not m/ \A \s* =cut \b /smx;
65 179         322 $self->{_lines_of_pod}++;
66             } elsif ( $in_pod ) {
67 291         425 $self->{_lines_of_pod}++;
68             } elsif ( q{__END__} eq $_ || q{__DATA__} eq $_ ) {
69 82         133 $in_data = 1;
70 82         141 $self->{_lines_of_perl}++;
71             } elsif ( $in_data ) {
72 183         318 $self->{_lines_of_data}++;
73             } elsif ( m/ \A \s* \# /smx ) {
74 4168         9363 $self->{_lines_of_comment}++;
75             } elsif ( m/ \A \s* \z /smx ) {
76 6189         13980 $self->{_lines_of_blank}++;
77             } else {
78 12214         22472 $self->{_lines_of_perl}++;
79             }
80             }
81             }
82              
83 2724         5752 foreach my $violation ( @{ $violations } ) {
  2724         6316  
84 3038         8754 $self->{_violations_by_severity}->{ $violation->severity() }++;
85 3038         7922 $self->{_violations_by_policy}->{ $violation->policy() }++;
86 3038         5569 $self->{_total_violations}++;
87             }
88              
89 2724         9257 return;
90             }
91              
92             #-----------------------------------------------------------------------------
93              
94             sub modules {
95 3     3 1 552 my ( $self ) = @_;
96              
97 3         25 return $self->{_modules};
98             }
99              
100             #-----------------------------------------------------------------------------
101              
102             sub subs {
103 5     5 1 544 my ( $self ) = @_;
104              
105 5         23 return $self->{_subs};
106             }
107              
108             #-----------------------------------------------------------------------------
109              
110             sub statements {
111 3     3 1 649 my ( $self ) = @_;
112              
113 3         13 return $self->{_statements};
114             }
115              
116             #-----------------------------------------------------------------------------
117              
118             sub lines {
119 3     3 1 732 my ( $self ) = @_;
120              
121 3         17 return $self->{_lines};
122             }
123              
124             #-----------------------------------------------------------------------------
125              
126             sub lines_of_blank {
127 0     0 1 0 my ( $self ) = @_;
128              
129 0         0 return $self->{_lines_of_blank};
130             }
131              
132             #-----------------------------------------------------------------------------
133              
134             sub lines_of_comment {
135 0     0 1 0 my ( $self ) = @_;
136              
137 0         0 return $self->{_lines_of_comment};
138             }
139              
140             #-----------------------------------------------------------------------------
141              
142             sub lines_of_data {
143 0     0 1 0 my ( $self ) = @_;
144              
145 0         0 return $self->{_lines_of_data};
146             }
147              
148             #-----------------------------------------------------------------------------
149              
150             sub lines_of_perl {
151 0     0 1 0 my ( $self ) = @_;
152              
153 0         0 return $self->{_lines_of_perl};
154             }
155              
156             #-----------------------------------------------------------------------------
157              
158             sub lines_of_pod {
159 0     0 1 0 my ( $self ) = @_;
160              
161 0         0 return $self->{_lines_of_pod};
162             }
163              
164             #-----------------------------------------------------------------------------
165              
166             sub _subs_total_mccabe {
167 1     1   4 my ( $self ) = @_;
168              
169 1         5 return $self->{_subs_total_mccabe};
170             }
171              
172             #-----------------------------------------------------------------------------
173              
174             sub violations_by_severity {
175 0     0 1 0 my ( $self ) = @_;
176              
177 0         0 return $self->{_violations_by_severity};
178             }
179              
180             #-----------------------------------------------------------------------------
181              
182             sub violations_by_policy {
183 0     0 1 0 my ( $self ) = @_;
184              
185 0         0 return $self->{_violations_by_policy};
186             }
187              
188             #-----------------------------------------------------------------------------
189              
190             sub total_violations {
191 4     4 1 579 my ( $self ) = @_;
192              
193 4         18 return $self->{_total_violations};
194             }
195              
196             #-----------------------------------------------------------------------------
197              
198             sub statements_other_than_subs {
199 2     2 1 562 my ( $self ) = @_;
200              
201 2         6 return $self->statements() - $self->subs();
202             }
203              
204             #-----------------------------------------------------------------------------
205              
206             sub average_sub_mccabe {
207 1     1 1 617 my ( $self ) = @_;
208              
209 1 50       4 return if $self->subs() == 0;
210              
211 1         9 return $self->_subs_total_mccabe() / $self->subs();
212             }
213              
214             #-----------------------------------------------------------------------------
215              
216             sub violations_per_file {
217 1     1 1 574 my ( $self ) = @_;
218              
219 1 50       5 return if $self->modules() == 0;
220              
221 1         4 return $self->total_violations() / $self->modules();
222             }
223              
224             #-----------------------------------------------------------------------------
225              
226             sub violations_per_statement {
227 1     1 1 543 my ( $self ) = @_;
228              
229 1         4 my $statements = $self->statements_other_than_subs();
230              
231 1 50       7 return if $statements == 0;
232              
233 1         6 return $self->total_violations() / $statements;
234             }
235              
236             #-----------------------------------------------------------------------------
237              
238             sub violations_per_line_of_code {
239 1     1 1 536 my ( $self ) = @_;
240              
241 1 50       5 return if $self->lines() == 0;
242              
243 1         6 return $self->total_violations() / $self->lines();
244             }
245              
246             #-----------------------------------------------------------------------------
247              
248             1;
249              
250             __END__
251              
252             #-----------------------------------------------------------------------------
253              
254             =pod
255              
256             =for stopwords McCabe
257              
258             =head1 NAME
259              
260             Perl::Critic::Statistics - Compile stats on Perl::Critic violations.
261              
262              
263             =head1 DESCRIPTION
264              
265             This class accumulates statistics on Perl::Critic violations across one or
266             more files. NOTE: This class is experimental and subject to change.
267              
268              
269             =head1 INTERFACE SUPPORT
270              
271             This is considered to be a non-public class. Its interface is subject
272             to change without notice.
273              
274              
275             =head1 METHODS
276              
277             =over
278              
279             =item C<new()>
280              
281             Create a new instance of Perl::Critic::Statistics. No arguments are supported
282             at this time.
283              
284              
285             =item C< accumulate( $doc, \@violations ) >
286              
287             Accumulates statistics about the C<$doc> and the C<@violations> that were
288             found.
289              
290              
291             =item C<modules()>
292              
293             The number of chunks of code (usually files) that have been analyzed.
294              
295              
296             =item C<subs()>
297              
298             The total number of subroutines analyzed by this Critic.
299              
300              
301             =item C<statements()>
302              
303             The total number of statements analyzed by this Critic.
304              
305              
306             =item C<lines()>
307              
308             The total number of lines of code analyzed by this Critic.
309              
310              
311             =item C<lines_of_blank()>
312              
313             The total number of blank lines analyzed by this Critic. This includes only
314             blank lines in code, not POD or data.
315              
316              
317             =item C<lines_of_comment()>
318              
319             The total number of comment lines analyzed by this Critic. This includes only
320             lines whose first non-whitespace character is C<#>.
321              
322              
323             =item C<lines_of_data()>
324              
325             The total number of lines of data section analyzed by this Critic, not
326             counting the C<__END__> or C<__DATA__> line. POD in a data section is counted
327             as POD, not data.
328              
329              
330             =item C<lines_of_perl()>
331              
332             The total number of lines of Perl code analyzed by this Critic. Perl appearing
333             in the data section is not counted.
334              
335              
336             =item C<lines_of_pod()>
337              
338             The total number of lines of POD analyzed by this Critic. Pod occurring in a
339             data section is counted as POD, not as data.
340              
341              
342             =item C<violations_by_severity()>
343              
344             The number of violations of each severity found by this Critic as a
345             reference to a hash keyed by severity.
346              
347              
348             =item C<violations_by_policy()>
349              
350             The number of violations of each policy found by this Critic as a
351             reference to a hash keyed by full policy name.
352              
353              
354             =item C<total_violations()>
355              
356             The total number of violations found by this Critic.
357              
358              
359             =item C<statements_other_than_subs()>
360              
361             The total number of statements minus the number of subroutines.
362             Useful because a subroutine is considered a statement by PPI.
363              
364              
365             =item C<average_sub_mccabe()>
366              
367             The average McCabe score of all scanned subroutines.
368              
369              
370             =item C<violations_per_file()>
371              
372             The total violations divided by the number of modules.
373              
374              
375             =item C<violations_per_statement()>
376              
377             The total violations divided by the number statements minus
378             subroutines.
379              
380              
381             =item C<violations_per_line_of_code()>
382              
383             The total violations divided by the lines of code.
384              
385              
386             =back
387              
388              
389             =head1 AUTHOR
390              
391             Elliot Shank C<< <perl@galumph.com> >>
392              
393              
394             =head1 COPYRIGHT
395              
396             Copyright (c) 2007-2011 Elliot Shank.
397              
398             This program is free software; you can redistribute it and/or modify
399             it under the same terms as Perl itself. The full text of this license
400             can be found in the LICENSE file included with this module.
401              
402             =cut
403              
404             ##############################################################################
405             # Local Variables:
406             # mode: cperl
407             # cperl-indent-level: 4
408             # fill-column: 78
409             # indent-tabs-mode: nil
410             # c-indentation-style: bsd
411             # End:
412             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :