File Coverage

blib/lib/Perl/ToPerl6/Statistics.pm
Criterion Covered Total %
statement 54 102 52.9
branch 10 24 41.6
condition 1 3 33.3
subroutine 7 25 28.0
pod 19 19 100.0
total 91 173 52.6


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